-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatepullrequest
194 lines (165 loc) · 5.72 KB
/
createpullrequest
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
HERE=$(dirname ${BASH_SOURCE[0]})
. ${HERE}/fixpath
if ! checkDANKEY ; then
exit 1
fi
if ! checkJiraCredentials ; then
exit 1
fi
BITBUCKETCRED=$(jirac)
bitbucketUser=
if [ -z "$BITBUCKETCRED" ] ; then
>&2 echo No encrypted jira credentials found.
>&2 echo Aborting.
exit 1
else
bitbucketUser=$(expr "$BITBUCKETCRED" : '\([^:]*\):.*')
fi
if [ "$#" -lt 1 ] ; then
>&2 echo Usage: $0 targetBranch
>&2 echo Usage: $0 targetBranch sourceBranch
exit 1
fi
currentBranch=$(git status | head -1 | sed -e 's/^On branch //')
targetBranch=$1
sourceBranch=$currentBranch
if [ "$#" -gt "1" ] ; then
sourceBranch=$2
fi
echo Creating pull request from $sourceBranch into $targetBranch
echo -n "Okay to continue? [y/N] "
read okay
if [ "$okay" != "y" -a "$okay" != "Y" ] ; then
echo "Aborting."
exit 1
fi
# echo Abort if not remotely tracking one or the other
sourceBranchInfo=$(git branch -vv | sed -e 's/^..//g' | grep '^'${sourceBranch})
targetBranchInfo=$(git branch -vv | sed -e 's/^..//g' | grep '^'${targetBranch})
# echo $sourceBranchInfo
# echo $targetBranchInfo
sourceBranchInfoPatchBrackets=$(echo $sourceBranchInfo | sed -e 's/\[/___LEFTSQUARE___/' -e 's/\]/___RIGHTSQUARE___/')
targetBranchInfoPatchBrackets=$(echo $targetBranchInfo | sed -e 's/\[/___LEFTSQUARE___/' -e 's/\]/___RIGHTSQUARE___/')
remoteSourceBranch=$(expr "$sourceBranchInfoPatchBrackets" : '.*___LEFTSQUARE___\(.*\)___RIGHTSQUARE___.*')
remoteTargetBranch=$(expr "$targetBranchInfoPatchBrackets" : '.*___LEFTSQUARE___\(.*\)___RIGHTSQUARE___.*')
# echo remoteSourceBranch=$remoteSourceBranch
# echo remoteTargetBranch=$remoteTargetBranch
if [ -z "$remoteTargetBranch" ] ; then
echo "No remote tracking branch found for branch $targetBranch"
fi
if [ -z "$remoteSourceBranch" ] ; then
echo "No remote tracking branch found for branch $sourceBranch"
fi
if [ -z "$remoteTargetBranch" -o -z "$remoteSourceBranch" ] ; then
echo "Aborting."
exit 1
fi
fixthese=()
if [[ $remoteSourceBranch =~ :\ ahead ]] ; then
fixthese+=("Your local source branch $sourceBranch is ahead. Please fix this first.")
fi
if [[ $remoteSourceBranch =~ :\ behind ]] ; then
fixthese+=("Your local source branch $sourceBranch is behind. Please fix this first: e.g. pull")
fi
if [[ $remoteTargetBranch =~ :\ ahead ]] ; then
fixthese+=("Your local target branch $targetBranch is ahead. Please fix this first.")
fi
if [[ $remoteTargetBranch =~ :\ behind ]] ; then
fixthese+=("Your local target branch $targetBranch is behind. Please fix this first: e.g. pull $targetBranch")
fi
if [ "${#fixthese[@]}" -gt 0 ] ; then
>&2 printf '%s\n' "${fixthese[@]}"
exit 1
fi
sourceRepoId=$(expr "$remoteSourceBranch" : '^\([^/]*\)/.*')
# echo sourceRepoId=$sourceRepoId
targetRepoId=$(expr "$remoteTargetBranch" : '^\([^/]*\)/.*')
# echo targetRepoId=$targetRepoId
sourceRepoUrl=$(git config remote.${sourceRepoId}.url)
targetRepoUrl=$(git config remote.${targetRepoId}.url)
# echo sourceRepoUrl=$sourceRepoUrl
# echo targetRepoUrl=$targetRepoUrl
sourceRepoSlug=$(expr "$sourceRepoUrl" : '.*/[^/]*/\([^/]*\).git$')
sourceProjectKey=$(expr "$sourceRepoUrl" : '.*/\([^/]*\)/[^/]*.git$')
targetRepoSlug=$(expr "$targetRepoUrl" : '.*/[^/]*/\([^/]*\).git$')
targetProjectKey=$(expr "$targetRepoUrl" : '.*/\([^/]*\)/[^/]*.git$')
# echo sourceRepoSlug=$sourceRepoSlug
# echo sourceProjectKey=$sourceProjectKey
# echo targetRepoSlug=$targetRepoSlug
# echo targetProjectKey=$targetProjectKey
sourceRefId=${remoteSourceBranch/${sourceRepoId}/refs\/heads}
targetRefId=${remoteTargetBranch/${targetRepoId}/refs\/heads}
# echo sourceRefId=$sourceRefId
# echo targetRefId=$targetRefId
createDataTemplate='{
"title": "Merging from SOURCEBRANCH into TARGETBRANCH",
"description": "",
"state": "OPEN",
"open": true,
"closed": false,
"fromRef": {
"id": "SOURCEREF",
"repository": {
"slug": "SOURCEREPOSLUG",
"name": null,
"project": {
"key": "SOURCEPROJECTKEY"
}
}
},
"toRef": {
"id": "TARGETREF",
"repository": {
"slug": "TARGETREPOSLUG",
"name": null,
"project": {
"key": "TARGETPROJECTKEY"
}
}
},
"locked": false,
"reviewers": [
{
"user": {
"name": "BITBUCKETUSER"
}
}
],
"links": {
"self": [
null
]
}
}
'
createData=${createDataTemplate/SOURCEBRANCH/$sourceBranch}
createData=${createData/TARGETBRANCH/$targetBranch}
createData=${createData/SOURCEREF/$sourceRefId}
createData=${createData/TARGETREF/$targetRefId}
createData=${createData/SOURCEPROJECTKEY/$sourceProjectKey}
createData=${createData/TARGETPROJECTKEY/$targetProjectKey}
createData=${createData/SOURCEREPOSLUG/$sourceRepoSlug}
createData=${createData/TARGETREPOSLUG/$targetRepoSlug}
createData=${createData/BITBUCKETUSER/$bitbucketUser}
echo
echo We will be posting:
echo " " "$(echo $createData | formatJson)"
bitbucketurl=https://git.code.one/rest/api/1.0/projects/${targetProjectKey}/repos/${targetRepoSlug}/pull-requests
echo To: bitbucketurl=$bitbucketurl
echo
echo "About to really create pull request this time. Please check that nothing seems alarming to you above."
echo -n "Okay to continue? [y/N] "
read okay
if [ "$okay" != "y" -a "$okay" != "Y" ] ; then
echo "Aborting."
exit 1
fi
curl -sS -k -u "$BITBUCKETCRED" -H "Content-Type: application/json" -X POST -d "$createData" "$bitbucketurl" \
> /tmp/bitbucketresponse$$
if badcredentials /tmp/bitbucketresponse$$ ; then
>&2 echo Unauthorized.
>&2 echo You should rerun ${HERE}/refreshsavedjiracredentials
exit 1
fi
cat /tmp/bitbucketresponse$$ | formatJson