Skip to content
Merged
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
2 changes: 1 addition & 1 deletion RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@

- Added `--target-api-url` as an optional arg to the `add-team-to-repo` command
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ public void Should_Have_Options()
var command = new AddTeamToRepoCommand();
Assert.NotNull(command);
Assert.Equal("add-team-to-repo", 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, "github-repo", true);
TestHelpers.VerifyCommandOption(command.Options, "team", true);
TestHelpers.VerifyCommandOption(command.Options, "role", true);
TestHelpers.VerifyCommandOption(command.Options, "github-pat", false);
TestHelpers.VerifyCommandOption(command.Options, "verbose", false);
TestHelpers.VerifyCommandOption(command.Options, "target-api-url", false);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,42 @@ public async Task ParallelScript_Rewire_Pipelines_Option_Should_Generate_Share_S
_scriptOutput.Should().Be(expected.ToString());
}

[Fact]
public async Task SequentialScript_CreateTeams_With_TargetApiUrl_Should_Include_TargetApiUrl_In_AddTeamToRepo_Commands()
{
// Arrange
const string TARGET_API_URL = "https://example.com/api/v3";

_mockAdoInspector.Setup(m => m.GetRepoCount()).ReturnsAsync(1);
_mockAdoInspector.Setup(m => m.GetOrgs()).ReturnsAsync(ADO_ORGS);
_mockAdoInspector.Setup(m => m.GetTeamProjects(ADO_ORG)).ReturnsAsync(ADO_TEAM_PROJECTS);
_mockAdoInspector.Setup(m => m.GetRepos(ADO_ORG, ADO_TEAM_PROJECT)).ReturnsAsync(ADO_REPOS);

var expected = new StringBuilder();
expected.AppendLine($"Exec {{ gh ado2gh create-team --target-api-url \"{TARGET_API_URL}\" --github-org \"{GITHUB_ORG}\" --team-name \"{ADO_TEAM_PROJECT}-Maintainers\" }}");
expected.AppendLine($"Exec {{ gh ado2gh create-team --target-api-url \"{TARGET_API_URL}\" --github-org \"{GITHUB_ORG}\" --team-name \"{ADO_TEAM_PROJECT}-Admins\" }}");
expected.AppendLine($"Exec {{ gh ado2gh migrate-repo --target-api-url \"{TARGET_API_URL}\" --ado-org \"{ADO_ORG}\" --ado-team-project \"{ADO_TEAM_PROJECT}\" --ado-repo \"{FOO_REPO}\" --github-org \"{GITHUB_ORG}\" --github-repo \"{ADO_TEAM_PROJECT}-{FOO_REPO}\" --target-repo-visibility private }}");
expected.AppendLine($"Exec {{ gh ado2gh add-team-to-repo --target-api-url \"{TARGET_API_URL}\" --github-org \"{GITHUB_ORG}\" --github-repo \"{ADO_TEAM_PROJECT}-{FOO_REPO}\" --team \"{ADO_TEAM_PROJECT}-Maintainers\" --role \"maintain\" }}");
expected.Append($"Exec {{ gh ado2gh add-team-to-repo --target-api-url \"{TARGET_API_URL}\" --github-org \"{GITHUB_ORG}\" --github-repo \"{ADO_TEAM_PROJECT}-{FOO_REPO}\" --team \"{ADO_TEAM_PROJECT}-Admins\" --role \"admin\" }}");

// Act
var args = new GenerateScriptCommandArgs
{
GithubOrg = GITHUB_ORG,
AdoOrg = ADO_ORG,
Sequential = true,
Output = new FileInfo("unit-test-output"),
CreateTeams = true,
TargetApiUrl = TARGET_API_URL
};
await _handler.Handle(args);

_scriptOutput = TrimNonExecutableLines(_scriptOutput);

// Assert
_scriptOutput.Should().Be(expected.ToString());
}

