This repository has been archived by the owner on Aug 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
githubClient.js
67 lines (56 loc) · 1.53 KB
/
githubClient.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
const https = require("https");
const makeRequest = path =>
new Promise((resolve, reject) => {
let req = {
hostname: "api.github.com",
port: 443,
path,
method: "GET",
headers: {
Accept: "application/vnd.github.antiope-preview+json",
"User-Agent": "node"
}
};
if (process.env.GITHUB_TOKEN) {
req.headers.Authorization = `token ${process.env.GITHUB_TOKEN}`;
}
https.get(req, resp => {
let data = "";
resp.on("data", chunk => {
data += chunk;
});
resp.on("error", error => reject(error));
resp.on("end", () => {
const parsed = JSON.parse(data);
if (resp.statusCode === 200) {
resolve(parsed);
} else {
reject(parsed);
}
});
});
});
const findCheckSuite = parsed => {
const matched = parsed.check_suites.find(
checkSuite => checkSuite.app.name === "GitHub Actions"
);
if (!matched) {
throw new Error("Could not find check suite named GitHub Actions");
}
return matched;
};
const getCheckSuite = (owner, repo, branch) =>
makeRequest(
`/repos/${owner}/${repo}/commits/${branch || "master"}/check-suites`
).then(findCheckSuite);
const getLatestRunURL = (owner, repo, branch) =>
getCheckSuite(owner, repo, branch)
.then(checkSuite => makeRequest(checkSuite.check_runs_url))
.then(response => ({
url: response.check_runs[0].html_url,
etag: response.check_runs[0].head_sha
}));
module.exports = {
getCheckSuite,
getLatestRunURL
};