-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
105 lines (87 loc) · 2.66 KB
/
script.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
import { composeCreateOrUpdateTextFile } from "@octokit/plugin-create-or-update-text-file";
/**
* Setup renovate by adding `{"renovate": {"extends": "..."}}` to the JSON file
*
* @param {import('@octoherd/cli').Octokit} octokit
* @param {import('@octoherd/cli').Repository} repository
* @param { {extends: string, path?: string} } options Custom user options passed to the CLI
*/
export async function script(octokit, repository, options) {
if (!options.extends) {
throw new Error("--extends is required");
}
if (!repository.owner) {
throw new Error("repository must have an 'owner' associated");
}
const owner = repository.owner.login;
const repo = repository.name;
const path = options.path || "package.json";
if (repository.archived) {
octokit.log.info(
{ owner, repo, updated: false },
`${repository.html_url} is archived`
);
return;
}
if (repository.name === ".github") {
octokit.log.info(
{ owner, repo, updated: false },
`${repository.name} is a blacklisted repository name. No changed applied.`
);
return;
}
let currentExtends;
const {
updated,
data: { commit },
} = await composeCreateOrUpdateTextFile(octokit, {
owner,
repo,
path,
content: ({ exists, content }) => {
let jsonFile = !exists ? {} : JSON.parse(content);
const isPackageJson = path.endsWith("package.json");
if (isPackageJson && !jsonFile.renovate) {
jsonFile.renovate = {};
}
const renovateConfigObj = isPackageJson ? jsonFile.renovate : jsonFile;
const newExtends = options.extends.split(/\s*,\s*/);
currentExtends = renovateConfigObj.extends;
if (
currentExtends &&
newExtends.sort().join(",") === currentExtends.sort().join(",")
) {
octokit.log.info(
{ owner, repo, currentExtends, updated: false },
`"extends" is already set to "${JSON.stringify(currentExtends)}" in ${
repository.html_url
}`
);
return content;
}
renovateConfigObj.extends = newExtends;
return JSON.stringify(jsonFile, null, " ") + "\n";
},
message: "build: renovate setup",
});
if (!updated) {
octokit.log.info(
{ owner, repo, updated },
`"no changes applied to ${path}`
);
return;
}
if (currentExtends) {
octokit.log.warn(
{ owner, repo, currentExtends, updated },
`Existing "extends" setting was changed from "${JSON.stringify(
currentExtends
)}" in ${commit.html_url}`
);
} else {
octokit.log.info(
{ owner, repo, updated },
`"extends" setting added in ${commit.html_url}`
);
}
}