Skip to content

Commit 6dcab84

Browse files
authored
Use pagination to get all repos (#11)
1 parent 4bf42f2 commit 6dcab84

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@ Run with the `--verbose` flag to see debug information
5252
| --keep-old | Keep the old branch rather than deleting it | false |
5353
| --dry-run | Output log messages only. Do not make any changes | false |
5454
| --list-repos-only | List repos that would be affected, then exit | false |
55+
| --skip-forks | Skips forked repositories | false |
5556
| --old | The name of the branch to rename | master |
5657
| --new | The new branch name | main |

bin/github-default-branch

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@
6767
}
6868

6969
const [owner, repo] = r.split("/", 2);
70-
const currentMasterSha = await getBranchSha(owner, repo, old, octokit);
70+
let currentMasterSha;
71+
72+
try {
73+
currentMasterSha = await getBranchSha(owner, repo, old, octokit);
74+
} catch (e) {
75+
// Typically, this will fail when the old branch, i.e. master, doesn't exist.
76+
console.log(`⚠️ Skipping ${r}: ${e.message}\n`)
77+
continue;
78+
}
7179

7280
if (argv.verbose) {
7381
console.log(`✏️ Creating branch [${target}] at [${currentMasterSha}]`);

src/get-repos.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,26 @@ module.exports = async function (args, octokit) {
33
return [args.repo];
44
}
55

6-
let repos;
6+
let repos = [];
77
if (args.org) {
8-
({ data: repos } = await octokit.repos.listForOrg({
9-
org: args.org,
10-
}));
8+
repos = await octokit.paginate(
9+
octokit.repos.listForOrg,
10+
{
11+
org: args.org,
12+
per_page: 100
13+
},
14+
(response) => response.data
15+
);
1116
}
1217

1318
if (args.user) {
14-
({ data: repos } = await octokit.repos.listForAuthenticatedUser());
19+
repos = await octokit.paginate(
20+
octokit.repos.listForAuthenticatedUser,
21+
{
22+
per_page: 100
23+
},
24+
(response) => response.data
25+
);
1526

1627
// Filter down to repos owned by the provided user
1728
// This is different to using affiliation: owner as it allows
@@ -20,5 +31,9 @@ module.exports = async function (args, octokit) {
2031
repos = repos.filter((repo) => repo.owner.login == args.user);
2132
}
2233

34+
if (args.skipForks) {
35+
repos = repos.filter((repo) => !repo.fork);
36+
}
37+
2338
return repos.map((repo) => repo.full_name);
2439
};

0 commit comments

Comments
 (0)