Skip to content

Commit 23f67cc

Browse files
committed
Revert 6RiverSystems specific changes
* Added default old/new parameters back * Update README instructions to point at original repo, not fork * Replacements should not use a regex * Add log message if default branch is not updated
1 parent d20c137 commit 23f67cc

File tree

3 files changed

+51
-65
lines changed

3 files changed

+51
-65
lines changed

README.md

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ npm install -g github-default-branch
2222
## Development
2323

2424
```shell
25-
git clone https://github.com/6RiverSystems/github-default-branch.git
25+
git clone https://github.com/mheap/github-default-branch.git
2626
cd github-default-branch
2727
npm ci
2828
```
@@ -85,35 +85,33 @@ Like this:
8585

8686
```javascript
8787
module.exports = function ({
88-
owner, // string - repo owner
89-
repo, // string - repo name
90-
old, // string - old branch name
91-
target, // string - new branch name
92-
octokit, // Octokit - oktokit instance
93-
verbose, // boolean - verbose flag
94-
isDryRun, // boolean - dry run flag
88+
owner, // string - repo owner
89+
repo, // string - repo name
90+
old, // string - old branch name
91+
target, // string - new branch name
92+
octokit, // Octokit - oktokit instance
93+
verbose, // boolean - verbose flag
94+
isDryRun, // boolean - dry run flag
9595
}) {
96-
// code goes here
97-
return {
98-
path: '<path to file in repo>',
99-
replacements: [
100-
{
101-
from: '<from pattern>',
102-
to: '<to pattern>',
103-
},
104-
{
105-
from: '<from pattern>',
106-
to: '<to pattern>',
107-
},
108-
],
109-
};
110-
}
111-
96+
// code goes here
97+
return {
98+
path: "<path to file in repo>",
99+
replacements: [
100+
{
101+
from: "<from pattern>",
102+
to: "<to pattern>",
103+
},
104+
{
105+
from: "<from pattern>",
106+
to: "<to pattern>",
107+
},
108+
],
109+
};
110+
};
112111
```
113112

114113
The file with the path in your repo will have any line matching `from` be swapped out with `to`
115114

116-
117115
### Known Issues
118116

119117
The replacement system gives you octokit, that's great! Unfortunately replacement functions do not currently support asynchronous calls, that's bad.

bin/github-default-branch

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@
105105
const updateContent = require("../src/update-content");
106106
const updateBranchProtection = require("../src/branch-protection");
107107

108-
const old = argv.old;
109-
const target = argv.new;
108+
const old = argv.old || "master";
109+
const target = argv.new || "main";
110110
const verbose = argv.verbose;
111111

112112
// Make sure they want to do this
@@ -145,7 +145,7 @@
145145
currentMasterSha = await getBranchSha(owner, repo, old, octokit);
146146
} catch (e) {
147147
// Typically, this will fail when the old branch, i.e. master, doesn't exist.
148-
console.log(`⚠️ Skipping ${r}: ${e.message}\n`)
148+
console.log(`⚠️ Skipping ${r}: ${e.message}\n`);
149149
continue;
150150
}
151151

@@ -154,13 +154,7 @@
154154
}
155155

156156
if (!isDryRun) {
157-
await createBranch(
158-
owner,
159-
repo,
160-
target,
161-
currentMasterSha,
162-
octokit
163-
);
157+
await createBranch(owner, repo, target, currentMasterSha, octokit);
164158
}
165159

166160
// List all PRs
@@ -205,16 +199,22 @@
205199
data: { default_branch: defaultBranch },
206200
} = await octokit.repos.get({
207201
owner,
208-
repo
202+
repo,
209203
});
210204

211-
if (!isDryRun && defaultBranch === old) {
212-
// Update the default branch in the repo
213-
await octokit.repos.update({
214-
owner,
215-
repo,
216-
default_branch: target,
217-
});
205+
if (!isDryRun) {
206+
if (defaultBranch === old) {
207+
// Update the default branch in the repo
208+
await octokit.repos.update({
209+
owner,
210+
repo,
211+
default_branch: target,
212+
});
213+
} else {
214+
console.log(
215+
`✏️ Default branch is different to 'old' value. Not updating default branch`
216+
);
217+
}
218218
}
219219

220220
if (verbose) {
@@ -244,7 +244,7 @@
244244
target,
245245
octokit,
246246
verbose,
247-
isDryRun
247+
isDryRun,
248248
});
249249

250250
// Add an empty new line to break up the output for each repo

src/update-content.js

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
const fs = require('fs');
2-
const util = require('util');
1+
const fs = require("fs");
2+
const util = require("util");
33
const replaceAll = require("string.prototype.replaceall");
44

55
const ls = util.promisify(fs.readdir);
66

77
module.exports = async function (options) {
8-
const {
9-
owner,
10-
repo,
11-
octokit,
12-
verbose,
13-
isDryRun,
14-
} = options;
8+
const { owner, repo, octokit, verbose, isDryRun } = options;
159
const replacementsDir = `${__dirname}/replacements`;
16-
const files = (await ls(replacementsDir)).filter((f) => f.endsWith('.js'));
10+
const files = (await ls(replacementsDir)).filter((f) => f.endsWith(".js"));
1711
const replacements = files.reduce((acc, next) => {
18-
const { path, replacements } = require(`${replacementsDir}/${next}`)(options);
12+
const { path, replacements } = require(`${replacementsDir}/${next}`)(
13+
options
14+
);
1915
return Object.assign(acc, { [path]: replacements });
2016
}, {});
2117
for (let path in replacements) {
@@ -24,23 +20,15 @@ module.exports = async function (options) {
2420

2521
let content = file.content;
2622
for (let r of replacements[path]) {
27-
const re = new RegExp(r.from, "g");
28-
content = replaceAll(content, re, r.to);
23+
content = replaceAll(content, r.from, r.to);
2924
}
3025

3126
if (content !== file.content) {
3227
if (verbose) {
3328
console.log(`✏️ Updating [${path}]`);
3429
}
3530
if (!isDryRun) {
36-
await writeFile(
37-
owner,
38-
repo,
39-
path,
40-
content,
41-
file.sha,
42-
octokit
43-
);
31+
await writeFile(owner, repo, path, content, file.sha, octokit);
4432
}
4533
} else {
4634
if (verbose) {

0 commit comments

Comments
 (0)