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

Fixes team membership bug #325

Merged
merged 2 commits into from
Jan 22, 2020
Merged
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
27 changes: 7 additions & 20 deletions github/resource_github_team_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"net/http"
"strconv"
"strings"

"github.com/google/go-github/v28/github"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -90,6 +89,13 @@ func resourceGithubTeamMembershipRead(d *schema.ResourceData, meta interface{})
if err != nil {
return unconvertibleIdErr(teamIdString, err)
}

// We intentionally set these early to allow reconciliation
// from an upstream bug which emptied team_id in state
// See https://github.com/terraform-providers/terraform-provider-github/issues/323
d.Set("team_id", teamIdString)
d.Set("username", username)

ctx := context.WithValue(context.Background(), ctxId, d.Id())
if !d.IsNewResource() {
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string))
Expand All @@ -113,12 +119,8 @@ func resourceGithubTeamMembershipRead(d *schema.ResourceData, meta interface{})
return err
}

team, user := getTeamAndUserFromURL(membership.URL)

d.Set("etag", resp.Header.Get("ETag"))
d.Set("username", user)
d.Set("role", membership.Role)
d.Set("team_id", team)

return nil
}
Expand All @@ -139,18 +141,3 @@ func resourceGithubTeamMembershipDelete(d *schema.ResourceData, meta interface{}

return err
}

func getTeamAndUserFromURL(url *string) (string, string) {
var team, user string

urlSlice := strings.Split(*url, "/")
for v := range urlSlice {
if urlSlice[v] == "teams" {
team = urlSlice[v+1]
}
if urlSlice[v] == "memberships" {
user = urlSlice[v+1]
}
}
return team, user
}