Skip to content

Commit 4b7a3e8

Browse files
committed
Added a bunch more validation
1 parent cda36e7 commit 4b7a3e8

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

management_instance/runbooks/shared_scripts/fork_repo_github.py

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def get_octopusvariable_quiet(variable):
5757
return ''
5858

5959

60-
def execute(args, cwd=None, env=None, print_args=None, print_output=printverbose_noansi):
60+
def execute(args, cwd=None, env=None, print_args=None, print_output=printverbose_noansi, raise_on_non_zero=False):
6161
"""
6262
The execute method provides the ability to execute external processes while capturing and returning the
6363
output to std err and std out and exit code.
@@ -71,6 +71,9 @@ def execute(args, cwd=None, env=None, print_args=None, print_output=printverbose
7171
stdout, stderr = process.communicate()
7272
retcode = process.returncode
7373

74+
if not retcode == 0 and raise_on_non_zero:
75+
raise Exception('command returned exit code ' + retcode)
76+
7477
if print_args is not None:
7578
print_output(' '.join(args))
7679

@@ -231,29 +234,60 @@ def create_new_repo(token, cac_org, new_repo):
231234

232235
def fork_repo(git_executable, token, cac_org, new_repo, template_repo):
233236
# Clone the repo and add the upstream repo
234-
execute([git_executable, 'clone', 'https://' + 'x-access-token:' + token + '@'
235-
+ 'github.com/' + cac_org + '/' + new_repo + '.git'])
236-
execute(
237+
_, _, retcode = execute([git_executable, 'clone', 'https://' + 'x-access-token:' + token + '@'
238+
+ 'github.com/' + cac_org + '/' + new_repo + '.git'])
239+
240+
if not retcode == 0:
241+
print('Failed to clone repo ' + 'https://github.com/' + cac_org + '/' + new_repo + '.git.' +
242+
' Check the verbose logs for details.')
243+
sys.exit(1)
244+
245+
_, _, retcode = execute(
237246
[git_executable, 'remote', 'add', 'upstream', 'https://' + 'x-access-token:' + token + '@'
238247
+ 'github.com/' + cac_org + '/' + template_repo + '.git'],
239248
cwd=new_repo)
240-
execute([git_executable, 'fetch', '--all'], cwd=new_repo)
241-
execute(['git', 'checkout', '-b', 'upstream-' + branch, 'upstream/' + branch], cwd=new_repo)
249+
250+
if not retcode == 0:
251+
print('Failed to add remote ' + 'https://github.com/' + cac_org + '/' + template_repo + '.git. ' +
252+
'Check the verbose logs for details.')
253+
sys.exit(1)
254+
255+
_, _, retcode = execute([git_executable, 'fetch', '--all'], cwd=new_repo)
256+
257+
if not retcode == 0:
258+
print('Failed to fetch. Check the verbose logs for details.')
259+
sys.exit(1)
260+
261+
_, _, retcode = execute(['git', 'checkout', '-b', 'upstream-' + branch, 'upstream/' + branch], cwd=new_repo)
262+
263+
if not retcode == 0:
264+
print('Failed to checkout branch ' + branch + '. Check the verbose logs for details.')
265+
sys.exit(1)
242266

243267
if branch != 'master' and branch != 'main':
244268
_, _, retcode = execute(['git', 'checkout', '-b', branch, 'origin/' + branch], cwd=new_repo)
245269
else:
246-
_, _, retcode = execute(['git', 'checkout', branch], cwd=new_repo)
270+
_, _, retcode = execute(['git', 'checkout', branch], cwd=new_repo)
247271

248272
if not retcode == 0:
249-
print('Failed to checkout branch ' + branch)
273+
print('Failed to checkout branch ' + branch + '. Check the verbose logs for details.')
250274
sys.exit(1)
251275

252276
# Hard reset it to the template main branch.
253-
execute([git_executable, 'reset', '--hard', 'upstream/' + branch], cwd=new_repo)
277+
_, _, retcode = execute([git_executable, 'reset', '--hard', 'upstream/' + branch], cwd=new_repo)
278+
279+
if not retcode == 0:
280+
print(
281+
'Failed to perform a hard reset against branch upstream/' + branch + '.'
282+
+ ' Check the verbose logs for details.')
283+
sys.exit(1)
254284

255285
# Push the changes.
256-
execute([git_executable, 'push', 'origin', branch], cwd=new_repo)
286+
_, _, retcode = execute([git_executable, 'push', 'origin', branch], cwd=new_repo)
287+
288+
if not retcode == 0:
289+
print('Failed to push changes. Check the verbose logs for details.')
290+
sys.exit(1)
257291

258292

259293
def is_windows():

0 commit comments

Comments
 (0)