Skip to content

Support non-main default branch via the branches map (+ demotion guard) - #81

Merged
pavlovic-ivan merged 4 commits into
mainfrom
non-main-default-branch-via-branches
Sep 23, 2026
Merged

pavlovic-ivan merged 4 commits into
mainfrom
non-main-default-branch-via-branches

Conversation

@pavlovic-ivan

@pavlovic-ivan pavlovic-ivan commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Lets a repo's default branch (e.g. master) be set declaratively from YAML, including on existing repos — replacing #57's rename (reverted here), which was create-only and forced hand-creating the branch + a PR for existing repos.

Changes

  • Module: inject a non-main default_branch into branches_map so github_branch creates it off the auto-init main, then github_branch_default points at it.
  • Root: import {} block for the default branch (scoped to importer_tmp_dir/) so existing repos import it instead of colliding.
  • Demotion guard (master → main, which otherwise fails apply with 422 Cannot delete the default branch): runbook docs/switching-default-branch-to-main.md (state rm the branch first), and a tf-plan check that warns + sets the check action_required to block merge until the runbook is followed.

Rollout note: on a workspace with existing non-main-default repos, import their default branches into state first, or the first apply fails (422/404).

Verified end-to-end on the dev workspace (all creation paths + a real demotion PR).

Reimplements non-main default branch support (previously #57's provider
`rename`, reverted in the prior commit) by injecting the desired default
branch into the module's branches map so it is created as a real
github_branch resource, then pointed at by github_branch_default.

- vendored module: when default_branch is non-main (and auto_init is set,
  and it is not already listed in var.branches), append it to branches_map
  so github_branch creates it off the auto-init "main".
- root module: add an import block for github_branch.branch[default_branch]
  scoped to generated_repos with a non-main default, so existing repos
  import the branch instead of failing to create one that already exists.
Switching a repository's default branch from a non-main branch (e.g. master)
back to main cannot be applied directly: Terraform destroys the managed
github_branch before demoting the default, and GitHub rejects deleting a
repository's default branch (422). The plan succeeds but the apply fails and
keeps failing until state is reconciled.

- docs/switching-default-branch-to-main.md: runbook — state rm the branch
  first (forgets it without deleting), then flip the YAML to main.
- tf-plan.yaml (Report result): detect a github_branch.branch deletion in the
  plan summary, prepend a warning linking the runbook to the PR comment, and
  set the check run to action_required so a required check blocks the merge
  until the runbook is followed. Self-clears once the branch is state-rm'd.
@dev-milos

Copy link
Copy Markdown
Collaborator

LGTM

@pavlovic-ivan
pavlovic-ivan merged commit 311f08b into main Sep 23, 2026
1 check passed
@pavlovic-ivan
pavlovic-ivan deleted the non-main-default-branch-via-branches branch September 23, 2026 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants