Skip to content

Commit 2be9555

Browse files
authored
Merge pull request #1484 from github/brianaj/external-pr-1400
Add --target-api-url support to add-team-to-repo command
2 parents a06667b + 2c1ea1c commit 2be9555

File tree

6 files changed

+54
-12
lines changed

6 files changed

+54
-12
lines changed

RELEASENOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
- Added `--target-api-url` as an optional arg to the `add-team-to-repo` command

src/OctoshiftCLI.Tests/ado2gh/Commands/AddTeamToRepo/AddTeamToRepoCommandTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ public void Should_Have_Options()
3333
var command = new AddTeamToRepoCommand();
3434
Assert.NotNull(command);
3535
Assert.Equal("add-team-to-repo", command.Name);
36-
Assert.Equal(6, command.Options.Count);
36+
Assert.Equal(7, command.Options.Count);
3737

3838
TestHelpers.VerifyCommandOption(command.Options, "github-org", true);
3939
TestHelpers.VerifyCommandOption(command.Options, "github-repo", true);
4040
TestHelpers.VerifyCommandOption(command.Options, "team", true);
4141
TestHelpers.VerifyCommandOption(command.Options, "role", true);
4242
TestHelpers.VerifyCommandOption(command.Options, "github-pat", false);
4343
TestHelpers.VerifyCommandOption(command.Options, "verbose", false);
44+
TestHelpers.VerifyCommandOption(command.Options, "target-api-url", false);
4445
}
4546

4647
[Fact]

src/OctoshiftCLI.Tests/ado2gh/Commands/GenerateScript/GenerateScriptCommandHandlerTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,42 @@ public async Task ParallelScript_Rewire_Pipelines_Option_Should_Generate_Share_S
13311331
_scriptOutput.Should().Be(expected.ToString());
13321332
}
13331333

1334+
[Fact]
1335+
public async Task SequentialScript_CreateTeams_With_TargetApiUrl_Should_Include_TargetApiUrl_In_AddTeamToRepo_Commands()
1336+
{
1337+
// Arrange
1338+
const string TARGET_API_URL = "https://example.com/api/v3";
1339+
1340+
_mockAdoInspector.Setup(m => m.GetRepoCount()).ReturnsAsync(1);
1341+
_mockAdoInspector.Setup(m => m.GetOrgs()).ReturnsAsync(ADO_ORGS);
1342+
_mockAdoInspector.Setup(m => m.GetTeamProjects(ADO_ORG)).ReturnsAsync(ADO_TEAM_PROJECTS);
1343+
_mockAdoInspector.Setup(m => m.GetRepos(ADO_ORG, ADO_TEAM_PROJECT)).ReturnsAsync(ADO_REPOS);
1344+
1345+
var expected = new StringBuilder();
1346+
expected.AppendLine($"Exec {{ gh ado2gh create-team --target-api-url \"{TARGET_API_URL}\" --github-org \"{GITHUB_ORG}\" --team-name \"{ADO_TEAM_PROJECT}-Maintainers\" }}");
1347+
expected.AppendLine($"Exec {{ gh ado2gh create-team --target-api-url \"{TARGET_API_URL}\" --github-org \"{GITHUB_ORG}\" --team-name \"{ADO_TEAM_PROJECT}-Admins\" }}");
1348+
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 }}");
1349+
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\" }}");
1350+
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\" }}");
1351+
1352+
// Act
1353+
var args = new GenerateScriptCommandArgs
1354+
{
1355+
GithubOrg = GITHUB_ORG,
1356+
AdoOrg = ADO_ORG,
1357+
Sequential = true,
1358+
Output = new FileInfo("unit-test-output"),
1359+
CreateTeams = true,
1360+
TargetApiUrl = TARGET_API_URL
1361+
};
1362+
await _handler.Handle(args);
1363+
1364+
_scriptOutput = TrimNonExecutableLines(_scriptOutput);
1365+
1366+
// Assert
1367+
_scriptOutput.Should().Be(expected.ToString());
1368+
}
1369+
13341370
private string TrimNonExecutableLines(string script, int skipFirst = 21, int skipLast = 0)
13351371
{
13361372
var lines = script.Split(new[] { Environment.NewLine, "\n" }, StringSplitOptions.RemoveEmptyEntries).AsEnumerable();

src/ado2gh/Commands/AddTeamToRepo/AddTeamToRepoCommand.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public AddTeamToRepoCommand() : base(
2121
AddOption(Role.FromAmong("pull", "push", "admin", "maintain", "triage"));
2222
AddOption(GithubPat);
2323
AddOption(Verbose);
24+
AddOption(TargetApiUrl);
2425
}
2526

2627
public Option<string> GithubOrg { get; } = new("--github-org")
@@ -42,7 +43,10 @@ public AddTeamToRepoCommand() : base(
4243
};
4344
public Option<string> GithubPat { get; } = new("--github-pat");
4445
public Option<bool> Verbose { get; } = new("--verbose");
45-
46+
public Option<string> TargetApiUrl { get; } = new("--target-api-url")
47+
{
48+
Description = "The URL of the target API, if not migrating to github.com. Defaults to https://api.github.com"
49+
};
4650
public override AddTeamToRepoCommandHandler BuildHandler(AddTeamToRepoCommandArgs args, IServiceProvider sp)
4751
{
4852
if (args is null)
@@ -57,7 +61,7 @@ public override AddTeamToRepoCommandHandler BuildHandler(AddTeamToRepoCommandArg
5761

5862
var log = sp.GetRequiredService<OctoLogger>();
5963
var targetGithubApiFactory = sp.GetRequiredService<ITargetGithubApiFactory>();
60-
var githubApi = targetGithubApiFactory.Create(targetPersonalAccessToken: args.GithubPat);
64+
var githubApi = targetGithubApiFactory.Create(apiUrl: args.TargetApiUrl, targetPersonalAccessToken: args.GithubPat);
6165

6266
return new AddTeamToRepoCommandHandler(log, githubApi);
6367
}

src/ado2gh/Commands/AddTeamToRepo/AddTeamToRepoCommandArgs.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public class AddTeamToRepoCommandArgs : CommandArgs
1010
public string Role { get; set; }
1111
[Secret]
1212
public string GithubPat { get; set; }
13+
public string TargetApiUrl { get; set; }
1314
}
1415
}

