Skip to content

Commit

Permalink
Merge branch 'main' into branches
Browse files Browse the repository at this point in the history
  • Loading branch information
kfcampbell authored Aug 26, 2022
2 parents 5534c8a + 772c71b commit f857f8f
Show file tree
Hide file tree
Showing 18 changed files with 243 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '1.18'
go-version: '1.19'
- run: make tools
- run: make lint
- run: make website-lint
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ website/node_modules
*.tfvars
.vscode/
testdata/

website/vendor
terraform-provider-github

# Test exclusions
!command/test-fixtures/**/*.tfstate
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# NOTE: CHANGELOG.md is deprecated

After the release of v4.24.0, please see the [GitHub release notes](https://github.com/integrations/terraform-provider-github/releases) for the provider in order to view the most up-to-date changes.

# 4.24.0 (Apr 28, 2022)

ENHANCEMENTS:
Expand Down
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ default: build

tools:
go install github.com/client9/misspell/cmd/misspell
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.45.2
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.48.0

build: fmtcheck
go build ./...
Expand Down
4 changes: 2 additions & 2 deletions github/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"time"

Expand Down Expand Up @@ -51,7 +51,7 @@ func getInstallationAccessToken(baseURL string, jwt string, installationID strin
}
defer func() { _ = res.Body.Close() }()

resBytes, err := ioutil.ReadAll(res.Body)
resBytes, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
Expand Down
6 changes: 3 additions & 3 deletions github/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"time"
Expand All @@ -24,7 +24,7 @@ const (
var (
testEpochTime = time.Unix(0, 0)

testGitHubAppPrivateKeyPemData, _ = ioutil.ReadFile(testGitHubAppPrivateKeyFile)
testGitHubAppPrivateKeyPemData, _ = os.ReadFile(testGitHubAppPrivateKeyFile)
)

func TestGenerateAppJWT(t *testing.T) {
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestGenerateAppJWT(t *testing.T) {
})

t.Run("produces a verifiable jwt", func(t *testing.T) {
publicKeyData, err := ioutil.ReadFile(testGitHubAppPublicKeyFile)
publicKeyData, err := os.ReadFile(testGitHubAppPublicKeyFile)
if err != nil {
t.Logf("Failed to read public key file '%s': %s", testGitHubAppPublicKeyFile, err)
t.FailNow()
Expand Down
1 change: 0 additions & 1 deletion github/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ func (c *Config) NewRESTClient(client *http.Client) (*github.Client, error) {
}

func (c *Config) ConfigureOwner(owner *Owner) (*Owner, error) {

ctx := context.Background()
owner.name = c.Owner
if owner.name == "" {
Expand Down
74 changes: 74 additions & 0 deletions github/data_source_github_external_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package github

import (
"context"
"encoding/json"
"fmt"

"github.com/google/go-github/v45/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceGithubExternalGroups() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubExternalGroupsRead,
Schema: map[string]*schema.Schema{
"external_groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"group_id": {
Type: schema.TypeInt,
Computed: true,
},
"group_name": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceGithubExternalGroupsRead(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name

ctx := context.WithValue(context.Background(), ctxId, d.Id())
opts := &github.ListExternalGroupsOptions{}

externalGroups, _, err := client.Teams.ListExternalGroups(ctx, orgName, opts)
if err != nil {
return err
}

// convert to JSON in order to martial to format we can return
jsonGroups, err := json.Marshal(externalGroups.Groups)
if err != nil {
return err
}

groupsState := make([]map[string]interface{}, 0)
err = json.Unmarshal(jsonGroups, &groupsState)
if err != nil {
return err
}

if err := d.Set("external_groups", groupsState); err != nil {
return err
}

d.SetId(fmt.Sprintf("/orgs/%v/external-groups", orgName))
return nil
}
71 changes: 59 additions & 12 deletions github/data_source_github_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (

"github.com/google/go-github/v45/github"

"github.com/shurcooL/githubv4"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

func dataSourceGithubTeam() *schema.Resource {
Expand Down Expand Up @@ -48,6 +51,12 @@ func dataSourceGithubTeam() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"membership_type": {
Type: schema.TypeString,
Default: "all",
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"all", "immediate"}, false),
},
},
}
}
Expand All @@ -71,22 +80,60 @@ func dataSourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error {
}

var members []string
for {
member, resp, err := client.Teams.ListTeamMembersByID(ctx, orgId, team.GetID(), &options)
if err != nil {
return err
if d.Get("membership_type").(string) == "all" {
for {
member, resp, err := client.Teams.ListTeamMembersByID(ctx, orgId, team.GetID(), &options)
if err != nil {
return err
}

for _, v := range member {
members = append(members, v.GetLogin())
}

if resp.NextPage == 0 {
break
}
options.Page = resp.NextPage
}

for _, v := range member {
members = append(members, v.GetLogin())
} else {
type member struct {
Login string
}

if resp.NextPage == 0 {
break
var query struct {
Organization struct {
Team struct {
Members struct {
Nodes []member
PageInfo struct {
EndCursor githubv4.String
HasNextPage bool
}
} `graphql:"members(first:100,after:$memberCursor,membership:IMMEDIATE)"`
} `graphql:"team(slug:$slug)"`
} `graphql:"organization(login:$owner)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(meta.(*Owner).name),
"slug": githubv4.String(slug),
"memberCursor": (*githubv4.String)(nil),
}
client := meta.(*Owner).v4client
for {
nameErr := client.Query(ctx, &query, variables)
if nameErr != nil {
return nameErr
}
for _, v := range query.Organization.Team.Members.Nodes {
members = append(members, v.Login)
}
if query.Organization.Team.Members.PageInfo.HasNextPage {
variables["memberCursor"] = query.Organization.Team.Members.PageInfo.EndCursor
} else {
break
}
}
options.Page = resp.NextPage
}

var repositories []string
for {
repository, resp, err := client.Teams.ListTeamReposByID(ctx, orgId, team.GetID(), &options.ListOptions)
Expand Down
44 changes: 44 additions & 0 deletions github/data_source_github_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,50 @@ func TestAccGithubTeamDataSource(t *testing.T) {

})

t.Run("queries an existing team without error with immediate membership", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_team" "test" {
name = "tf-acc-test-%s"
}
data "github_team" "test" {
slug = github_team.test.slug
membership_type = "immediate"
}
`, randomID)

check := resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.github_team.test", "name"),
resource.TestCheckResourceAttr("data.github_team.test", "name", fmt.Sprintf("tf-acc-test-%s", randomID)),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
t.Skip("individual account not supported for this operation")
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})
})

t.Run("errors when querying a non-existing team", func(t *testing.T) {

config := `
Expand Down
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func Provider() terraform.ResourceProvider {
"github_branch": dataSourceGithubBranch(),
"github_collaborators": dataSourceGithubCollaborators(),
"github_dependabot_public_key": dataSourceGithubDependabotPublicKey(),
"github_external_groups": dataSourceGithubExternalGroups(),
"github_ip_ranges": dataSourceGithubIpRanges(),
"github_membership": dataSourceGithubMembership(),
"github_organization": dataSourceGithubOrganization(),
Expand Down
8 changes: 0 additions & 8 deletions github/resource_github_team_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ func resourceGithubTeamMembersUpdate(d *schema.ResourceData, meta interface{}) e
if err != nil {
return err
}

continue
}

if create {
Expand All @@ -167,12 +165,6 @@ func resourceGithubTeamMembersUpdate(d *schema.ResourceData, meta interface{}) e
if err != nil {
return err
}
continue
}

// no change
if reflect.DeepEqual(change.Old, change.New) {
continue
}
}

Expand Down
3 changes: 1 addition & 2 deletions github/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package github
import (
"bytes"
"io"
"io/ioutil"
"log"
"net/http"
"sync"
Expand Down Expand Up @@ -176,7 +175,7 @@ func drainBody(b io.ReadCloser) (r1, r2 io.ReadCloser, err error) {
if err = b.Close(); err != nil {
return nil, b, err
}
return ioutil.NopCloser(&buf), ioutil.NopCloser(bytes.NewReader(buf.Bytes())), nil
return io.NopCloser(&buf), io.NopCloser(bytes.NewReader(buf.Bytes())), nil
}

func isWriteMethod(method string) bool {
Expand Down
4 changes: 2 additions & 2 deletions github/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package github
import (
"context"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -51,7 +51,7 @@ func githubApiMock(responseSequence []*mockResponse) *httptest.Server {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Server", "GitHub.com")

bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("[DEBUG] Error: %s", err)
}
Expand Down
Loading

0 comments on commit f857f8f

Please sign in to comment.