Skip to content

Commit a802f5c

Browse files
author
Arin Ghazarian
authored
Add githubPat to createMigrationSource mutation (#161)
1 parent 884f732 commit a802f5c

File tree

6 files changed

+52
-11
lines changed

6 files changed

+52
-11
lines changed

src/OctoshiftCLI.Tests/Commands/MigrateRepoCommandTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,20 @@ public async Task HappyPath()
3737
var githubOrgId = Guid.NewGuid().ToString();
3838
var migrationSourceId = Guid.NewGuid().ToString();
3939
var migrationId = Guid.NewGuid().ToString();
40+
var githubPat = Guid.NewGuid().ToString();
4041

4142
var mockGithub = new Mock<GithubApi>(null);
4243
mockGithub.Setup(x => x.GetOrganizationId(githubOrg).Result).Returns(githubOrgId);
43-
mockGithub.Setup(x => x.CreateMigrationSource(githubOrgId, adoToken).Result).Returns(migrationSourceId);
44+
mockGithub.Setup(x => x.CreateMigrationSource(githubOrgId, adoToken, githubPat).Result).Returns(migrationSourceId);
4445
mockGithub.Setup(x => x.StartMigration(migrationSourceId, adoRepoUrl, githubOrgId, githubRepo).Result).Returns(migrationId);
4546
mockGithub.Setup(x => x.GetMigrationState(migrationId).Result).Returns("SUCCEEDED");
4647

48+
var githubFactory = new Mock<GithubApiFactory>(mockGithub.Object);
49+
githubFactory.Setup(m => m.GetGithubToken()).Returns(githubPat);
50+
4751
using var adoFactory = new AdoApiFactory(adoToken);
48-
using var githubFactory = new GithubApiFactory(mockGithub.Object);
4952

50-
var command = new MigrateRepoCommand(new Mock<OctoLogger>().Object, adoFactory, githubFactory);
53+
var command = new MigrateRepoCommand(new Mock<OctoLogger>().Object, adoFactory, githubFactory.Object);
5154
await command.Invoke(adoOrg, adoTeamProject, adoRepo, githubOrg, githubRepo);
5255

5356
mockGithub.Verify(x => x.GetMigrationState(migrationId));
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Net.Http;
2+
using System.Threading.Tasks;
3+
using Moq;
4+
using Xunit;
5+
6+
namespace OctoshiftCLI.Tests
7+
{
8+
public class GithubApiTests
9+
{
10+
[Fact]
11+
public async Task CreateMigrationSourceReturnsNewMigrationSourceId()
12+
{
13+
// Arrange
14+
const string url = "https://api.github.com/graphql";
15+
const string orgId = "ORG_ID";
16+
const string adoToken = "ADO_TOKEN";
17+
const string githubPat = "GITHUB_PAT";
18+
var payload =
19+
"{\"query\":\"mutation createMigrationSource($name: String!, $url: String!, $ownerId: ID!, $accessToken: String!, $type: MigrationSourceType!, $githubPat: String!) " +
20+
"{ createMigrationSource(input: {name: $name, url: $url, ownerId: $ownerId, accessToken: $accessToken, type: $type, githubPat: $githubPat}) { migrationSource { id, name, url, type } } }\"" +
21+
$",\"variables\":{{\"name\":\"Azure DevOps Source\",\"url\":\"https://dev.azure.com\",\"ownerId\":\"{orgId}\",\"type\":\"AZURE_DEVOPS\",\"accessToken\":\"{adoToken}\", \"githubPat\":\"{githubPat}\"}},\"operationName\":\"createMigrationSource\"}}";
22+
const string migrationSourceId = "MS_kgC4NjFhOTVjOTc4ZTRhZjEwMDA5NjNhOTdm";
23+
var result = $"{{\"data\":{{\"createMigrationSource\":{{\"migrationSource\": {{\"id\":\"{migrationSourceId}\",\"name\":\"Azure Devops Source\",\"url\":\"https://dev.azure.com\",\"type\":\"AZURE_DEVOPS\"}}}}}}}}";
24+
25+
var githubClientMock = new Mock<GithubClient>(null, null);
26+
githubClientMock
27+
.Setup(m => m.PostAsync(url, It.Is<StringContent>(x => x.ReadAsStringAsync().Result == payload)))
28+
.ReturnsAsync(result);
29+
30+
// Act
31+
using var githubApi = new GithubApi(githubClientMock.Object);
32+
var id = await githubApi.CreateMigrationSource(orgId, adoToken, githubPat);
33+
34+
// Assert
35+
Assert.Equal(migrationSourceId, id);
36+
}
37+
}
38+
}

src/OctoshiftCLI/Commands/MigrateRepoCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public async Task Invoke(string adoOrg, string adoTeamProject, string adoRepo, s
6868

6969
using var github = _githubFactory.Create();
7070
var adoToken = _adoFactory.GetAdoToken();
71-
71+
var githubPat = _githubFactory.GetGithubToken();
7272
var githubOrgId = await github.GetOrganizationId(githubOrg);
73-
var migrationSourceId = await github.CreateMigrationSource(githubOrgId, adoToken);
73+
var migrationSourceId = await github.CreateMigrationSource(githubOrgId, adoToken, githubPat);
7474
var migrationId = await github.StartMigration(migrationSourceId, adoRepoUrl, githubOrgId, githubRepo);
7575

7676
var migrationState = await github.GetMigrationState(migrationId);

src/OctoshiftCLI/GithubApi.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ public virtual async Task<string> GetOrganizationId(string org)
9999
return (string)data["data"]["organization"]["id"];
100100
}
101101

102-
public virtual async Task<string> CreateMigrationSource(string orgId, string adoToken)
102+
public virtual async Task<string> CreateMigrationSource(string orgId, string adoToken, string githubPat)
103103
{
104104
var url = $"https://api.github.com/graphql";
105105

106-
var query = "mutation createMigrationSource($name: String!, $url: String!, $ownerId: ID!, $accessToken: String!, $type: MigrationSourceType!)";
107-
var gql = "createMigrationSource(input: {name: $name, url: $url, ownerId: $ownerId, accessToken: $accessToken, type: $type}) { migrationSource { id, name, url, type } }";
108-
var variables = $"{{\"name\":\"Azure DevOps Source\",\"url\":\"https://dev.azure.com\",\"ownerId\":\"{orgId}\",\"type\":\"AZURE_DEVOPS\",\"accessToken\":\"{adoToken}\"}}";
106+
var query = "mutation createMigrationSource($name: String!, $url: String!, $ownerId: ID!, $accessToken: String!, $type: MigrationSourceType!, $githubPat: String!)";
107+
var gql = "createMigrationSource(input: {name: $name, url: $url, ownerId: $ownerId, accessToken: $accessToken, type: $type, githubPat: $githubPat}) { migrationSource { id, name, url, type } }";
108+
var variables = $"{{\"name\":\"Azure DevOps Source\",\"url\":\"https://dev.azure.com\",\"ownerId\":\"{orgId}\",\"type\":\"AZURE_DEVOPS\",\"accessToken\":\"{adoToken}\", \"githubPat\":\"{githubPat}\"}}";
109109

110110
var payload = $"{{\"query\":\"{query} {{ {gql} }}\",\"variables\":{variables},\"operationName\":\"createMigrationSource\"}}";
111111
using var body = new StringContent(payload.ToString(), Encoding.UTF8, "application/json");

src/OctoshiftCLI/GithubApiFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public GithubApi Create()
2727
return _api;
2828
}
2929

30-
public string GetGithubToken()
30+
public virtual string GetGithubToken()
3131
{
3232
if (!string.IsNullOrWhiteSpace(_token))
3333
{

src/OctoshiftCLI/GithubClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task<string> GetAsync(string url)
3535
return content;
3636
}
3737

38-
public async Task<string> PostAsync(string url, HttpContent body)
38+
public virtual async Task<string> PostAsync(string url, HttpContent body)
3939
{
4040
url = url?.Replace(" ", "%20");
4141

0 commit comments

Comments
 (0)