-
Notifications
You must be signed in to change notification settings - Fork 20
/
getPullChanges.ts
59 lines (50 loc) · 1.69 KB
/
getPullChanges.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
import * as github from '@actions/github';
import * as core from '@actions/core';
import { RequestError } from '@octokit/request-error'
import { FileCoverageMode } from './FileCoverageMode'
type Octokit = ReturnType<typeof github.getOctokit>;
export async function getPullChanges(fileCoverageMode: FileCoverageMode): Promise<string[]> {
// Skip Changes collection if we don't need it
if (fileCoverageMode === FileCoverageMode.None) {
return [];
}
// Skip Changes collection if we can't do it
if (!github.context.payload?.pull_request) {
return [];
}
const gitHubToken = core.getInput('github-token').trim();
const prNumber = github.context.payload.pull_request.number
try {
const octokit: Octokit = github.getOctokit(gitHubToken);
const paths: string[] = []
core.startGroup(`Fetching list of changed files for PR#${prNumber} from Github API`)
const iterator = octokit.paginate.iterator(
octokit.rest.pulls.listFiles, {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: prNumber,
per_page: 100
}
);
for await (const response of iterator) {
core.info(`Received ${response.data.length} items`)
for (const file of response.data) {
core.debug(`[${file.status}] ${file.filename}`)
if (['added', 'modified'].includes(file.status)) {
paths.push(file.filename)
}
}
}
return paths
}
catch(error) {
if (error instanceof RequestError && (error.status === 404 || error.status === 403)) {
core.warning(`Couldn't fetch changes of PR due to error:\n[${error.name}]\n${error.message}`)
return [];
} else {
throw error;
}
} finally {
core.endGroup()
}
}