Load repo and parent in single query

This commit is contained in:
Jonathan Lloyd 2020-10-13 20:41:16 +01:00
parent a5ec03d000
commit 8f44aee76a
3 changed files with 25 additions and 36 deletions

View file

@ -88,16 +88,23 @@ func (r Repository) ViewerCanTriage() bool {
func GitHubRepo(client *Client, repo ghrepo.Interface) (*Repository, error) {
query := `
fragment repo on Repository {
id
name
owner { login }
hasIssuesEnabled
description
viewerPermission
defaultBranchRef {
name
}
}
query RepositoryInfo($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
id
name
owner { login }
hasIssuesEnabled
description
viewerPermission
defaultBranchRef {
name
...repo
parent {
...repo
}
}
}`

View file

@ -126,8 +126,7 @@ func cloneRun(opts *CloneOptions) error {
// Load the repo from the API to get the username/repo name in its
// canonical capitalization
var canonicalRepo ghrepo.Interface
canonicalRepo, err = api.GitHubRepo(apiClient, repo)
canonicalRepo, err := api.GitHubRepo(apiClient, repo)
if err != nil {
return err
}
@ -139,18 +138,12 @@ func cloneRun(opts *CloneOptions) error {
}
// If the repo is a fork, add the parent as an upstream
var parentRepo ghrepo.Interface
parentRepo, err = api.RepoParent(apiClient, canonicalRepo)
if err != nil {
return err
}
if parentRepo != nil {
protocol, err := cfg.Get(parentRepo.RepoHost(), "git_protocol")
if canonicalRepo.Parent != nil {
protocol, err := cfg.Get(canonicalRepo.Parent.RepoHost(), "git_protocol")
if err != nil {
return err
}
upstreamURL := ghrepo.FormatRemoteURL(parentRepo, protocol)
upstreamURL := ghrepo.FormatRemoteURL(canonicalRepo.Parent, protocol)
err = git.AddUpstreamRemote(upstreamURL, cloneDir)
if err != nil {

View file

@ -103,13 +103,6 @@ func Test_RepoClone(t *testing.T) {
}
} } }
`))
reg.Register(
httpmock.GraphQL(`query RepositoryFindParent\b`),
httpmock.StringResponse(`
{ "data": { "repository": {
"parent": null
} } }
`))
httpClient := &http.Client{Transport: reg}
@ -141,19 +134,15 @@ func Test_RepoClone_hasParent(t *testing.T) {
"name": "REPO",
"owner": {
"login": "OWNER"
},
"parent": {
"name": "ORIG",
"owner": {
"login": "hubot"
}
}
} } }
`))
reg.Register(
httpmock.GraphQL(`query RepositoryFindParent\b`),
httpmock.StringResponse(`
{ "data": { "repository": {
"parent": {
"owner": {"login": "hubot"},
"name": "ORIG"
}
} } }
`))
httpClient := &http.Client{Transport: reg}