Skip to content

Commit 029ac19

Browse files
authored
Merge pull request #1591 from jessehouwing/fix/rewire-pipeline-ghecom
Fix rewire-pipeline command to include target API URL and add unit test for sequential script rewire
2 parents ec62c85 + b3743f4 commit 029ac19

3 files changed

Lines changed: 70 additions & 4 deletions

File tree

RELEASENOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fixed a bug where `ado2gh generate-script` did not pass `--target-api-url` to the generated `rewire-pipeline` commands. When migrating to GitHub Enterprise Cloud with data residency (ghe.com), rewired Azure Pipelines were incorrectly pointed at github.com URLs instead of the data residency domain.

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,39 @@ public async Task SequentialScript_Single_Repo_Two_Pipelines_All_Options()
364364
_scriptOutput.Should().Be(expected.ToString());
365365
}
366366

367+
[Fact]
368+
public async Task SequentialScript_Rewire_Pipeline_With_TargetApiUrl_Includes_TargetApiUrl()
369+
{
370+
// Arrange
371+
var targetApiUrl = "https://api.tenant.ghe.com";
372+
373+
_mockAdoApi.Setup(m => m.GetTeamProjects(ADO_ORG)).ReturnsAsync(ADO_TEAM_PROJECTS);
374+
_mockAdoApi.Setup(m => m.GetGithubAppId(ADO_ORG, GITHUB_ORG, ADO_TEAM_PROJECTS)).ReturnsAsync(APP_ID);
375+
376+
_mockAdoInspector.Setup(m => m.GetRepoCount()).ReturnsAsync(1);
377+
_mockAdoInspector.Setup(m => m.GetOrgs()).ReturnsAsync(ADO_ORGS);
378+
_mockAdoInspector.Setup(m => m.GetTeamProjects(ADO_ORG)).ReturnsAsync(ADO_TEAM_PROJECTS);
379+
_mockAdoInspector.Setup(m => m.GetRepos(ADO_ORG, ADO_TEAM_PROJECT)).ReturnsAsync(ADO_REPOS);
380+
_mockAdoInspector.Setup(m => m.GetPipelines(ADO_ORG, ADO_TEAM_PROJECT, FOO_REPO)).ReturnsAsync(ADO_PIPELINES);
381+
382+
var expectedRewire = $"Exec {{ gh ado2gh rewire-pipeline --target-api-url \"{targetApiUrl}\" --ado-org \"{ADO_ORG}\" --ado-team-project \"{ADO_TEAM_PROJECT}\" --ado-pipeline \"{FOO_PIPELINE}\" --github-org \"{GITHUB_ORG}\" --github-repo \"{ADO_TEAM_PROJECT}-{FOO_REPO}\" --service-connection-id \"{APP_ID}\" }}";
383+
384+
// Act
385+
var args = new GenerateScriptCommandArgs
386+
{
387+
GithubOrg = GITHUB_ORG,
388+
AdoOrg = ADO_ORG,
389+
Sequential = true,
390+
Output = new FileInfo("unit-test-output"),
391+
RewirePipelines = true,
392+
TargetApiUrl = targetApiUrl
393+
};
394+
await _handler.Handle(args);
395+
396+
// Assert
397+
_scriptOutput.Should().Contain(expectedRewire);
398+
}
399+
367400
[Fact]
368401
public async Task SequentialScript_Single_Repo_Two_Pipelines_No_Service_Connection_All_Options()
369402
{
@@ -1331,6 +1364,38 @@ public async Task ParallelScript_Rewire_Pipelines_Option_Should_Generate_Share_S
13311364
_scriptOutput.Should().Be(expected.ToString());
13321365
}
13331366

