-
Notifications
You must be signed in to change notification settings - Fork 776
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
Add github_branch data and resource #364
Merged
jcudit
merged 24 commits into
integrations:master
from
cornfeedhobo:add-github_repository_branch
May 1, 2020
+656
−1
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
29c18ba
add github_repository_branch data and resource
cornfeedhobo 7a77eb4
rename github_repository_branch to github_branch and adjust tests to …
cornfeedhobo 09505f2
nit
cornfeedhobo dd1cb11
Trigger travis
cornfeedhobo 3f9891f
Trigger travis
cornfeedhobo 793f674
Merge branch 'master' into add-github_repository_branch
cornfeedhobo 34759ab
add github_branch documentation
cornfeedhobo 399eaac
revert go.sum change.
cornfeedhobo 5fa3cbb
remove sha attribute
cornfeedhobo 9a856d8
fix importing
cornfeedhobo b826d09
Merge branch 'master' into add-github_repository_branch
cornfeedhobo a26f674
include doc links
cornfeedhobo fea50d9
fix broken doc link
cornfeedhobo 0eac1ce
Merge branch 'master' into add-github_repository_branch
cornfeedhobo 4f600b5
clean up github_branch:
cornfeedhobo d2fc48e
review
cornfeedhobo 89b1e1e
review
cornfeedhobo a3e87a0
review
cornfeedhobo 57fb500
Merge branch 'master' into add-github_repository_branch
cornfeedhobo 15abf85
fix client references for github v3 client
cornfeedhobo 1b768ac
fix util call
cornfeedhobo 12be2ab
nit
cornfeedhobo 6550ac2
Merge branch 'master' into add-github_repository_branch
cornfeedhobo 62fcc4d
fix import id when specifying source_branch
cornfeedhobo 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 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,69 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func dataSourceGithubBranch() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceGithubBranchRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"repository": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"branch": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"etag": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"ref": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"sha": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGithubBranchRead(d *schema.ResourceData, meta interface{}) error { | ||
err := checkOrganization(meta) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client := meta.(*Organization).v3client | ||
orgName := meta.(*Organization).name | ||
repoName := d.Get("repository").(string) | ||
branchName := d.Get("branch").(string) | ||
branchRefName := "refs/heads/" + branchName | ||
|
||
log.Printf("[DEBUG] Reading GitHub branch reference %s/%s (%s)", | ||
orgName, repoName, branchRefName) | ||
ref, resp, err := client.Git.GetRef( | ||
context.TODO(), orgName, repoName, branchRefName) | ||
if err != nil { | ||
return fmt.Errorf("Error reading GitHub branch reference %s/%s (%s): %s", | ||
orgName, repoName, branchRefName, err) | ||
} | ||
|
||
d.SetId(buildTwoPartID(repoName, branchName)) | ||
d.Set("etag", resp.Header.Get("ETag")) | ||
d.Set("ref", *ref.Ref) | ||
d.Set("sha", *ref.Object.SHA) | ||
|
||
return nil | ||
} |
This file contains 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,74 @@ | ||
package github | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccGithubBranchDataSource_basic(t *testing.T) { | ||
|
||
var ( | ||
name = "main" | ||
repo = "test-repo" | ||
rn = "data.github_branch." + name | ||
) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckGithubBranchDataSourceConfig(name, repo, "master"), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(rn, "repository", repo), | ||
resource.TestCheckResourceAttr(rn, "branch", "master"), | ||
resource.TestCheckResourceAttrSet(rn, "etag"), | ||
resource.TestCheckResourceAttrSet(rn, "ref"), | ||
resource.TestCheckResourceAttrSet(rn, "sha"), | ||
), | ||
}, | ||
{ | ||
Config: testAccCheckGithubBranchDataSourceConfig(name, repo, "master"), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(rn, "repository", repo), | ||
resource.TestCheckResourceAttr(rn, "branch", "master"), | ||
resource.TestCheckResourceAttrSet(rn, "etag"), | ||
resource.TestCheckResourceAttrSet(rn, "ref"), | ||
resource.TestCheckResourceAttrSet(rn, "sha"), | ||
), | ||
}, | ||
{ | ||
Config: testAccCheckGithubBranchDataSourceConfig(name, repo, "test-branch"), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(rn, "repository", repo), | ||
resource.TestCheckResourceAttr(rn, "branch", "test-branch"), | ||
resource.TestCheckResourceAttrSet(rn, "etag"), | ||
resource.TestCheckResourceAttrSet(rn, "ref"), | ||
resource.TestCheckResourceAttrSet(rn, "sha"), | ||
), | ||
}, | ||
{ | ||
Config: testAccCheckGithubBranchDataSourceConfig(name, repo, "test-branch"), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(rn, "repository", repo), | ||
resource.TestCheckResourceAttr(rn, "branch", "test-branch"), | ||
resource.TestCheckResourceAttrSet(rn, "etag"), | ||
resource.TestCheckResourceAttrSet(rn, "ref"), | ||
resource.TestCheckResourceAttrSet(rn, "sha"), | ||
), | ||
}, | ||
Comment on lines
+53
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a duplicate of the block above? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same answer as above. |
||
}, | ||
}) | ||
|
||
} | ||
|
||
func testAccCheckGithubBranchDataSourceConfig(name, repo, branch string) string { | ||
return fmt.Sprintf(` | ||
data "github_branch" "%s" { | ||
repository = "%s" | ||
branch = "%s" | ||
} | ||
`, name, repo, branch) | ||
} |
This file contains 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 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,202 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/google/go-github/v29/github" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func resourceGithubBranch() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceGithubBranchCreate, | ||
Read: resourceGithubBranchRead, | ||
Delete: resourceGithubBranchDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: resourceGithubBranchImport, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"repository": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"branch": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"source_branch": { | ||
Type: schema.TypeString, | ||
Default: "master", | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"source_sha": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Computed: true, | ||
}, | ||
"etag": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"ref": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"sha": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceGithubBranchCreate(d *schema.ResourceData, meta interface{}) error { | ||
err := checkOrganization(meta) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx := context.Background() | ||
if !d.IsNewResource() { | ||
ctx = context.WithValue(ctx, ctxId, d.Id()) | ||
} | ||
|
||
client := meta.(*Organization).v3client | ||
orgName := meta.(*Organization).name | ||
repoName := d.Get("repository").(string) | ||
branchName := d.Get("branch").(string) | ||
branchRefName := "refs/heads/" + branchName | ||
sourceBranchName := d.Get("source_branch").(string) | ||
sourceBranchRefName := "refs/heads/" + sourceBranchName | ||
|
||
if _, hasSourceSHA := d.GetOk("source_sha"); !hasSourceSHA { | ||
log.Printf("[DEBUG] Querying GitHub branch reference %s/%s (%s) to derive source_sha", | ||
orgName, repoName, sourceBranchRefName) | ||
ref, _, err := client.Git.GetRef(ctx, orgName, repoName, sourceBranchRefName) | ||
if err != nil { | ||
return fmt.Errorf("Error querying GitHub branch reference %s/%s (%s): %s", | ||
orgName, repoName, sourceBranchRefName, err) | ||
} | ||
d.Set("source_sha", *ref.Object.SHA) | ||
} | ||
sourceBranchSHA := d.Get("source_sha").(string) | ||
|
||
log.Printf("[DEBUG] Creating GitHub branch reference %s/%s (%s)", | ||
orgName, repoName, branchRefName) | ||
_, _, err = client.Git.CreateRef(ctx, orgName, repoName, &github.Reference{ | ||
Ref: &branchRefName, | ||
Object: &github.GitObject{SHA: &sourceBranchSHA}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Error creating GitHub branch reference %s/%s (%s): %s", | ||
orgName, repoName, branchRefName, err) | ||
} | ||
|
||
d.SetId(buildTwoPartID(repoName, branchName)) | ||
|
||
return resourceGithubBranchRead(d, meta) | ||
} | ||
|
||
func resourceGithubBranchRead(d *schema.ResourceData, meta interface{}) error { | ||
err := checkOrganization(meta) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx := context.WithValue(context.Background(), ctxId, d.Id()) | ||
if !d.IsNewResource() { | ||
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string)) | ||
} | ||
|
||
client := meta.(*Organization).v3client | ||
orgName := meta.(*Organization).name | ||
repoName, branchName, err := parseTwoPartID(d.Id(), "repository", "branch") | ||
if err != nil { | ||
return err | ||
} | ||
branchRefName := "refs/heads/" + branchName | ||
|
||
log.Printf("[DEBUG] Querying GitHub branch reference %s/%s (%s)", | ||
orgName, repoName, branchRefName) | ||
ref, resp, err := client.Git.GetRef(ctx, orgName, repoName, branchRefName) | ||
if err != nil { | ||
if ghErr, ok := err.(*github.ErrorResponse); ok { | ||
if ghErr.Response.StatusCode == http.StatusNotModified { | ||
return nil | ||
} | ||
if ghErr.Response.StatusCode == http.StatusNotFound { | ||
log.Printf("[WARN] Removing branch %s/%s (%s) from state because it no longer exists in Github", | ||
orgName, repoName, branchName) | ||
d.SetId("") | ||
return nil | ||
} | ||
} | ||
return fmt.Errorf("Error querying GitHub branch reference %s/%s (%s): %s", | ||
orgName, repoName, branchRefName, err) | ||
} | ||
|
||
d.SetId(buildTwoPartID(repoName, branchName)) | ||
d.Set("etag", resp.Header.Get("ETag")) | ||
d.Set("repository", repoName) | ||
d.Set("branch", branchName) | ||
d.Set("ref", *ref.Ref) | ||
d.Set("sha", *ref.Object.SHA) | ||
|
||
return nil | ||
} | ||
|
||
func resourceGithubBranchDelete(d *schema.ResourceData, meta interface{}) error { | ||
err := checkOrganization(meta) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx := context.WithValue(context.Background(), ctxId, d.Id()) | ||
|
||
client := meta.(*Organization).v3client | ||
orgName := meta.(*Organization).name | ||
repoName, branchName, err := parseTwoPartID(d.Id(), "repository", "branch") | ||
if err != nil { | ||
return err | ||
} | ||
branchRefName := "refs/heads/" + branchName | ||
|
||
log.Printf("[DEBUG] Deleting GitHub branch reference %s/%s (%s)", | ||
orgName, repoName, branchRefName) | ||
_, err = client.Git.DeleteRef(ctx, orgName, repoName, branchRefName) | ||
if err != nil { | ||
return fmt.Errorf("Error deleting GitHub branch reference %s/%s (%s): %s", | ||
orgName, repoName, branchRefName, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceGithubBranchImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
repoName, branchName, err := parseTwoPartID(d.Id(), "repository", "branch") | ||
cornfeedhobo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sourceBranch := "master" | ||
if strings.Contains(branchName, ":") { | ||
branchName, sourceBranch, err = parseTwoPartID(branchName, "branch", "source_branch") | ||
if err != nil { | ||
return nil, err | ||
} | ||
d.SetId(buildTwoPartID(repoName, branchName)) | ||
} | ||
|
||
d.Set("source_branch", sourceBranch) | ||
|
||
return []*schema.ResourceData{d}, resourceGithubBranchRead(d, meta) | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a duplicate of the block above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jcudit Yes. My understanding is that this checks for drift, essentially making sure my state doesn't accidentally drift between runs. This helped me catch an error with where I was setting
etag
.