export class WorkRepository { constructor(res) { if (res) { this.update(res); } } update({ id, id_user, id_work, uri, secret, last_check }) { this.id = id; this.id_user = id_user; this.id_work = id_work; this.uri = uri; this.secret = secret; this.last_check = last_check; } async delete() { const res = await fetch(`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(`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() { const res = await fetch(`api/repositories/${this.id}/state-logs`, { headers: {'Accept': 'application/json'} }); if (res.status == 200) { return await res.json(); } else { throw new Error((await res.json()).errmsg); } } async retrieveWork() { const res = await fetch(`api/repositories/${this.id}/trigger`, { method: 'POST', headers: {'Accept': 'application/json'} }); if (res.status == 200) { const data = await res.json(); this.update(data); return data; } else { throw new Error((await res.json()).errmsg); } } async save() { const res = await fetch(this.id?`api/repositories/${this.id}`:'api/repositories', { 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) { return (await res.json()).map((r) => new WorkRepository(r)); } 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); } }