const axios = require('axios') const md5 = require('md5') class Gitea { constructor(api, key) { this.api = `${api}/repos` this.key = key } trackingCode(repo, issue) { return(md5(`${this.api}${repo}${issue}`)) } checkLabelExistance(label, repo) { return new Promise((resolve, reject) => { axios.get( `${this.api}/${repo}/labels`, {headers: {"Authorization" : `token ${this.key}`} }) .then((response) => { response.data.forEach((el) => { if (el.name == label) resolve(true) }) resolve(false) }) .catch(e => console.log(e)) }) } // check if label exists in repo, if not, craete it. Needs label name, label color, repo url and api key. createLabel(label, color, repo) { this.checkLabelExistance(label, repo).then((exists) => { if(!exists) { axios.post( `${this.api}/${repo}/labels`, {color: color,name: label}, {"headers": {"Authorization" : `token ${this.key}`} }) .then(console.log(`[${this.api}] - CREATE LABEL: '${label}|${color}' created in '${repo}'`)) } }) .catch(e => console.log(e)) } labelStatus(label, repo, issue) { let tmpVal = false return new Promise((resolve, reject) => { axios.get( `${this.api}/${repo}/issues/${issue}`, {headers: {"Authorization" : `token ${this.key}`} }) .then((response) => { response.data.labels.forEach((el) => { if (el.name == label) { console.log(`[${this.api}] - LABEL STATUS: '${label}' detected in issue '${repo} ${issue}'`) resolve(true) tmpVal = true } }) }) .then(() => { if(!tmpVal) { console.log(`[${this.api}] - LABEL STATUS: '${label}' not detected in issue '${repo} ${issue}'`) resolve(false) } }) .catch(e => console.log(e)) }) } comment(message, repo, issue) { axios.post( `${this.api}/${repo}/issues/${issue}/comments`, {body: message}, {"headers": {"Authorization" : `token ${this.key}`} }) .then(console.log(`[${this.api}] - POST MESSAGE: '${message}' posted to '${repo} ${issue}'`)) .catch(e => console.log(e)) } } module.exports = Gitea