Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / modules / gitlab / lib / issuable-service.js
blobfe3b3e9bc866bf26e8734f15cf2e9d145b5980f7
1 'use strict';
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) {
12 var state = '';
13 if (response.state === 'opened' || response.state === 'reopened') {
14 state = 'open';
15 } else if (response.state === 'closed' || response.state === 'merged') {
16 state = 'closed';
19 return {
20 id: response.id,
21 iid: response.iid,
22 title: response.title,
23 body: response.description,
24 state: state,
25 labels: response.labels || [],
26 author: {
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';
46 this.user = user;
47 this.getAccessTokenPromise = getGitlabAccessTokenFromUser(user);
50 GitLabIssuableService.prototype.getIssue = function(project, iid) {
51 var type = this.type;
52 return this.getAccessTokenPromise
53 .then(function(accessToken) {
54 const gitlabLibOpts = {
55 oauthToken: accessToken,
56 token: getPublicTokenFromPool()
59 var resource = new Issues(gitlabLibOpts);
60 if (type === 'mr') {
61 resource = new MergeRequests(gitlabLibOpts);
64 return resource.show(project, iid);
66 .then(standardizeResponse)
67 .catch(err => {
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);
72 throw err;
73 });
76 module.exports = cacheWrapper('GitLabIssuableService', GitLabIssuableService, {
77 getInstanceId: function(gitLabIssuableService) {
78 return gitLabIssuableService.getAccessTokenPromise;
80 });