2019-12-01 22:12:13 +01:00
|
|
|
|
|
|
|
export class GiteaUnauthorizedError extends Error {
|
|
|
|
constructor(...args) {
|
|
|
|
super(...args)
|
|
|
|
Error.captureStackTrace(this, GiteaUnauthorizedError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 14:12:48 +01:00
|
|
|
export class GiteaClient {
|
|
|
|
|
2019-12-05 14:44:33 +01:00
|
|
|
fetchIssues(project, page = 1) {
|
|
|
|
return fetch(`/gitea/api/v1/repos/${project}/issues?page=${page}`)
|
2019-12-01 22:12:13 +01:00
|
|
|
.then(this.assertAuthorization)
|
2019-12-06 17:15:18 +01:00
|
|
|
.then(this.assertOk)
|
2019-12-01 22:12:13 +01:00
|
|
|
.then(res => res.json())
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchUserProjects() {
|
|
|
|
return fetch(`/gitea/api/v1/user/repos`)
|
|
|
|
.then(this.assertAuthorization)
|
2019-12-06 17:15:18 +01:00
|
|
|
.then(this.assertOk)
|
2019-12-01 22:12:13 +01:00
|
|
|
.then(res => res.json())
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
addIssueLabel(project, issueNumber, labelID) {
|
|
|
|
return fetch(`/gitea/api/v1/repos/${project}/issues/${issueNumber}/labels`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ labels: [labelID] }),
|
|
|
|
})
|
|
|
|
.then(this.assertAuthorization)
|
2019-12-06 17:15:18 +01:00
|
|
|
.then(this.assertOk)
|
2019-12-01 22:12:13 +01:00
|
|
|
.then(res => res.json())
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchProjectLabels(project) {
|
|
|
|
return fetch(`/gitea/api/v1/repos/${project}/labels`)
|
|
|
|
.then(this.assertAuthorization)
|
2019-12-06 17:15:18 +01:00
|
|
|
.then(this.assertOk)
|
2019-12-01 22:12:13 +01:00
|
|
|
.then(res => res.json())
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
removeIssueLabel(project, issueNumber, labelID) {
|
|
|
|
return fetch(`/gitea/api/v1/repos/${project}/issues/${issueNumber}/labels/${labelID}`, {
|
|
|
|
method: 'DELETE'
|
|
|
|
})
|
|
|
|
.then(this.assertAuthorization)
|
2019-12-06 17:15:18 +01:00
|
|
|
.then(this.assertOk)
|
2019-12-01 22:12:13 +01:00
|
|
|
}
|
|
|
|
|
2019-12-05 22:37:09 +01:00
|
|
|
createIssue(project, title, body, labelID) {
|
|
|
|
return fetch(`/gitea/api/v1/repos/${project}/issues`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
title,
|
|
|
|
body,
|
|
|
|
labels: [labelID],
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
.then(this.assertAuthorization)
|
2019-12-06 17:15:18 +01:00
|
|
|
.then(this.assertOk)
|
2019-12-05 22:37:09 +01:00
|
|
|
.then(res => res.json())
|
|
|
|
}
|
|
|
|
|
2019-12-01 22:12:13 +01:00
|
|
|
assertOk(res) {
|
|
|
|
if (!res.ok) return Promise.reject(new Error('Request failed'));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertAuthorization(res) {
|
|
|
|
if (res.status === 401 || res.status === 404) return Promise.reject(new GiteaUnauthorizedError());
|
|
|
|
return res;
|
2019-11-28 14:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export const gitea = new GiteaClient();
|