@@ -71,39 +71,64 @@ if [ -z "$GIT_CMD" ] || ! $GIT_CMD --version >/dev/null 2>&1 ; then
7171 exit 1
7272fi
7373
74- python_data_script () {
75- local SCRIPT=$1
76- local OPT_ARG=$2
77-
74+ make_tmp_file () {
7875 $PYTHON_CMD -c "
79- try:
80- import json
81- except ImportError:
82- import simplejson as json
83- import sys
84- data = sys.stdin.read().strip().replace('\n',r'\n').replace('\r','')
85- data = json.loads(data)
86- $SCRIPT
87- " $OPT_ARG
76+ import tempfile, sys
77+ _, path = tempfile.mkstemp()
78+ sys.stdout.write(path)
79+ "
8880}
8981
90- json_escape () {
91- $PYTHON_CMD -c "
82+ python_helper () {
83+ $PYTHON_CMD -c '
9284import sys
9385try:
9486 import json
9587except ImportError:
9688 import simplejson as json
97- sys.stdout.write(json.dumps(sys.stdin.read().strip()))
98- "
99- }
10089
101- make_tmp_file () {
102- $PYTHON_CMD -c "
103- import tempfile, sys
104- _, path = tempfile.mkstemp()
105- sys.stdout.write(path)
106- "
90+ if sys.argv[1] == "json_escape":
91+ # take either a second argument, or stdin
92+ input_txt = None
93+ if len(sys.argv) > 2:
94+ input_txt = sys.argv[2]
95+ else:
96+ input_txt = sys.stdin.read().strip()
97+ sys.stdout.write(json.dumps(input_txt))
98+ sys.exit(0)
99+
100+ # all following commands take a json blob as the second argument
101+ data = json.loads(sys.argv[2].replace("\r","").replace("\n",""))
102+
103+ if sys.argv[1] == "get_field":
104+ field = sys.argv[3]
105+ if field in data:
106+ print(data[field])
107+ sys.exit(0)
108+ sys.exit(1)
109+
110+ if sys.argv[1] == "count_branches":
111+ matches = [x for x in data if x.get("name") == sys.argv[3]]
112+ print(len(matches))
113+ sys.exit(0)
114+
115+ if sys.argv[1] == "print_branches":
116+ names = [x.get("name") for x in data]
117+ print(", ".join(names))
118+ sys.exit(0)
119+
120+ if sys.argv[1] == "check_pull_request":
121+ pull_link = data.get("html_url")
122+ if not pull_link:
123+ print("ERROR updating issue to a pull request: " + data.get("message"))
124+ for error in data.get("errors", []):
125+ if error.get("message"):
126+ print(" --> ERROR DETAIL: " + error.get("message"))
127+ else:
128+ print("Pull Request Opened")
129+ print(pull_link)
130+
131+ ' " $@ "
107132}
108133
109134# grab defaults where we can
@@ -116,7 +141,7 @@ GITHUB_PASSWORD=`$GIT_CMD config --get github.password`
116141GITHUB_TOKEN=` $GIT_CMD config --get gitOpenPull.token`
117142GIT_EDITOR=` $GIT_CMD config --get core.editor`
118143if [ -z " $GIT_EDITOR " ]; then
119- GIT_EDITOR=" $ EDITOR"
144+ GIT_EDITOR=${ EDITOR:- vi}
120145fi
121146
122147FEATURE_BRANCH=` $GIT_CMD rev-parse --abbrev-ref HEAD`
@@ -229,10 +254,7 @@ if [ -z "$GITHUB_TOKEN" ]; then
229254 echo " ... getting access token (run with --save to save access token)"
230255 endpoint=" https://api.github.com/authorizations"
231256 OAUTH_JSON=` curl --silent -H " $otp_header " -u " $GITHUB_USER :$GITHUB_PASSWORD " -d " {\" scopes\" :[\" repo\" ],\" note\" :\" git-open-pull $BASE_ACCOUNT /$BASE_REPO \" }" $endpoint `
232- GITHUB_TOKEN=$( echo $OAUTH_JSON | python_data_script '
233- if "token" in data:
234- print(data["token"])
235- ' )
257+ GITHUB_TOKEN=$( python_helper get_field " $OAUTH_JSON " token)
236258
237259 if [ -z " $GITHUB_TOKEN " ]; then
238260 echo $OAUTH_JSON
@@ -260,7 +282,6 @@ if [ -z "$ISSUE_NUMBER" ]; then
260282 read -p " enter issue number (or 'c' to create): " ISSUE_NUMBER
261283 if [ " $ISSUE_NUMBER " == " c" ]; then
262284
263- EDITOR=${EDITOR:- vi}
264285 TMPFILE=$( make_tmp_file)
265286
266287 echo "
@@ -282,21 +303,16 @@ if [ -z "$ISSUE_NUMBER" ]; then
282303 ISSUE_TITLE=$( echo " $RAW_ISSUE " | head -n 1)
283304 ISSUE_DESCRIPTION=$( echo " $RAW_ISSUE " | tail -n +2)
284305
285- ISSUE_TITLE=$( echo " $ISSUE_TITLE " | json_escape )
286- ISSUE_DESCRIPTION=$( echo " $ISSUE_DESCRIPTION " | json_escape )
306+ ISSUE_TITLE=$( python_helper json_escape " $ISSUE_TITLE " )
307+ ISSUE_DESCRIPTION=$( python_helper json_escape " $ISSUE_DESCRIPTION " )
287308
288309 endpoint=" https://api.github.com/repos/$BASE_ACCOUNT /$BASE_REPO /issues"
289310 json=" {\" title\" : $ISSUE_TITLE , \" body\" : $ISSUE_DESCRIPTION }"
290311 ISSUE_JSON=` curl --silent -H " Accept: application/vnd.github-issue.text+json,application/json" --data-binary " $json " " $endpoint ?access_token=$GITHUB_TOKEN " `
291- ISSUE_NUMBER=$( echo $ISSUE_JSON | python_data_script '
292- if "number" not in data:
293- print("ERROR verifying issue number: %r" % data)
294- else:
295- print(data.get("number"))
296- ' )
297- if ! echo $ISSUE_NUMBER | egrep -q ' ^[0-9]+$' ; then
298- echo " Error creating issue $ISSUE_NUMBER "
299- exit 1;
312+ ISSUE_NUMBER=$( python_helper get_field " $ISSUE_JSON " number)
313+ if [ $? != 0 ] || [ -z " ISSUE_NUMBER" ] ; then
314+ echo " Error creating issue: $ISSUE_JSON "
315+ exit 1
300316 fi
301317
302318 echo " created issue $ISSUE_NUMBER "
344360# endpoint => /repos/:user/:repo/issues/:id
345361endpoint=" https://api.github.com/repos/$BASE_ACCOUNT /$BASE_REPO /issues/$ISSUE_NUMBER "
346362ISSUE_JSON=` curl --silent -H " Accept: application/vnd.github-issue.text+json,application/json" " $endpoint ?access_token=$GITHUB_TOKEN " `
347- ISSUE_STATE=$( echo $ISSUE_JSON | python_data_script '
348- if "message" in data:
349- print("ERROR verifying issue number: %r" % data["message"])
350- else:
351- print(data.get("state", "unknown-issue"))
352- ' )
353- [[ $? != 0 ]] && echo " unknown error $ISSUE_JSON " && exit 1
354- [[ " $ISSUE_STATE " =~ " ERROR" ]] && echo $ISSUE_STATE && exit 1
363+ ISSUE_STATE=$( python_helper get_field " $ISSUE_JSON " state)
364+ if [ $? != 0 ] || [ -z " $ISSUE_STATE " ]; then
365+ echo " Error verifying issue number: $ISSUE_JSON "
366+ exit 1
367+ fi
355368
356369if [ " $ISSUE_STATE " != " open" ]; then
357370 echo " "
358371 echo " Error: $BASE_ACCOUNT /$BASE_REPO issue $ISSUE_NUMBER is $ISSUE_STATE "
359372 exit 1;
360373fi
361- ISSUE_TITLE=$( echo $ISSUE_JSON | python_data_script ' print(data["title"])' )
362- [[ $? != 0 ]] && echo " unknown error extracting issue title $ISSUE_JSON " && exit 1
363-
374+ ISSUE_TITLE=$( python_helper get_field " $ISSUE_JSON " title)
375+ if [ $? != 0 ] || [ -z " $ISSUE_TITLE " ]; then
376+ echo " Error extracting issue title from $ISSUE_JSON "
377+ exit 1
378+ fi
364379
365380# check for source branch
366381# endpoint -> /repos/:user/:repo/branches
367382source_user=$( echo " $FEATURE_BRANCH " | awk -F ' :' ' {print $1}' )
368383endpoint=" https://api.github.com/repos/$source_user /$BASE_REPO /branches"
369384branch_name=$( echo " $FEATURE_BRANCH " | awk -F ' :' ' {print $NF}' )
370385BRANCHES_JSON=` curl --silent -H " Accept: application/vnd.github-branches.text+json,application/json" " $endpoint ?access_token=$GITHUB_TOKEN " `
371- BRANCH_EXISTS=$( echo $BRANCHES_JSON | python_data_script '
372- data = [x for x in data if x.get("name") == sys.argv[-1]]
373- print(len(data))
374- ' " $branch_name " )
386+ BRANCH_EXISTS=$( python_helper count_branches " $BRANCHES_JSON " " $branch_name " )
375387if [ " $BRANCH_EXISTS " != " 1" ]; then
376388 echo " "
377389 echo " Error: branch ($branch_name ) does not exist in $source_user /$BASE_REPO "
378390 echo -n -e " \tvalid branches are: "
379- echo $BRANCHES_JSON | python_data_script '
380- data = [x.get("name") for x in data]
381- print(", ".join(data))
382- '
383- exit 1;
391+ python_helper print_branches " $BRANCHES_JSON "
392+ exit 1
384393fi
385394
386395echo " Opening pull request on $BASE_ACCOUNT /$BASE_REPO "
395404json=" {\" issue\" :$ISSUE_NUMBER , \" base\" :\" $BASE_BRANCH \" , \" head\" :\" $FEATURE_BRANCH \" }"
396405endpoint=" https://api.github.com/repos/$BASE_ACCOUNT /$BASE_REPO /pulls"
397406PULL_JSON=` curl --silent -H " Accept: application/vnd.github-pull.text+json,application/json" --data-binary " $json " " $endpoint ?access_token=$GITHUB_TOKEN " `
398- echo $PULL_JSON | python_data_script '
399- pull_link = data.get("html_url")
400- if not pull_link:
401- print("ERROR updating issue to a pull request: " + data.get("message"))
402- for error in data.get("errors", []):
403- if error.get("message"):
404- print(" --> ERROR DETAIL: " + error.get("message"))
405- else:
406- print("Pull Request Opened")
407- print(pull_link)
408- '
407+ python_helper check_pull_request " $PULL_JSON "
0 commit comments