Skip to content

Commit d73f7c1

Browse files
committed
Use the github api to check for the presence of repos
1 parent 1c25eaa commit d73f7c1

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

management_instance/runbooks/shared_scripts/preview_merge_repo.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,23 @@ def generate_github_token(github_app_id, github_app_private_key, github_app_inst
117117
return response_json['token']
118118

119119

120-
def check_repo_exists(url, username, password):
120+
def check_repo_exists(git_protocol, git_host, git_organization, new_repo, username, password):
121+
url = git_protocol + '://' + git_host + '/' + git_organization + '/' + new_repo + '.git'
122+
try:
123+
auth = base64.b64encode((username + ':' + password).encode('ascii'))
124+
auth_header = "Basic " + auth.decode('ascii')
125+
headers = {
126+
"Authorization": auth_header
127+
}
128+
request = urllib.request.Request(url, headers=headers)
129+
urllib.request.urlopen(request)
130+
return True
131+
except:
132+
return False
133+
134+
135+
def check_github_repo_exists(git_organization, new_repo, username, password):
136+
url = 'https://api.github.com/repos/' + git_organization + '/' + new_repo
121137
try:
122138
auth = base64.b64encode((username + ':' + password).encode('ascii'))
123139
auth_header = "Basic " + auth.decode('ascii')
@@ -218,13 +234,22 @@ def init_argparse():
218234
token + '@' + parser.git_host + '/' + \
219235
parser.git_organization + '/' + parser.template_repo_name + '.git'
220236

221-
if not check_repo_exists(new_repo_url, username, token):
222-
print('Downstream repo ' + new_repo_url + ' is not available')
223-
sys.exit(exit_code)
237+
if parser.git_host == 'github.com':
238+
if not check_github_repo_exists(parser.git_organization, new_repo, username, token):
239+
print('Downstream repo ' + new_repo_url + ' is not available')
240+
sys.exit(exit_code)
241+
242+
if not check_github_repo_exists(parser.git_organization, parser.template_repo_name, username, token):
243+
print('Upstream repo ' + template_repo_name_url + ' is not available')
244+
sys.exit(exit_code)
245+
else:
246+
if not check_repo_exists(parser.git_protocol, parser.git_host, parser.git_organization, new_repo, username, token):
247+
print('Downstream repo ' + new_repo_url + ' is not available')
248+
sys.exit(exit_code)
224249

225-
if not check_repo_exists(template_repo_name_url, username, token):
226-
print('Upstream repo ' + template_repo_name_url + ' is not available')
227-
sys.exit(exit_code)
250+
if not check_repo_exists(parser.git_protocol, parser.git_host, parser.git_organization, parser.template_repo_name, username, token):
251+
print('Upstream repo ' + template_repo_name_url + ' is not available')
252+
sys.exit(exit_code)
228253

229254
# Set some default user details
230255
execute(['git', 'config', '--global', 'user.email', '[email protected]'])

0 commit comments

Comments
 (0)