-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetChangedFiles.ts
40 lines (35 loc) · 1.18 KB
/
getChangedFiles.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
import { context, getOctokit } from '@actions/github';
import { octobase, vars } from './constants';
export interface ChangedFile {
fileName: string,
patch?: string,
}
export async function getChangedFiles(
packagePath: string
): Promise<Record<string, ChangedFile>> {
const octokit = getOctokit(vars.token);
const { before, after } = context.payload;
// Get the list of commits between the before and after commits
const { data: commitsData } = await octokit.rest.repos.compareCommitsWithBasehead({
...octobase,
basehead: `${before}...${after}`,
});
// Iterate over each commit and get the list of changed files
const changedFiles: Record<string, ChangedFile> = {};
for (const commit of commitsData.commits) {
const { data: filesData } = await octokit.rest.repos.getCommit({
...octobase,
ref: commit.sha,
});
if (Array.isArray(filesData.files)) {
filesData.files.forEach((file) => {
changedFiles[file.filename] = {
fileName: file.filename,
patch: file.filename === packagePath ? file.patch : undefined,
};
});
}
}
// Return the list of all changed files in the push
return changedFiles;
}