|
| 1 | +import util from "util"; |
| 2 | +import cp from "child_process"; |
| 3 | +import {parseVersion} from "./helpers/parser.js"; |
| 4 | +import githubAxios from "./githubAxios.js"; |
| 5 | +import memoize from 'memoizee'; |
| 6 | + |
| 7 | +const exec = util.promisify(cp.exec); |
| 8 | + |
| 9 | +export default class GithubAPI { |
| 10 | + constructor(owner, repo) { |
| 11 | + if (!owner) { |
| 12 | + throw new Error('repo owner must be specified'); |
| 13 | + } |
| 14 | + |
| 15 | + if (!repo) { |
| 16 | + throw new Error('repo must be specified'); |
| 17 | + } |
| 18 | + |
| 19 | + this.repo = repo; |
| 20 | + this.owner = owner; |
| 21 | + this.axios = githubAxios.create({ |
| 22 | + baseURL: `https://api.github.com/repos/${this.owner}/${this.repo}/`, |
| 23 | + }) |
| 24 | + } |
| 25 | + |
| 26 | + async createComment(issue, body) { |
| 27 | + return (await this.axios.post(`/issues/${issue}/comments`, {body})).data; |
| 28 | + } |
| 29 | + |
| 30 | + async getComments(issue, {desc = false, per_page= 100, page = 1}) { |
| 31 | + return (await this.axios.get(`/issues/${issue}/comments`, {params: {direction: desc ? 'desc' : 'asc', per_page, page}})).data; |
| 32 | + } |
| 33 | + |
| 34 | + async getComment(id) { |
| 35 | + return (await this.axios.get(`/issues/comments/${id}`)).data; |
| 36 | + } |
| 37 | + |
| 38 | + async updateComment(id, body) { |
| 39 | + return (await this.axios.patch(`/issues/comments/${id}`, {body})).data; |
| 40 | + } |
| 41 | + |
| 42 | + async appendLabels(issue, labels) { |
| 43 | + return (await this.axios.post(`issues/${issue}/labels`, {labels})).data; |
| 44 | + } |
| 45 | + |
| 46 | + async getUser(user) { |
| 47 | + return (await this.axios.get(`users/${user}`)).data; |
| 48 | + } |
| 49 | + |
| 50 | + async isCollaborator(user) { |
| 51 | + try { |
| 52 | + return (await this.axios.get(`/collaborators/${user}`)).status === 204; |
| 53 | + } catch (e) { |
| 54 | + |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + async deleteLabel(issue, label) { |
| 59 | + return (await this.axios.delete(`/issues/${issue}/labels/${label}`)).data; |
| 60 | + } |
| 61 | + |
| 62 | + async getIssue(issue) { |
| 63 | + return (await this.axios.get(`/issues/${issue}`)).data; |
| 64 | + } |
| 65 | + |
| 66 | + async getPR(issue) { |
| 67 | + return (await this.axios.get(`/pulls/${issue}`)).data; |
| 68 | + } |
| 69 | + |
| 70 | + async getIssues({state= 'open', labels, sort = 'created', desc = false, per_page = 100, page = 1}) { |
| 71 | + return (await this.axios.get(`/issues`, {params: {state, labels, sort, direction: desc ? 'desc' : 'asc', per_page, page}})).data; |
| 72 | + } |
| 73 | + |
| 74 | + async updateIssue(issue, data) { |
| 75 | + return (await this.axios.patch(`/issues/${issue}`, data)).data; |
| 76 | + } |
| 77 | + |
| 78 | + async closeIssue(issue) { |
| 79 | + return this.updateIssue(issue, { |
| 80 | + state: "closed" |
| 81 | + }) |
| 82 | + } |
| 83 | + |
| 84 | + async getReleases({per_page = 30, page= 1} = {}) { |
| 85 | + return (await this.axios.get(`/releases`, {params: {per_page, page}})).data; |
| 86 | + } |
| 87 | + |
| 88 | + async getRelease(release = 'latest') { |
| 89 | + return (await this.axios.get(parseVersion(release) ? `/releases/tags/${release}` : `/releases/${release}`)).data; |
| 90 | + } |
| 91 | + |
| 92 | + async getTags({per_page = 30, page= 1} = {}) { |
| 93 | + return (await this.axios.get(`/tags`, {params: {per_page, page}})).data; |
| 94 | + } |
| 95 | + |
| 96 | + async reopenIssue(issue) { |
| 97 | + return this.updateIssue(issue, { |
| 98 | + state: "open" |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + static async getTagRef(tag) { |
| 103 | + try { |
| 104 | + return (await exec(`git show-ref --tags "refs/tags/${tag}"`)).stdout.split(' ')[0]; |
| 105 | + } catch (e) { |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +const {prototype} = GithubAPI; |
| 111 | + |
| 112 | +['getUser', 'isCollaborator'].forEach(methodName => { |
| 113 | + prototype[methodName] = memoize(prototype[methodName], { promise: true }) |
| 114 | +}); |
| 115 | + |
| 116 | +['get', 'post', 'put', 'delete', 'isAxiosError'].forEach((method) => prototype[method] = function(...args){ |
| 117 | + return this.axios[method](...args); |
| 118 | +}); |
| 119 | + |
0 commit comments