Skip to content

Commit

Permalink
Fix Merge Conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Udit committed Sep 15, 2020
1 parent d75da11 commit a1292f4
Showing 1 changed file with 93 additions and 70 deletions.
163 changes: 93 additions & 70 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"
"regexp"

"github.com/google/go-github/v29/github"
"github.com/google/go-github/v31/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
Expand Down Expand Up @@ -41,8 +41,17 @@ func resourceGithubRepository() *schema.Resource {
Optional: true,
},
"private": {
Type: schema.TypeBool,
Optional: true,
Type: schema.TypeBool,
Computed: true, // is affected by "visibility"
Optional: true,
ConflictsWith: []string{"visibility"},
Deprecated: "use visibility instead",
},
"visibility": {
Type: schema.TypeString,
Optional: true,
Computed: true, // is affected by "private"
ValidateFunc: validation.StringInSlice([]string{"public", "private", "internal"}, false),
},
"has_issues": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -87,7 +96,6 @@ func resourceGithubRepository() *schema.Resource {
"auto_init": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"default_branch": {
Type: schema.TypeString,
Expand Down Expand Up @@ -183,6 +191,7 @@ func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
Description: github.String(d.Get("description").(string)),
Homepage: github.String(d.Get("homepage_url").(string)),
Private: github.Bool(d.Get("private").(bool)),
Visibility: github.String(d.Get("visibility").(string)),
HasDownloads: github.Bool(d.Get("has_downloads").(bool)),
HasIssues: github.Bool(d.Get("has_issues").(bool)),
HasProjects: github.Bool(d.Get("has_projects").(bool)),
Expand All @@ -201,23 +210,24 @@ func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
}

func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).v3client
client := meta.(*Owner).v3client

if branchName, hasDefaultBranch := d.GetOk("default_branch"); hasDefaultBranch && (branchName != "master") {
return fmt.Errorf("Cannot set the default branch on a new repository to something other than 'master'.")
}

repoReq := resourceGithubRepositoryObject(d)
orgName := meta.(*Organization).name
owner := meta.(*Owner).name

// Auth issues (403 You need admin access to the organization before adding a repository to it.)
// are encountered when the resources is created with the visibility parameter. As
// resourceGithubRepositoryUpdate is called immediately after, this is subsequently corrected.
repoReq.Visibility = nil

repoName := repoReq.GetName()
ctx := context.Background()

log.Printf("[DEBUG] Creating repository: %s/%s", orgName, repoName)
log.Printf("[DEBUG] Creating repository: %s/%s", owner, repoName)

if template, ok := d.GetOk("template"); ok {
templateConfigBlocks := template.([]interface{})
Expand All @@ -232,7 +242,7 @@ func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) er
templateRepoOwner := templateConfigMap["owner"].(string)
templateRepoReq := github.TemplateRepoRequest{
Name: &repoName,
Owner: &orgName,
Owner: &owner,
Description: github.String(d.Get("description").(string)),
Private: github.Bool(d.Get("private").(bool)),
}
Expand All @@ -251,16 +261,24 @@ func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) er
}
} else {
// Create without a repository template
repo, _, err := client.Repositories.Create(ctx, orgName, repoReq)
var repo *github.Repository
var err error
if meta.(*Owner).IsOrganization {
repo, _, err = client.Repositories.Create(ctx, owner, repoReq)
} else {
// Create repository within authenticated user's account
repo, _, err = client.Repositories.Create(ctx, "", repoReq)

}
if err != nil {
return err
}
d.SetId(*repo.Name)
d.SetId(repo.GetName())
}

topics := repoReq.Topics
if len(topics) > 0 {
_, _, err = client.Repositories.ReplaceAllTopics(ctx, orgName, repoName, topics)
_, _, err := client.Repositories.ReplaceAllTopics(ctx, owner, repoName, topics)
if err != nil {
return err
}
Expand All @@ -270,31 +288,26 @@ func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) er
}

func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).v3client
orgName := meta.(*Organization).name
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repoName := d.Id()

log.Printf("[DEBUG] Reading repository: %s/%s", orgName, repoName)
log.Printf("[DEBUG] Reading repository: %s/%s", owner, repoName)

ctx := context.WithValue(context.Background(), ctxId, d.Id())
if !d.IsNewResource() {
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string))
}

repo, resp, err := client.Repositories.Get(ctx, orgName, repoName)
repo, resp, err := client.Repositories.Get(ctx, owner, repoName)
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 repository %s/%s from state because it no longer exists in GitHub",
orgName, repoName)
owner, repoName)
d.SetId("")
return nil
}
Expand All @@ -304,26 +317,27 @@ func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) erro

