This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
notify-about-routes-changes.js
71 lines (59 loc) · 1.9 KB
/
notify-about-routes-changes.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
module.exports = notifyAboutRoutesChanges;
const execa = require("execa");
const ghGot = require("gh-got");
async function notifyAboutRoutesChanges({ repoSlug, token }) {
console.log("🤖 Checking for route changes ...");
const branchName = `cron/routes-changes/${new Date()
.toISOString()
.substr(0, 10)}`;
// check if any routes files have been changed
const { stdout } = await execa("git", ["status", "openapi"]);
if (/nothing to commit/.test(stdout)) {
console.log("🤖 No changes found.");
return;
}
console.log(
"🤖 Routes changes detected in cron job. Creating pull request ..."
);
// count changes
const diffResult = await execa("git diff --stat openapi/*/*/", {
shell: true
});
const changesSummary = diffResult.stdout
.split("\n")
.pop()
.trim()
.replace(/file/, "operation");
// push changes back to GitHub
await execa("git", ["checkout", "-b", branchName]);
await execa("git", ["add", "cache"]);
await execa("git", ["commit", "-m", "build: cache"]);
await execa("git", ["add", "openapi"]);
await execa("git", ["commit", "-m", "build: openapi"]);
await execa("git", [
"push",
`https://x-access-token:${token}@github.com/${repoSlug}.git`,
`HEAD:${branchName}`
]);
await execa("git", ["checkout", "-"]);
const listOfChangedFiles = await execa("git diff --name-only openapi/*/*/", {
shell: true
});
const listOfChangedFilesString = listOfChangedFiles
.split(/\n/)
.map(line => "- " + line)
.join("\n");
// start pullrequest
const { body } = await ghGot.post(`repos/${repoSlug}/pulls`, {
token,
body: {
title: `🤖🚨 ${changesSummary}`,
head: branchName,
base: "master",
body: `Dearest humans,
I found changes in the following operations, please have a look:
${listOfChangedFilesString}`
}
});
console.log(`🤖 Pull request created: ${body.html_url}`);
}