|
| 1 | +// https://gitlab.com/api/v4/projects/coala%2Fmobans/issues?order_by=created_at&state=opened |
| 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 | + host: 'https://gitlab.com/', |
| 19 | + _issueUrl: '', |
| 20 | + init() { |
| 21 | + this._super(...arguments); |
| 22 | + this.set('headers', {'Private-Token': 'sVfZzagWtemrV-suxYK-'}); |
| 23 | + }, |
| 24 | + |
| 25 | + tasks({projects}) { |
| 26 | + let tasks = projects.map((projectId) => { |
| 27 | + const embedRepoInfo = (tasks) => { |
| 28 | + return new Promise((resolve, reject) => { |
| 29 | + this.fetchRepositoryInfo(projectId).then((repoInfo) => { |
| 30 | + tasks.map((task) => { |
| 31 | + task.repository = repoInfo; |
| 32 | + task.type = 'gitlab-task'; |
| 33 | + return task; |
| 34 | + }) |
| 35 | + resolve(tasks) |
| 36 | + }).catch((error) => reject(error)); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + return this.fetchIssues(projectId).then(embedRepoInfo) |
| 41 | + }); |
| 42 | + |
| 43 | + return RSVP.all(tasks).then((tasks) => { |
| 44 | + return tasks.reduce((combinedTasks, tasks) => { |
| 45 | + return combinedTasks.concat(tasks); |
| 46 | + }, []) |
| 47 | + }) |
| 48 | + }, |
| 49 | + |
| 50 | + fetchRepositoryInfo(projectId) { |
| 51 | + return this.request(buildProjectUrl(projectId)); |
| 52 | + }, |
| 53 | + |
| 54 | + fetchIssues(projectId) { |
| 55 | + return this.request(buildIssuesUrl(projectId)); |
| 56 | + }, |
| 57 | + |
| 58 | +}); |
0 commit comments