forked from ellismg/github-pr-changelog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangelog.ts
77 lines (62 loc) · 3.03 KB
/
changelog.ts
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
import * as GitHub from "@octokit/rest";
import * as child_process from "child_process";
import * as os from "os";
export type PullRequestInfo = { pullRequests: any[], contributors: string[] };
/**
* For a range of git tags, returns the GitHub pull requests that were merged in this range,
* and that have one of the labels to filter on.
* For response format, see see https://developer.github.com/v3/pulls/#list-pull-requests
*
* @param owner GitHub owner (also known as "organization")
* @param repo GitHub repo
* @param gitDirectory Path to a git working tree that contains the tag range
* @param fromTag git tag or commit hash for start of range (must exist in `gitDirectory` tree)
* @param toTag git tag or commit hash for end of range (must exist in `gitDirectory` tree)
* @param labelFilter Array of label names to filter on. Pass `null` for no filtering.
* @returns Array of GitHub pull request response objects
*/
export async function getPullsInRange(
owner: string, repo: string,
gitDirectory: string, fromTag: string, toTag: string,
gitHubToken: string,
labelFilter?: string[]): Promise<PullRequestInfo> {
// output to standard error as informational, in case there are GitHub or git errors
console.error(`--- Getting closed PRs for ${owner}:${repo} ----`);
let allClosedPrs = await getClosedPullRequests(owner, repo, gitHubToken); // get all PRs that have been closed in this repo
console.error(`+++ Running git rev-list ${fromTag}..${toTag} in ${gitDirectory} +++`);
try {
// get git hashes for the revision range
let gitOutput =
child_process.execSync(
`git rev-list ${fromTag}..${toTag}`, { cwd: gitDirectory, encoding: "utf8" }
);
let gitHashes = gitOutput.split(os.EOL);
let mergedPrs = allClosedPrs.filter(pr => gitHashes.includes(pr.merge_commit_sha)); // filter to only PRs merged in range
let contributors = mergedPrs.map(pr => pr.user.login); // Get PR authors
// filter to only pull requests in `labelFilter`
if (labelFilter) {
mergedPrs = mergedPrs.filter(
pr => pr.labels.find( (l:any) => labelFilter.includes(l.name)) );
}
return { pullRequests: mergedPrs, contributors };
} catch (ex) {
console.error(`Exception while processing pull requests: ${ex}`);
return { pullRequests: [], contributors: [] };
}
}
async function getClosedPullRequests(owner: string, repo: string, gitHubToken: string): Promise<any[]> {
const octokit = new GitHub();
octokit.authenticate({
type: 'token',
token: gitHubToken
})
let response =
await octokit.pullRequests.getAll(
{ owner: owner, repo: repo, state: "closed", per_page: 100 });
let data: any[] = response.data;
while (octokit.hasNextPage(response)) {
response = await octokit.getNextPage(response);
data = data.concat(response.data);
}
return data;
}