1367+
[Fact]
1368+
public async Task ParallelScript_Rewire_Pipeline_With_TargetApiUrl_Includes_TargetApiUrl()
1369+
{
1370+
// Arrange
1371+
var targetApiUrl = "https://api.tenant.ghe.com";
1372+
1373+
_mockAdoApi.Setup(m => m.GetTeamProjects(ADO_ORG)).ReturnsAsync(ADO_TEAM_PROJECTS);
1374+
_mockAdoApi.Setup(m => m.GetGithubAppId(ADO_ORG, GITHUB_ORG, new[] { ADO_TEAM_PROJECT })).ReturnsAsync(APP_ID);
1375+
1376+
_mockAdoInspector.Setup(m => m.GetRepoCount()).ReturnsAsync(1);
1377+
_mockAdoInspector.Setup(m => m.GetOrgs()).ReturnsAsync(ADO_ORGS);
1378+
_mockAdoInspector.Setup(m => m.GetTeamProjects(ADO_ORG)).ReturnsAsync(ADO_TEAM_PROJECTS);
1379+
_mockAdoInspector.Setup(m => m.GetRepos(ADO_ORG, ADO_TEAM_PROJECT)).ReturnsAsync(ADO_REPOS);
1380+
_mockAdoInspector.Setup(m => m.GetPipelines(ADO_ORG, ADO_TEAM_PROJECT, FOO_REPO)).ReturnsAsync(ADO_PIPELINES);
1381+
1382+
var expectedRewireWithUrl = $"{{ gh ado2gh rewire-pipeline --target-api-url \"{targetApiUrl}\" --ado-org \"{ADO_ORG}\" --ado-team-project \"{ADO_TEAM_PROJECT}\" --ado-pipeline \"{FOO_PIPELINE}\" --github-org \"{GITHUB_ORG}\" --github-repo \"{ADO_TEAM_PROJECT}-{FOO_REPO}\" --service-connection-id \"{APP_ID}\" }}";
1383+
1384+
// Act
1385+
var args = new GenerateScriptCommandArgs
1386+
{
1387+
GithubOrg = GITHUB_ORG,
1388+
AdoOrg = ADO_ORG,
1389+
Output = new FileInfo("unit-test-output"),
1390+
RewirePipelines = true,
1391+
TargetApiUrl = targetApiUrl
1392+
};
1393+
await _handler.Handle(args);
1394+
1395+
// Assert
1396+
_scriptOutput.Should().Contain(expectedRewireWithUrl);
1397+
}
1398+
13341399
[Fact]
13351400
public async Task SequentialScript_CreateTeams_With_TargetApiUrl_Should_Include_TargetApiUrl_In_AddTeamToRepo_Commands()
13361401
{

src/ado2gh/Commands/GenerateScript/GenerateScriptCommandHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private async Task<string> GenerateSequentialScript(IDictionary<string, string>
172172

173173
foreach (var adoPipeline in await _adoInspectorService.GetPipelines(adoOrg, adoTeamProject, adoRepo.Name))
174174
{
175-
AppendLine(content, Exec(RewireAzurePipelineScript(adoOrg, adoTeamProject, adoPipeline, githubOrg, githubRepo, appId)));
175+
AppendLine(content, Exec(RewireAzurePipelineScript(adoOrg, adoTeamProject, adoPipeline, githubOrg, githubRepo, appId, targetApiUrl)));
176176
}
177177
}
178178
}
@@ -282,7 +282,7 @@ private async Task<string> GenerateParallelScript(IDictionary<string, string> ap
282282
appIds.TryGetValue(adoOrg, out var appId);
283283
foreach (var adoPipeline in await _adoInspectorService.GetPipelines(adoOrg, adoTeamProject, adoRepo.Name))
284284
{
285-
AppendLine(content, " " + Wrap(RewireAzurePipelineScript(adoOrg, adoTeamProject, adoPipeline, githubOrg, githubRepo, appId)));
285+
AppendLine(content, " " + Wrap(RewireAzurePipelineScript(adoOrg, adoTeamProject, adoPipeline, githubOrg, githubRepo, appId, targetApiUrl)));
286286
}
287287

288288
AppendLine(content, " )");
@@ -370,9 +370,9 @@ private string AddAdminsToGithubRepoScript(string adoTeamProject, string githubO
370370
? $"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

373-
private string RewireAzurePipelineScript(string adoOrg, string adoTeamProject, string adoPipeline, string githubOrg, string githubRepo, string appId) =>
373+
private string RewireAzurePipelineScript(string adoOrg, string adoTeamProject, string adoPipeline, string githubOrg, string githubRepo, string appId, string targetApiUrl) =>
374374
_generateScriptOptions.RewirePipelines && appId.HasValue()
375-
? $"gh ado2gh rewire-pipeline --ado-org \"{adoOrg}\" --ado-team-project \"{adoTeamProject}\" --ado-pipeline \"{adoPipeline}\" --github-org \"{githubOrg}\" --github-repo \"{githubRepo}\" --service-connection-id \"{appId}\"{(_log.Verbose ? " --verbose" : string.Empty)}"
375+
? $"gh ado2gh rewire-pipeline{(targetApiUrl.HasValue() ? $" --target-api-url \"{targetApiUrl}\"" : string.Empty)} --ado-org \"{adoOrg}\" --ado-team-project \"{adoTeamProject}\" --ado-pipeline \"{adoPipeline}\" --github-org \"{githubOrg}\" --github-repo \"{githubRepo}\" --service-connection-id \"{appId}\"{(_log.Verbose ? " --verbose" : string.Empty)}"
376376
: null;
377377

378378
private string WaitForMigrationScript(string repoMigrationKey, string targetApiUrl) => $"gh ado2gh wait-for-migration{(targetApiUrl.HasValue() ? $" --target-api-url \"{targetApiUrl}\"" : string.Empty)} --migration-id $RepoMigrations[\"{repoMigrationKey}\"]";

0 commit comments

Comments
 (0)