generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (56 loc) · 1.71 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
54
55
56
57
58
59
60
61
const core = require('@actions/core');
const github = require('@actions/github');
const octokit = github.getOctokit(core.getInput('github-token'));
async function run() {
const repoInfo = github.context.repo;
const latestGeneration = await octokit.git.getCommit({
...repoInfo,
commit_sha: github.context.sha,
});
const planet = await octokit.git.getTree({
...repoInfo,
tree_sha: latestGeneration.data.tree.sha,
recursive: 1,
});
const newPlanet = await snap(repoInfo, planet.data.tree);
const newPopulation = await octokit.git.createCommit({
...repoInfo,
tree: newPlanet.data.sha,
message: 'Restore perfect balance ✨',
parents: [latestGeneration.data.sha],
});
await octokit.git.updateRef({
...repoInfo,
ref: github.context.ref.substr(5), // removing refs/ from context's ref string
sha: newPopulation.data.sha,
});
}
async function snap(repoInfo, citizens) {
const theLuckyHalf = [];
for (let citizen of citizens) {
if (citizen.type == 'tree' && citizen.path != '.github') {
const city = await octokit.git.getTree({
...repoInfo,
tree_sha: citizen.sha,
});
const newCity = await snap(repoInfo, city.data.tree);
const allSnapped = !newCity;
if (allSnapped) {
// if all citizens snapped, city is considerred snapped
continue;
}
citizen.sha = newCity.data.sha;
theLuckyHalf.push(citizen);
} else if (citizen.type == 'blob') {
if (Math.random() > 0.5) {
theLuckyHalf.push(citizen);
}
}
}
const noOneSurvived = theLuckyHalf.length === 0;
if (noOneSurvived) {
return null;
}
return octokit.git.createTree({ ...repoInfo, tree: theLuckyHalf });
}
run();