Skip to content

Commit 61775c0

Browse files
committed
Rename branch only if it's different from current default branch
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent 9606a76 commit 61775c0

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

github/resource_github_branch_default.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,17 @@ func resourceGithubBranchDefaultUpdate(ctx context.Context, d *schema.ResourceDa
228228
var etag string
229229

230230
if rename {
231-
tflog.Debug(ctx, "Renaming branch to new default")
231+
tflog.Debug(ctx, "Rename enabled, checking if branch rename is needed")
232232
repository, resp, err := client.Repositories.Get(ctx, owner, repoName)
233233
if err != nil {
234234
return diag.FromErr(err)
235235
}
236-
etag = resp.Header.Get("ETag")
237-
if _, _, err := client.Repositories.RenameBranch(ctx, owner, repoName, repository.GetDefaultBranch(), defaultBranch); err != nil {
238-
return diag.FromErr(err)
236+
if repository.GetDefaultBranch() != defaultBranch {
237+
tflog.Debug(ctx, "Renaming branch to new default")
238+
etag = resp.Header.Get("ETag")
239+
if _, _, err := client.Repositories.RenameBranch(ctx, owner, repoName, repository.GetDefaultBranch(), defaultBranch); err != nil {
240+
return diag.FromErr(err)
241+
}
239242
}
240243
} else {
241244
tflog.Debug(ctx, "Setting new default branch")

github/resource_github_branch_default_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,45 @@ func TestAccGithubBranchDefault(t *testing.T) {
296296
},
297297
})
298298
})
299+
t.Run("regression_prevent_trying_rename_to_same_name", func(t *testing.T) {
300+
randomID := acctest.RandString(5)
301+
repoName := fmt.Sprintf("%sbranch-def-%s", testResourcePrefix, randomID)
302+
config := `
303+
resource "github_repository" "test" {
304+
name = "%s"
305+
auto_init = true
306+
}
307+
308+
resource "github_branch_default" "test"{
309+
repository = github_repository.test.name
310+
branch = "development"
311+
rename = %t
312+
}
313+
`
314+
315+
resource.Test(t, resource.TestCase{
316+
PreCheck: func() { skipUnauthenticated(t) },
317+
ProviderFactories: providerFactories,
318+
Steps: []resource.TestStep{
319+
{
320+
Config: fmt.Sprintf(config, repoName, true),
321+
ConfigStateChecks: []statecheck.StateCheck{
322+
statecheck.ExpectKnownValue("github_branch_default.test", tfjsonpath.New("branch"), knownvalue.StringExact("development")),
323+
},
324+
},
325+
{
326+
Config: fmt.Sprintf(config, repoName, false),
327+
ConfigStateChecks: []statecheck.StateCheck{
328+
statecheck.ExpectKnownValue("github_branch_default.test", tfjsonpath.New("branch"), knownvalue.StringExact("development")),
329+
},
330+
},
331+
{
332+
Config: fmt.Sprintf(config, repoName, true),
333+
ConfigStateChecks: []statecheck.StateCheck{
334+
statecheck.ExpectKnownValue("github_branch_default.test", tfjsonpath.New("branch"), knownvalue.StringExact("development")),
335+
},
336+
},
337+
},
338+
})
339+
})
299340
}

0 commit comments

Comments
 (0)