3 var { Issues
, MergeRequests
} = require('gitlab');
4 var cacheWrapper
= require('gitter-web-cache-wrapper');
5 var avatars
= require('gitter-web-avatars');
6 var getGitlabAccessTokenFromUser
= require('./get-gitlab-access-token-from-user');
7 var getPublicTokenFromPool
= require('./get-public-token-from-pool');
8 const StatusError
= require('statuserror');
10 // eslint-disable-next-line complexity
11 function standardizeResponse(response
) {
13 if (response
.state
=== 'opened' || response
.state
=== 'reopened') {
15 } else if (response
.state
=== 'closed' || response
.state
=== 'merged') {
22 title
: response
.title
,
23 body
: response
.description
,
25 labels
: response
.labels
|| [],
27 id
: response
.author
&& response
.author
.id
,
28 username
: response
.author
&& response
.author
.username
,
29 displayName
: response
.author
&& response
.author
.name
,
30 avatarUrl
: (response
.author
&& response
.author
.avatar_url
) || avatars
.getDefault()
32 assignee
: response
.assignees
&&
33 response
.assignees
.length
> 0 && {
34 id
: response
.assignees
[0].id
,
35 username
: response
.assignees
[0].username
,
36 displayName
: response
.assignees
[0].name
,
37 avatarUrl
: response
.assignees
[0].avatar_url
39 createdAt
: response
.created_at
,
40 updatedAt
: response
.updated_at
44 function GitLabIssuableService(user
, type
) {
45 this.type
= type
|| 'issues';
47 this.getAccessTokenPromise
= getGitlabAccessTokenFromUser(user
);
50 GitLabIssuableService
.prototype.getIssue = function(project
, iid
) {
52 return this.getAccessTokenPromise
53 .then(function(accessToken
) {
54 const gitlabLibOpts
= {
55 oauthToken
: accessToken
,
56 token
: getPublicTokenFromPool()
59 var resource
= new Issues(gitlabLibOpts
);
61 resource
= new MergeRequests(gitlabLibOpts
);
64 return resource
.show(project
, iid
);
66 .then(standardizeResponse
)
68 // make sure HTTP errors get translated into StatusErrors so the rest of webapp can handle them
69 if (err
.name
=== 'HTTPError') {
70 throw new StatusError(err
.response
.status
, err
.description
);
76 module
.exports
= cacheWrapper('GitLabIssuableService', GitLabIssuableService
, {
77 getInstanceId: function(gitLabIssuableService
) {
78 return gitLabIssuableService
.getAccessTokenPromise
;