Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]Support GitLab #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/components/settings-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default Component.extend({

// Properties
token_github_com: oneWay('userSettings.tokens.github_com'),
token_gitlab_com: oneWay('userSettings.tokens.gitlab_com'),

init(...args) {
this._super(...args);
Expand All @@ -19,6 +20,7 @@ export default Component.extend({
},
saveSettings() {
this.userSettings.setToken('github_com', this.get('token_github_com'));
this.userSettings.setToken('gitlab_com', this.get('token_gitlab_com'));
this.set('isActive', false);
},
},
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/tasks/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Controller from '@ember/controller';

export default Controller.extend({
});
6 changes: 5 additions & 1 deletion app/data/organizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export default {
},
{
type: 'gitlab',
identifier: 'coala',
identifier: 'coala/mobans',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is obviously wrong, as it is not an org identifier. It is a project identifier.

This is not too different from the problem I raised in the PR a month ago.
#44 (comment)

fetchGitlabProjects still doesnt get the projects for the org.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identifier in gitlab currently only support per repository, however, in github, only support per organization.

In gitlab, supporting per org, will make multiple request.

It fit nicely with wikidata where, it already support github organization, but not gitlab. It can be identified via issue tracker used, which is currently support full link.

},
{
type: 'gitlab',
identifier: 'coala/coala-utils',
},
],
},
Expand Down
3 changes: 3 additions & 0 deletions app/models/gitlab-task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import TaskModel from './task';

