diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 8b1378917..88da814d6 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -1 +1 @@ - +- When running migrations with `gh bbs2gh` and migrations from GitHub Enterprise Server with `gh gei`, create the migration source before starting the export diff --git a/src/bbs2gh/Commands/MigrateRepo/MigrateRepoCommandHandler.cs b/src/bbs2gh/Commands/MigrateRepo/MigrateRepoCommandHandler.cs index af6738927..99d8a834f 100644 --- a/src/bbs2gh/Commands/MigrateRepo/MigrateRepoCommandHandler.cs +++ b/src/bbs2gh/Commands/MigrateRepo/MigrateRepoCommandHandler.cs @@ -53,6 +53,7 @@ public async Task Handle(MigrateRepoCommandArgs args) ValidateOptions(args); var exportId = 0L; + var migrationSourceId = ""; if (args.ShouldImportArchive()) { @@ -62,6 +63,8 @@ public async Task Handle(MigrateRepoCommandArgs args) { throw new OctoshiftCliException($"A repository called {args.GithubOrg}/{args.GithubRepo} already exists"); } + + migrationSourceId = await CreateMigrationSource(args); } if (args.ShouldGenerateArchive()) @@ -99,7 +102,7 @@ public async Task Handle(MigrateRepoCommandArgs args) if (args.ShouldImportArchive()) { - await ImportArchive(args, args.ArchiveUrl); + await ImportArchive(args, migrationSourceId, args.ArchiveUrl); } } @@ -190,22 +193,16 @@ private async Task UploadArchiveToAws(string bucketName, string archiveP return archiveBlobUrl; } - private async Task ImportArchive(MigrateRepoCommandArgs args, string archiveUrl = null) + private async Task CreateMigrationSource(MigrateRepoCommandArgs args) { - _log.LogInformation("Importing Archive..."); - - archiveUrl ??= args.ArchiveUrl; - - var bbsRepoUrl = GetBbsRepoUrl(args); + _log.LogInformation("Creating Migration Source..."); args.GithubPat ??= _environmentVariableProvider.TargetGithubPersonalAccessToken(); var githubOrgId = await _githubApi.GetOrganizationId(args.GithubOrg); - string migrationSourceId; - try { - migrationSourceId = await _githubApi.CreateBbsMigrationSource(githubOrgId); + return await _githubApi.CreateBbsMigrationSource(githubOrgId); } catch (OctoshiftCliException ex) when (ex.Message.Contains("not have the correct permissions to execute")) { @@ -213,6 +210,18 @@ private async Task ImportArchive(MigrateRepoCommandArgs args, string archiveUrl var message = $"{ex.Message}{insufficientPermissionsMessage}"; throw new OctoshiftCliException(message, ex); } + } + + private async Task ImportArchive(MigrateRepoCommandArgs args, string migrationSourceId, string archiveUrl = null) + { + _log.LogInformation("Importing Archive..."); + + archiveUrl ??= args.ArchiveUrl; + + var bbsRepoUrl = GetBbsRepoUrl(args); + + args.GithubPat ??= _environmentVariableProvider.TargetGithubPersonalAccessToken(); + var githubOrgId = await _githubApi.GetOrganizationId(args.GithubOrg); string migrationId; diff --git a/src/gei/Commands/MigrateRepo/MigrateRepoCommandHandler.cs b/src/gei/Commands/MigrateRepo/MigrateRepoCommandHandler.cs index cf6e3c3f2..df4bc1ce2 100644 --- a/src/gei/Commands/MigrateRepo/MigrateRepoCommandHandler.cs +++ b/src/gei/Commands/MigrateRepo/MigrateRepoCommandHandler.cs @@ -85,7 +85,25 @@ public async Task Handle(MigrateRepoCommandArgs args) { throw new OctoshiftCliException($"The target org \"{args.GithubTargetOrg}\" does not exist."); } + } + + string migrationSourceId; + + var githubOrgId = await _targetGithubApi.GetOrganizationId(args.GithubTargetOrg); + + try + { + migrationSourceId = await _targetGithubApi.CreateGhecMigrationSource(githubOrgId); + } + catch (OctoshiftCliException ex) when (ex.Message.Contains("not have the correct permissions to execute")) + { + var insufficientPermissionsMessage = InsufficientPermissionsMessageGenerator.Generate(args.GithubTargetOrg); + var message = $"{ex.Message}{insufficientPermissionsMessage}"; + throw new OctoshiftCliException(message, ex); + } + if (args.GhesApiUrl.HasValue()) + { (args.GitArchiveUrl, args.MetadataArchiveUrl) = await GenerateAndUploadArchive( args.GithubSourceOrg, args.SourceRepo, @@ -102,24 +120,10 @@ public async Task Handle(MigrateRepoCommandArgs args) } } - var githubOrgId = await _targetGithubApi.GetOrganizationId(args.GithubTargetOrg); var sourceRepoUrl = GetSourceRepoUrl(args); var sourceToken = GetSourceToken(args); var targetToken = args.GithubTargetPat ?? _environmentVariableProvider.TargetGithubPersonalAccessToken(); - string migrationSourceId; - - try - { - migrationSourceId = await _targetGithubApi.CreateGhecMigrationSource(githubOrgId); - } - catch (OctoshiftCliException ex) when (ex.Message.Contains("not have the correct permissions to execute")) - { - var insufficientPermissionsMessage = InsufficientPermissionsMessageGenerator.Generate(args.GithubTargetOrg); - var message = $"{ex.Message}{insufficientPermissionsMessage}"; - throw new OctoshiftCliException(message, ex); - } - string migrationId; try