Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support remaining target-api-url commands (-1) #1219

Merged
merged 9 commits into from
Feb 7, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class AbortMigrationCommandArgs : CommandArgs
public string MigrationId { get; set; }
[Secret]
public string GithubPat { get; set; }
public string TargetApiUrl { get; set; }

public override void Validate(OctoLogger log)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ public AbortMigrationCommandBase() : base(
{
Description = "Display more information to the console."
};

public Option<string> TargetApiUrl { get; } = new("--target-api-url")
{
Description = "The URL of the target API, if not migrating to github.com. Defaults to https://api.github.com"
};
protected void AddOptions()
{
AddOption(MigrationId);
AddOption(GithubPat);
AddOption(Verbose);
AddOption(TargetApiUrl);
}

public override AbortMigrationCommandHandler BuildHandler(AbortMigrationCommandArgs args, IServiceProvider sp)
Expand All @@ -51,7 +55,7 @@ public override AbortMigrationCommandHandler BuildHandler(AbortMigrationCommandA
}

var log = sp.GetRequiredService<OctoLogger>();
var githubApi = sp.GetRequiredService<ITargetGithubApiFactory>().Create(targetPersonalAccessToken: args.GithubPat);
var githubApi = sp.GetRequiredService<ITargetGithubApiFactory>().Create(args.TargetApiUrl, args.GithubPat);

return new AbortMigrationCommandHandler(log, githubApi);
}
Expand Down
1 change: 1 addition & 0 deletions src/Octoshift/Commands/CreateTeam/CreateTeamCommandArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public class CreateTeamCommandArgs : CommandArgs
public string IdpGroup { get; set; }
[Secret]
public string GithubPat { get; set; }
public string TargetApiUrl { get; set; }
}
8 changes: 6 additions & 2 deletions src/Octoshift/Commands/CreateTeam/CreateTeamCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public CreateTeamCommandBase() : base(name: "create-team", description: "Creates
{
Description = "Personal access token of the GitHub target. Overrides GH_PAT environment variable."
};

public Option<string> TargetApiUrl { get; } = new("--target-api-url")
{
Description = "The URL of the target API, if not migrating to github.com. Defaults to https://api.github.com"
};
public virtual Option<bool> Verbose { get; } = new("--verbose") { IsRequired = false };

public override CreateTeamCommandHandler BuildHandler(CreateTeamCommandArgs args, IServiceProvider sp)
Expand All @@ -40,7 +43,7 @@ public override CreateTeamCommandHandler BuildHandler(CreateTeamCommandArgs args
var log = sp.GetRequiredService<OctoLogger>();
var githubApiFactory = sp.GetRequiredService<ITargetGithubApiFactory>();

var githubApi = githubApiFactory.Create(targetPersonalAccessToken: args.GithubPat);
var githubApi = githubApiFactory.Create(args.TargetApiUrl, args.GithubPat);

return new CreateTeamCommandHandler(log, githubApi);
}
Expand All @@ -52,5 +55,6 @@ protected void AddOptions()
AddOption(IdpGroup);
AddOption(GithubPat);
AddOption(Verbose);
AddOption(TargetApiUrl);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OctoshiftCLI.Services;
using OctoshiftCLI.Extensions;
using OctoshiftCLI.Services;

namespace OctoshiftCLI.Commands.RevokeMigratorRole;

Expand All @@ -10,6 +11,7 @@ public class RevokeMigratorRoleCommandArgs : CommandArgs
[Secret]
public string GithubPat { get; set; }
public string GhesApiUrl { get; set; }
public string TargetApiUrl { get; set; }

public override void Validate(OctoLogger log)
{
Expand All @@ -23,5 +25,10 @@ public override void Validate(OctoLogger log)
{
throw new OctoshiftCliException("Actor type must be either TEAM or USER.");
}

if (GhesApiUrl.HasValue() && TargetApiUrl.HasValue())
{
throw new OctoshiftCliException("Only one of --ghes-api-url or --target-api-url can be set at a time.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,22 @@ public override RevokeMigratorRoleCommandHandler BuildHandler(RevokeMigratorRole

var log = sp.GetRequiredService<OctoLogger>();
var githubApiFactory = sp.GetRequiredService<ITargetGithubApiFactory>();
var githubApi = githubApiFactory.Create(args.GhesApiUrl, args.GithubPat);
var apiUrl = args.TargetApiUrl ?? args.GhesApiUrl;
var githubApi = githubApiFactory.Create(apiUrl, args.GithubPat);

return new RevokeMigratorRoleCommandHandler(log, githubApi);
}

public virtual Option<string> GhesApiUrl { get; } = new("--ghes-api-url") { IsRequired = false };

public virtual Option<string> GhesApiUrl { get; } = new("--ghes-api-url")
{
IsRequired = false,
Description = "The URL of the GitHub Enterprise Server instance, if migrating from GHES. Supports revoking access for exports. Can only configure one of --ghes-api-url or --target-api-url at a time."
};
public Option<string> TargetApiUrl { get; } = new("--target-api-url")
{
IsRequired = false,
Description = "The URL of the target API, if not migrating to github.com. Defaults to https://api.github.com. Can only configure one of --ghes-api-url or --target-api-url at a time."
};
protected void AddOptions()
{
AddOption(GithubOrg);
Expand All @@ -56,5 +65,6 @@ protected void AddOptions()
AddOption(GithubPat);
AddOption(Verbose);
AddOption(GhesApiUrl);
AddOption(TargetApiUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class WaitForMigrationCommandArgs : CommandArgs
public string MigrationId { get; set; }
[Secret]
public string GithubPat { get; set; }
public string TargetApiUrl { get; set; }

public const string REPO_MIGRATION_ID_PREFIX = "RM_";
public const string ORG_MIGRATION_ID_PREFIX = "OM_";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ public WaitForMigrationCommandBase() : base(
{
Description = "Personal access token of the GitHub target. Overrides GH_PAT environment variable."
};

public Option<string> TargetApiUrl { get; } = new("--target-api-url")
{
Description = "The URL of the target API, if not migrating to github.com. Defaults to https://api.github.com"
};
public virtual Option<bool> Verbose { get; } = new("--verbose");

protected void AddOptions()
{
AddOption(MigrationId);
AddOption(GithubPat);
AddOption(Verbose);
AddOption(TargetApiUrl);
}

public override WaitForMigrationCommandHandler BuildHandler(WaitForMigrationCommandArgs args, IServiceProvider sp)
Expand All @@ -50,7 +54,7 @@ public override WaitForMigrationCommandHandler BuildHandler(WaitForMigrationComm
}

var log = sp.GetRequiredService<OctoLogger>();
var githubApi = sp.GetRequiredService<ITargetGithubApiFactory>().Create(targetPersonalAccessToken: args.GithubPat);
var githubApi = sp.GetRequiredService<ITargetGithubApiFactory>().Create(args.TargetApiUrl, args.GithubPat);
var warningsCountLogger = sp.GetRequiredService<WarningsCountLogger>();

return new WaitForMigrationCommandHandler(log, githubApi, warningsCountLogger);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.Extensions.DependencyInjection;
using Moq;
using OctoshiftCLI.Commands.AbortMigration;
using OctoshiftCLI.Contracts;
using OctoshiftCLI.Services;
using Xunit;

namespace OctoshiftCLI.Tests.Octoshift.Commands.AbortMigration;

public class AbortMigrationCommandBaseTests
{
private readonly Mock<ITargetGithubApiFactory> _mockGithubApiFactory = new();
private readonly Mock<OctoLogger> _mockOctoLogger = TestHelpers.CreateMock<OctoLogger>();
private readonly ServiceProvider _serviceProvider;
private readonly AbortMigrationCommandBase _command = new();

private const string REPO_MIGRATION_ID = "RM_MIGRATION_ID";

public AbortMigrationCommandBaseTests()
{
var serviceCollection = new ServiceCollection();
serviceCollection
.AddSingleton(_mockOctoLogger.Object)
.AddSingleton(_mockGithubApiFactory.Object);

_serviceProvider = serviceCollection.BuildServiceProvider();
}

[Fact]
public void It_Uses_The_TargetApiUrl_When_Provided()
{
var targetApiUrl = "TargetApiUrl";

var args = new AbortMigrationCommandArgs
{
MigrationId = REPO_MIGRATION_ID,
TargetApiUrl = targetApiUrl
};

_command.BuildHandler(args, _serviceProvider);

_mockGithubApiFactory.Verify(m => m.Create(targetApiUrl, It.IsAny<string>()));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,21 @@ public void It_Uses_Github_Source_Pat_When_Provided()

_mockGithubApiFactory.Verify(m => m.Create(It.IsAny<string>(), args.GithubPat), Times.Once);
}

[Fact]
public void It_Uses_Target_Api_Url_When_Provided()
{
var targetApiUrl = "https://api.github.com";

var args = new CreateTeamCommandArgs
{
GithubOrg = "foo",
TeamName = "blah",
TargetApiUrl = targetApiUrl
};

_command.BuildHandler(args, _serviceProvider);

_mockGithubApiFactory.Verify(m => m.Create(args.TargetApiUrl, null), Times.Once);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ public void Invalid_Actor_Type()
ActorType = "INVALID",
};

FluentActions
.Invoking(() => args.Validate(_mockOctoLogger.Object))
.Should()
.ThrowExactly<OctoshiftCliException>();
}
[Fact]
public void It_Validates_GhesApiUrl_And_TargetApiUrl()
{
var args = new RevokeMigratorRoleCommandArgs
{
GithubOrg = GITHUB_ORG,
Actor = ACTOR,
ActorType = "USER",
GhesApiUrl = "https://ghes.example.com",
TargetApiUrl = "https://api.github.com",
};

FluentActions
.Invoking(() => args.Validate(_mockOctoLogger.Object))
.Should()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,24 @@ public void It_Uses_The_GhesApiUrl_When_Provided()

_mockGithubApiFactory.Verify(m => m.Create(ghesApiUrl, null));
}

[Fact]
public void It_Uses_The_TargetApiUrl_When_Provided()
{
var ghesApiUrl = "GhesApiUrl";
var targetApiUrl = "TargetApiUrl";

var args = new RevokeMigratorRoleCommandArgs
{
GithubOrg = "foo",
Actor = "blah",
ActorType = "TEAM",
GhesApiUrl = ghesApiUrl,
TargetApiUrl = targetApiUrl
};

_command.BuildHandler(args, _serviceProvider);

_mockGithubApiFactory.Verify(m => m.Create(targetApiUrl, null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.Extensions.DependencyInjection;
using Moq;
using OctoshiftCLI.Commands.WaitForMigration;
using OctoshiftCLI.Contracts;
using OctoshiftCLI.Services;
using Xunit;

namespace OctoshiftCLI.Tests.Octoshift.Commands.WaitForMigration;

public class WaitForMigrationCommandBaseTests
{
private readonly Mock<ITargetGithubApiFactory> _mockGithubApiFactory = new();
private readonly Mock<OctoLogger> _mockOctoLogger = TestHelpers.CreateMock<OctoLogger>();
private readonly Mock<WarningsCountLogger> _mockWarningsCountLogger = TestHelpers.CreateMock<WarningsCountLogger>();
private readonly ServiceProvider _serviceProvider;
private readonly WaitForMigrationCommandBase _command = new();

private const string REPO_MIGRATION_ID = "RM_MIGRATION_ID";
public WaitForMigrationCommandBaseTests()
{
var serviceCollection = new ServiceCollection();
serviceCollection
.AddSingleton(_mockOctoLogger.Object)
.AddSingleton(_mockGithubApiFactory.Object)
.AddSingleton(_mockWarningsCountLogger.Object);

_serviceProvider = serviceCollection.BuildServiceProvider();
}

[Fact]
public void It_Uses_The_TargetApiUrl_When_Provided()
{
var targetApiUrl = "TargetApiUrl";

var args = new WaitForMigrationCommandArgs
{
MigrationId = REPO_MIGRATION_ID,
TargetApiUrl = targetApiUrl
};

_command.BuildHandler(args, _serviceProvider);

_mockGithubApiFactory.Verify(m => m.Create(targetApiUrl, It.IsAny<string>()));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ public void Should_Have_Options()
var command = new AbortMigrationCommand();
command.Should().NotBeNull();
command.Name.Should().Be("abort-migration");
command.Options.Count.Should().Be(3);
command.Options.Count.Should().Be(4);

TestHelpers.VerifyCommandOption(command.Options, "migration-id", true);
TestHelpers.VerifyCommandOption(command.Options, "github-pat", false);
TestHelpers.VerifyCommandOption(command.Options, "verbose", false);
TestHelpers.VerifyCommandOption(command.Options, "target-api-url", false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ public void Should_Have_Options()
var command = new CreateTeamCommand();
Assert.NotNull(command);
Assert.Equal("create-team", command.Name);
Assert.Equal(5, command.Options.Count);
Assert.Equal(6, command.Options.Count);

TestHelpers.VerifyCommandOption(command.Options, "github-org", true);
TestHelpers.VerifyCommandOption(command.Options, "team-name", true);
TestHelpers.VerifyCommandOption(command.Options, "idp-group", false);
TestHelpers.VerifyCommandOption(command.Options, "github-pat", false);
TestHelpers.VerifyCommandOption(command.Options, "verbose", false);
TestHelpers.VerifyCommandOption(command.Options, "target-api-url", false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ public void Should_Have_Options()
var command = new RevokeMigratorRoleCommand();
Assert.NotNull(command);
Assert.Equal("revoke-migrator-role", command.Name);
Assert.Equal(6, command.Options.Count);
Assert.Equal(7, command.Options.Count);

TestHelpers.VerifyCommandOption(command.Options, "github-org", true);
TestHelpers.VerifyCommandOption(command.Options, "actor", true);
TestHelpers.VerifyCommandOption(command.Options, "actor-type", true);
TestHelpers.VerifyCommandOption(command.Options, "github-pat", false);
TestHelpers.VerifyCommandOption(command.Options, "verbose", false);
TestHelpers.VerifyCommandOption(command.Options, "ghes-api-url", false);
TestHelpers.VerifyCommandOption(command.Options, "target-api-url", false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ public void Should_Have_Options()
var command = new WaitForMigrationCommand();
command.Should().NotBeNull();
command.Name.Should().Be("wait-for-migration");
command.Options.Count.Should().Be(3);
command.Options.Count.Should().Be(4);

TestHelpers.VerifyCommandOption(command.Options, "migration-id", true);
TestHelpers.VerifyCommandOption(command.Options, "github-pat", false);
TestHelpers.VerifyCommandOption(command.Options, "verbose", false);
TestHelpers.VerifyCommandOption(command.Options, "target-api-url", false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ public void Should_Have_Options()
var command = new AbortMigrationCommand();
command.Should().NotBeNull();
command.Name.Should().Be("abort-migration");
command.Options.Count.Should().Be(3);
command.Options.Count.Should().Be(4);

TestHelpers.VerifyCommandOption(command.Options, "migration-id", true);
TestHelpers.VerifyCommandOption(command.Options, "github-pat", false);
TestHelpers.VerifyCommandOption(command.Options, "verbose", false);
TestHelpers.VerifyCommandOption(command.Options, "target-api-url", false);
}
}
Loading
Loading