Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Added `--migration-id` option to `download-logs` command to allow downloading logs directly by migration ID without requiring org/repo lookup
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class DownloadLogsCommandArgs : CommandArgs
{
public string GithubOrg { get; set; }
public string GithubRepo { get; set; }
public string MigrationId { get; set; }
public string GithubApiUrl { get; set; }
[Secret]
public string GithubPat { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ public class DownloadLogsCommandBase : CommandBase<DownloadLogsCommandArgs, Down

public virtual Option<string> GithubOrg { get; } = new("--github-org")
{
IsRequired = true,
Description = "GitHub organization to download logs from."
};

public virtual Option<string> GithubRepo { get; } = new("--github-repo")
{
IsRequired = true,
Description = "Target repository to download latest log for."
};

public virtual Option<string> MigrationId { get; } = new("--migration-id")
{
Description = "Migration ID to download logs for. If specified, --github-org and --github-repo are not required."
};

public virtual Option<string> GithubApiUrl { get; } = new("--github-api-url")
{
Description = "Target GitHub API URL if not targeting github.com (default: https://api.github.com)."
Expand Down Expand Up @@ -79,6 +82,7 @@ protected void AddOptions()
{
AddOption(GithubOrg);
AddOption(GithubRepo);
AddOption(MigrationId);
AddOption(GithubApiUrl);
AddOption(GithubPat);
AddOption(MigrationLogFile);
Expand Down
66 changes: 54 additions & 12 deletions src/Octoshift/Commands/DownloadLogs/DownloadLogsCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@
throw new ArgumentNullException(nameof(args));
}

if (args.MigrationId.HasValue())
{
if (args.GithubOrg.HasValue() || args.GithubRepo.HasValue())
{
_log.LogWarning("--github-org and --github-repo are ignored when --migration-id is specified.");
}
}
else
{
if (!args.GithubOrg.HasValue() || !args.GithubRepo.HasValue())
{
throw new OctoshiftCliException("Either --migration-id or both --github-org and --github-repo must be specified.");
}
}

_log.LogWarning("Migration logs are only available for 24 hours after a migration finishes!");

_log.LogInformation("Downloading migration logs...");
Expand All @@ -49,22 +64,49 @@
throw new OctoshiftCliException($"File {args.MigrationLogFile} already exists! Use --overwrite to overwrite this file.");
}

var result = await _retryPolicy.RetryOnResult(async () => await _githubApi.GetMigrationLogUrl(args.GithubOrg, args.GithubRepo), r => r?.MigrationLogUrl.IsNullOrWhiteSpace() ?? false,
"Waiting for migration log to populate...");
string logUrl;
string migrationId;
string repositoryName;

if (result.Outcome == OutcomeType.Successful && result.Result is null)
if (args.MigrationId.HasValue())
{
throw new OctoshiftCliException($"Migration for repository {args.GithubRepo} not found!");
}
// Use migration ID directly
migrationId = args.MigrationId;
var migrationResult = await _retryPolicy.RetryOnResult(
async () => await _githubApi.GetMigration(migrationId),
r => string.IsNullOrWhiteSpace(r.MigrationLogUrl),
"Waiting for migration log to populate...");

if (migrationResult.Outcome == OutcomeType.Failure)
{
throw new OctoshiftCliException($"Migration log for migration {migrationId} unavailable!");
}

if (result.Outcome == OutcomeType.Failure)
{
throw new OctoshiftCliException($"Migration log for repository {args.GithubRepo} unavailable!");
var migration = migrationResult.Result;

Check warning on line 85 in src/Octoshift/Commands/DownloadLogs/DownloadLogsCommandHandler.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, csharp)

Variable declaration can be deconstructed

Check warning on line 85 in src/Octoshift/Commands/DownloadLogs/DownloadLogsCommandHandler.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, actions)

Variable declaration can be deconstructed

Check warning on line 85 in src/Octoshift/Commands/DownloadLogs/DownloadLogsCommandHandler.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, actions)

Variable declaration can be deconstructed

Check warning on line 85 in src/Octoshift/Commands/DownloadLogs/DownloadLogsCommandHandler.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, csharp)

Variable declaration can be deconstructed

Check warning on line 85 in src/Octoshift/Commands/DownloadLogs/DownloadLogsCommandHandler.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, csharp)

Variable declaration can be deconstructed

Check warning on line 85 in src/Octoshift/Commands/DownloadLogs/DownloadLogsCommandHandler.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, actions)

Variable declaration can be deconstructed
logUrl = migration.MigrationLogUrl;
repositoryName = migration.RepositoryName;
}
else
{
// Use org/repo to find migration
var result = await _retryPolicy.RetryOnResult(async () => await _githubApi.GetMigrationLogUrl(args.GithubOrg, args.GithubRepo), r => r?.MigrationLogUrl.IsNullOrWhiteSpace() ?? false,
"Waiting for migration log to populate...");

if (result.Outcome == OutcomeType.Successful && result.Result is null)
{
throw new OctoshiftCliException($"Migration for repository {args.GithubRepo} not found!");
}

var (logUrl, migrationId) = result.Result.Value;
if (result.Outcome == OutcomeType.Failure)
{
throw new OctoshiftCliException($"Migration log for repository {args.GithubRepo} unavailable!");
}

(logUrl, migrationId) = result.Result.Value;
repositoryName = args.GithubRepo;
}

args.MigrationLogFile ??= $"migration-log-{args.GithubOrg}-{args.GithubRepo}-{migrationId}.log";
args.MigrationLogFile ??= $"migration-log-{repositoryName}-{migrationId}.log";

if (FileExists(args.MigrationLogFile))
{
Expand All @@ -76,9 +118,9 @@
_log.LogWarning($"Overwriting {args.MigrationLogFile} due to --overwrite option.");
}

_log.LogInformation($"Downloading log for repository {args.GithubRepo} to {args.MigrationLogFile}...");
_log.LogInformation($"Downloading log for repository {repositoryName} to {args.MigrationLogFile}...");
await _httpDownloadService.DownloadToFile(logUrl, args.MigrationLogFile);

_log.LogSuccess($"Downloaded {args.GithubRepo} log to {args.MigrationLogFile}.");
_log.LogSuccess($"Downloaded {repositoryName} log to {args.MigrationLogFile}.");
}
}
Loading