Skip to content

Commit

Permalink
fix: fix unmarshall error in graphql call (runatlantis#2128)
Browse files Browse the repository at this point in the history
* Fix unmarshall error in GraphQL call runatlantis#2090

* Add unit tests for GetTeamNamesForUser

* Fix lint error
  • Loading branch information
raymondchen625 authored and krrrr38 committed Dec 16, 2022
1 parent 4e82a97 commit e0192ee
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
15 changes: 14 additions & 1 deletion server/events/vcs/github_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/runatlantis/atlantis/server/events/vcs/common"
"github.com/runatlantis/atlantis/server/logging"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)

// maxCommentLength is the maximum number of chars allowed in a single comment
Expand All @@ -40,6 +41,7 @@ type GithubClient struct {
user string
client *github.Client
v4MutateClient *graphql.Client
v4QueryClient *githubv4.Client
ctx context.Context
logger logging.SimpleLogging
}
Expand Down Expand Up @@ -91,6 +93,16 @@ func NewGithubClient(hostname string, credentials GithubCredentials, logger logg
transport,
graphql.WithHeader("Accept", "application/vnd.github.queen-beryl-preview+json"),
)
token, err := credentials.GetToken()
if err != nil {
return nil, errors.Wrap(err, "Failed to get GitHub token")
}
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
httpClient := oauth2.NewClient(context.Background(), src)
// Use the client from shurcooL's githubv4 library for queries.
v4QueryClient := githubv4.NewEnterpriseClient(graphqlURL, httpClient)

user, err := credentials.GetUser()
logger.Debug("GH User: %s", user)
Expand All @@ -102,6 +114,7 @@ func NewGithubClient(hostname string, credentials GithubCredentials, logger logg
user: user,
client: client,
v4MutateClient: v4MutateClient,
v4QueryClient: v4QueryClient,
ctx: context.Background(),
logger: logger,
}, nil
Expand Down Expand Up @@ -461,7 +474,7 @@ func (g *GithubClient) GetTeamNamesForUser(repo models.Repo, user models.User) (
var teamNames []string
ctx := context.Background()
for {
err := g.v4MutateClient.Query(ctx, &q, variables)
err := g.v4QueryClient.Query(ctx, &q, variables)
if err != nil {
return nil, err
}
Expand Down
46 changes: 46 additions & 0 deletions server/events/vcs/github_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,3 +981,49 @@ func TestGithubClient_Retry404Files(t *testing.T) {
Ok(t, err)
Equals(t, 3, numCalls)
}

// GetTeamNamesForUser returns a list of team names for a user.
func TestGithubClient_GetTeamNamesForUser(t *testing.T) {
logger := logging.NewNoopLogger(t)
// Mocked GraphQL response for two teams
resp := `{
"data":{
"organization": {
"teams":{
"edges":[
{"node":{"name":"frontend-developers"}},
{"node":{"name":"employees"}}
],
"pageInfo":{
"endCursor":"Y3Vyc29yOnYyOpHOAFMoLQ==",
"hasNextPage":false
}
}
}
}
}`
testServer := httptest.NewTLSServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
case "/api/graphql":
w.Write([]byte(resp)) // nolint: errcheck
default:
t.Errorf("got unexpected request at %q", r.RequestURI)
http.Error(w, "not found", http.StatusNotFound)
return
}
}))
testServerURL, err := url.Parse(testServer.URL)
Ok(t, err)
client, err := vcs.NewGithubClient(testServerURL.Host, &vcs.GithubUserCredentials{"user", "pass"}, logger)
Ok(t, err)
defer disableSSLVerification()()

teams, err := client.GetTeamNamesForUser(models.Repo{
Owner: "testrepo",
}, models.User{
Username: "testuser",
})
Ok(t, err)
Equals(t, []string{"frontend-developers", "employees"}, teams)
}

0 comments on commit e0192ee

Please sign in to comment.