-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·53 lines (45 loc) · 1.64 KB
/
index.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
import fetch from 'node-fetch'
// Let's try just a fetch
function getClosedPullRequests(ghUrl, ghToken) {
return fetch(ghUrl + "/pulls?state=closed", {
method: 'GET',
headers: {
Aceept: 'application/vnd.github.v3+json',
Authorization: `Bearer ${ghToken}`,
}
}).then(data => data.json());
}
// TODO: expose this in the cli tool
export async function getLastMergedPR(ghOwner, ghRepo) {
const ghToken = process.env.GH_TOKEN;
if (!ghToken) {
// TODO: this error doesn't really help explain how to get the token in the env. Or provide a way to set the token.
throw new Error("Must provide a GitHub token in the process.env")
}
const ghUrl = `https://api.github.com/repos/${ghOwner}/${ghRepo}`
const fetchPRList = await getClosedPullRequests(ghUrl, ghToken)
const prList = fetchPRList.map(pr => {
return {
branchName: pr.head.ref,
prNumber: pr.number,
merged_at: pr.merged_at
}
})
// inspried by https://github.com/intuit/auto/blob/7427c3937186b456a803c26c923aaa905efb8d56/packages/core/src/auto.ts#L859
const lastMerged = prList
.sort((a, b) => {
const aDate = a.merged_at ? new Date(a.merged_at).getTime() : 0;
const bDate = b.merged_at ? new Date(b.merged_at).getTime() : 0;
return bDate - aDate;
})
.find((pull) => pull.merged_at);
return lastMerged;
}
export async function getLastMergedPrNumber(ghOwner, ghRepo) {
const lastMerged = await getLastMergedPR(ghOwner, ghRepo)
return lastMerged.prNumber;
}
export async function getLastMergedPrRef(ghOwner, ghRepo) {
const lastMerged = await getLastMergedPR(ghOwner, ghRepo)
return lastMerged.branchName;
}