Skip to content

Support non-main default branch on repo creation#57

Merged
dev-milos merged 1 commit into
mainfrom
fix/default-branch-on-create
Jun 29, 2026
Merged

Support non-main default branch on repo creation#57
dev-milos merged 1 commit into
mainfrom
fix/default-branch-on-create

Conversation

@dev-milos

Copy link
Copy Markdown
Collaborator

Problem

When a YAML config for a new repository sets default_branch to anything other than main, Terraform fails at apply (issue #14).

auto_init creates the repo with a single main branch, then github_branch_default tries to point the default at e.g. master, which does not exist yet:

Error: PATCH /repos/<org>/<repo>: 422 Validation Failed
  Field: default_branch  Code: invalid
  Message: The branch master was not found. Please push that ref first or create it via the Git Data API.

The repo is left created but with the wrong default branch.

Fix

Set rename = true on github_branch_default so the provider renames the auto-init branch (main) to the requested default instead of expecting it to already exist.

Renaming is applied only when needed:

rename = local.default_branch != "main" && !contains(keys(local.branches_map), local.default_branch)
  • target is main → no rename (it's already the auto-init branch)
  • target is an explicitly managed github_branch → no rename (that branch is created separately)
  • otherwise (e.g. master) → rename the auto-init branch to the target

rename is wrapped in lifecycle { ignore_changes = [rename] } so existing and imported repos whose default already matches do not show a perpetual diff.

Testing

Reproduced and verified against a test org via local terraform apply:

  • Before: new repo with default_branch: master → repo created, apply errors with the 422 above; repo left with default main.
  • After: same config → apply succeeds, repo default branch is master, sole branch renamed from main to master.

Closes #14

When a new repository is created with auto_init, GitHub initialises it with a
single `main` branch. Setting `github_branch_default` to any other branch (e.g.
`master`) then fails at apply with:

    422 Validation Failed - The branch master was not found.

Use the provider's `rename` option to rename the auto-init branch to the desired
default in that case. Renaming is skipped when the target is `main` (already the
auto-init branch) or an explicitly managed `github_branch`, and `rename` is
ignored after creation so existing/imported repos do not show a perpetual diff.

Fixes #14
@dev-milos dev-milos requested a review from pavlovic-ivan June 26, 2026 12:06
@dev-milos dev-milos marked this pull request as ready for review June 26, 2026 12:06
@dev-milos dev-milos marked this pull request as draft June 26, 2026 12:45
@dev-milos dev-milos marked this pull request as ready for review June 26, 2026 12:47
@dev-milos dev-milos merged commit a0a6784 into main Jun 29, 2026
1 check passed
@dev-milos dev-milos deleted the fix/default-branch-on-create branch June 29, 2026 07:15
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.

[GCSS] Creating default master branch fails

2 participants