Skip to content

Draft: Support GitLab #63

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

Open
wants to merge 2 commits 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
4 changes: 4 additions & 0 deletions addon/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ module.exports = {
"PHABRICATOR_REVIEW_HEADERS": true,
"BUGZILLA_API": true,
"GITHUB_API": true,
"GITLAB_API_DEFAULT_DOMAIN": true,
"GITLAB_API_PATH": true,
"GITLAB_DEFAULT_DOMAIN": true,
"GITLAB_TODOS_PATH": true,
"DEFAULT_UPDATE_INTERVAL": true,
"ALARM_NAME": true,
"FEATURE_ALERT_REV": true,
Expand Down
57 changes: 57 additions & 0 deletions addon/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,58 @@ var MyQOnly = {
return { reviewTotal, needinfoTotal, };
},

async updateGitLab(settings) {
let username = settings.username;
if (!username) {
return { reviewTotal: 0, };
}
let token = settings.token;
if (!token) {
return { reviewTotal: 0, };
}

let url = new URL(GITLAB_API_PATH,
settings.apiUrl || GITLAB_API_DEFAULT_DOMAIN);
url.searchParams.append("action", "review_requested");
let headers = {
"PRIVATE-TOKEN": token,
};
const apiRequestOptions = {
method: "GET",
headers: headers,
// Probably doesn't matter.
credentials: "omit",
};
// Note: we might need to paginate if we care about fetching more than the
// first 100.
let response = await window.fetch(url, apiRequestOptions);
if (!response.ok) {
console.error("Failed to request from github", response);
throw new Error(`GitLab request failed (${response.status}): ` +
`${await response.text()}`);
}
const data = await response.json();

let ignoredRepos = new Set((settings.ignoredRepos || "")
.split(",")
.map(s => s.trim())
.filter(Boolean));

if (ignoredRepos.length === 0) {
return { reviewTotal: data.total_count, };
}
const reviewTotal = data.filter(request =>
!ignoredRepos.has(request.project.path)
&& !ignoredRepos.has(request.project.path_with_namespace))
.length;
const todoPath = new URL(GITLAB_TODOS_PATH,
settings.userUrl || GITLAB_DEFAULT_DOMAIN).toString();
return {
reviewTotal,
todoPath,
};
},

async updateGitHub(settings) {
let username = settings.username;
if (!username) {
Expand Down Expand Up @@ -593,6 +645,11 @@ var MyQOnly = {
console.log(`Found ${data.reviewTotal} GitHub reviews to do`);
break;
}
case "gitlab": {
data = await this.updateGitLab(service.settings);
console.log(`Found ${data.reviewTotal} GitLab reviews to do`);
break;
}
}
} catch (e) {
console.error(`Error when updating ${service.type}: `, e.toString());
Expand Down
4 changes: 4 additions & 0 deletions addon/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const PHABRICATOR_REVIEW_HEADERS = [
];
const BUGZILLA_API = "https://bugzilla.mozilla.org/jsonrpc.cgi";
const GITHUB_API = "https://api.github.com/search/issues";
const GITLAB_API_DEFAULT_DOMAIN = "https://api.gitlab.com";
const GITLAB_DEFAULT_DOMAIN = "https://gitlab.com";
const GITLAB_API_PATH = "api/v4/todos";
const GITLAB_TODOS_PATH = "/dashboard/todos";

const DEFAULT_UPDATE_INTERVAL = 5; // minutes
const ALARM_NAME = "check-for-updates";
Expand Down
12 changes: 12 additions & 0 deletions addon/content/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ <h3>GitHub</h3>
<label for="github-ignored-repos">Comma-separated list of github repositories to ignore as a substring of the url (only applies when requested as part of a team).</label>
<input type="text" id="github-ignored-repos" data-setting="ignoredRepos">
</div>

<h3>GitLab</h3>
<div class="service-settings" data-type="gitlab">
<label for="gitlab-token">GitLab Access Token</label>
<input type="password" id="gitlab-token" data-setting="token">
<label for="gitlab-api-url">GitLab API URI (e.g. https://api.gitlab.com, only if you use a custom instance of GitLab)</label>
<input type="text" id="gitlab-api-url" data-setting="apiUrl">
<label for="gitlab-user-url">GitLab user URI (e.g. https://gitlab.example.com, only if you use a custom instance of GitLab)</label>
<input type="text" id="gitlab-user-url" data-setting="userUrl">
<label for="gitlab-ignored-repos">Comma-separated list of gitlab repositories to ignore (e.g. "myqonly" or "mikeconley/mqonly").</label>
<input type="text" id="gitlab-ignored-repos" data-setting="ignoredRepos">
</div>
</section>

<section id="working-hours">
Expand Down
24 changes: 24 additions & 0 deletions addon/content/options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const Options = {
this.populateGitHub(service);
break;
}
case "gitlab": {
this.populateGitLab(service);
break;
}
}
this._nextID = Math.max(this._nextID, service.id);
}
Expand Down Expand Up @@ -98,6 +102,26 @@ const Options = {
ignoredRepos.value = service.settings.ignoredRepos || "";
},

populateGitLab(service) {
let gitlabSettings =
document.querySelector(".service-settings[data-type='gitlab']");

let token = gitlabSettings.querySelector("[data-setting='token']");
token.value = service.settings.token || "";

let ignoredRepos =
gitlabSettings.querySelector("[data-setting='ignoredRepos']");
ignoredRepos.value = service.settings.ignoredRepos || "";

let apiUrl =
gitlabSettings.querySelector("[data-setting='apiUrl']");
apiUrl.value = service.settings.apiUrl || GITLAB_API_DEFAULT_DOMAIN;

let userUrl =
gitlabSettings.querySelector("[data-setting='userUrl']");
userUrl.value = service.settings.userUrl || GITLAB_DEFAULT_DOMAIN;
},

onUpdateService(event, serviceType) {
let changedSetting = event.target.dataset.setting;
let newValue;
Expand Down
4 changes: 4 additions & 0 deletions addon/content/popup/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<a href="https://github.com/pulls/review-requested"><span id="github-review-num"></span> review(s) on Github</a>
</section>

<section id="gitlab-reviews">
<a id="gitlab-path" href="https://gitlab.com/dashboard/todos"><span id="gitlab-reviews-num"></span> review(s) on GitLab</a>
</section>

<a id="has-new-features" target="_blank" href="/content/release-notes/release-notes.html">
New features
</a>
Expand Down
12 changes: 12 additions & 0 deletions addon/content/popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ const Panel = {
total += serviceTotal;
break;
}
case "gitlab": {
console.debug("YORIC","gitlab",state);
let serviceTotal = state.data.reviewTotal || 0;
document.body.setAttribute("total-gitlab-reviews",
serviceTotal || 0);
document.getElementById("gitlab-review-num").textContent =
serviceTotal || 0;
document.getElementById("gitlab-path").href =
state.data.todoPath;
total += serviceTotal;
break;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"test": "karma start",
"test:manual": "web-ext run -s ./addon",
"test:manual": "web-ext run -s ./addon --pre-install",
"build": "web-ext build -s ./addon",
"lint": "./node_modules/eslint/bin/eslint.js .",
"lint-file": "./node_modules/eslint/bin/eslint.js"
Expand Down