Skip to content
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

github/repository*: support individual user for resource management (limited to authenticated user) #465

Merged
merged 5 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions github/data_source_github_ip_ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func dataSourceGithubIpRanges() *schema.Resource {
}

func dataSourceGithubIpRangesRead(d *schema.ResourceData, meta interface{}) error {
org := meta.(*Owner)
owner := meta.(*Owner)

api, _, err := org.v3client.APIMeta(org.StopContext)
api, _, err := owner.v3client.APIMeta(owner.StopContext)
if err != nil {
return err
}
Expand Down
5 changes: 0 additions & 5 deletions github/data_source_github_repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ func dataSourceGithubRepositories() *schema.Resource {
}

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

client := meta.(*Owner).v3client

query := d.Get("query").(string)
Expand Down
13 changes: 2 additions & 11 deletions github/data_source_github_repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ import (
)

func TestAccGithubRepositoriesDataSource_basic(t *testing.T) {
if err := testAccCheckOrganization(); err != nil {
t.Skipf("Skipping because %s.", err.Error())
}

query := "org:hashicorp repository:terraform"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
Expand All @@ -32,9 +29,6 @@ func TestAccGithubRepositoriesDataSource_basic(t *testing.T) {
})
}
func TestAccGithubRepositoriesDataSource_Sort(t *testing.T) {
if err := testAccCheckOrganization(); err != nil {
t.Skipf("Skipping because %s.", err.Error())
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
Expand Down Expand Up @@ -63,11 +57,8 @@ func TestAccGithubRepositoriesDataSource_Sort(t *testing.T) {
}

func TestAccGithubRepositoriesDataSource_noMatch(t *testing.T) {
if err := testAccCheckOrganization(); err != nil {
t.Skipf("Skipping because %s.", err.Error())
}

query := "klsafj_23434_doesnt_exist"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
Expand Down
15 changes: 5 additions & 10 deletions github/data_source_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,13 @@ func dataSourceGithubRepository() *schema.Resource {
}

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

client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
owner := meta.(*Owner).name
var repoName string

if fullName, ok := d.GetOk("full_name"); ok {
var err error
orgName, repoName, err = splitRepoFullName(fullName.(string))
owner, repoName, err = splitRepoFullName(fullName.(string))
if err != nil {
return err
}
Expand All @@ -131,8 +126,8 @@ func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) er
return fmt.Errorf("One of %q or %q has to be provided", "full_name", "name")
}

log.Printf("[DEBUG] Reading GitHub repository %s/%s", orgName, repoName)
repo, _, err := client.Repositories.Get(context.TODO(), orgName, repoName)
log.Printf("[DEBUG] Reading GitHub repository %s/%s", owner, repoName)
repo, _, err := client.Repositories.Get(context.TODO(), owner, repoName)
if err != nil {
return err
}
Expand Down Expand Up @@ -170,7 +165,7 @@ func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) er
func splitRepoFullName(fullName string) (string, string, error) {
parts := strings.Split(fullName, "/")
if len(parts) != 2 {
return "", "", fmt.Errorf("Unexpected full name format (%q), expected org/repo_name", fullName)
return "", "", fmt.Errorf("Unexpected full name format (%q), expected owner/repo_name", fullName)
}
return parts[0], parts[1], nil
}
20 changes: 4 additions & 16 deletions github/data_source_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ import (
)

func TestAccGithubRepositoryDataSource_fullName_noMatchReturnsError(t *testing.T) {
if err := testAccCheckOrganization(); err != nil {
t.Skipf("Skipping because %s.", err.Error())
}

fullName := "klsafj_23434_doesnt_exist/not-exists"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
Expand All @@ -29,11 +26,8 @@ func TestAccGithubRepositoryDataSource_fullName_noMatchReturnsError(t *testing.T
}

func TestAccGithubRepositoryDataSource_name_noMatchReturnsError(t *testing.T) {
if err := testAccCheckOrganization(); err != nil {
t.Skipf("Skipping because %s.", err.Error())
}

name := "not-exists"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
Expand All @@ -49,11 +43,8 @@ func TestAccGithubRepositoryDataSource_name_noMatchReturnsError(t *testing.T) {
}

func TestAccGithubRepositoryDataSource_fullName_existing(t *testing.T) {
if err := testAccCheckOrganization(); err != nil {
t.Skipf("Skipping because %s.", err.Error())
}

fullName := testOwner + "/test-repo"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
Expand All @@ -69,11 +60,8 @@ func TestAccGithubRepositoryDataSource_fullName_existing(t *testing.T) {
}

func TestAccGithubRepositoryDataSource_name_existing(t *testing.T) {
if err := testAccCheckOrganization(); err != nil {
t.Skipf("Skipping because %s.", err.Error())
}

name := "test-repo"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
Expand Down
62 changes: 25 additions & 37 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,23 +196,18 @@ 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.(*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.(*Owner).name
owner := meta.(*Owner).name
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 @@ -227,7 +222,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 @@ -246,16 +241,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 @@ -265,31 +268,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.(*Owner).v3client
orgName := meta.(*Owner).name
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 Down Expand Up @@ -337,11 +335,6 @@ func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) erro
}

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

client := meta.(*Owner).v3client

repoReq := resourceGithubRepositoryObject(d)
Expand All @@ -355,19 +348,19 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er
}

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

log.Printf("[DEBUG] Updating repository: %s/%s", orgName, repoName)
repo, _, err := client.Repositories.Edit(ctx, orgName, repoName, repoReq)
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)

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 @@ -377,18 +370,13 @@ 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.(*Owner).v3client
repoName := d.Id()
orgName := meta.(*Owner).name
owner := meta.(*Owner).name
ctx := context.WithValue(context.Background(), ctxId, d.Id())

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
}
15 changes: 0 additions & 15 deletions github/resource_github_repository_deploy_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ func resourceGithubRepositoryDeployKey() *schema.Resource {
}

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

client := meta.(*Owner).v3client

repoName := d.Get("repository").(string)
Expand Down Expand Up @@ -87,11 +82,6 @@ func resourceGithubRepositoryDeployKeyCreate(d *schema.ResourceData, meta interf
}

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

client := meta.(*Owner).v3client

owner := meta.(*Owner).name
Expand Down Expand Up @@ -136,11 +126,6 @@ func resourceGithubRepositoryDeployKeyRead(d *schema.ResourceData, meta interfac
}

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

client := meta.(*Owner).v3client

owner := meta.(*Owner).name
Expand Down
12 changes: 4 additions & 8 deletions github/resource_github_repository_deploy_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ func TestSuppressDeployKeyDiff(t *testing.T) {
}

func TestAccGithubRepositoryDeployKey_basic(t *testing.T) {
if err := testAccCheckOrganization(); err != nil {
t.Skipf("Skipping because %s.", err.Error())
}

testUserEmail := os.Getenv("GITHUB_TEST_USER_EMAIL")
if testUserEmail == "" {
t.Skip("Skipping because `GITHUB_TEST_USER_EMAIL` is not set")
Expand Down Expand Up @@ -103,7 +99,7 @@ func testAccCheckGithubRepositoryDeployKeyDestroy(s *terraform.State) error {
continue
}

orgName := testAccProvider.Meta().(*Owner).name
owner := testAccProvider.Meta().(*Owner).name
repoName, idString, err := parseTwoPartID(rs.Primary.ID, "repository", "ID")
if err != nil {
return err
Expand All @@ -114,7 +110,7 @@ func testAccCheckGithubRepositoryDeployKeyDestroy(s *terraform.State) error {
return unconvertibleIdErr(idString, err)
}

_, resp, err := conn.Repositories.GetKey(context.TODO(), orgName, repoName, id)
_, resp, err := conn.Repositories.GetKey(context.TODO(), owner, repoName, id)

if err != nil && resp.Response.StatusCode != 404 {
return err
Expand All @@ -137,7 +133,7 @@ func testAccCheckGithubRepositoryDeployKeyExists(n string) resource.TestCheckFun
}

conn := testAccProvider.Meta().(*Owner).v3client
orgName := testAccProvider.Meta().(*Owner).name
owner := testAccProvider.Meta().(*Owner).name
repoName, idString, err := parseTwoPartID(rs.Primary.ID, "repository", "ID")
if err != nil {
return err
Expand All @@ -148,7 +144,7 @@ func testAccCheckGithubRepositoryDeployKeyExists(n string) resource.TestCheckFun
return unconvertibleIdErr(idString, err)
}

_, _, err = conn.Repositories.GetKey(context.TODO(), orgName, repoName, id)
_, _, err = conn.Repositories.GetKey(context.TODO(), owner, repoName, id)
if err != nil {
return err
}
Expand Down
Loading