-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.js
121 lines (105 loc) · 2.86 KB
/
source.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const yaml = require("yaml");
const INITIAL_COMMIT_SHA = "0000000000000000000000000000000000000000";
const fetchConfigData = async ({octokit, owner, repo, path, ref}) => {
try {
const {data} = await octokit.repos.getContents({
owner,
repo,
path,
ref
});
return Buffer.from(data.content, "base64").toString();
} catch (e) {
console.error("fetchConfigData", e);
return null;
}
};
const getConfig = async options => {
const rawConfig = await fetchConfigData(options);
if (!rawConfig) return {rawConfig: null, parsedConfig: null};
try {
const parsedConfig = rawConfig.split("\n---\n").map(yaml.parse);
return {rawConfig, parsedConfig};
} catch (e) {
console.error("getConfig", e);
return {rawConfig, parsedConfig: null};
}
};
const getBranchHead = async ({octokit, owner, repo, branch}) => {
try {
const {data} = await octokit.repos.getBranch({
owner,
repo,
branch
});
return data.commit.sha;
} catch (e) {
console.error("getBranchHead", e);
return null;
}
};
const getFilesDiff = async apiCallPromise => {
try {
const {data} = await apiCallPromise;
return data.files.map(file => file.filename);
} catch (e) {
console.error("getFilesDiff", e);
return [];
}
};
const getDiffWithBase = ({octokit, owner, repo, base, head}) =>
getFilesDiff(
octokit.repos.compareCommits({
owner,
repo,
base,
head
})
);
const getDiffForCommit = ({octokit, owner, repo, ref}) =>
getFilesDiff(
octokit.repos.getCommit({
owner,
repo,
ref
})
);
const getChangesForPullRequest = async ({octokit, owner, repo, buildData}) => {
const baseBranchHead = await getBranchHead({
octokit,
owner,
repo,
branch: buildData.target
});
if (!baseBranchHead) return [];
return getDiffWithBase({
octokit,
owner,
repo,
base: baseBranchHead,
head: buildData.after
});
};
const getChangesForBranch = ({octokit, owner, repo, buildData}) => {
if (!buildData.before || buildData.before === INITIAL_COMMIT_SHA) {
return getDiffForCommit({octokit, owner, repo, ref: buildData.after});
}
return getDiffWithBase({
octokit,
owner,
repo,
base: buildData.before,
head: buildData.after
});
};
const getChangedFiles = async options => {
const isPullRequest = options.buildData.source !== options.buildData.target;
if (isPullRequest) {
return getChangesForPullRequest(options);
}
return getChangesForBranch(options);
};
module.exports = {
getConfig,
getChangedFiles
};