export default TaskModel.extend({});
1 change: 1 addition & 0 deletions app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Router = EmberRouter.extend({
Router.map(function mainRoute() {
return this.route('tasks', function tasksRoute() {
this.route('github');
this.route('gitlab');
});
});

Expand Down
23 changes: 23 additions & 0 deletions app/routes/tasks/gitlab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Route from '@ember/routing/route';
import { inject } from '@ember/service';

export default Route.extend({
gitlab: inject(),
store: inject(),
organizations: inject(),

model(params, transition) {
const { org } = transition.queryParams;
const store = this.get('store');
const projects = this.get('organizations').fetchGitlabProjects(org);
if (projects.length > 0) {
return this.get('gitlab').tasks({ projects }).then((data) => {
data.forEach((task) => {
store.pushPayload('gitlab-task', task);
});
return store.peekAll('gitlab-task');
});
}
return [];
},
});
30 changes: 30 additions & 0 deletions app/serializers/gitlab-task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import DS from 'ember-data';

export default DS.JSONSerializer.extend({
pushPayload(store, payload) {
const task = {};
task.id = payload.id;
task.bodyText = payload.description;
task.commentCount = payload.user_notes_count;
// Set the default color to 65C8FF, otherwise, we need to refetch the default
// color from `project/:id/labels` endpoint.
task.labels = payload.labels.map(label => ({ color: '65C8FF', name: label }));
task.updatedAt = payload.updated_at;
task.title = payload.title;
task.url = payload.web_url;

task.author = {};
task.author.url = payload.author.web_url;
task.author.login = payload.author.username;
task.author.avatarUrl = payload.author.avatar_url;

task.repository = {};
task.repository.nameWithOwner = payload.repository.path_with_namespace;
task.repository.url = payload.repository.web_url;

task.isPullRequest = payload._type === 'PullRequest';

store.push(this.normalize(store.modelFor('gitlab-task'), task));
return task;
},
});
53 changes: 53 additions & 0 deletions app/services/gitlab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { inject } from '@ember/service';
import AjaxService from 'ember-ajax/services/ajax';
import RSVP from 'rsvp';

const ENDPOINT = 'https://gitlab.com/api/v4';
const PROJECT_ENDPOINT = `${ENDPOINT}/projects/{projectId}`;
const ISSUE_ENDPOINT = `${PROJECT_ENDPOINT}/issues?order_by=created_at&state=opened&per_page=100`;

function buildIssuesUrl(projectId) {
return ISSUE_ENDPOINT.replace('{projectId}', encodeURIComponent(projectId));
}

function buildProjectUrl(projectId) {
return PROJECT_ENDPOINT.replace('{projectId}', encodeURIComponent(projectId));
}

export default AjaxService.extend({
userSettings: inject(),

host: 'https://gitlab.com/',
_issueUrl: '',

init(...arg) {
this._super(...arg);
this.set('headers', { 'Private-Token': this.get('userSettings').tokens.get('gitlab_com') });
},

tasks({ projects }) {
const tasks = projects.map((projectId) => {
const embedRepoInfo = taskList => this.fetchRepositoryInfo(projectId)
.then(repoInfo => taskList.map((task) => {
const decoratedTask = Object.assign({}, task);
decoratedTask.repository = repoInfo;
decoratedTask.type = 'gitlab-task';
return decoratedTask;
}));
return this.fetchIssues(projectId).then(embedRepoInfo);
});

return RSVP.all(tasks).then(taskList =>
taskList
.reduce((combinedTasks, initialTasklist) => combinedTasks.concat(initialTasklist), []));
},

fetchRepositoryInfo(projectId) {
return this.request(buildProjectUrl(projectId));
},

fetchIssues(projectId) {
return this.request(buildIssuesUrl(projectId));
},

});
18 changes: 16 additions & 2 deletions app/services/organizations.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Object, { computed } from '@ember/object';
import EmberObject, { computed } from '@ember/object';
import Service from '@ember/service';
import organizations from '../data/organizations';

export default Service.extend({
init(...args) {
this._super(...args);
this.organizations = Object.create(organizations);
this.organizations = EmberObject.create(organizations);
},

list: computed('organizations', function getOrganizationList() {
Expand All @@ -15,4 +15,18 @@ export default Service.extend({
fetch(slug) {
return this.organizations.get(slug);
},

fetchGitlabProjects(slug) {
const { trackers } = this.fetch(slug);
if (!trackers) {
return [];
}
return trackers.reduce((previous, tracker) => {
if (tracker.type === 'gitlab') {
return [...previous, tracker.identifier];
}
return previous;
}, []);
},

});
8 changes: 8 additions & 0 deletions app/templates/components/settings-modal.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
<p class="help is-danger">In order to fetch results from GitHub, you must specify GitHub token. With ANY scope you like. No exact scope is required.</p>
<p class="help">Get GitHub user token from <a href="https://github.com/settings/tokens" title="https://github.com/settings/tokens" target="_blank">https://github.com/settings/tokens</a></p>
</div>
<div class="field">
<label class="label">GitLab Token</label>
<div class="control">
{{input class="input" value=token_gitlab_com}}
</div>
<p class="help is-danger">In order to fetch results from GitLab, you must specify GitLab token. With ANY scope you like. No exact scope is required.</p>
<p class="help">Get GitHub user token from <a href="https://github.com/settings/tokens" title="https://github.com/settings/tokens" target="_blank">https://github.com/settings/tokens</a></p>
</div>
</section>
<footer class="modal-card-foot">
<button class="button is-success" onclick={{action "saveSettings"}}>Save</button>
Expand Down
5 changes: 0 additions & 5 deletions app/templates/tasks.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@
</aside>
</section>
<div class="column is-three-quarters">
<nav class="panel">
<p class="panel-tabs is-size-4">
<a class="is-active {{if isGithubpage "disabled"}}">GitHub</a>
</p>
</nav>
{{outlet}}
</div>
</div>
9 changes: 9 additions & 0 deletions app/templates/tasks/github.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
<nav class="panel">
<p class="panel-tabs is-size-4">
<a class="is-active" disabled>GitHub</a>
<a>|</a>
{{#link-to 'tasks.gitlab'}}
GitLab
{{/link-to}}
</p>
</nav>
{{#each tasks as |task|}}
{{task-item task=task}}
<hr>
Expand Down
16 changes: 16 additions & 0 deletions app/templates/tasks/gitlab.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<nav class="panel">
<p class="panel-tabs is-size-4">
{{#link-to 'tasks.github'}}
GitHub
{{/link-to}}
<a>|</a>
<a class="is-active" disabled>GitLab</a>
</p>
</nav>
{{#each model as |task|}}
{{task-item task=task}}
<hr>
{{/each}}
{{#unless model}}
<p>This Org doesn't have any gitlab repository.</p>
{{/unless}}
24 changes: 24 additions & 0 deletions tests/acceptance/tasks/list-gitlab-tasks-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Service from '@ember/service';
import { visit, find } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';

// Simulating empty gitlab Projects
const emptyGitlabOrgService = Service.extend({
fetchGitlabProjects: () => [],
fetch: () => ({}),
});

module('Acceptance/tasks/list-gitlab-tasks-test', function listGitlabTaskTests(hooks) {
setupApplicationTest(hooks);

hooks.beforeEach(function beforeEach() {
this.owner.register('service:organizations', emptyGitlabOrgService);
});

test('should show no gitlab repository', async function listGitlabTasks(assert) {
await visit('/tasks/gitlab?org=discourse');
const $noGitlabRepo = find('.is-three-quarters > p');
assert.equal($noGitlabRepo.innerText, "This Org doesn't have any gitlab repository.");
});
});