-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathCreatePr.cmd
296 lines (247 loc) · 7.44 KB
/
CreatePr.cmd
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
@if not defined _ECHO @echo off
setlocal enabledelayedexpansion
set ENLISTMENTROOT=%~dp0
set SCRIPTROOT=%~dp0Shared\Scripts\
set PRBRANCHPREFIX=dev/%USERNAME%/pr
set CREATEPULLREQUESTURL=https://dev.azure.com/mseng/Domino/_git/BuildXL.Internal
if /I "%1" == "-help" (
goto :Usage
)
if /I "%1" == "/help" (
goto :Usage
)
if /I "%1" == "/?" (
goto :Usage
)
if /I "%1" == "-list" (
goto :ListPrs
)
if /I "%1" == "/list" (
goto :ListPrs
)
if /I "%1" == "-remove" (
goto :RemovePr
)
if /I "%1" == "/remove" (
goto :RemovePr
)
if /I "%1" == "-removeall" (
goto :RemoveAll
)
if /I "%1" == "/removeall" (
goto :RemoveAll
)
goto :CreatePr
REM ************************************************************
REM
REM CreatePr
REM
REM ************************************************************
:CreatePr
set PrName=%1
:CheckArguments
if "%PrName%"=="" (
goto :Usage
)
:EnsureWorktreeClean
for /f %%F in ('call git status --porcelain') do (
echo.
echo Your working directory does not seem to be clean:
call git status
echo.
echo Commit or revert changes and try again.
goto :Error
)
:CheckForNoChanges
call git diff --no-ext-diff --quiet
if %ERRORLEVEL% NEQ 0 (
echo >&2 You have uncommited and unstaged changes. Refusing to push for gated checkin.
goto :Error
)
call git diff --no-ext-diff --cached --quiet
if %ERRORLEVEL% NEQ 0 (
echo >&2 You have uncommited staged changes. Refusing to push for gated checkin.
goto :Error
)
:PinCurrentCommitWithABranch
call git branch --force RebaseAndPushWorkflowBranch
if %ERRORLEVEL% NEQ 0 (
echo.
echo Could not checkpoint current work.
goto :Error
)
:CreateBranchName
set PR_BRANCH_NAME=%PRBRANCHPREFIX%/%PrName%
:CheckIfExists
for /f %%R in ('git branch -r --list */%PR_BRANCH_NAME%') do (
set PR_BRANCH_EXISTS=1
)
:AllowUserToBailOnUpdate
if "%PR_BRANCH_EXISTS%"=="1" (
echo.
choice /c YN /M "Branch already exists, do you want to update the pull request?"
set ANSWER=!ERRORLEVEL!
)
if "%ANSWER%" == "1" (
echo.
echo Updating the existing pull request with new bits
)
if "%ANSWER%" == "2" (
echo.
echo Cancelling updating the pull request. If you intended to create a new pull request, please call this script with a new name: %~nx0 ^<NewName^>
echo.
echo ---------------------------------------------------------------
echo - Cancelled
echo ---------------------------------------------------------------
exit /b 1
goto :Error
)
:ForcePushToServer
call git push --force origin HEAD:%PR_BRANCH_NAME%
if %ERRORLEVEL% NEQ 0 (
echo >&2 Failed to force-push to branch %PR_BRANCH_NAME%
goto :Error
)
start %CREATEPULLREQUESTURL%/pullrequestcreate?targetRef=main^&sourceRef=%PR_BRANCH_NAME%
goto :Success
REM ************************************************************
REM
REM ListPrs
REM
REM ************************************************************
:ListPrs
echo ---------------------------------------------------------------
echo Listing all existing PR's
echo ---------------------------------------------------------------
REM Remove all local branches that have been removed on the server
git remote prune origin > NUL
for /f "tokens=1-6 delims=/" %%a in ('git branch --all ^| findstr /ic:"%username%/pr/"') do (
if "%%f" NEQ "" (
echo %%f
)
)
exit /b 0
goto :Done
REM ************************************************************
REM
REM RemovePr
REM
REM ************************************************************
:RemovePr
set PrName=%2
:CheckRemovePrArguments
if "%PrName%"=="" (
echo ERROR: Missing ^<PrName^> argument
goto :Usage
)
set PR_BRANCH_NAME=%PRBRANCHPREFIX%/%PrName%
:CheckIfExists
for /f %%R in ('git branch -r --list */%PR_BRANCH_NAME%') do (
set PR_BRANCH_EXISTS=1
)
if "%PR_BRANCH_EXISTS%" NEQ "1" (
echo ERROR: Branch %PR_BRANCH_NAME% does not exist on server
exit /b 1
)
:AllowUserToCancel
echo ---------------------------------------------------------------
echo Removing PR branch: %PR_BRANCH_NAME%
echo ---------------------------------------------------------------
choice /c YN /M "Are you sure you want to remove '%PR_BRANCH_NAME%'?"
set ANSWER=!ERRORLEVEL!
if "%ANSWER%" == "1" (
echo.
echo Removing...
)
if "%ANSWER%" == "2" (
echo.
echo ---------------------------------------------------------------
echo - Cancelled
echo ---------------------------------------------------------------
exit /b 1
goto :Error
)
:ForceRemoveOnServer
call git push --force origin :%PR_BRANCH_NAME%
if %ERRORLEVEL% NEQ 0 (
echo >&2 Failed to remove branch %PR_BRANCH_NAME%
goto :Error
)
goto :Success
REM ************************************************************
REM
REM RemovePr
REM
REM ************************************************************
:RemoveAll
echo ---------------------------------------------------------------
echo Are you sure you want to remove the following PR branches?
echo ---------------------------------------------------------------
REM Remove all local branches that have been removed on the server
git remote prune origin > NUL
for /f "tokens=1-6 delims=/" %%a in ('git branch --all ^| findstr /ic:"%username%/pr/"') do (
if "%%f" NEQ "" (
echo - %%f
)
)
choice /c YN /M "Are you sure?"
set ANSWER=!ERRORLEVEL!
if "%ANSWER%" == "1" (
echo.
echo Removing all ...
)
if "%ANSWER%" == "2" (
echo.
echo ---------------------------------------------------------------
echo - Cancelled
echo ---------------------------------------------------------------
exit /b 1
goto :Error
)
for /f "tokens=1-6 delims=/" %%a in ('git branch --all ^| findstr /ic:"%username%/pr/"') do (
if "%%f" NEQ "" (
call git push --force origin :%PRBRANCHPREFIX%/%%f
if %ERRORLEVEL% NEQ 0 (
echo >&2 Failed to remove branch '%PRBRANCHPREFIX%/%%f'
set SomeError=1
)
)
)
if "%SomeError%" == "1" (
goto :Error
exit /b 1
)
goto :Success
exit /b 0
:Usage
echo.
echo ---------------------------------------------------------------
echo - Usage: %~nx0 ^<PrName^>
echo - This will create serverside branch with name '%PRBRANCHPREFIX%/^<PrName^>'
echo - and open a browser to create a pull request for that branch
echo - If you already have a pull request open with that name it will update it.
echo -
echo - Usage: %~nx0 /list
echo - Lists all existing pr branches you have open.
echo -
echo - Usage: %~nx0 /remove ^<PrName^>
echo - Removes the serverside branch with name '%PRBRANCHPREFIX%/^<PrName^>'
echo -
echo - Usage: %~nx0 /removeAll
echo - Removes all of the the serverside branches under '%PRBRANCHPREFIX%'
echo ---------------------------------------------------------------
exit /b 1
:Success
echo.
echo ++++++++++++++++++++++++++++++++++++++++++++++++
echo + SUCCESS :-) Pull request created, please publish on the web UI. +
echo ++++++++++++++++++++++++++++++++++++++++++++++++
echo.
exit /b 0
:Error
echo.
echo ---------------------------------------------------------------
echo - FAILURE :-( Fix the issues and "%~nx0 %PrName%" again. -
echo ---------------------------------------------------------------
exit /b 1
:End