Skip to content

Commit

Permalink
feat(script): add support to 'path' option to specify the filepath of…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
oscard0m authored Aug 31, 2022
1 parent 295ffe0 commit 0e763e9
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ npx @octoherd/script-setup-renovate \
| option | type | description |
| ---------------------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--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"` |
| `--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"` |
| `--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 |
| `--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 |
| `--octoherd-bypass-confirms` | boolean | Bypass prompts to confirm mutating requests |
Expand Down
26 changes: 12 additions & 14 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { composeCreateOrUpdateTextFile } from "@octokit/plugin-create-or-update-text-file";

/**
* Setup renovate by adding `{"renovate": {"extends": "..."}}` to the package.json
* 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} } options Custom user options passed to the CLI
* @param { {extends: string, path?: string} } options Custom user options passed to the CLI
*/
export async function script(octokit, repository, options) {
if (!options.extends) {
Expand All @@ -18,7 +18,7 @@ export async function script(octokit, repository, options) {

const owner = repository.owner.login;
const repo = repository.name;
const path = "package.json";
const path = options.path || "package.json";

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

if (!exists) {
pkg = {};
} else {
pkg = JSON.parse(content);
}
const isPackageJson = path.endsWith("package.json");

if (!pkg.renovate) {
pkg.renovate = {};
if (isPackageJson && !jsonFile.renovate) {
jsonFile.renovate = {};
}

const renovateConfigObj = isPackageJson ? jsonFile.renovate : jsonFile;

const newExtends = options.extends.split(/\s*,\s*/);
currentExtends = pkg.renovate.extends;
currentExtends = renovateConfigObj.extends;

if (
currentExtends &&
Expand All @@ -67,9 +65,9 @@ export async function script(octokit, repository, options) {
return content;
}

pkg.renovate.extends = newExtends;
renovateConfigObj.extends = newExtends;

return JSON.stringify(pkg);
return JSON.stringify(jsonFile);
},
message: "build: renovate setup",
});
Expand Down
125 changes: 120 additions & 5 deletions script.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,103 @@ test("adds 'renovate' entry to package.json if it did not exist", async () => {
});
});

test("adds 'extends' entry to 'renovate' entry if it did not exist", async () => {
test("adds 'renovate' entry ONLY to package.json files", async () => {
const path = "renovate.json";

const originalPackageJson = {
name: "octoherd-cli",
version: "0.0.0",
description: "",
main: "index.js",
scripts: {
test: 'echo "Error: no test specified" && exit 1',
},
author: "",
license: "ISC",
};

nock("https://api.github.com")
.get(`/repos/${repository.owner.login}/${repository.name}/contents/${path}`)
.reply(200, {
type: "file",
sha: "randomSha",
content: Buffer.from(JSON.stringify(originalPackageJson)).toString(
"base64"
),
})
.put(
`/repos/${repository.owner.login}/${repository.name}/contents/${path}`,
(body) => {
const pkg = JSON.parse(Buffer.from(body.content, "base64").toString());

equal(pkg, {
...originalPackageJson,
extends: ["github>octoherd/.github"],
});

return true;
}
)
.reply(200, { commit: { html_url: "link to commit" } });

await script(getOctokitForTests(), repository, {
extends: "github>octoherd/.github",
path,
});
});

test("'path' option recognizes paths containing package.json as package.json files", async () => {
const path = "my/random/path/package.json";

const originalPackageJson = {
name: "octoherd-cli",
version: "0.0.0",
description: "",
main: "index.js",
scripts: {
test: 'echo "Error: no test specified" && exit 1',
},
author: "",
license: "ISC",
};

nock("https://api.github.com")
.get(
`/repos/${repository.owner.login}/${
repository.name
}/contents/${encodeURIComponent(path)}`
)
.reply(200, {
type: "file",
sha: "randomSha",
content: Buffer.from(JSON.stringify(originalPackageJson)).toString(
"base64"
),
})
.put(
`/repos/${repository.owner.login}/${
repository.name
}/contents/${encodeURIComponent(path)}`,
(body) => {
const pkg = JSON.parse(Buffer.from(body.content, "base64").toString());

equal(pkg, {
...originalPackageJson,
renovate: { extends: ["github>octoherd/.github"] },
});

return true;
}
)
.reply(200, { commit: { html_url: "link to commit" } });

await script(getOctokitForTests(), repository, {
extends: "github>octoherd/.github",
path,
});
});

test("adds 'extends' entry if it did not exist", async () => {
const originalPackageJson = {
name: "octoherd-cli",
version: "0.0.0",
Expand Down Expand Up @@ -113,7 +209,7 @@ test("adds 'extends' entry to 'renovate' entry if it did not exist", async () =>
});
});

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

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

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

test("creates package.json if file does NOT exist in the repository", async () => {
test("creates JSON file if file does NOT exist in the repository", async () => {
const path = "renovate.json";

nock("https://api.github.com")
.get(`/repos/${repository.owner.login}/${repository.name}/contents/${path}`)
.reply(404)
.put(`/repos/${repository.owner.login}/${repository.name}/contents/${path}`)
.reply(201, { commit: { html_url: "link to commit" } });

try {
await script(getOctokitForTests(), repository, {
extends: "github>octoherd/.github",
path,
});
} catch (error) {
unreachable("should have NOT thrown");
}
});

test("creates package.json file if file no 'path' option is provided", async () => {
nock("https://api.github.com")
.get(
`/repos/${repository.owner.login}/${repository.name}/contents/package.json`
Expand Down

0 comments on commit 0e763e9

Please sign in to comment.