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

Rebased #96: Create repos for non-organizations #322

53 changes: 28 additions & 25 deletions github/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@ import (
)

type Config struct {
Token string
Organization string
BaseURL string
Insecure bool
Individual bool
Anonymous bool
Token string
Owner string
BaseURL string
Insecure bool
Individual bool
Anonymous bool
}

type Organization struct {
name string
client *github.Client
StopContext context.Context
type Owner struct {
name string
client *github.Client
StopContext context.Context
IsOrganization bool
}

// Client configures and returns a fully initialized GithubClient
func (c *Config) Client() (interface{}, error) {
var org Organization
var owner Owner
var ts oauth2.TokenSource
var tc *http.Client

Expand All @@ -40,18 +41,16 @@ func (c *Config) Client() (interface{}, error) {
ctx = context.WithValue(ctx, oauth2.HTTPClient, insecureClient)
}

// Either Organization needs to be set, or Individual needs to be true
if c.Organization != "" && c.Individual {
return nil, fmt.Errorf("If `individual` is true, `organization` cannot be set.")
}
if c.Organization == "" && !c.Individual {
return nil, fmt.Errorf("If `individual` is false, `organization` is required.")
if !c.Individual {
owner.IsOrganization = true
}

if c.Individual {
org.name = ""
} else {
org.name = c.Organization
// Either Owner needs to be set, or Individual needs to be true
if c.Owner != "" && c.Individual {
return nil, fmt.Errorf("If `individual` is true, `owner` cannot be set.")
}
if c.Owner == "" && !c.Individual {
return nil, fmt.Errorf("If `individual` is false, `owner` is required.")
}

// Either run as anonymous, or run with a Token
Expand Down Expand Up @@ -79,17 +78,21 @@ func (c *Config) Client() (interface{}, error) {
tc.Transport = NewRateLimitTransport(tc.Transport)
tc.Transport = logging.NewTransport("Github", tc.Transport)

org.client = github.NewClient(tc)

owner.client = github.NewClient(tc)
if c.BaseURL != "" {
u, err := url.Parse(c.BaseURL)
if err != nil {
return nil, err
}
org.client.BaseURL = u
owner.client.BaseURL = u
}

_, _, err := (*owner.client).Organizations.Get(context.TODO(), owner.name)
if err != nil {
owner.IsOrganization = false
}

return &org, nil
return &owner, nil
}

func insecureHttpClient() *http.Client {
Expand Down
2 changes: 1 addition & 1 deletion github/data_source_github_collaborators.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func dataSourceGithubCollaborators() *schema.Resource {

func dataSourceGithubCollaboratorsRead(d *schema.ResourceData, meta interface{}) error {

client := meta.(*Organization).client
client := meta.(*Owner).client
ctx := context.Background()

owner := d.Get("owner").(string)
Expand Down
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.(*Organization)
owner := meta.(*Owner)

api, _, err := org.client.APIMeta(org.StopContext)
api, _, err := owner.client.APIMeta(owner.StopContext)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion github/data_source_github_repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func dataSourceGithubRepositoriesRead(d *schema.ResourceData, meta interface{})
return err
}

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

query := d.Get("query").(string)
opt := &github.SearchOptions{
Expand Down
4 changes: 2 additions & 2 deletions github/data_source_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) er
return err
}

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

if fullName, ok := d.GetOk("full_name"); ok {
Expand Down
8 changes: 4 additions & 4 deletions github/data_source_github_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func dataSourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error {
slug := d.Get("slug").(string)
log.Printf("[INFO] Refreshing GitHub Team: %s", slug)

client := meta.(*Organization).client
client := meta.(*Owner).client
ctx := context.Background()

team, err := getGithubTeamBySlug(ctx, client, meta.(*Organization).name, slug)
team, err := getGithubTeamBySlug(ctx, client, meta.(*Owner).name, slug)
if err != nil {
return err
}
Expand All @@ -76,10 +76,10 @@ func dataSourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

func getGithubTeamBySlug(ctx context.Context, client *github.Client, org string, slug string) (team *github.Team, err error) {
func getGithubTeamBySlug(ctx context.Context, client *github.Client, owner string, slug string) (team *github.Team, err error) {
opt := &github.ListOptions{PerPage: 10}
for {
teams, resp, err := client.Teams.ListTeams(ctx, org, opt)
teams, resp, err := client.Teams.ListTeams(ctx, owner, opt)
if err != nil {
return team, err
}
Expand Down
2 changes: 1 addition & 1 deletion github/data_source_github_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func dataSourceGithubUserRead(d *schema.ResourceData, meta interface{}) error {
username := d.Get("username").(string)
log.Printf("[INFO] Refreshing GitHub User: %s", username)

client := meta.(*Organization).client
client := meta.(*Owner).client
ctx := context.Background()

user, _, err := client.Users.Get(ctx, username)
Expand Down
30 changes: 21 additions & 9 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("GITHUB_TOKEN", nil),
Description: descriptions["token"],
},
"owner": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("GITHUB_OWNER", nil),
Description: descriptions["owner"],
},
"organization": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -88,8 +94,10 @@ func init() {
"token": "The OAuth token used to connect to GitHub. " +
"If `anonymous` is false, `token` is required.",

"organization": "The GitHub organization name to manage. " +
"If `individual` is false, `organization` is required.",
"owner": "The GitHub owner name to manage. " +
"If `individual` is false, owner is required.",

"organization": "The GitHub owner name to manage.",

"base_url": "The GitHub Base API URL",

Expand All @@ -108,21 +116,25 @@ func init() {

func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
return func(d *schema.ResourceData) (interface{}, error) {
owner := d.Get("organization").(string)
if owner == "" {
owner = d.Get("owner").(string)
}
config := Config{
Token: d.Get("token").(string),
Organization: d.Get("organization").(string),
BaseURL: d.Get("base_url").(string),
Insecure: d.Get("insecure").(bool),
Individual: d.Get("individual").(bool),
Anonymous: d.Get("anonymous").(bool),
Token: d.Get("token").(string),
Owner: owner,
BaseURL: d.Get("base_url").(string),
Insecure: d.Get("insecure").(bool),
Individual: d.Get("individual").(bool),
Anonymous: d.Get("anonymous").(bool),
}

meta, err := config.Client()
if err != nil {
return nil, err
}

meta.(*Organization).StopContext = p.StopContext()
meta.(*Owner).StopContext = p.StopContext()

return meta, nil
}
Expand Down
3 changes: 3 additions & 0 deletions github/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func testAccPreCheck(t *testing.T) {
if v := os.Getenv("GITHUB_TOKEN"); v == "" {
t.Fatal("GITHUB_TOKEN must be set for acceptance tests")
}
if v := os.Getenv("GITHUB_OWNER"); v == "" {
t.Fatal("GITHUB_OWNER must be set for acceptance tests")
}
if v := os.Getenv("GITHUB_ORGANIZATION"); v == "" {
t.Fatal("GITHUB_ORGANIZATION must be set for acceptance tests")
}
Expand Down
24 changes: 12 additions & 12 deletions github/resource_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ func resourceGithubBranchProtectionCreate(d *schema.ResourceData, meta interface
return err
}

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

orgName := meta.(*Organization).name
orgName := meta.(*Owner).name
repoName := d.Get("repository").(string)
branch := d.Get("branch").(string)

Expand Down Expand Up @@ -193,13 +193,13 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta interface{}
return err
}

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

repoName, branch, err := parseTwoPartID(d.Id())
if err != nil {
return err
}
orgName := meta.(*Organization).name
orgName := meta.(*Owner).name

ctx := context.WithValue(context.Background(), ctxId, d.Id())
if !d.IsNewResource() {
Expand Down Expand Up @@ -259,7 +259,7 @@ func resourceGithubBranchProtectionUpdate(d *schema.ResourceData, meta interface
return err
}

client := meta.(*Organization).client
client := meta.(*Owner).client
repoName, branch, err := parseTwoPartID(d.Id())
if err != nil {
return err
Expand All @@ -270,7 +270,7 @@ func resourceGithubBranchProtectionUpdate(d *schema.ResourceData, meta interface
return err
}

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

log.Printf("[DEBUG] Updating branch protection: %s/%s (%s)",
Expand Down Expand Up @@ -315,13 +315,13 @@ func resourceGithubBranchProtectionDelete(d *schema.ResourceData, meta interface
return err
}

client := meta.(*Organization).client
client := meta.(*Owner).client
repoName, branch, err := parseTwoPartID(d.Id())
if err != nil {
return err
}

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

log.Printf("[DEBUG] Deleting branch protection: %s/%s (%s)", orgName, repoName, branch)
Expand Down Expand Up @@ -376,13 +376,13 @@ func flattenAndSetRequiredStatusChecks(d *schema.ResourceData, protection *githu
}

func requireSignedCommitsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
client := meta.(*Owner).client

repoName, branch, err := parseTwoPartID(d.Id())
if err != nil {
return err
}
orgName := meta.(*Organization).name
orgName := meta.(*Owner).name

ctx := context.WithValue(context.Background(), ctxId, d.Id())
if !d.IsNewResource() {
Expand All @@ -402,13 +402,13 @@ func requireSignedCommitsRead(d *schema.ResourceData, meta interface{}) error {

func requireSignedCommitsUpdate(d *schema.ResourceData, meta interface{}) (err error) {
requiredSignedCommit := d.Get("require_signed_commits").(bool)
client := meta.(*Organization).client
client := meta.(*Owner).client

repoName, branch, err := parseTwoPartID(d.Id())
if err != nil {
return err
}
orgName := meta.(*Organization).name
orgName := meta.(*Owner).name

ctx := context.WithValue(context.Background(), ctxId, d.Id())
if !d.IsNewResource() {
Expand Down
8 changes: 4 additions & 4 deletions github/resource_github_branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ func testAccCheckGithubProtectedBranchExists(n, id string, protection *github.Pr
return fmt.Errorf("Expected ID to be %v, got %v", id, rs.Primary.ID)
}

conn := testAccProvider.Meta().(*Organization).client
o := testAccProvider.Meta().(*Organization).name
conn := testAccProvider.Meta().(*Owner).client
o := testAccProvider.Meta().(*Owner).name
r, b, err := parseTwoPartID(rs.Primary.ID)
if err != nil {
return err
Expand Down Expand Up @@ -360,14 +360,14 @@ func testAccCheckGithubBranchProtectionNoPullRequestReviewsExist(protection *git
}

func testAccGithubBranchProtectionDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*Organization).client
conn := testAccProvider.Meta().(*Owner).client

for _, rs := range s.RootModule().Resources {
if rs.Type != "github_branch_protection" {
continue
}

o := testAccProvider.Meta().(*Organization).name
o := testAccProvider.Meta().(*Owner).name
r, b, err := parseTwoPartID(rs.Primary.ID)
if err != nil {
return err
Expand Down
12 changes: 6 additions & 6 deletions github/resource_github_issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func resourceGithubIssueLabelCreateOrUpdate(d *schema.ResourceData, meta interfa
return err
}

client := meta.(*Organization).client
orgName := meta.(*Organization).name
client := meta.(*Owner).client
orgName := meta.(*Owner).name
repoName := d.Get("repository").(string)
name := d.Get("name").(string)
color := d.Get("color").(string)
Expand Down Expand Up @@ -140,13 +140,13 @@ func resourceGithubIssueLabelRead(d *schema.ResourceData, meta interface{}) erro
return err
}

client := meta.(*Organization).client
client := meta.(*Owner).client
repoName, name, err := parseTwoPartID(d.Id())
if err != nil {
return err
}

orgName := meta.(*Organization).name
orgName := meta.(*Owner).name
ctx := context.WithValue(context.Background(), ctxId, d.Id())
if !d.IsNewResource() {
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string))
Expand Down Expand Up @@ -186,9 +186,9 @@ func resourceGithubIssueLabelDelete(d *schema.ResourceData, meta interface{}) er
return err
}

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

orgName := meta.(*Organization).name
orgName := meta.(*Owner).name
repoName := d.Get("repository").(string)
name := d.Get("name").(string)
ctx := context.WithValue(context.Background(), ctxId, d.Id())
Expand Down
Loading