feat(repository): add CREATE_DEFAULT_BRANCH flag to create default branch instead of renaming - #1051
Conversation
…anch instead of renaming When repository.default_branch is configured and differs from the repo's current default branch, safe-settings renames the current default branch. With CREATE_DEFAULT_BRANCH=true, if the configured branch does not exist, safe-settings instead creates a new branch off the current default and promotes it to default, leaving the existing default branch untouched. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds an opt-in feature flag to change how repository.default_branch reconciliation works when the configured branch does not exist, allowing safe-settings to create the configured default branch (instead of renaming the current default branch) and then promote it to default.
Changes:
- Introduces
CREATE_DEFAULT_BRANCHenv flag and routes missing-branch reconciliation through a create-vs-rename gate. - Implements
createDefaultBranch()to createrefs/heads/<new>from the current default branch HEAD SHA and update the repo default branch. - Adds unit tests for the new branching behavior and documents the new env var in user docs.
Show a summary per file
| File | Description |
|---|---|
| test/unit/lib/plugins/repository.test.js | Adds unit coverage for default-branch reconciliation under the new flag; removes stray it.only. |
| README.md | Documents the new CREATE_DEFAULT_BRANCH environment variable behavior and example usage. |
| lib/plugins/repository.js | Adds create-vs-rename dispatch and new createDefaultBranch() implementation. |
| lib/env.js | Exposes CREATE_DEFAULT_BRANCH env flag as a boolean. |
| docs/github-settings/1. repository-settings.md | Updates default_branch docs to describe the new flag behavior. |
Review details
Tip
Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
- Files reviewed: 5/5 changed files
- Comments generated: 2
- Review effort level: Lite
…lowing them Re-throw after logging in createDefaultBranch so a failed branch creation or default-branch update stops the sync flow and is recorded, instead of allowing sync to continue and potentially report success. Adds a regression test. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
Good catch. Fixed in 59b9ad9: |
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
Fixed in 8f75cab: corrected the |
There was a problem hiding this comment.
Review details
Suppressed comments (1)
lib/plugins/repository.js:184
CREATE_DEFAULT_BRANCHis intended (per PR description) to only change the behavior when the configured branch is missing (404). This hunk also routes theres.data.name !== newnamecase throughcreateOrRenameBranch(). That case is explicitly documented as the "GitHub rename redirect" scenario where the ref doesn’t exist and the code needs to rename the branch back; attemptingcreateDefaultBranch()here (when the flag is on) will likely fail with422 Reference already existsand won’t restore the branch name as intended. Keep this path usingrenameBranch()regardless of the flag, and only gate the 404/missing-branch path.
// If the old branch was renamed github will find the branch with the oldname the branch but the ref doesn't exist
// So we'd have to rename it back to the oldname
if (res.data.name !== newname) {
return this.createOrRenameBranch(oldname, newname, resArray)
} else {
- Files reviewed: 5/5 changed files
- Comments generated: 0 new
- Review effort level: Lite
Why
Today, when
repository.default_branchis configured and differs from the repo's current default branch, safe-settings renames the current default branch to the configured name. Renaming is disruptive for teams that want their default branch pointer to move to a brand new branch while keeping the original branch intact (for history, open PRs, tooling, or automation that still references it).This adds an opt-in variation so operators can migrate a repo's default branch by creating the new branch rather than renaming the existing one.
What
Introduces a new environment feature flag
CREATE_DEFAULT_BRANCH(defaultfalse). It only affects the path where the configureddefault_branchdiffers from the current one:git.createRef) and promotes it to the default (repos.update). The existing default branch is left untouched (no rename).How it fits together
The two missing-branch code paths in
updateDefaultBranchnow route through a smallcreateOrRenameBranch()gate that dispatches on the flag. The newcreateDefaultBranch()method resolves the current default branch SHA, createsrefs/heads/<newname>, then updates the repo's default branch, with full nop (dry-run) support that emitsNopCommands for both the ref creation and the default-branch update.Notes for reviewers
it.onlyintest/unit/lib/plugins/repository.test.jsthat was silently suppressing the other tests in that file; those two tests now run again alongside the new suite.README.md(env var section) anddocs/github-settings/1. repository-settings.md.Testing
standardlint clean on all changed files.