-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-ca
executable file
·119 lines (107 loc) · 3.4 KB
/
gh-ca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python3 -u
# coding=utf-8
import argparse
import subprocess
import sys
import webbrowser
import json
import collections
import urllib.parse
def _check_output(cmd):
"""
Captures the output of `cmd`, exiting the program if the command fails.
"""
proc = subprocess.run(cmd, stdout=subprocess.PIPE)
output = proc.stdout.decode("utf-8").strip()
if proc.returncode != 0:
print(output, end="", file=sys.stdout)
sys.exit(proc.returncode)
return output
def create_pr(args):
"""
The PR subcommand for the CodeApprove CLI. This pushes the current branch and then calls gh pr create.
"""
repo_info = json.loads(
_check_output(
[
"gh",
"repo",
"view",
"--json",
"defaultBranchRef,owner,name",
]
)
)
branch_name = _check_output([
"git",
"rev-parse",
"--abbrev-ref",
"HEAD",
])
# TODO(rockwood): Don't assume the remote is named "origin", but look it up.
remote = "origin"
base = args.base if args.base is not None else repo_info["defaultBranchRef"]["name"]
if not args.no_push:
# Check that we're not pushing the main branch
master_check_proc = subprocess.run(
[
"git",
"merge-base",
"--is-ancestor",
"HEAD",
f"remotes/{remote}/{base}",
]
)
if master_check_proc.returncode == 0:
print(f"You are on the {base} branch", file=sys.stderr)
sys.exit(1)
# Push this branch to the origin.
push_proc = subprocess.run(["git", "push", "--set-upstream", remote, "HEAD"])
if push_proc.returncode != 0:
sys.exit(push_proc.returncode)
# Supported Query Params for this page:
# owner - GitHub repo owner
# repo - the repo name
# base - the base branch name
# head - the head branch name
# title - the pr title
# body - the pr body
base_url = "https://codeapprove.com/create"
query = collections.OrderedDict()
query["owner"] = repo_info["owner"]["login"]
query["repo"] = repo_info["name"]
query["base"] = base
query["head"] = branch_name
# TODO(rockwood): Autofill the title and body if there is only a single commit in this branch.
webbrowser.open_new_tab(base_url + "?" + urllib.parse.urlencode(query))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="A GitHub CLI extension for CodeApprove"
)
subparsers = parser.add_subparsers(
title="subcommands", help="The subcommand to issue"
)
# PR START
pr_parser = subparsers.add_parser(
"pr", help="Create a PR on CodeApprove - similar to `gh pr create`"
)
# This is a trick from the python documentation to get dynamic dispatch for each subcommand.
pr_parser.set_defaults(cmd=create_pr)
pr_parser.add_argument(
"--no_push",
action="store_true",
help="Disable pushing this branch to the origin first",
)
pr_parser.add_argument(
"-B",
"--base",
metavar="branch",
type=str,
help="The branch into which you want your code merged",
)
# PR END
args = parser.parse_args()
if not hasattr(args, "cmd"):
parser.print_help()
sys.exit(1)
args.cmd(args)