-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergepullrequest
More file actions
207 lines (169 loc) · 7.58 KB
/
mergepullrequest
File metadata and controls
207 lines (169 loc) · 7.58 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
HERE=$(dirname ${BASH_SOURCE[0]})
. ${HERE}/fixpath
if ! checkDANKEY ; then
exit 1
fi
if ! checkJiraCredentials ; then
exit 1
fi
BITBUCKETCRED=$(jirac)
branches=$( arg operands $@ )
if [ "$#" -lt 1 ] ; then
>&2 echo Usage: $0 targetBranch '[--strategyId=no-ff] [--strategyId=ff]'
>&2 echo Usage: $0 targetBranch sourceBranch
exit 1
fi
branches=($( arg operands $@ ))
if [ "${#branches[@]}" -lt 1 ] ; then
>&2 echo Usage: $0 targetBranch '[--strategyId=no-ff] [--strategyId=ff]'
>&2 echo Usage: $0 targetBranch sourceBranch
exit 1
fi
strategyId='no-ff'
schemarelargvalue=$( arg value --strategyId $@ )
if [ -n "$schemarelargvalue" ] ; then
strategyId=$schemarelargvalue
fi
mergePost='{"message":"","strategyId":"'${strategyId}'"}'
currentBranch=$(git status | head -1 | sed -e 's/^On branch //')
targetBranch=${branches[0]}
sourceBranch=$currentBranch
if [ "${#branches[@]}" -gt "1" ] ; then
sourceBranch=${branches[1]}
fi
echo Merging pull request from $sourceBranch into $targetBranch using mergePost=$mergePost
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
>&2 echo "No remote tracking branch found for branch $targetBranch"
fi
if [ -z "$remoteSourceBranch" ] ; then
>&2 echo "No remote tracking branch found for branch $sourceBranch"
fi
if [ -z "$remoteTargetBranch" -o -z "$remoteSourceBranch" ] ; then
>&2 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
bitbucketurl=https://git.code.one/rest/api/1.0/projects/${targetProjectKey}/repos/${targetRepoSlug}/pull-requests
echo Pull requests bitbucketurl=$bitbucketurl
pullrequestsfile=/tmp/pullrequests$$
echo pullrequestsfile=$pullrequestsfile
curl -sS -k -u "$BITBUCKETCRED" -H "Content-Type: application/json" -X GET "$bitbucketurl" > $pullrequestsfile
if badcredentials $pullrequestsfile ; then
>&2 echo Unauthorized.
>&2 echo You should rerun ${HERE}/refreshsavedjiracredentials
exit 1
fi
pullrequestidextractTemplate="
s=process.openStdin();\
d=[];\
s.on('data',function(c) {\
d.push(c);\
});\
s.on('end',function() {\
var pulrequests=JSON.parse(d.join('')).values;\
//console.log(JSON.stringify(pulrequests, null, 2));
//var =pulrequests[\"Items\"][0][\"ConfigSettingsList\"][\"${suite}\"];
var id=undefined;
pulrequests.forEach (function (pullrequest) {\
if (pullrequest.fromRef.id == \"SOURCEREF\"\
&& pullrequest.fromRef.repository.slug.toUpperCase() == \"SOURCEREPOSLUG\".toUpperCase()\
&& pullrequest.fromRef.repository.project.key.toUpperCase() == \"SOURCEPROJECTKEY\".toUpperCase()\
&& pullrequest.toRef.id == \"TARGETREF\"\
&& pullrequest.toRef.repository.slug.toUpperCase() == \"TARGETREPOSLUG\".toUpperCase()\
&& pullrequest.toRef.repository.project.key.toUpperCase() == \"TARGETPROJECTKEY\".toUpperCase()\
&& pullrequest.title == \"Merging from SOURCEBRANCH into TARGETBRANCH\"\
) {\
process.stdout.write(pullrequest.id.toString() + \",\" + pullrequest.version.toString());\
return;\
}\
});\
});"
pullrequestidextract=${pullrequestidextractTemplate/SOURCEBRANCH/$sourceBranch}
pullrequestidextract=${pullrequestidextract/TARGETBRANCH/$targetBranch}
pullrequestidextract=${pullrequestidextract/SOURCEREF/$sourceRefId}
pullrequestidextract=${pullrequestidextract/TARGETREF/$targetRefId}
pullrequestidextract=${pullrequestidextract/SOURCEPROJECTKEY/$sourceProjectKey}
pullrequestidextract=${pullrequestidextract/TARGETPROJECTKEY/$targetProjectKey}
pullrequestidextract=${pullrequestidextract/SOURCEREPOSLUG/$sourceRepoSlug}
pullrequestidextract=${pullrequestidextract/TARGETREPOSLUG/$targetRepoSlug}
#echo pullrequestidextract=$pullrequestidextract
pullRequestIdAndVersion=$(cat $pullrequestsfile | node -e "$pullrequestidextract")
# echo pullRequestIdAndVersion=$pullRequestIdAndVersion
pullRequestId=$(expr "$pullRequestIdAndVersion" : '\([^,]*\),.*')
pullRequestVersion=$(expr "$pullRequestIdAndVersion" : '[^,]*,\(.*\)')
echo pullRequestId=$pullRequestId
echo pullRequestVersion=$pullRequestVersion
if [ -z "$pullRequestId" -o -z "$pullRequestVersion" ] ; then
echo "Aborting."
exit 1
fi
bitbucketpullrequesturl=https://git.code.one/rest/api/1.0/projects/${targetProjectKey}/repos/${targetRepoSlug}/pull-requests/$pullRequestId
echo Found Pull Request $pullRequestId
curl -sS -k -u "$BITBUCKETCRED" -H "Content-Type: application/json" -X GET "$bitbucketpullrequesturl" \
| formatJson
echo
echo "About to merge pull request $pullRequestId version ${pullRequestVersion}. Okay? "
echo -n "Okay to continue? [y/N] "
read okay
if [ "$okay" != "y" -a "$okay" != "Y" ] ; then
echo "Aborting."
exit 1
fi
bitbucketpullrequestmergeurl=https://git.code.one/rest/api/1.0/projects/${targetProjectKey}/repos/${targetRepoSlug}/pull-requests/${pullRequestId}/merge?version=${pullRequestVersion}
echo bitbucketpullrequestmergeurl=$bitbucketpullrequestmergeurl
curl -sS -k -u "$BITBUCKETCRED" -H "Content-Type: application/json" -X POST -d "$mergePost" "$bitbucketpullrequestmergeurl" \
| formatJson