Skip to content

Commit

Permalink
In bbs2gh migrate-repo and gei migrate-repo, create the migration…
Browse files Browse the repository at this point in the history
… source before starting the export

This reworks `bbs2gh migrate-repo` and `gei migrate-repo` to
create the migration source before running the export from the
migration origin.

This change will allow us to deduce a rough estimate of how long
exports take on GitHub Enterprise Server and Bitbucket Server.
  • Loading branch information
timrogers committed Oct 2, 2023
1 parent 166eb64 commit 9cd8661
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
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 9cd8661

Please sign in to comment.