Merge pull request #362 from cli/cross-repo-create

Cross repo pr create
This commit is contained in:
Nate Smith 2020-02-13 17:23:47 -06:00 committed by GitHub
commit 2e7feddcc4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 1 deletions

View file

@ -176,12 +176,17 @@ func prCreate(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("pull request title must not be blank")
}
headRefName := headBranch
if !ghrepo.IsSame(headRemote, baseRepo) {
headRefName = fmt.Sprintf("%s:%s", headRemote.RepoOwner(), headBranch)
}
params := map[string]interface{}{
"title": title,
"body": body,
"draft": isDraft,
"baseRefName": baseBranch,
"headRefName": headBranch,
"headRefName": headRefName,
}
pr, err := api.CreatePullRequest(client, baseRepo, params)

View file

@ -10,6 +10,7 @@ import (
"strings"
"testing"
"github.com/cli/cli/context"
"github.com/cli/cli/git"
"github.com/cli/cli/test"
"github.com/cli/cli/utils"
@ -131,3 +132,85 @@ Creating pull request for feature into master in OWNER/REPO
`)
}
func TestPRCreate_cross_repo_same_branch(t *testing.T) {
ctx := context.NewBlank()
ctx.SetBranch("default")
ctx.SetRemotes(map[string]string{
"origin": "OWNER/REPO",
"fork": "MYSELF/REPO",
})
initContext = func() context.Context {
return ctx
}
http := initFakeHTTP()
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repo_000": {
"id": "REPOID0",
"name": "REPO",
"owner": {"login": "OWNER"},
"defaultBranchRef": {
"name": "default",
"target": {"oid": "deadbeef"}
},
"viewerPermission": "READ"
},
"repo_001" : {
"parent": {
"id": "REPOID0",
"name": "REPO",
"owner": {"login": "OWNER"},
"defaultBranchRef": {
"name": "default",
"target": {"oid": "deadbeef"}
},
"viewerPermission": "READ"
},
"id": "REPOID1",
"name": "REPO",
"owner": {"login": "MYSELF"},
"defaultBranchRef": {
"name": "default",
"target": {"oid": "deadbeef"}
},
"viewerPermission": "WRITE"
} } }
`))
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "createPullRequest": { "pullRequest": {
"URL": "https://github.com/OWNER/REPO/pull/12"
} } } }
`))
origGitCommand := git.GitCommand
git.GitCommand = test.StubExecCommand("TestPrCreateHelperProcess", "clean")
defer func() {
git.GitCommand = origGitCommand
}()
output, err := RunCommand(prCreateCmd, `pr create -t "cross repo" -b "same branch"`)
eq(t, err, nil)
bodyBytes, _ := ioutil.ReadAll(http.Requests[1].Body)
reqBody := struct {
Variables struct {
Input struct {
RepositoryID string
Title string
Body string
BaseRefName string
HeadRefName string
}
}
}{}
json.Unmarshal(bodyBytes, &reqBody)
eq(t, reqBody.Variables.Input.RepositoryID, "REPOID0")
eq(t, reqBody.Variables.Input.Title, "cross repo")
eq(t, reqBody.Variables.Input.Body, "same branch")
eq(t, reqBody.Variables.Input.BaseRefName, "default")
eq(t, reqBody.Variables.Input.HeadRefName, "MYSELF:default")
eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n")
// goal: only care that gql is formatted properly
}