d.Set("etag", resp.Header.Get("ETag"))
d.Set("name", repoName)
d.Set("description", repo.Description)
d.Set("homepage_url", repo.Homepage)
d.Set("private", repo.Private)
d.Set("has_issues", repo.HasIssues)
d.Set("has_projects", repo.HasProjects)
d.Set("has_wiki", repo.HasWiki)
d.Set("is_template", repo.IsTemplate)
d.Set("allow_merge_commit", repo.AllowMergeCommit)
d.Set("allow_squash_merge", repo.AllowSquashMerge)
d.Set("allow_rebase_merge", repo.AllowRebaseMerge)
d.Set("delete_branch_on_merge", repo.DeleteBranchOnMerge)
d.Set("has_downloads", repo.HasDownloads)
d.Set("full_name", repo.FullName)
d.Set("default_branch", repo.DefaultBranch)
d.Set("html_url", repo.HTMLURL)
d.Set("ssh_clone_url", repo.SSHURL)
d.Set("svn_url", repo.SVNURL)
d.Set("git_clone_url", repo.GitURL)
d.Set("http_clone_url", repo.CloneURL)
d.Set("archived", repo.Archived)
d.Set("description", repo.GetDescription())
d.Set("homepage_url", repo.GetHomepage())
d.Set("private", repo.GetPrivate())
d.Set("visibility", repo.GetVisibility())
d.Set("has_issues", repo.GetHasIssues())
d.Set("has_projects", repo.GetHasProjects())
d.Set("has_wiki", repo.GetHasWiki())
d.Set("is_template", repo.GetIsTemplate())
d.Set("allow_merge_commit", repo.GetAllowMergeCommit())
d.Set("allow_squash_merge", repo.GetAllowSquashMerge())
d.Set("allow_rebase_merge", repo.GetAllowRebaseMerge())
d.Set("delete_branch_on_merge", repo.GetDeleteBranchOnMerge())
d.Set("has_downloads", repo.GetHasDownloads())
d.Set("full_name", repo.GetFullName())
d.Set("default_branch", repo.GetDefaultBranch())
d.Set("html_url", repo.GetHTMLURL())
d.Set("ssh_clone_url", repo.GetSSHURL())
d.Set("svn_url", repo.GetSVNURL())
d.Set("git_clone_url", repo.GetGitURL())
d.Set("http_clone_url", repo.GetCloneURL())
d.Set("archived", repo.GetArchived())
d.Set("topics", flattenStringList(repo.Topics))
d.Set("node_id", repo.GetNodeID())

Expand All @@ -342,14 +356,23 @@ func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) erro
}

func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err

// Can only update a repository if it is not archived or the update is to
// archive the repository (unarchiving is not supported by the Github API)
if d.Get("archived").(bool) && !d.HasChange("archived") {
log.Printf("[DEBUG] Skipping update of archived repository")
return nil
}

client := meta.(*Organization).v3client
client := meta.(*Owner).v3client

repoReq := resourceGithubRepositoryObject(d)

// The endpoint will throw an error if trying to PATCH with a visibility value that is the same
if !d.HasChange("visibility") {
repoReq.Visibility = nil
}

// Can only set `default_branch` on an already created repository with the target branches ref already in-place
if v, ok := d.GetOk("default_branch"); ok {
branch := v.(string)
Expand All @@ -360,23 +383,27 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er
}

repoName := d.Id()
orgName := meta.(*Organization).name
owner := meta.(*Owner).name
ctx := context.WithValue(context.Background(), ctxId, d.Id())

log.Printf("[DEBUG] Updating repository: %s/%s", orgName, repoName)
log.Printf("[DEBUG] Updating repository: %s/%s", owner, repoName)
repo, _, err := client.Repositories.Edit(ctx, owner, repoName, repoReq)
if err != nil {
return err
}
d.SetId(*repo.Name)

// Can only update a repository if it is not archived
// or the update is to archive the repository (unarchiving is not supported by the Github API)
if !d.Get("archived").(bool) || d.HasChange("archived") {
repo, _, err := client.Repositories.Edit(ctx, orgName, repoName, repoReq)
if d.HasChange("topics") {
topics := repoReq.Topics
_, _, err = client.Repositories.ReplaceAllTopics(ctx, owner, *repo.Name, topics)
if err != nil {
return err
}
d.SetId(*repo.Name)

if d.HasChange("topics") {
topics := repoReq.Topics
_, _, err = client.Repositories.ReplaceAllTopics(ctx, orgName, *repo.Name, topics)
_, _, err = client.Repositories.ReplaceAllTopics(ctx, owner, *repo.Name, topics)
if err != nil {
return err
}
Expand All @@ -387,30 +414,26 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er
}

func resourceGithubRepositoryDelete(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).v3client
client := meta.(*Owner).v3client
repoName := d.Id()
orgName := meta.(*Organization).name
owner := meta.(*Owner).name
ctx := context.WithValue(context.Background(), ctxId, d.Id())

archiveOnDestroy := d.Get("archive_on_destroy").(bool)
if archiveOnDestroy {
if d.Get("archived").(bool) {
log.Printf("[DEBUG] Repository already archived, nothing to do on delete: %s/%s", orgName, repoName)
log.Printf("[DEBUG] Repository already archived, nothing to do on delete: %s/%s", owner, repoName)
return nil
} else {
d.Set("archived", true)
repoReq := resourceGithubRepositoryObject(d)
log.Printf("[DEBUG] Archiving repository on delete: %s/%s", orgName, repoName)
_, _, err = client.Repositories.Edit(ctx, orgName, repoName, repoReq)
log.Printf("[DEBUG] Archiving repository on delete: %s/%s", owner, repoName)
_, _, err := client.Repositories.Edit(ctx, owner, repoName, repoReq)
return err
}
} else {
log.Printf("[DEBUG] Deleting repository: %s/%s", orgName, repoName)
_, err = client.Repositories.Delete(ctx, orgName, repoName)
}

log.Printf("[DEBUG] Deleting repository: %s/%s", owner, repoName)
_, err := client.Repositories.Delete(ctx, owner, repoName)
return err
}

0 comments on commit a1292f4

Please sign in to comment.