forked from devonfw-tutorials/katacoda-scenarios-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunForAllPullRequests.js
75 lines (66 loc) · 2.62 KB
/
runForAllPullRequests.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
const https = require('https');
const child_process = require('child_process');
const fse = require('fs-extra');
const fs = require('fs');
const rimraf = require("rimraf");
const options = {
port: 443,
method: 'GET',
headers: {
'Accept': 'application/json',
'User-Agent': 'action'
}
}
function download(url, f) {
https.get(url, options, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
f(data);
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}
download('https://api.github.com/repos/devonfw-forge/tutorials/pulls', function (data) {
let json = JSON.parse(data);
console.log(json);
let exitCode = 0;
let repoDir = "repo/";
let reposDirs = fs.readdirSync(repoDir);
for (let index in reposDirs) {
if (/^[0-9]+_.+$/.test(reposDirs[index])) {
rimraf.sync(repoDir + reposDirs[index]);
}
}
for (var i in json) {
var e = json[i];
console.log("Clone " + e.head.repo.clone_url + " -> " + e.head.ref);
let cp = child_process.spawnSync("rm -R playbooks && git clone " + e.head.repo.clone_url + " playbooks && branch_name=$(git symbolic-ref -q HEAD) && branch_name=${branch_name##refs/heads/} && branch_name=${branch_name:-HEAD} && echo $branch_name && if [ \"$branch_name\" != \"" + e.head.ref + "\" ]; then git checkout " + e.head.ref + "; fi && sh buildRun.sh", { shell: true, encoding: 'utf-8' });
console.log(cp);
if (cp.status != 0) {
exitCode = cp.status;
}
else {
let katacodaDir = "build/output/katacoda/";
let tutorialDirs = fs.readdirSync(katacodaDir);
for (let index in tutorialDirs) {
let dir = katacodaDir + tutorialDirs[index];
let targetDir = "repo/" + e.number + "_" + e.title.replace(/[^A-Za-z0-9]/g, "-") + "_" + tutorialDirs[index];
console.log("Copy " + dir + " -> " + targetDir);
fse.copySync(dir, targetDir);
}
}
rimraf.sync("build/output/katacoda/");
}
let cp = child_process.spawnSync("cd repo && ls -al && git fetch --all && git checkout main && git add -A && git diff-index --quiet HEAD -- || (git config user.email \"devonfw\" && git config user.name \"devonfw\" && git commit -m \"Updated tutorials\" && git push)", { shell: true, encoding: 'utf-8' });
console.log(cp);
if (cp.status != 0) {
process.exit(cp.status);
}
if (exitCode != 0) {
process.exit(exitCode);
}
});