Skip to content

Commit c6e91b1

Browse files
committed
Add support for fetching the default branch
This works with both 'master' and 'main' defaults (or other custom defined default branches)
1 parent 28cf34a commit c6e91b1

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function normalizeName(problemName) {
2929
return problemName.toLowerCase().replace(/\s/g, '_');
3030
}
3131

32-
async function commit(octokit, owner, repo, commitInfo, treeSHA, latestCommitSHA, submission) {
32+
async function commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission) {
3333
const name = normalizeName(submission.title);
3434
console.log(`Committing solution for ${name}...`);
3535

@@ -75,7 +75,7 @@ async function commit(octokit, owner, repo, commitInfo, treeSHA, latestCommitSHA
7575
owner: owner,
7676
repo: repo,
7777
sha: commitResponse.data.sha,
78-
ref: 'heads/master',
78+
ref: 'heads/' + defaultBranch,
7979
force: true
8080
});
8181

@@ -185,14 +185,21 @@ async function sync(githubToken, owner, repo, filterDuplicateSecs, leetcodeCSRFT
185185
} while (response.data.has_next);
186186

187187
// We have all submissions we want to write to GitHub now.
188+
// First, get the default branch to write to.
189+
const repoInfo = await octokit.repos.get({
190+
owner: owner,
191+
repo: repo,
192+
});
193+
const defaultBranch = repoInfo.data.default_branch;
194+
console.log(`Default branch for ${owner}/${repo}: ${defaultBranch}`);
188195
// Write in reverse order (oldest first), so that if there's errors, the last sync time
189196
// is still valid.
190197
console.log(`Syncing ${submissions.length} submissions...`);
191198
let latestCommitSHA = commits.data[0].sha;
192199
let treeSHA = commits.data[0].commit.tree.sha;
193200
for (i = submissions.length - 1; i >= 0; i--) {
194201
submission = submissions[i];
195-
[treeSHA, latestCommitSHA] = await commit(octokit, owner, repo, commitInfo, treeSHA, latestCommitSHA, submission);
202+
[treeSHA, latestCommitSHA] = await commit(octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission);
196203
}
197204
console.log('Done syncing all submissions.');
198205
}

0 commit comments

Comments
 (0)