Skip to content

Commit

Permalink
Modify github_team_repository to accept slug as a valid team_id as we…
Browse files Browse the repository at this point in the history
…ll (#693)

* First attempt

* Add comment

* Attempt to modify unit tests

* Edit docs to reflect change

* Make sure team_id is set appropriately

* fixing lint

* add passing tests for `github_team_repository`

Co-authored-by: Jeremy Udit <jcudit@github.com>
  • Loading branch information
k24dizzle and Jeremy Udit authored Feb 18, 2021
1 parent 2d5e38f commit 97c7957
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 203 deletions.
40 changes: 24 additions & 16 deletions github/resource_github_team_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ func resourceGithubTeamRepository() *schema.Resource {

Schema: map[string]*schema.Schema{
"team_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateTeamIDFunc,
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "ID or slug of team",
},
"repository": {
Type: schema.TypeString,
Expand Down Expand Up @@ -55,18 +55,20 @@ func resourceGithubTeamRepositoryCreate(d *schema.ResourceData, meta interface{}
client := meta.(*Owner).v3client
orgId := meta.(*Owner).id

teamIdString := d.Get("team_id").(string)
teamId, err := strconv.ParseInt(teamIdString, 10, 64)
// The given team id could be an id or a slug
givenTeamId := d.Get("team_id").(string)
teamId, err := getTeamID(givenTeamId, meta)
if err != nil {
return unconvertibleIdErr(teamIdString, err)
return err
}

orgName := meta.(*Owner).name
repoName := d.Get("repository").(string)
permission := d.Get("permission").(string)
ctx := context.Background()

log.Printf("[DEBUG] Creating team repository association: %s:%s (%s/%s)",
teamIdString, permission, orgName, repoName)
givenTeamId, permission, orgName, repoName)
_, err = client.Teams.AddTeamRepoByID(ctx,
orgId,
teamId,
Expand All @@ -81,7 +83,7 @@ func resourceGithubTeamRepositoryCreate(d *schema.ResourceData, meta interface{}
return err
}

d.SetId(buildTwoPartID(teamIdString, repoName))
d.SetId(buildTwoPartID(strconv.FormatInt(teamId, 10), repoName))

return resourceGithubTeamRepositoryRead(d, meta)
}
Expand All @@ -99,7 +101,6 @@ func resourceGithubTeamRepositoryRead(d *schema.ResourceData, meta interface{})
if err != nil {
return err
}

teamId, err := strconv.ParseInt(teamIdString, 10, 64)
if err != nil {
return unconvertibleIdErr(teamIdString, err)
Expand Down Expand Up @@ -128,7 +129,11 @@ func resourceGithubTeamRepositoryRead(d *schema.ResourceData, meta interface{})
}

d.Set("etag", resp.Header.Get("ETag"))
d.Set("team_id", teamIdString)
if d.Get("team_id") == "" {
// If team_id is empty, that means we are importing the resource.
// Set the team_id to be the id of the team.
d.Set("team_id", teamIdString)
}
d.Set("repository", repo.GetName())

permName, permErr := getRepoPermission(repo.GetPermissions())
Expand All @@ -150,13 +155,15 @@ func resourceGithubTeamRepositoryUpdate(d *schema.ResourceData, meta interface{}
client := meta.(*Owner).v3client
orgId := meta.(*Owner).id

teamIdString := d.Get("team_id").(string)
teamIdString, repoName, err := parseTwoPartID(d.Id(), "team_id", "repository")
if err != nil {
return err
}
teamId, err := strconv.ParseInt(teamIdString, 10, 64)
if err != nil {
return unconvertibleIdErr(teamIdString, err)
}
orgName := meta.(*Owner).name
repoName := d.Get("repository").(string)
permission := d.Get("permission").(string)
ctx := context.WithValue(context.Background(), ctxId, d.Id())

Expand Down Expand Up @@ -190,14 +197,15 @@ func resourceGithubTeamRepositoryDelete(d *schema.ResourceData, meta interface{}
client := meta.(*Owner).v3client
orgId := meta.(*Owner).id

teamIdString := d.Get("team_id").(string)

teamIdString, repoName, err := parseTwoPartID(d.Id(), "team_id", "repository")
if err != nil {
return err
}
teamId, err := strconv.ParseInt(teamIdString, 10, 64)
if err != nil {
return unconvertibleIdErr(teamIdString, err)
}
orgName := meta.(*Owner).name
repoName := d.Get("repository").(string)
ctx := context.WithValue(context.Background(), ctxId, d.Id())

log.Printf("[DEBUG] Deleting team repository association: %s (%s/%s)",
Expand Down
Loading

0 comments on commit 97c7957

Please sign in to comment.