This repository has been archived on 2024-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
atsebay.t/ui/src/lib/repositories.js
Pierre-Olivier Mercier 7cdfafc329
All checks were successful
continuous-integration/drone/push Build is passing
Warn and give hint when using an already used repo for another work
2022-09-30 12:41:34 +02:00

129 lines
3.6 KiB
JavaScript

export class WorkRepository {
constructor(res) {
if (res) {
this.update(res);
}
}
update({ id, id_user, id_work, uri, secret, last_check, already_used }) {
this.id = id;
this.id_user = id_user;
this.id_work = id_work;
this.uri = uri;
this.secret = secret;
this.last_check = last_check;
this.already_used = already_used == true;
}
async delete() {
const res = await fetch(this.id_work?`api/works/${this.id_work}/repositories/${this.id}`:`api/repositories/${this.id}`, {
method: 'DELETE',
headers: {'Accept': 'application/json'}
});
if (res.status == 200) {
return true;
} else {
throw new Error((await res.json()).errmsg);
}
}
async getBuildState() {
const res = await fetch(this.id_work?`api/works/${this.id_work}/repositories/${this.id}/state`:`api/repositories/${this.id}/state`, {
headers: {'Accept': 'application/json'}
});
if (res.status == 200) {
return await res.json();
} else {
throw new Error((await res.json()).errmsg);
}
}
async getBuildLogs(userid) {
let url = this.id_work?`works/${this.id_work}/repositories/${this.id}/state-logs`:`repositories/${this.id}/state-logs`;
if (userid) {
url = `users/${userid}/` + url;
}
const res = await fetch("api/" + url, {
headers: {'Accept': 'application/json'}
});
if (res.status == 200) {
return await res.json();
} else {
throw new Error((await res.json()).errmsg);
}
}
async retrieveWork(tag) {
const res = await fetch(this.id_work?`api/works/${this.id_work}/repositories/${this.id}/trigger`:`api/repositories/${this.id}/trigger`, {
method: 'POST',
headers: {'Accept': 'application/json'},
body: !tag || tag.length == 0?null:JSON.stringify(tag)
});
if (res.status == 200) {
const data = await res.json();
this.update(data);
return data;
} else {
throw new Error((await res.json()).errmsg);
}
}
async save(user) {
let url = this.id?`repositories/${this.id}`:'repositories';
if (this.id_work) {
url = `works/${this.id_work}/` + url;
}
if (user != null) {
url = `users/${user.id}/` + url;
}
const res = await fetch("api/"+url, {
method: this.id?'PUT':'POST',
headers: {'Accept': 'application/json'},
body: JSON.stringify(this),
});
if (res.status == 200) {
const data = await res.json();
this.update(data);
return data;
} else {
throw new Error((await res.json()).errmsg);
}
}
}
export async function getRemoteRepositories(userid) {
const res = await fetch(userid?`api/users/${userid}/gitlabcri/repositories`:`api/gitlabcri/repositories`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return await res.json();
} else {
throw new Error((await res.json()).errmsg);
}
}
export async function getRepositories(wid, userid) {
const res = await fetch(userid?`api/users/${userid}/works/${wid}/repositories`:`api/works/${wid}/repositories`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
const data = await res.json()
if (data) {
return data.map((r) => new WorkRepository(r));
} else {
throw new Error("No repository attached");
}
} else {
throw new Error((await res.json()).errmsg);
}
}
export async function getRepository(kid) {
const res = await fetch(`api/repositories/${kid}`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return new Repository(await res.json());
} else {
throw new Error((await res.json()).errmsg);
}
}