src/ado2gh/Commands/GenerateScript/GenerateScriptCommandHandler.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ private async Task<string> GenerateSequentialScript(IDictionary<string, string>
166166
AppendLine(content, Exec(LockAdoRepoScript(adoOrg, adoTeamProject, adoRepo.Name)));
167167
AppendLine(content, Exec(MigrateRepoScript(adoOrg, adoTeamProject, adoRepo.Name, githubOrg, githubRepo, true, adoServerUrl, targetApiUrl)));
168168
AppendLine(content, Exec(DisableAdoRepoScript(adoOrg, adoTeamProject, adoRepo.Name)));
169-
AppendLine(content, Exec(AddMaintainersToGithubRepoScript(adoTeamProject, githubOrg, githubRepo)));
170-
AppendLine(content, Exec(AddAdminsToGithubRepoScript(adoTeamProject, githubOrg, githubRepo)));
169+
AppendLine(content, Exec(AddMaintainersToGithubRepoScript(adoTeamProject, githubOrg, githubRepo, targetApiUrl)));
170+
AppendLine(content, Exec(AddAdminsToGithubRepoScript(adoTeamProject, githubOrg, githubRepo, targetApiUrl)));
171171
AppendLine(content, Exec(DownloadMigrationLogScript(githubOrg, githubRepo, targetApiUrl)));
172172

173173
foreach (var adoPipeline in await _adoInspectorService.GetPipelines(adoOrg, adoTeamProject, adoRepo.Name))
@@ -275,8 +275,8 @@ private async Task<string> GenerateParallelScript(IDictionary<string, string> ap
275275
{
276276
AppendLine(content, " ExecBatch @(");
277277
AppendLine(content, " " + Wrap(DisableAdoRepoScript(adoOrg, adoTeamProject, adoRepo.Name)));
278-
AppendLine(content, " " + Wrap(AddMaintainersToGithubRepoScript(adoTeamProject, githubOrg, githubRepo)));
279-
AppendLine(content, " " + Wrap(AddAdminsToGithubRepoScript(adoTeamProject, githubOrg, githubRepo)));
278+
AppendLine(content, " " + Wrap(AddMaintainersToGithubRepoScript(adoTeamProject, githubOrg, githubRepo, targetApiUrl)));
279+
AppendLine(content, " " + Wrap(AddAdminsToGithubRepoScript(adoTeamProject, githubOrg, githubRepo, targetApiUrl)));
280280
AppendLine(content, " " + Wrap(DownloadMigrationLogScript(githubOrg, githubRepo, targetApiUrl)));
281281

282282
appIds.TryGetValue(adoOrg, out var appId);
@@ -360,14 +360,14 @@ private string CreateGithubAdminsTeamScript(string adoTeamProject, string github
360360
? $"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)}"
361361
: null;
362362

363-
private string AddMaintainersToGithubRepoScript(string adoTeamProject, string githubOrg, string githubRepo) =>
363+
private string AddMaintainersToGithubRepoScript(string adoTeamProject, string githubOrg, string githubRepo, string targetApiUrl) =>
364364
_generateScriptOptions.CreateTeams
365-
? $"gh ado2gh add-team-to-repo --github-org \"{githubOrg}\" --github-repo \"{githubRepo}\" --team \"{adoTeamProject.ReplaceInvalidCharactersWithDash()}-Maintainers\" --role \"maintain\"{(_log.Verbose ? " --verbose" : string.Empty)}"
365+
? $"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)}"
366366
: null;
367367

368-
private string AddAdminsToGithubRepoScript(string adoTeamProject, string githubOrg, string githubRepo) =>
368+
private string AddAdminsToGithubRepoScript(string adoTeamProject, string githubOrg, string githubRepo, string targetApiUrl) =>
369369
_generateScriptOptions.CreateTeams
370-
? $"gh ado2gh add-team-to-repo --github-org \"{githubOrg}\" --github-repo \"{githubRepo}\" --team \"{adoTeamProject.ReplaceInvalidCharactersWithDash()}-Admins\" --role \"admin\"{(_log.Verbose ? " --verbose" : string.Empty)}"
370+
? $"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)}"
371371
: null;
372372

373373
private string RewireAzurePipelineScript(string adoOrg, string adoTeamProject, string adoPipeline, string githubOrg, string githubRepo, string appId) =>

0 commit comments

Comments
 (0)