|
| 1 | +import { inject } from '@ember/service'; |
| 2 | +import AjaxService from 'ember-ajax/services/ajax'; |
| 3 | +import RSVP from 'rsvp'; |
| 4 | + |
| 5 | +const ENDPOINT = 'https://gitlab.com/api/v4'; |
| 6 | +const PROJECT_ENDPOINT = `${ENDPOINT}/projects/{projectId}`; |
| 7 | +const ISSUE_ENDPOINT = `${PROJECT_ENDPOINT}/issues?order_by=created_at&state=opened&per_page=100`; |
| 8 | + |
| 9 | +function buildIssuesUrl(projectId) { |
| 10 | + return ISSUE_ENDPOINT.replace('{projectId}', encodeURIComponent(projectId)); |
| 11 | +} |
| 12 | + |
| 13 | +function buildProjectUrl(projectId) { |
| 14 | + return PROJECT_ENDPOINT.replace('{projectId}', encodeURIComponent(projectId)); |
| 15 | +} |
| 16 | + |
| 17 | +export default AjaxService.extend({ |
| 18 | + userSettings: inject(), |
| 19 | + |
| 20 | + host: 'https://gitlab.com/', |
| 21 | + _issueUrl: '', |
| 22 | + |
| 23 | + init(...arg) { |
| 24 | + this._super(...arg); |
| 25 | + this.set('headers', { 'Private-Token': this.get('userSettings').tokens.get('gitlab_com') }); |
| 26 | + }, |
| 27 | + |
| 28 | + tasks({ projects }) { |
| 29 | + const tasks = projects.map((projectId) => { |
| 30 | + const embedRepoInfo = taskList => this.fetchRepositoryInfo(projectId) |
| 31 | + .then(repoInfo => taskList.map((task) => { |
| 32 | + const decoratedTask = Object.assign({}, task); |
| 33 | + decoratedTask.repository = repoInfo; |
| 34 | + decoratedTask.type = 'gitlab-task'; |
| 35 | + return decoratedTask; |
| 36 | + })); |
| 37 | + return this.fetchIssues(projectId).then(embedRepoInfo); |
| 38 | + }); |
| 39 | + |
| 40 | + return RSVP.all(tasks).then(taskList => |
| 41 | + taskList |
| 42 | + .reduce((combinedTasks, initialTasklist) => combinedTasks.concat(initialTasklist), [])); |
| 43 | + }, |
| 44 | + |
| 45 | + fetchRepositoryInfo(projectId) { |
| 46 | + return this.request(buildProjectUrl(projectId)); |
| 47 | + }, |
| 48 | + |
| 49 | + fetchIssues(projectId) { |
| 50 | + return this.request(buildIssuesUrl(projectId)); |
| 51 | + }, |
| 52 | + |
| 53 | +}); |
0 commit comments