Skip to content

Commit

Permalink
Merge pull request #1119 from github/create-migration-source-first
Browse files Browse the repository at this point in the history
In `bbs2gh migrate-repo` and `gei migrate-repo`, create the migration source before starting the export
  • Loading branch information
timrogers authored Oct 6, 2023
2 parents b4c8322 + 6e584ec commit 7a28e63
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
2 changes: 1 addition & 1 deletion RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -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
29 changes: 19 additions & 10 deletions src/bbs2gh/Commands/MigrateRepo/MigrateRepoCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public async Task Handle(MigrateRepoCommandArgs args)
ValidateOptions(args);

var exportId = 0L;
var migrationSourceId = "";

if (args.ShouldImportArchive())
{
Expand All @@ -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())
Expand Down Expand Up @@ -99,7 +102,7 @@ public async Task Handle(MigrateRepoCommandArgs args)

if (args.ShouldImportArchive())
{
await ImportArchive(args, args.ArchiveUrl);
await ImportArchive(args, migrationSourceId, args.ArchiveUrl);
}
}

Expand Down Expand Up @@ -190,29 +193,35 @@ private async Task<string> UploadArchiveToAws(string bucketName, string archiveP
return archiveBlobUrl;
}

private async Task ImportArchive(MigrateRepoCommandArgs args, string archiveUrl = null)
private async Task<string> 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"))
{
var insufficientPermissionsMessage = InsufficientPermissionsMessageGenerator.Generate(args.GithubOrg);
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;

Expand Down
32 changes: 18 additions & 14 deletions src/gei/Commands/MigrateRepo/MigrateRepoCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 7a28e63

Please sign in to comment.