-
Notifications
You must be signed in to change notification settings - Fork 970
Allow default branch to be set to master/main on update, using a new github_branch_default resource #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Allow default branch to be set to master/main on update, using a new github_branch_default resource #194
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
| "log" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
| ) | ||
|
|
||
| func resourceGithubBranchDefault() *schema.Resource { | ||
| return &schema.Resource{ | ||
| Create: resourceGithubBranchDefaultCreate, | ||
| Read: resourceGithubBranchDefaultRead, | ||
| Delete: resourceGithubBranchDefaultDelete, | ||
| Update: resourceGithubBranchDefaultUpdate, | ||
| Importer: &schema.ResourceImporter{ | ||
| State: schema.ImportStatePassthrough, | ||
| }, | ||
|
|
||
| Schema: map[string]*schema.Schema{ | ||
| "branch": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| }, | ||
| "repository": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func resourceGithubBranchDefaultCreate(d *schema.ResourceData, meta interface{}) error { | ||
|
|
||
| client := meta.(*Owner).v3client | ||
| owner := meta.(*Owner).name | ||
| repoName := d.Get("repository").(string) | ||
| defaultBranch := d.Get("branch").(string) | ||
|
|
||
| ctx := context.Background() | ||
|
|
||
| repository, _, err := client.Repositories.Get(ctx, owner, repoName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| repository.DefaultBranch = &defaultBranch | ||
|
|
||
| log.Printf("[DEBUG] Creating branch default: %s (%s/%s)", defaultBranch, owner, repoName) | ||
| if _, _, err := client.Repositories.Edit(ctx, owner, repoName, repository); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| d.SetId(repoName) | ||
|
|
||
| return resourceGithubBranchDefaultRead(d, meta) | ||
| } | ||
|
|
||
| func resourceGithubBranchDefaultRead(d *schema.ResourceData, meta interface{}) error { | ||
|
|
||
| client := meta.(*Owner).v3client | ||
| owner := meta.(*Owner).name | ||
| repoName := d.Id() | ||
|
|
||
| ctx := context.WithValue(context.Background(), ctxId, d.Id()) | ||
|
|
||
| repository, _, err := client.Repositories.Get(ctx, owner, repoName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if repository.DefaultBranch == nil { | ||
| d.SetId("") | ||
| return nil | ||
| } | ||
|
|
||
| d.Set("branch", *repository.DefaultBranch) | ||
| d.Set("repository", *repository.Name) | ||
| return nil | ||
| } | ||
|
|
||
| func resourceGithubBranchDefaultDelete(d *schema.ResourceData, meta interface{}) error { | ||
|
|
||
| client := meta.(*Owner).v3client | ||
| owner := meta.(*Owner).name | ||
| repoName := d.Id() | ||
| defaultBranch := d.Get("branch").(string) | ||
|
|
||
| ctx := context.Background() | ||
|
|
||
| repository, _, err := client.Repositories.Get(ctx, owner, repoName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| repository.DefaultBranch = nil | ||
|
|
||
| log.Printf("[DEBUG] Removing branch default: %s (%s/%s)", defaultBranch, owner, repoName) | ||
| _, _, err = client.Repositories.Edit(ctx, owner, repoName, repository) | ||
| return err | ||
| } | ||
|
|
||
| func resourceGithubBranchDefaultUpdate(d *schema.ResourceData, meta interface{}) error { | ||
|
|
||
| client := meta.(*Owner).v3client | ||
| owner := meta.(*Owner).name | ||
| repoName := d.Id() | ||
| defaultBranch := d.Get("branch").(string) | ||
|
|
||
| ctx := context.Background() | ||
|
|
||
| repository, _, err := client.Repositories.Get(ctx, owner, repoName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| repository.DefaultBranch = &defaultBranch | ||
|
|
||
| log.Printf("[DEBUG] Updating branch default: %s (%s/%s)", defaultBranch, owner, repoName) | ||
| if _, _, err := client.Repositories.Edit(ctx, owner, repoName, repository); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return resourceGithubBranchDefaultRead(d, meta) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
| "github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
| ) | ||
|
|
||
| func TestAccGithubBranchDefault(t *testing.T) { | ||
|
|
||
| randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) | ||
|
|
||
| t.Run("creates and manages branch defaults", func(t *testing.T) { | ||
|
|
||
| config := fmt.Sprintf(` | ||
|
|
||
| resource "github_repository" "test" { | ||
| name = "tf-acc-test-%s" | ||
| auto_init = true | ||
| } | ||
|
|
||
| resource "github_branch_default" "test" { | ||
| repository = github_repository.test.name | ||
| branch = "main" | ||
| } | ||
| `, randomID) | ||
|
|
||
| check := resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr( | ||
| "github_branch_default.test", "branch", | ||
| "main", | ||
| ), | ||
| resource.TestCheckResourceAttr( | ||
| "github_branch_default.test", "repository", | ||
| fmt.Sprintf("tf-acc-test-%s", randomID), | ||
| ), | ||
| ) | ||
|
|
||
| testCase := func(t *testing.T, mode string) { | ||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { skipUnlessMode(t, mode) }, | ||
| Providers: testAccProviders, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: config, | ||
| Check: check, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| t.Run("with an anonymous account", func(t *testing.T) { | ||
| t.Skip("anonymous account not supported for this operation") | ||
| }) | ||
|
|
||
| t.Run("with an individual account", func(t *testing.T) { | ||
| testCase(t, individual) | ||
| }) | ||
|
|
||
| t.Run("with an organization account", func(t *testing.T) { | ||
| testCase(t, organization) | ||
| }) | ||
|
|
||
| }) | ||
|
|
||
| t.Run("can be configured to override the default_branch of the repository", func(t *testing.T) { | ||
|
|
||
| config := fmt.Sprintf(` | ||
| resource "github_repository" "test" { | ||
| name = "tf-acc-test-%s" | ||
| default_branch = "main" | ||
| auto_init = true | ||
| } | ||
|
|
||
| resource "github_branch_default" "test" { | ||
| repository = github_repository.test.name | ||
| branch = "override" | ||
| } | ||
|
|
||
| `, randomID) | ||
|
|
||
| check := resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr( | ||
| "github_branch_default.test", "branch", | ||
| "override", | ||
| ), | ||
| resource.TestCheckResourceAttr( | ||
| "github_branch_default.test", "repository", | ||
| fmt.Sprintf("tf-acc-test-%s", randomID), | ||
| ), | ||
| ) | ||
|
|
||
| testCase := func(t *testing.T, mode string) { | ||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { skipUnlessMode(t, mode) }, | ||
| Providers: testAccProviders, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: config, | ||
| Check: check, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| t.Run("with an anonymous account", func(t *testing.T) { | ||
| t.Skip("anonymous account not supported for this operation") | ||
| }) | ||
|
|
||
| t.Run("with an individual account", func(t *testing.T) { | ||
| testCase(t, individual) | ||
| }) | ||
|
|
||
| t.Run("with an organization account", func(t *testing.T) { | ||
| testCase(t, organization) | ||
| }) | ||
|
|
||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| --- | ||
| layout: "github" | ||
| page_title: "GitHub: github_branch_default" | ||
| description: |- | ||
| Provides a GitHub branch default for a given repository. | ||
| --- | ||
|
|
||
| # github_branch_default | ||
|
|
||
| Provides a GitHub branch default resource. | ||
|
|
||
| This resource allows you to set the default branch for a given repository. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ```hcl | ||
| resource "github_repository" "example" { | ||
| name = "example" | ||
| description = "My awesome codebase" | ||
|
|
||
| private = true | ||
|
|
||
| template { | ||
| owner = "github" | ||
| repository = "terraform-module-template" | ||
| } | ||
| } | ||
|
|
||
| resource "github_branch" "development" { | ||
| repository = github_repository.example.name | ||
| branch = "development" | ||
| } | ||
|
|
||
| resource "github_branch_default" "default"{ | ||
| repository = github_repository.example.name | ||
| branch = github_branch.development.branch | ||
| } | ||
| ``` | ||
|
|
||
| ## Argument Reference | ||
|
|
||
| The following arguments are supported: | ||
|
|
||
| * `repository` - (Required) The GitHub repository | ||
| * `branch` - (Required) The branch (e.g. `main`) | ||
|
|
||
| ## Import | ||
|
|
||
| GitHub Branch Defaults can be imported using an ID made up of `repository`, e.g. | ||
|
|
||
| ``` | ||
| $ terraform import github_branch_default.branch_default my-repo | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.