forked from stream-labs/desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-client.js
81 lines (74 loc) · 2.16 KB
/
github-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const { App } = require("@octokit/app");
const { request } = require("@octokit/request");
/**
* A wrapper for the Github API
*/
module.exports.GithubClient = class GithubClient {
constructor (appId, privateKey, owner, repo) {
this.appId = appId;
this.owner = owner;
this.repo = repo;
this.privateKey = privateKey;
this.installationAccessToken = '';
}
async login() {
const app = new App({ id: this.appId, privateKey: this.privateKey });
const jwt = app.getSignedJsonWebToken();
// GET an individual installation
// https://developer.github.com/v3/apps/#find-repository-installation
const { data } = await request('GET /repos/:owner/:repo/installation', {
owner: this.owner,
repo: this.repo,
headers: {
authorization: `Bearer ${jwt}`,
accept: 'application/vnd.github.machine-man-preview+json',
}
});
const installationId = data.id;
this.installationAccessToken = await app.getInstallationAccessToken({ installationId });
}
/**
* Create or update a Github Check
*
* @example
* postCheck({
* head_sha: 'a9a4333436d7d2f9f82cbf33085d307084a7330f',
* status: "in_progress",
* name: 'My Name',
* output: {
* title: 'My Title',
* }
* })
*
* @see
* https://developer.github.com/v3/checks/runs/#create-a-check-run
*/
async postCheck(params) {
return await request('POST /repos/:owner/:repo/check-runs', {
owner: this.owner,
repo: this.repo,
...params,
headers: {
authorization: `token ${this.installationAccessToken}`,
accept: 'application/vnd.github.antiope-preview+json',
},
});
}
/**
* Get a list of PRs associated with the commit
*
* @see
* https://developer.github.com/v3/repos/commits/#list-pull-requests-associated-with-commit
*/
async getPullRequestsForCommit(sha) {
return await request('GET /repos/:owner/:repo/commits/:sha/pulls', {
owner: this.owner,
repo: this.repo,
sha,
headers: {
authorization: `token ${this.installationAccessToken}`,
accept: 'application/vnd.github.groot-preview+json',
},
});
}
};