Skip to content

Commit 3704f11

Browse files
committed
Support GitLab Issues
Move GitHub Tasks to github namespace `/tasks/github` Add a new gitlab route `/tasks/gitlab` Add a new gitlab serializer Closes #38
1 parent b09fdec commit 3704f11

File tree

9 files changed

+133
-0
lines changed

9 files changed

+133
-0
lines changed

app/controllers/tasks.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ import { inject } from '@ember/service';
44
export default Controller.extend({
55
organizations: inject(),
66
queryParams: ['org'],
7+
78
});

app/controllers/tasks/github.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Controller from '@ember/controller';
2+
3+
export default Controller.extend({
4+
5+
});

app/models/gitlab-task.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import TaskModel from './task';
2+
3+
export default TaskModel.extend({});

app/router.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const Router = EmberRouter.extend({
99
Router.map(function mainRoute() {
1010
return this.route('tasks', function tasksRoute() {
1111
this.route('github');
12+
this.route('gitlab');
1213
});
1314
});
1415

app/routes/tasks/gitlab.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Route from '@ember/routing/route';
2+
import {inject} from '@ember/service';
3+
4+
export default Route.extend({
5+
gitlab: inject(),
6+
store: inject(),
7+
model() {
8+
const store = this.get('store');
9+
return this.get('gitlab').tasks({projects: ['coala/mobans']}).then(data => {
10+
for (let task of data) {
11+
store.pushPayload('gitlab-task', task);
12+
}
13+
return store.peekAll('gitlab-task');
14+
});
15+
}
16+
});

app/serializers/gitlab-task.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import DS from 'ember-data';
2+
3+
export default DS.JSONSerializer.extend({
4+
pushPayload(store, payload) {
5+
const task = {};
6+
task.id = payload.id
7+
task.bodyText = payload.description
8+
task.commentCount = payload.user_notes_count
9+
task.updatedAt = payload.updated_at
10+
task.url = payload.web_url
11+
task.title = payload.title
12+
13+
task.author = {}
14+
task.author.url = payload.author.web_url
15+
task.author.login = payload.author.username
16+
task.author.avatarUrl = payload.author.avatar_url
17+
18+
task.repository = {}
19+
task.repository.nameWithOwner = payload.repository.path_with_namespace
20+
task.repository.url = payload.repository.web_url
21+
22+
task.isPullRequest = payload._type == 'PullRequest';
23+
24+
store.push(this.normalize(store.modelFor('gitlab-task'), task));
25+
return task;
26+
},
27+
});

app/services/gitlab.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
});

app/templates/tasks/github.hbs

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
<nav class="panel">
2+
<p class="panel-tabs is-size-4">
3+
<a class="is-active" disabled>GitHub</a>
4+
<a>|</a>
5+
{{#link-to 'tasks.gitlab'}}
6+
GitLab
7+
{{/link-to}}
8+
</p>
9+
</nav>
110
{{#each tasks as |task|}}
211
{{task-item task=task}}
312
<hr>

app/templates/tasks/gitlab.hbs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<nav class="panel">
2+
<p class="panel-tabs is-size-4">
3+
{{#link-to 'tasks.github'}}
4+
GitHub
5+
{{/link-to}}
6+
<a>|</a>
7+
<a class="is-active" disabled>GitLab</a>
8+
</p>
9+
</nav>
10+
{{#each model as |task|}}
11+
{{task-item task=task}}
12+
<hr>
13+
{{/each}}

0 commit comments

Comments
 (0)