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

Add github_organization_block resource #181

Merged
merged 9 commits into from
May 2, 2019
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion github/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"
"net/url"

"github.com/google/go-github/github"
"github.com/google/go-github/v25/github"
"github.com/hashicorp/terraform/helper/logging"
"golang.org/x/oauth2"
)
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 @@ -4,7 +4,7 @@ import (
"context"
"log"

"github.com/google/go-github/github"
"github.com/google/go-github/v25/github"
"github.com/hashicorp/terraform/helper/schema"
)

Expand Down
10 changes: 5 additions & 5 deletions github/data_source_github_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"
"strconv"

"github.com/google/go-github/github"
"github.com/google/go-github/v25/github"
"github.com/hashicorp/terraform/helper/schema"
)

Expand Down Expand Up @@ -51,12 +51,12 @@ func dataSourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
ctx := context.Background()

team, err := getGithubTeamBySlug(client, meta.(*Organization).name, slug)
team, err := getGithubTeamBySlug(ctx, client, meta.(*Organization).name, slug)
if err != nil {
return err
}

member, _, err := client.Organizations.ListTeamMembers(ctx, team.GetID(), nil)
member, _, err := client.Teams.ListTeamMembers(ctx, team.GetID(), nil)
if err != nil {
return err
}
Expand All @@ -76,10 +76,10 @@ func dataSourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

func getGithubTeamBySlug(client *github.Client, org string, slug string) (team *github.Team, err error) {
func getGithubTeamBySlug(ctx context.Context, client *github.Client, org string, slug string) (team *github.Team, err error) {
opt := &github.ListOptions{PerPage: 10}
for {
teams, resp, err := client.Organizations.ListTeams(context.TODO(), org, opt)
teams, resp, err := client.Teams.ListTeams(ctx, org, opt)
if err != nil {
return team, err
}
Expand Down
7 changes: 4 additions & 3 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,27 @@ func Provider() terraform.ResourceProvider {
"github_branch_protection": resourceGithubBranchProtection(),
"github_issue_label": resourceGithubIssueLabel(),
"github_membership": resourceGithubMembership(),
"github_organization_block": resourceOrganizationBlock(),
"github_organization_project": resourceGithubOrganizationProject(),
"github_organization_webhook": resourceGithubOrganizationWebhook(),
"github_project_column": resourceGithubProjectColumn(),
"github_repository": resourceGithubRepository(),
"github_repository_collaborator": resourceGithubRepositoryCollaborator(),
"github_repository_deploy_key": resourceGithubRepositoryDeployKey(),
"github_repository_project": resourceGithubRepositoryProject(),
"github_repository_webhook": resourceGithubRepositoryWebhook(),
"github_team": resourceGithubTeam(),
"github_repository": resourceGithubRepository(),
"github_team_membership": resourceGithubTeamMembership(),
"github_team_repository": resourceGithubTeamRepository(),
"github_team": resourceGithubTeam(),
"github_user_gpg_key": resourceGithubUserGpgKey(),
"github_user_invitation_accepter": resourceGithubUserInvitationAccepter(),
"github_user_ssh_key": resourceGithubUserSshKey(),
},

DataSourcesMap: map[string]*schema.Resource{
"github_ip_ranges": dataSourceGithubIpRanges(),
"github_repository": dataSourceGithubRepository(),
"github_repositories": dataSourceGithubRepositories(),
"github_repository": dataSourceGithubRepository(),
"github_team": dataSourceGithubTeam(),
"github_user": dataSourceGithubUser(),
},
Expand Down
19 changes: 14 additions & 5 deletions github/resource_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"log"
"net/http"

"github.com/google/go-github/github"
"github.com/google/go-github/v25/github"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceGithubBranchProtection() *schema.Resource {
Expand Down Expand Up @@ -96,6 +97,12 @@ func resourceGithubBranchProtection() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"required_approving_review_count": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
ValidateFunc: validation.IntBetween(1, 6),
},
},
},
},
Expand Down Expand Up @@ -338,10 +345,11 @@ func flattenAndSetRequiredPullRequestReviews(d *schema.ResourceData, protection

return d.Set("required_pull_request_reviews", []interface{}{
map[string]interface{}{
"dismiss_stale_reviews": rprr.DismissStaleReviews,
"dismissal_users": schema.NewSet(schema.HashString, users),
"dismissal_teams": schema.NewSet(schema.HashString, teams),
"require_code_owner_reviews": rprr.RequireCodeOwnerReviews,
"dismiss_stale_reviews": rprr.DismissStaleReviews,
"dismissal_users": schema.NewSet(schema.HashString, users),
"dismissal_teams": schema.NewSet(schema.HashString, teams),
"require_code_owner_reviews": rprr.RequireCodeOwnerReviews,
"required_approving_review_count": rprr.RequiredApprovingReviewCount,
},
})
}
Expand Down Expand Up @@ -427,6 +435,7 @@ func expandRequiredPullRequestReviews(d *schema.ResourceData) (*github.PullReque
rprr.DismissalRestrictionsRequest = drr
rprr.DismissStaleReviews = m["dismiss_stale_reviews"].(bool)
rprr.RequireCodeOwnerReviews = m["require_code_owner_reviews"].(bool)
rprr.RequiredApprovingReviewCount = m["required_approving_review_count"].(int)
}

