@@ -86,13 +86,10 @@ def init_argparse():
86
86
usage = '%(prog)s [OPTION] [FILE]...' ,
87
87
description = 'Fork a GitHub repo'
88
88
)
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' ))
92
89
parser .add_argument ('--new-project-name' , action = 'store' ,
93
90
default = get_octopusvariable_quiet (
94
91
'ForkGithubRepo.Exported.Project.Name' ) or get_octopusvariable_quiet (
95
- 'Exported.Project.Name' ))
92
+ 'Exported.Project.Name' ) or get_octopusvariable_quiet ( 'Octopus.Project.Name' ) )
96
93
parser .add_argument ('--github-app-id' , action = 'store' ,
97
94
default = get_octopusvariable_quiet (
98
95
'ForkGithubRepo.GitHub.App.Id' ) or get_octopusvariable_quiet ('GitHub.App.Id' ))
@@ -119,11 +116,13 @@ def init_argparse():
119
116
'ForkGithubRepo.Git.Url.NewRepoNamePrefix' ) or get_octopusvariable_quiet (
120
117
'Git.Url.NewRepoNamePrefix' ))
121
118
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 ()))
123
122
parser .add_argument ('--mainline-branch' ,
124
123
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' ) ,
127
126
help = 'The branch name to use for the fork. Defaults to "main".' )
128
127
return parser .parse_known_args ()
129
128
@@ -201,16 +200,31 @@ def create_new_repo(token, cac_org, new_repo):
201
200
# https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#create-an-organization-repository
202
201
# Note you have to use the token rather than the JWT:
203
202
# https://stackoverflow.com/questions/39600396/bad-credentails-for-jwt-for-github-integrations-api
204
- url = 'https://api.github.com/orgs/' + cac_org + '/repos'
203
+
205
204
headers = {
206
205
'Authorization' : 'token ' + token ,
207
206
'Content-Type' : 'application/json' ,
208
207
'Accept' : 'application/vnd.github+json' ,
209
208
'X-GitHub-Api-Version' : '2022-11-28' ,
210
209
}
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
214
228
215
229
216
230
def fork_repo (git_executable , token , cac_org , new_repo , template_repo ):
@@ -264,6 +278,7 @@ def ensure_git_exists():
264
278
'https://github.com/git-for-windows/git/releases/download/v2.42.0.windows.2/PortableGit-2.42.0.2-64-bit.7z.exe' ,
265
279
'PortableGit.7z.exe' )
266
280
print ("Installing git" )
281
+ print ("Consider installing git on the worker or using a standard worker-tools image" )
267
282
execute (['7zr.exe' , 'x' , 'PortableGit.7z.exe' , '-o' + os .getcwd () + '\\ git' , '-y' ])
268
283
return os .getcwd () + '\\ git\\ bin\\ git'
269
284
@@ -273,7 +288,8 @@ def ensure_git_exists():
273
288
git_executable = ensure_git_exists ()
274
289
parser , _ = init_argparse ()
275
290
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 ()):
277
293
print ("You must supply the GitHub token, or the GitHub App ID and private key and installation ID" )
278
294
sys .exit (1 )
279
295
@@ -302,18 +318,13 @@ def ensure_git_exists():
302
318
template_repo = parser .template_repo_name .strip ()
303
319
new_repo_custom_prefix = re .sub ('[^a-zA-Z0-9]' , '_' , parser .new_repo_name_prefix .strip ())
304
320
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 ())
311
322
312
323
# The new repo is prefixed either with the custom prefix or the tenant name if no custom prefix is defined
313
324
new_repo_prefix = new_repo_custom_prefix if len (new_repo_custom_prefix ) != 0 else tenant_name_sanitized
314
325
315
326
# 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
317
328
318
329
# Assume the main branch if nothing else was specified
319
330
branch = parser .mainline_branch or 'main'
0 commit comments