private string TrimNonExecutableLines(string script, int skipFirst = 21, int skipLast = 0)
{
var lines = script.Split(new[] { Environment.NewLine, "\n" }, StringSplitOptions.RemoveEmptyEntries).AsEnumerable();
Expand Down
8 changes: 6 additions & 2 deletions src/ado2gh/Commands/AddTeamToRepo/AddTeamToRepoCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public AddTeamToRepoCommand() : base(
AddOption(Role.FromAmong("pull", "push", "admin", "maintain", "triage"));
AddOption(GithubPat);
AddOption(Verbose);
AddOption(TargetApiUrl);
}

public Option<string> GithubOrg { get; } = new("--github-org")
Expand All @@ -42,7 +43,10 @@ public AddTeamToRepoCommand() : base(
};
public Option<string> GithubPat { get; } = new("--github-pat");
public Option<bool> Verbose { get; } = new("--verbose");

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 override AddTeamToRepoCommandHandler BuildHandler(AddTeamToRepoCommandArgs args, IServiceProvider sp)
{
if (args is null)
Expand All @@ -57,7 +61,7 @@ public override AddTeamToRepoCommandHandler BuildHandler(AddTeamToRepoCommandArg

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

return new AddTeamToRepoCommandHandler(log, githubApi);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class AddTeamToRepoCommandArgs : CommandArgs
public string Role { get; set; }
[Secret]
public string GithubPat { get; set; }
public string TargetApiUrl { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ private async Task<string> GenerateSequentialScript(IDictionary<string, string>
AppendLine(content, Exec(LockAdoRepoScript(adoOrg, adoTeamProject, adoRepo.Name)));
AppendLine(content, Exec(MigrateRepoScript(adoOrg, adoTeamProject, adoRepo.Name, githubOrg, githubRepo, true, adoServerUrl, targetApiUrl)));
AppendLine(content, Exec(DisableAdoRepoScript(adoOrg, adoTeamProject, adoRepo.Name)));
AppendLine(content, Exec(AddMaintainersToGithubRepoScript(adoTeamProject, githubOrg, githubRepo)));
AppendLine(content, Exec(AddAdminsToGithubRepoScript(adoTeamProject, githubOrg, githubRepo)));
AppendLine(content, Exec(AddMaintainersToGithubRepoScript(adoTeamProject, githubOrg, githubRepo, targetApiUrl)));
AppendLine(content, Exec(AddAdminsToGithubRepoScript(adoTeamProject, githubOrg, githubRepo, targetApiUrl)));
AppendLine(content, Exec(DownloadMigrationLogScript(githubOrg, githubRepo, targetApiUrl)));

foreach (var adoPipeline in await _adoInspectorService.GetPipelines(adoOrg, adoTeamProject, adoRepo.Name))
Expand Down Expand Up @@ -275,8 +275,8 @@ private async Task<string> GenerateParallelScript(IDictionary<string, string> ap
{
AppendLine(content, " ExecBatch @(");
AppendLine(content, " " + Wrap(DisableAdoRepoScript(adoOrg, adoTeamProject, adoRepo.Name)));
AppendLine(content, " " + Wrap(AddMaintainersToGithubRepoScript(adoTeamProject, githubOrg, githubRepo)));
AppendLine(content, " " + Wrap(AddAdminsToGithubRepoScript(adoTeamProject, githubOrg, githubRepo)));
AppendLine(content, " " + Wrap(AddMaintainersToGithubRepoScript(adoTeamProject, githubOrg, githubRepo, targetApiUrl)));
AppendLine(content, " " + Wrap(AddAdminsToGithubRepoScript(adoTeamProject, githubOrg, githubRepo, targetApiUrl)));
AppendLine(content, " " + Wrap(DownloadMigrationLogScript(githubOrg, githubRepo, targetApiUrl)));

appIds.TryGetValue(adoOrg, out var appId);
Expand Down Expand Up @@ -360,14 +360,14 @@ private string CreateGithubAdminsTeamScript(string adoTeamProject, string github
? $"gh ado2gh create-team{(targetApiUrl.HasValue() ? $" --target-api-url \"{targetApiUrl}\"" : string.Empty)} --github-org \"{githubOrg}\" --team-name \"{adoTeamProject.ReplaceInvalidCharactersWithDash()}-Admins\"{(_log.Verbose ? " --verbose" : string.Empty)}{(linkIdpGroups ? $" --idp-group \"{adoTeamProject.ReplaceInvalidCharactersWithDash()}-Admins\"" : string.Empty)}"
: null;

private string AddMaintainersToGithubRepoScript(string adoTeamProject, string githubOrg, string githubRepo) =>
private string AddMaintainersToGithubRepoScript(string adoTeamProject, string githubOrg, string githubRepo, string targetApiUrl) =>
_generateScriptOptions.CreateTeams
? $"gh ado2gh add-team-to-repo --github-org \"{githubOrg}\" --github-repo \"{githubRepo}\" --team \"{adoTeamProject.ReplaceInvalidCharactersWithDash()}-Maintainers\" --role \"maintain\"{(_log.Verbose ? " --verbose" : string.Empty)}"
? $"gh ado2gh add-team-to-repo{(targetApiUrl.HasValue() ? $" --target-api-url \"{targetApiUrl}\"" : string.Empty)} --github-org \"{githubOrg}\" --github-repo \"{githubRepo}\" --team \"{adoTeamProject.ReplaceInvalidCharactersWithDash()}-Maintainers\" --role \"maintain\"{(_log.Verbose ? " --verbose" : string.Empty)}"
: null;

private string AddAdminsToGithubRepoScript(string adoTeamProject, string githubOrg, string githubRepo) =>
private string AddAdminsToGithubRepoScript(string adoTeamProject, string githubOrg, string githubRepo, string targetApiUrl) =>
_generateScriptOptions.CreateTeams
? $"gh ado2gh add-team-to-repo --github-org \"{githubOrg}\" --github-repo \"{githubRepo}\" --team \"{adoTeamProject.ReplaceInvalidCharactersWithDash()}-Admins\" --role \"admin\"{(_log.Verbose ? " --verbose" : string.Empty)}"
? $"gh ado2gh add-team-to-repo{(targetApiUrl.HasValue() ? $" --target-api-url \"{targetApiUrl}\"" : string.Empty)} --github-org \"{githubOrg}\" --github-repo \"{githubRepo}\" --team \"{adoTeamProject.ReplaceInvalidCharactersWithDash()}-Admins\" --role \"admin\"{(_log.Verbose ? " --verbose" : string.Empty)}"
: null;
Comment on lines +363 to 371
Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test case that verifies when both CreateTeams and TargetApiUrl are set, the generated script includes --target-api-url in the add-team-to-repo commands. This would ensure the integration between the GenerateScript command and the AddTeamToRepo command works correctly when migrating to GHE.com. For example, the test should verify that the generated script contains commands like "gh ado2gh add-team-to-repo --target-api-url "https://example.com/api/v3\" --github-org ..."

Copilot uses AI. Check for mistakes.

private string RewireAzurePipelineScript(string adoOrg, string adoTeamProject, string adoPipeline, string githubOrg, string githubRepo, string appId) =>
Expand Down
Loading