Skip to content

Commit 5087ec5

Browse files
author
nate smith
committed
restore pr create with new context/client
1 parent eefb6d1 commit 5087ec5

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

api/queries.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,34 @@ type Repo interface {
2222
RepoOwner() string
2323
}
2424

25+
func GitHubRepoId(client *Client, ghRepo Repo) (string, error) {
26+
owner := ghRepo.RepoOwner()
27+
repo := ghRepo.RepoName()
28+
29+
query := `
30+
query FindRepoID($owner:String!, $name:String!) {
31+
repository(owner:$owner, name:$name) {
32+
id
33+
}
34+
}`
35+
variables := map[string]interface{}{
36+
"owner": owner,
37+
"name": repo,
38+
}
39+
40+
result := struct {
41+
Repository struct {
42+
Id string
43+
}
44+
}{}
45+
err := client.GraphQL(query, variables, &result)
46+
if err != nil {
47+
return "", fmt.Errorf("failed to determine GH repo ID: %s", err)
48+
}
49+
50+
return result.Repository.Id, nil
51+
}
52+
2553
func PullRequests(client *Client, ghRepo Repo, currentBranch, currentUsername string) (*PullRequestsPayload, error) {
2654
type edges struct {
2755
Edges []struct {
@@ -171,3 +199,46 @@ func PullRequestsForBranch(client *Client, ghRepo Repo, branch string) ([]PullRe
171199

172200
return prs, nil
173201
}
202+
203+
func CreatePullRequest(client *Client, ghRepo Repo, title string, body string, draft bool, base string, head string) (string, error) {
204+
repoId, err := GitHubRepoId(client, ghRepo)
205+
if err != nil {
206+
return "", err
207+
}
208+
if repoId == "" {
209+
return "", fmt.Errorf("could not determine GH repo ID")
210+
}
211+
212+
query := `
213+
mutation CreatePullRequest($input: CreatePullRequestInput!) {
214+
createPullRequest(input: $input) {
215+
pullRequest {
216+
url
217+
}
218+
}
219+
}`
220+
221+
variables := map[string]interface{}{
222+
"input": map[string]interface{}{
223+
"repositoryId": repoId,
224+
"baseRefName": base,
225+
"headRefName": head,
226+
"title": title,
227+
"body": body,
228+
"draft": draft,
229+
},
230+
}
231+
232+
result := struct {
233+
CreatePullRequest struct {
234+
PullRequest PullRequest
235+
}
236+
}{}
237+
238+
err = client.GraphQL(query, variables, &result)
239+
if err != nil {
240+
return "", err
241+
}
242+
243+
return result.CreatePullRequest.PullRequest.URL, nil
244+
}

command/pr.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func init() {
2222
Short: "Open a pull request in the browser",
2323
RunE: prView,
2424
},
25+
prCreateCmd,
2526
)
2627
}
2728

command/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) {
5858
opts := []api.ClientOption{
5959
api.AddHeader("Authorization", fmt.Sprintf("token %s", token)),
6060
api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", version.Version)),
61+
api.AddHeader("Accept", "application/vnd.github.shadow-cat-preview+json"),
6162
}
6263
if verbose := os.Getenv("DEBUG"); verbose != "" {
6364
opts = append(opts, api.VerboseLog(os.Stderr))

git/git.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,29 @@ func LocalBranches() ([]string, error) {
216216
return branches, nil
217217
}
218218

219+
func UncommittedChangeCount() (int, error) {
220+
statusCmd := exec.Command("git", "status", "--porcelain")
221+
output, err := statusCmd.Output()
222+
if err != nil {
223+
return 0, fmt.Errorf("failed to run git status: %s", err)
224+
}
225+
lines := strings.Split(string(output), "\n")
226+
227+
count := 0
228+
229+
for _, l := range lines {
230+
if l != "" {
231+
count++
232+
}
233+
}
234+
235+
return count, nil
236+
}
237+
238+
var Push = func(remote string, ref string) error {
239+
return Run("push", "--set-upstream", remote, ref)
240+
}
241+
219242
func outputLines(output []byte) []string {
220243
lines := strings.TrimSuffix(string(output), "\n")
221244
if lines == "" {

0 commit comments

Comments
 (0)