Skip to content

Commit 71f79f6

Browse files
committed
Fixed up the ability to fork personal repos
1 parent 5b6507b commit 71f79f6

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

management_instance/runbooks/shared_scripts/fork_repo_github.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,10 @@ def init_argparse():
8686
usage='%(prog)s [OPTION] [FILE]...',
8787
description='Fork a GitHub repo'
8888
)
89-
parser.add_argument('--original-project-name', action='store',
90-
default=get_octopusvariable_quiet(
91-
'ForkGithubRepo.Octopus.Project.Name') or get_octopusvariable_quiet('Octopus.Project.Name'))
9289
parser.add_argument('--new-project-name', action='store',
9390
default=get_octopusvariable_quiet(
9491
'ForkGithubRepo.Exported.Project.Name') or get_octopusvariable_quiet(
95-
'Exported.Project.Name'))
92+
'Exported.Project.Name') or get_octopusvariable_quiet('Octopus.Project.Name'))
9693
parser.add_argument('--github-app-id', action='store',
9794
default=get_octopusvariable_quiet(
9895
'ForkGithubRepo.GitHub.App.Id') or get_octopusvariable_quiet('GitHub.App.Id'))
@@ -119,11 +116,13 @@ def init_argparse():
119116
'ForkGithubRepo.Git.Url.NewRepoNamePrefix') or get_octopusvariable_quiet(
120117
'Git.Url.NewRepoNamePrefix'))
121118
parser.add_argument('--template-repo-name', action='store',
122-
default=re.sub('[^a-zA-Z0-9]', '_', get_octopusvariable_quiet('Octopus.Project.Name').lower()))
119+
default=re.sub('[^a-zA-Z0-9]', '_', get_octopusvariable_quiet(
120+
'ForkGithubRepo.Original.Project.Name') or get_octopusvariable_quiet(
121+
'Octopus.Project.Name').lower()))
123122
parser.add_argument('--mainline-branch',
124123
action='store',
125-
default=get_octopusvariable_quiet('Git.Branch.MainLine') or get_octopusvariable_quiet(
126-
'ForkGiteaRepo.Git.Branch.MainLine'),
124+
default=get_octopusvariable_quiet(
125+
'ForkGiteaRepo.Git.Branch.MainLine') or get_octopusvariable_quiet('Git.Branch.MainLine'),
127126
help='The branch name to use for the fork. Defaults to "main".')
128127
return parser.parse_known_args()
129128

@@ -201,16 +200,31 @@ def create_new_repo(token, cac_org, new_repo):
201200
# https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#create-an-organization-repository
202201
# Note you have to use the token rather than the JWT:
203202
# https://stackoverflow.com/questions/39600396/bad-credentails-for-jwt-for-github-integrations-api
204-
url = 'https://api.github.com/orgs/' + cac_org + '/repos'
203+
205204
headers = {
206205
'Authorization': 'token ' + token,
207206
'Content-Type': 'application/json',
208207
'Accept': 'application/vnd.github+json',
209208
'X-GitHub-Api-Version': '2022-11-28',
210209
}
211-
body = {'name': new_repo}
212-
request = urllib.request.Request(url, headers=headers, data=json.dumps(body).encode('utf-8'))
213-
urllib.request.urlopen(request)
210+
211+
try:
212+
# First try to create an organization repo:
213+
# https://docs.github.com/en/free-pro-team@latest/rest/repos/repos#create-an-organization-repository
214+
url = 'https://api.github.com/orgs/' + cac_org + '/repos'
215+
body = {'name': new_repo}
216+
request = urllib.request.Request(url, headers=headers, data=json.dumps(body).encode('utf-8'))
217+
urllib.request.urlopen(request)
218+
except urllib.error.URLError as ex:
219+
# Then fall back to creating a repo for the user:
220+
# https://docs.github.com/en/free-pro-team@latest/rest/repos/repos?apiVersion=2022-11-28#create-a-repository-for-the-authenticated-user
221+
if ex.code == 404:
222+
url = 'https://api.github.com/user/repos'
223+
body = {'name': new_repo}
224+
request = urllib.request.Request(url, headers=headers, data=json.dumps(body).encode('utf-8'))
225+
urllib.request.urlopen(request)
226+
else:
227+
raise ex
214228

215229

216230
def fork_repo(git_executable, token, cac_org, new_repo, template_repo):
@@ -264,6 +278,7 @@ def ensure_git_exists():
264278
'https://github.com/git-for-windows/git/releases/download/v2.42.0.windows.2/PortableGit-2.42.0.2-64-bit.7z.exe',
265279
'PortableGit.7z.exe')
266280
print("Installing git")
281+
print("Consider installing git on the worker or using a standard worker-tools image")
267282
execute(['7zr.exe', 'x', 'PortableGit.7z.exe', '-o' + os.getcwd() + '\\git', '-y'])
268283
return os.getcwd() + '\\git\\bin\\git'
269284

@@ -273,7 +288,8 @@ def ensure_git_exists():
273288
git_executable = ensure_git_exists()
274289
parser, _ = init_argparse()
275290

276-
if not parser.github_access_token.strip() and not (parser.github_app_id.strip() and parser.github_app_private_key.strip() and parser.github_app_installation_id.strip()):
291+
if not parser.github_access_token.strip() and not (
292+
parser.github_app_id.strip() and parser.github_app_private_key.strip() and parser.github_app_installation_id.strip()):
277293
print("You must supply the GitHub token, or the GitHub App ID and private key and installation ID")
278294
sys.exit(1)
279295

@@ -302,18 +318,13 @@ def ensure_git_exists():
302318
template_repo = parser.template_repo_name.strip()
303319
new_repo_custom_prefix = re.sub('[^a-zA-Z0-9]', '_', parser.new_repo_name_prefix.strip())
304320
tenant_name_sanitized = re.sub('[^a-zA-Z0-9]', '_', parser.tenant_name.lower().strip())
305-
new_project_name_sanitized = re.sub('[^a-zA-Z0-9]', '_', parser.new_project_name.lower().strip())
306-
original_project_name_sanitized = re.sub('[^a-zA-Z0-9]', '_', parser.original_project_name.lower().strip())
307-
308-
# The new project name is either the custom name, or the name of the original project
309-
project_name_sanitized = new_project_name_sanitized if len(new_project_name_sanitized) != 0 \
310-
else original_project_name_sanitized
321+
project_name_sanitized = re.sub('[^a-zA-Z0-9]', '_', parser.new_project_name.lower().strip())
311322

312323
# The new repo is prefixed either with the custom prefix or the tenant name if no custom prefix is defined
313324
new_repo_prefix = new_repo_custom_prefix if len(new_repo_custom_prefix) != 0 else tenant_name_sanitized
314325

315326
# The new repo name is the prefix + the name of thew new project
316-
new_repo = new_repo_prefix + '_' + project_name_sanitized
327+
new_repo = new_repo_prefix + '_' + project_name_sanitized if len(new_repo_prefix) != 0 else project_name_sanitized
317328

318329
# Assume the main branch if nothing else was specified
319330
branch = parser.mainline_branch or 'main'

0 commit comments

Comments
 (0)