Skip to content

Commit

Permalink
Merge pull request #331 from patrickmarabeas/graphql/client
Browse files Browse the repository at this point in the history
Add GraphQL Client
  • Loading branch information
Jeremy Udit authored Apr 15, 2020
2 parents 90eae46 + a73dbfa commit e8af748
Show file tree
Hide file tree
Showing 131 changed files with 7,118 additions and 7,616 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Acceptance test prerequisites
-----------------------------
In order to successfully run the full suite of acceptance tests, you will need to have the following:

export `https://api.github.com/` as the environment variable `GITHUB_BASE_URL`.

### GitHub personal access token
You will need to create a [personal access token](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line) for
testing. It will need to have the following scopes selected:
Expand Down
40 changes: 29 additions & 11 deletions github/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"fmt"
"net/http"
"net/url"
"path"

"github.com/google/go-github/v29/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/logging"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)

Expand All @@ -24,12 +26,13 @@ type Config struct {
type Organization struct {
name string
id int64
client *github.Client
v3client *github.Client
v4client *githubv4.Client
StopContext context.Context
}

// Client configures and returns a fully initialized GithubClient
func (c *Config) Client() (interface{}, error) {
// Clients configures and returns a fully initialized GithubClient and Githubv4Client
func (c *Config) Clients() (interface{}, error) {
var org Organization
var ts oauth2.TokenSource
var tc *http.Client
Expand Down Expand Up @@ -74,22 +77,37 @@ func (c *Config) Client() (interface{}, error) {
tc.Transport = NewRateLimitTransport(tc.Transport)
tc.Transport = logging.NewTransport("Github", tc.Transport)

org.client = github.NewClient(tc)
// Create GraphQL Client
uv4, err := url.Parse(c.BaseURL)
if err != nil {
return nil, err
}
uv4.Path = path.Join(uv4.Path, "graphql")
v4client := githubv4.NewEnterpriseClient(uv4.String(), tc)

if c.BaseURL != "" {
u, err := url.Parse(c.BaseURL)
if err != nil {
return nil, err
}
org.client.BaseURL = u
// Create Rest Client
uv3, err := url.Parse(c.BaseURL)
if err != nil {
return nil, err
}
if uv3.String() != "https://api.github.com/" {
uv3.Path = uv3.Path + "v3/"
}
v3client, err := github.NewEnterpriseClient(uv3.String(), "", tc)
if err != nil {
return nil, err
}
v3client.BaseURL = uv3

org.v3client = v3client
org.v4client = v4client

if c.Individual {
org.name = ""
} else {
org.name = c.Organization

remoteOrg, _, err := org.client.Organizations.Get(ctx, org.name)
remoteOrg, _, err := org.v3client.Organizations.Get(ctx, org.name)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion github/data_source_github_actions_public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func dataSourceGithubActionsPublicKeyRead(d *schema.ResourceData, meta interface
owner := meta.(*Organization).name
log.Printf("[INFO] Refreshing GitHub Actions Public Key from: %s/%s", owner, repository)

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

publicKey, _, err := client.Actions.GetPublicKey(ctx, owner, repository)
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.(*Organization).v3client
ctx := context.Background()

owner := d.Get("owner").(string)
Expand Down
2 changes: 1 addition & 1 deletion github/data_source_github_ip_ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func dataSourceGithubIpRanges() *schema.Resource {
func dataSourceGithubIpRangesRead(d *schema.ResourceData, meta interface{}) error {
org := meta.(*Organization)

api, _, err := org.client.APIMeta(org.StopContext)
api, _, err := org.v3client.APIMeta(org.StopContext)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion github/data_source_github_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func dataSourceGithubMembershipRead(d *schema.ResourceData, meta interface{}) er
username := d.Get("username").(string)
log.Printf("[INFO] Refreshing GitHub membership: %s", username)

client := meta.(*Organization).client
client := meta.(*Organization).v3client
orgName := meta.(*Organization).name

ctx := context.Background()
Expand Down
2 changes: 1 addition & 1 deletion github/data_source_github_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func dataSourceGithubReleaseRead(d *schema.ResourceData, meta interface{}) error
repository := d.Get("repository").(string)
owner := d.Get("owner").(string)

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

var err error
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.(*Organization).v3client

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

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

Expand Down
2 changes: 1 addition & 1 deletion github/data_source_github_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ 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.(*Organization).v3client
ctx := context.Background()

team, err := getGithubTeamBySlug(ctx, client, meta.(*Organization).name, slug)
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 @@ -103,7 +103,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.(*Organization).v3client
ctx := context.Background()

user, _, err := client.Users.Get(ctx, username)
Expand Down
4 changes: 2 additions & 2 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Provider() terraform.ResourceProvider {
"base_url": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("GITHUB_BASE_URL", ""),
DefaultFunc: schema.EnvDefaultFunc("GITHUB_BASE_URL", "https://api.github.com/"),
Description: descriptions["base_url"],
},
"insecure": {
Expand Down Expand Up @@ -122,7 +122,7 @@ func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
Anonymous: d.Get("anonymous").(bool),
}

meta, err := config.Client()
meta, err := config.Clients()
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions github/resource_github_actions_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func resourceGithubActionsSecretCreateOrUpdate(d *schema.ResourceData, meta inte
return err
}

client := meta.(*Organization).client
client := meta.(*Organization).v3client
owner := meta.(*Organization).name
ctx := context.Background()

Expand Down Expand Up @@ -90,7 +90,7 @@ func resourceGithubActionsSecretRead(d *schema.ResourceData, meta interface{}) e
return err
}

client := meta.(*Organization).client
client := meta.(*Organization).v3client
owner := meta.(*Organization).name
ctx := context.Background()

Expand Down Expand Up @@ -118,7 +118,7 @@ func resourceGithubActionsSecretDelete(d *schema.ResourceData, meta interface{})
return err
}

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

Expand All @@ -134,7 +134,7 @@ func resourceGithubActionsSecretDelete(d *schema.ResourceData, meta interface{})
}

func getPublicKeyDetails(owner, repository string, meta interface{}) (keyId, pkValue string, err error) {
client := meta.(*Organization).client
client := meta.(*Organization).v3client
ctx := context.Background()

publicKey, _, err := client.Actions.GetPublicKey(ctx, owner, repository)
Expand Down
4 changes: 2 additions & 2 deletions github/resource_github_actions_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func testAccCheckGithubActionsSecretExists(resourceName, secretName string, t *t
}

org := testAccProvider.Meta().(*Organization)
conn := org.client
conn := org.v3client
_, _, err := conn.Actions.GetSecret(context.TODO(), org.name, repoName, secretName)
if err != nil {
t.Log("Failed to get secret")
Expand All @@ -89,7 +89,7 @@ func testAccCheckGithubActionsSecretExists(resourceName, secretName string, t *t
}

func testAccCheckGithubActionsSecretDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*Organization).client
client := testAccProvider.Meta().(*Organization).v3client

for _, rs := range s.RootModule().Resources {
if rs.Type != "github_actions_secret" {
Expand Down
12 changes: 6 additions & 6 deletions github/resource_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func resourceGithubBranchProtectionCreate(d *schema.ResourceData, meta interface
return err
}

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

orgName := meta.(*Organization).name
repoName := d.Get("repository").(string)
Expand Down Expand Up @@ -198,7 +198,7 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta interface{}
return err
}

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

repoName, branch, err := parseTwoPartID(d.Id(), "repository", "branch")
if err != nil {
Expand Down Expand Up @@ -264,7 +264,7 @@ func resourceGithubBranchProtectionUpdate(d *schema.ResourceData, meta interface
return err
}

client := meta.(*Organization).client
client := meta.(*Organization).v3client
repoName, branch, err := parseTwoPartID(d.Id(), "repository", "branch")
if err != nil {
return err
Expand Down Expand Up @@ -320,7 +320,7 @@ func resourceGithubBranchProtectionDelete(d *schema.ResourceData, meta interface
return err
}

client := meta.(*Organization).client
client := meta.(*Organization).v3client
repoName, branch, err := parseTwoPartID(d.Id(), "repository", "branch")
if err != nil {
return err
Expand Down Expand Up @@ -381,7 +381,7 @@ func flattenAndSetRequiredStatusChecks(d *schema.ResourceData, protection *githu
}

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

repoName, branch, err := parseTwoPartID(d.Id(), "repository", "branch")
if err != nil {
Expand All @@ -407,7 +407,7 @@ 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.(*Organization).v3client

repoName, branch, err := parseTwoPartID(d.Id(), "repository", "branch")
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions github/resource_github_branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ 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
conn := testAccProvider.Meta().(*Organization).v3client
o := testAccProvider.Meta().(*Organization).name
r, b, err := parseTwoPartID(rs.Primary.ID, "repository", "branch")
if err != nil {
Expand Down Expand Up @@ -356,7 +356,7 @@ func testAccCheckGithubBranchProtectionNoPullRequestReviewsExist(protection *git
}

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

for _, rs := range s.RootModule().Resources {
if rs.Type != "github_branch_protection" {
Expand Down
6 changes: 3 additions & 3 deletions github/resource_github_issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func resourceGithubIssueLabelCreateOrUpdate(d *schema.ResourceData, meta interfa
return err
}

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

client := meta.(*Organization).client
client := meta.(*Organization).v3client
repoName, name, err := parseTwoPartID(d.Id(), "repository", "name")
if err != nil {
return err
Expand Down Expand Up @@ -199,7 +199,7 @@ func resourceGithubIssueLabelDelete(d *schema.ResourceData, meta interface{}) er
return err
}

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

orgName := meta.(*Organization).name
repoName := d.Get("repository").(string)
Expand Down
4 changes: 2 additions & 2 deletions github/resource_github_issue_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func testAccCheckGithubIssueLabelExists(n string, label *github.Label) resource.
return fmt.Errorf("No issue label ID is set")
}

conn := testAccProvider.Meta().(*Organization).client
conn := testAccProvider.Meta().(*Organization).v3client
orgName := testAccProvider.Meta().(*Organization).name
repoName, name, err := parseTwoPartID(rs.Primary.ID, "repository", "name")
if err != nil {
Expand Down Expand Up @@ -179,7 +179,7 @@ func testAccCheckGithubIssueLabelIDUnchanged(label, updatedLabel *github.Label)
}

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

for _, rs := range s.RootModule().Resources {
if rs.Type != "github_issue_label" {
Expand Down
6 changes: 3 additions & 3 deletions github/resource_github_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func resourceGithubMembershipCreateOrUpdate(d *schema.ResourceData, meta interfa
return err
}

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

orgName := meta.(*Organization).name
username := d.Get("username").(string)
Expand Down Expand Up @@ -79,7 +79,7 @@ func resourceGithubMembershipRead(d *schema.ResourceData, meta interface{}) erro
return err
}

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

orgName := meta.(*Organization).name
_, username, err := parseTwoPartID(d.Id(), "organization", "username")
Expand Down Expand Up @@ -122,7 +122,7 @@ func resourceGithubMembershipDelete(d *schema.ResourceData, meta interface{}) er
return err
}

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

Expand Down
Loading

0 comments on commit e8af748

Please sign in to comment.