Skip to content

Commit

Permalink
Cleanup and consolidate supporting GraphQL code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Udit committed Nov 1, 2020
1 parent 144606b commit 5308f7e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 337 deletions.
120 changes: 98 additions & 22 deletions github/util_v4.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package github

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"context"

"github.com/shurcooL/githubv4"
)

Expand All @@ -10,33 +11,108 @@ type PageInfo struct {
HasNextPage bool
}

func expandNestedSet(m map[string]interface{}, target string) []string {
res := make([]string, 0)
if v, ok := m[target]; ok {
vL := v.(*schema.Set).List()
for _, v := range vL {
res = append(res, v.(string))
}
func getRepositoryID(name string, meta interface{}) (githubv4.ID, error) {
var query struct {
Repository struct {
ID githubv4.ID
} `graphql:"repository(owner:$owner, name:$name)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(meta.(*Owner).name),
"name": githubv4.String(name),
}
ctx := context.Background()
client := meta.(*Owner).v4client
err := client.Query(ctx, &query, variables)
if err != nil {
return nil, err
}
return res

return query.Repository.ID, nil
}

func githubv4StringSlice(ss []string) []githubv4.String {
var vGh4 []githubv4.String
for _, s := range ss {
vGh4 = append(vGh4, githubv4.String(s))
func getRepositoryName(id string, meta interface{}) (githubv4.String, error) {

// {
// node(id: "MDEwOlJlcG9zaXRvcnkzMDkxNjc3NjY=") {
// ... on Repository {
// name
// }
// }
// }

var query struct {
Node struct {
Repository struct {
Name githubv4.String
} `graphql:"... on Repository"`
} `graphql:"node(id:$id)"`
}

variables := map[string]interface{}{
"id": githubv4.ID(id),
}
return vGh4
}

func githubv4IDSlice(ss []string) []githubv4.ID {
var vGh4 []githubv4.ID
for _, s := range ss {
vGh4 = append(vGh4, githubv4.ID(s))
ctx := context.Background()
client := meta.(*Owner).v4client
err := client.Query(ctx, &query, variables)
if err != nil {
return "", err
}
return vGh4

return query.Node.Repository.Name, nil
}

func githubv4NewStringSlice(v []githubv4.String) *[]githubv4.String { return &v }
func getBranchProtectionID(name string, pattern string, meta interface{}) (githubv4.ID, error) {
var query struct {
Node struct {
Repository struct {
BranchProtectionRules struct {
Nodes []struct {
ID string
Pattern string
}
PageInfo PageInfo
} `graphql:"branchProtectionRules(first: $first, after: $cursor)"`
ID string
} `graphql:"... on Repository"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(meta.(*Owner).name),
"name": githubv4.String(name),
"first": githubv4.Int(100),
"cursor": (*githubv4.String)(nil),
}

func githubv4NewIDSlice(v []githubv4.ID) *[]githubv4.ID { return &v }
ctx := context.Background()
client := meta.(*Owner).v4client

var allRules []struct {
ID string
Pattern string
}
for {
err := client.Query(ctx, &query, variables)
if err != nil {
return nil, err
}

allRules = append(allRules, query.Node.Repository.BranchProtectionRules.Nodes...)

if !query.Node.Repository.BranchProtectionRules.PageInfo.HasNextPage {
break
}
variables["cursor"] = githubv4.NewString(query.Node.Repository.BranchProtectionRules.PageInfo.EndCursor)
}

var id string
for i := range allRules {
if allRules[i].Pattern == pattern {
id = allRules[i].ID
break
}
}

return id, nil
}
Loading

0 comments on commit 5308f7e

Please sign in to comment.