Skip to content

Commit

Permalink
Merge pull request #436 from CircleCI-Public/fix-list-contexts
Browse files Browse the repository at this point in the history
Fix overly harsh auth requirements for listing contexts
  • Loading branch information
aengelberg authored Jun 29, 2020
2 parents 51a6fc2 + b2b5f46 commit e30b234
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
17 changes: 12 additions & 5 deletions api/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,18 @@ func CreateContext(cl *client.Client, vcsType, orgName, contextName string) erro
}

func ListContexts(cl *client.Client, orgName, vcsType string) (*ContextsQueryResponse, error) {
// In theory we can lookup the organization by name and its contexts in
// the same query, but using separate requests to circumvent a bug in
// the API
org, err := getOrganization(cl, orgName, vcsType)

if err != nil {
return nil, err
}

query := `
query ContextsQuery($orgName: String!, $vcsType: VCSType!) {
organization(name: $orgName, vcsType: $vcsType) {
query ContextsQuery($orgId: ID!) {
organization(id: $orgId) {
id
contexts {
edges {
Expand Down Expand Up @@ -151,11 +159,10 @@ func ListContexts(cl *client.Client, orgName, vcsType string) (*ContextsQueryRes
request := client.NewRequest(query)
request.SetToken(cl.Token)

request.Var("orgName", orgName)
request.Var("vcsType", strings.ToUpper(vcsType))
request.Var("orgId", org.Organization.ID)

var response ContextsQueryResponse
err := cl.Run(request, &response)
err = cl.Run(request, &response)
return &response, errors.Wrapf(improveVcsTypeError(err), "failed to load context list")
}

Expand Down
9 changes: 7 additions & 2 deletions api/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,13 @@ var _ = Describe("API", func() {
}

server, client := createSingleUseGraphQLServer(list, func(count uint64, req *graphQLRequst) {
Expect(req.Variables["orgName"]).To(Equal("test-org"))
Expect(req.Variables["vcsType"]).To(Equal("TEST-VCS"))
switch count {
case 1:
Expect(req.Variables["organizationName"]).To(Equal("test-org"))
Expect(req.Variables["organizationVcs"]).To(Equal("TEST-VCS"))
case 2:
Expect(req.Variables["orgId"]).To(Equal("C3D79A95-6BD5-40B4-9958-AB6BDC4CAD50"))
}
})
defer server.Close()

Expand Down

0 comments on commit e30b234

Please sign in to comment.