Skip to content

Commit 0e763e9

Browse files
authored
feat(script): add support to 'path' option to specify the filepath of renovate's config (#29)
* feat(script): add support to 'path' option to specify the filepath of renovate's config * docs(readme): add information about the new flag
1 parent 295ffe0 commit 0e763e9

File tree

3 files changed

+133
-19
lines changed

3 files changed

+133
-19
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ npx @octoherd/script-setup-renovate \
2525
| option | type | description |
2626
| ---------------------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
2727
| `--extends` | string | **Required**. Location from where should inherit settings from, see the [`extends` documentation](https://docs.renovatebot.com/configuration-options/#extends). Example: `--extends "github>octoherd/.github"` |
28+
| `--path` | string | File path of the configuration file for Renovate, see the [`Configuration options` documentation](https://docs.renovatebot.com/configuration-options/). Example: `--path ".github/renovate.json"` |
2829
| `--octoherd-token`, `-T` | string | A personal access token ([create](https://github.com/settings/tokens/new?scopes=repo)). Script will create one if option is not set |
2930
| `--octoherd-repos`, `-R` | array of strings | One or multiple space-separated repositories in the form of `repo-owner/repo-name`. `repo-owner/*` will find all repositories for one owner. `*` will find all repositories the user has access to. Will prompt for repositories if not set |
3031
| `--octoherd-bypass-confirms` | boolean | Bypass prompts to confirm mutating requests |

script.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { composeCreateOrUpdateTextFile } from "@octokit/plugin-create-or-update-text-file";
22

33
/**
4-
* Setup renovate by adding `{"renovate": {"extends": "..."}}` to the package.json
4+
* Setup renovate by adding `{"renovate": {"extends": "..."}}` to the JSON file
55
*
66
* @param {import('@octoherd/cli').Octokit} octokit
77
* @param {import('@octoherd/cli').Repository} repository
8-
* @param { {extends: string} } options Custom user options passed to the CLI
8+
* @param { {extends: string, path?: string} } options Custom user options passed to the CLI
99
*/
1010
export async function script(octokit, repository, options) {
1111
if (!options.extends) {
@@ -18,7 +18,7 @@ export async function script(octokit, repository, options) {
1818

1919
const owner = repository.owner.login;
2020
const repo = repository.name;
21-
const path = "package.json";
21+
const path = options.path || "package.json";
2222

2323
if (repository.archived) {
2424
octokit.log.info(
@@ -38,20 +38,18 @@ export async function script(octokit, repository, options) {
3838
repo,
3939
path,
4040
content: ({ exists, content }) => {
41-
let pkg;
41+
let jsonFile = !exists ? {} : JSON.parse(content);
4242

43-
if (!exists) {
44-
pkg = {};
45-
} else {
46-
pkg = JSON.parse(content);
47-
}
43+
const isPackageJson = path.endsWith("package.json");
4844

49-
if (!pkg.renovate) {
50-
pkg.renovate = {};
45+
if (isPackageJson && !jsonFile.renovate) {
46+
jsonFile.renovate = {};
5147
}
5248

49+
const renovateConfigObj = isPackageJson ? jsonFile.renovate : jsonFile;
50+
5351
const newExtends = options.extends.split(/\s*,\s*/);
54-
currentExtends = pkg.renovate.extends;
52+
currentExtends = renovateConfigObj.extends;
5553

5654
if (
5755
currentExtends &&
@@ -67,9 +65,9 @@ export async function script(octokit, repository, options) {
6765
return content;
6866
}
6967

70-
pkg.renovate.extends = newExtends;
68+
renovateConfigObj.extends = newExtends;
7169

72-
return JSON.stringify(pkg);
70+
return JSON.stringify(jsonFile);
7371
},
7472
message: "build: renovate setup",
7573
});

script.test.js

+120-5
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,103 @@ test("adds 'renovate' entry to package.json if it did not exist", async () => {
6868
});
6969
});
7070

71-
test("adds 'extends' entry to 'renovate' entry if it did not exist", async () => {
71+
test("adds 'renovate' entry ONLY to package.json files", async () => {
72+
const path = "renovate.json";
73+
74+
const originalPackageJson = {
75+
name: "octoherd-cli",
76+
version: "0.0.0",
77+
description: "",
78+
main: "index.js",
79+
scripts: {
80+
test: 'echo "Error: no test specified" && exit 1',
81+
},
82+
author: "",
83+
license: "ISC",
84+
};
85+
86+
nock("https://api.github.com")
87+
.get(`/repos/${repository.owner.login}/${repository.name}/contents/${path}`)
88+
.reply(200, {
89+
type: "file",
90+
sha: "randomSha",
91+
content: Buffer.from(JSON.stringify(originalPackageJson)).toString(
92+
"base64"
93+
),
94+
})
95+
.put(
96+
`/repos/${repository.owner.login}/${repository.name}/contents/${path}`,
97+
(body) => {
98+
const pkg = JSON.parse(Buffer.from(body.content, "base64").toString());
99+
100+
equal(pkg, {
101+
...originalPackageJson,
102+
extends: ["github>octoherd/.github"],
103+
});
104+
105+
return true;
106+
}
107+
)
108+
.reply(200, { commit: { html_url: "link to commit" } });
109+
110+
await script(getOctokitForTests(), repository, {
111+
extends: "github>octoherd/.github",
112+
path,
113+
});
114+
});
115+
116+
test("'path' option recognizes paths containing package.json as package.json files", async () => {
117+
const path = "my/random/path/package.json";
118+
119+
const originalPackageJson = {
120+
name: "octoherd-cli",
121+
version: "0.0.0",
122+
description: "",
123+
main: "index.js",
124+
scripts: {
125+
test: 'echo "Error: no test specified" && exit 1',
126+
},
127+
author: "",
128+
license: "ISC",
129+
};
130+
131+
nock("https://api.github.com")
132+
.get(
133+
`/repos/${repository.owner.login}/${
134+
repository.name
135+
}/contents/${encodeURIComponent(path)}`
136+
)
137+
.reply(200, {
138+
type: "file",
139+
sha: "randomSha",
140+
content: Buffer.from(JSON.stringify(originalPackageJson)).toString(
141+
"base64"
142+
),
143+
})
144+
.put(
145+
`/repos/${repository.owner.login}/${
146+
repository.name
147+
}/contents/${encodeURIComponent(path)}`,
148+
(body) => {
149+
const pkg = JSON.parse(Buffer.from(body.content, "base64").toString());
150+
151+
equal(pkg, {
152+
...originalPackageJson,
153+
renovate: { extends: ["github>octoherd/.github"] },
154+
});
155+
156+
return true;
157+
}
158+
)
159+
.reply(200, { commit: { html_url: "link to commit" } });
160+
161+
await script(getOctokitForTests(), repository, {
162+
extends: "github>octoherd/.github",
163+
path,
164+
});
165+
});
166+
167+
test("adds 'extends' entry if it did not exist", async () => {
72168
const originalPackageJson = {
73169
name: "octoherd-cli",
74170
version: "0.0.0",
@@ -113,7 +209,7 @@ test("adds 'extends' entry to 'renovate' entry if it did not exist", async () =>
113209
});
114210
});
115211

116-
test("replaces 'extends' entry if renovate.extends already existed", async () => {
212+
test("replaces 'extends' entry if extends already existed", async () => {
117213
const originalPackageJson = {
118214
name: "octoherd-cli",
119215
version: "0.0.0",
@@ -217,7 +313,7 @@ test("throws if --extends option is NOT provided", async () => {
217313
equal(nock.pendingMocks().length, 0);
218314
});
219315

220-
test("throws if package.json is NOT a file", async () => {
316+
test("throws if JSON file provided is NOT a file", async () => {
221317
nock("https://api.github.com")
222318
.get(
223319
`/repos/${repository.owner.login}/${repository.name}/contents/package.json`
@@ -240,7 +336,7 @@ test("throws if package.json is NOT a file", async () => {
240336
}
241337
});
242338

243-
test("throws if server fails when retrieving package.json", async () => {
339+
test("throws if server fails when retrieving the JSON file", async () => {
244340
nock("https://api.github.com")
245341
.get(
246342
`/repos/${repository.owner.login}/${repository.name}/contents/package.json`
@@ -287,7 +383,26 @@ test("returns if repository is archived", async () => {
287383
equal(nock.pendingMocks().length, 0);
288384
});
289385

290-
test("creates package.json if file does NOT exist in the repository", async () => {
386+
test("creates JSON file if file does NOT exist in the repository", async () => {
387+
const path = "renovate.json";
388+
389+
nock("https://api.github.com")
390+
.get(`/repos/${repository.owner.login}/${repository.name}/contents/${path}`)
391+
.reply(404)
392+
.put(`/repos/${repository.owner.login}/${repository.name}/contents/${path}`)
393+
.reply(201, { commit: { html_url: "link to commit" } });
394+
395+
try {
396+
await script(getOctokitForTests(), repository, {
397+
extends: "github>octoherd/.github",
398+
path,
399+
});
400+
} catch (error) {
401+
unreachable("should have NOT thrown");
402+
}
403+
});
404+
405+
test("creates package.json file if file no 'path' option is provided", async () => {
291406
nock("https://api.github.com")
292407
.get(
293408
`/repos/${repository.owner.login}/${repository.name}/contents/package.json`

0 commit comments

Comments
 (0)