Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix graphql client creation when only v3 url is given #103

Merged
merged 4 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion github.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ func NewGitHubClient(source Source) (*GitHubClient, error) {
return nil, err
}

clientV4 = githubv4.NewEnterpriseClient(source.GitHubAPIURL+"graphql", httpClient)
var v4URL string
if strings.HasSuffix(source.GitHubAPIURL, "/v3/") {
v4URL = strings.TrimSuffix(source.GitHubAPIURL, "/v3/") + "/graphql"
} else {
v4URL = source.GitHubAPIURL + "graphql"
}
clientV4 = githubv4.NewEnterpriseClient(v4URL, httpClient)
}

if source.GitHubV4APIURL != "" {
Expand Down
46 changes: 46 additions & 0 deletions github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,52 @@ var _ = Describe("GitHub Client", func() {
})
})

Context("with good URLs", func() {
var err error
BeforeEach(func() {
source = Source{
Owner: "concourse",
Repository: "concourse",
}
})
Context("given only the v3 API endpoint", func() {
It("should replace v3 with graphql", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/api/graphql"),
ghttp.RespondWith(200, singlePageResp),
),
)

source.GitHubAPIURL = server.URL() + "/api/v3"
//setting the access token is how we ensure the v4 client is used
source.AccessToken = "abc123"
client, err = NewGitHubClient(source)
Ω(err).ShouldNot(HaveOccurred())

_, err := client.ListReleases()
Ω(err).ShouldNot(HaveOccurred())
})
It("should always append graphql", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/api/graphql"),
ghttp.RespondWith(200, singlePageResp),
),
)

source.GitHubAPIURL = server.URL() + "/api/"
//setting the access token is how we ensure the v4 client is used
source.AccessToken = "abc123"
client, err = NewGitHubClient(source)
Ω(err).ShouldNot(HaveOccurred())

_, err := client.ListReleases()
Ω(err).ShouldNot(HaveOccurred())
})
})
})

Context("with an OAuth Token", func() {
BeforeEach(func() {
source = Source{
Expand Down