return rprr, nil
Expand Down
2 changes: 1 addition & 1 deletion github/resource_github_branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sort"
"testing"

"github.com/google/go-github/github"
"github.com/google/go-github/v25/github"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
Expand Down
2 changes: 1 addition & 1 deletion github/resource_github_issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"net/http"

"github.com/google/go-github/github"
"github.com/google/go-github/v25/github"
"github.com/hashicorp/terraform/helper/schema"
)

Expand Down
2 changes: 1 addition & 1 deletion github/resource_github_issue_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"testing"

"github.com/google/go-github/github"
"github.com/google/go-github/v25/github"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
Expand Down
2 changes: 1 addition & 1 deletion github/resource_github_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"net/http"

"github.com/google/go-github/github"
"github.com/google/go-github/v25/github"
"github.com/hashicorp/terraform/helper/schema"
)

Expand Down
2 changes: 1 addition & 1 deletion github/resource_github_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"testing"

"github.com/google/go-github/github"
"github.com/google/go-github/v25/github"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
Expand Down
15 changes: 9 additions & 6 deletions github/resource_github_organization_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"net/http"
"strconv"

"github.com/google/go-github/github"
"github.com/google/go-github/v25/github"
"github.com/hashicorp/terraform/helper/schema"
)

Expand Down Expand Up @@ -46,14 +46,15 @@ func resourceGithubOrganizationProjectCreate(d *schema.ResourceData, meta interf
client := meta.(*Organization).client
orgName := meta.(*Organization).name
name := d.Get("name").(string)
body := d.Get("body").(string)
ctx := context.Background()

log.Printf("[DEBUG] Creating organization project: %s (%s)", name, orgName)
project, _, err := client.Organizations.CreateProject(ctx,
orgName,
&github.ProjectOptions{
Name: name,
Body: d.Get("body").(string),
Name: &name,
Body: &body,
},
)
if err != nil {
Expand Down Expand Up @@ -105,12 +106,14 @@ func resourceGithubOrganizationProjectRead(d *schema.ResourceData, meta interfac

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

orgName := meta.(*Organization).name

name := d.Get("name").(string)
body := d.Get("body").(string)

options := github.ProjectOptions{
Name: name,
Body: d.Get("body").(string),
Name: &name,
Body: &body,
}

projectID, err := strconv.ParseInt(d.Id(), 10, 64)
Expand Down
2 changes: 1 addition & 1 deletion github/resource_github_organization_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
"testing"

"github.com/google/go-github/github"
"github.com/google/go-github/v25/github"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
Expand Down
21 changes: 2 additions & 19 deletions github/resource_github_organization_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package github

import (
"context"
"fmt"
"log"
"net/http"
"strconv"

"github.com/google/go-github/github"
"github.com/google/go-github/v25/github"
"github.com/hashicorp/terraform/helper/schema"
)

Expand All @@ -23,12 +22,6 @@ func resourceGithubOrganizationWebhook() *schema.Resource {
MigrateState: resourceGithubWebhookMigrateState,

Schema: map[string]*schema.Schema{
"name": {
paultyng marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateGithubOrganizationWebhookName,
},
"events": {
Type: schema.TypeSet,
Required: true,
Expand All @@ -53,13 +46,6 @@ func resourceGithubOrganizationWebhook() *schema.Resource {
}
}

func validateGithubOrganizationWebhookName(v interface{}, k string) (ws []string, errors []error) {
if v.(string) != "web" {
errors = append(errors, fmt.Errorf("Github: name can only be web"))
}
return
}

func resourceGithubOrganizationWebhookObject(d *schema.ResourceData) *github.Hook {
events := []string{}
eventSet := d.Get("events").(*schema.Set)
Expand All @@ -68,7 +54,6 @@ func resourceGithubOrganizationWebhookObject(d *schema.ResourceData) *github.Hoo
}

hook := &github.Hook{
Name: github.String(d.Get("name").(string)),
URL: github.String(d.Get("url").(string)),
Events: events,
Active: github.Bool(d.Get("active").(bool)),
Expand All @@ -89,8 +74,7 @@ func resourceGithubOrganizationWebhookCreate(d *schema.ResourceData, meta interf
webhookObj := resourceGithubOrganizationWebhookObject(d)
ctx := context.Background()

log.Printf("[DEBUG] Creating organization webhook: %s (%s)",
webhookObj.GetName(), orgName)
log.Printf("[DEBUG] Creating organization webhook: %d (%s)", webhookObj.GetID(), orgName)
hook, _, err := client.Organizations.CreateHook(ctx, orgName, webhookObj)

if err != nil {
Expand Down Expand Up @@ -132,7 +116,6 @@ func resourceGithubOrganizationWebhookRead(d *schema.ResourceData, meta interfac
}

d.Set("etag", resp.Header.Get("ETag"))
d.Set("name", hook.Name)
d.Set("url", hook.URL)
d.Set("active", hook.Active)
d.Set("events", hook.Events)
Expand Down
18 changes: 4 additions & 14 deletions github/resource_github_organization_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"testing"

"github.com/google/go-github/github"
"github.com/google/go-github/v25/github"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
Expand All @@ -26,12 +26,11 @@ func TestAccGithubOrganizationWebhook_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubOrganizationWebhookExists("github_organization_webhook.foo", &hook),
testAccCheckGithubOrganizationWebhookAttributes(&hook, &testAccGithubOrganizationWebhookExpectedAttributes{
Name: "web",
Events: []string{"pull_request"},
Configuration: map[string]interface{}{
"url": "https://google.de/webhook",
"content_type": "json",
"insecure_ssl": "1",
"insecure_ssl": "true",
},
Active: true,
}),
Expand All @@ -42,12 +41,11 @@ func TestAccGithubOrganizationWebhook_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubOrganizationWebhookExists("github_organization_webhook.foo", &hook),
testAccCheckGithubOrganizationWebhookAttributes(&hook, &testAccGithubOrganizationWebhookExpectedAttributes{
Name: "web",
Events: []string{"issues"},
Configuration: map[string]interface{}{
"url": "https://google.de/webhooks",
"content_type": "form",
"insecure_ssl": "0",
"insecure_ssl": "false",
},
Active: false,
}),
Expand All @@ -70,13 +68,12 @@ func TestAccGithubOrganizationWebhook_secret(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubOrganizationWebhookExists("github_organization_webhook.foo", &hook),
testAccCheckGithubOrganizationWebhookAttributes(&hook, &testAccGithubOrganizationWebhookExpectedAttributes{
Name: "web",
Events: []string{"pull_request"},
Configuration: map[string]interface{}{
"url": "https://www.terraform.io/webhook",
"content_type": "json",
"secret": "********",
"insecure_ssl": "0",
"insecure_ssl": "false",
},
Active: true,
}),
Expand Down Expand Up @@ -113,7 +110,6 @@ func testAccCheckGithubOrganizationWebhookExists(n string, hook *github.Hook) re
}

type testAccGithubOrganizationWebhookExpectedAttributes struct {
Name string
Events []string
Configuration map[string]interface{}
Active bool
Expand All @@ -122,9 +118,6 @@ type testAccGithubOrganizationWebhookExpectedAttributes struct {
func testAccCheckGithubOrganizationWebhookAttributes(hook *github.Hook, want *testAccGithubOrganizationWebhookExpectedAttributes) resource.TestCheckFunc {
return func(s *terraform.State) error {

if *hook.Name != want.Name {
return fmt.Errorf("got hook %q; want %q", *hook.Name, want.Name)
}
if *hook.Active != want.Active {
return fmt.Errorf("got hook %t; want %t", *hook.Active, want.Active)
}
Expand Down Expand Up @@ -172,7 +165,6 @@ func testAccCheckGithubOrganizationWebhookDestroy(s *terraform.State) error {

const testAccGithubOrganizationWebhookConfig = `
resource "github_organization_webhook" "foo" {
name = "web"
configuration {
url = "https://google.de/webhook"
content_type = "json"
Expand All @@ -185,7 +177,6 @@ resource "github_organization_webhook" "foo" {

const testAccGithubOrganizationWebhookUpdateConfig = `
resource "github_organization_webhook" "foo" {
name = "web"
configuration {
url = "https://google.de/webhooks"
content_type = "form"
Expand All @@ -199,7 +190,6 @@ resource "github_organization_webhook" "foo" {

const testAccGithubOrganizationWebhookConfig_secret = `
resource "github_organization_webhook" "foo" {
name = "web"
configuration {
url = "https://www.terraform.io/webhook"
content_type = "json"
Expand Down
Loading