Skip to content
Closed
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
93 changes: 93 additions & 0 deletions src/Octoshift/GithubApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Octoshift.Models;
Expand Down Expand Up @@ -158,6 +159,25 @@ public virtual async Task<string> GetOrganizationId(string org)
return (string)data["data"]["organization"]["id"];
}

public virtual async Task<string> GetRepositoryId(string org, string repo)
{
var url = $"{_apiUrl}/graphql";

var payload = new
{
// TODO: this is super ugly, need to find a graphql library to make this code nicer
query = "query repository($owner: String!, $name: String!) { repository(name: $name, owner: $owner) { id } }",
variables = new { owner = org, name = repo }
};

var response = await _client.PostAsync(url, payload);
var data = JObject.Parse(response);

CheckForErrors(data);

return (string)data["data"]!["repository"]!["id"];
}

public virtual async Task<string> CreateAdoMigrationSource(string orgId, string adoServerUrl)
{
var url = $"{_apiUrl}/graphql";
Expand Down Expand Up @@ -617,5 +637,78 @@ private static Mannequin BuildMannequin(JToken mannequin)
: null
};
}

public virtual async Task ArchiveRepository(string sourceOrg, string sourceRepo)
{
var repositoryId = await GetRepositoryId(sourceOrg, sourceRepo);

var url = $"{_apiUrl}/graphql";

var query = "mutation archiveRepository($repoId: ID!)";
var gql = "archiveRepository(input: {repositoryId: $repoId}) { clientMutationId }";

var payload = new
{
query = $"{query} {{ {gql} }}",
variables = new
{
repoId = repositoryId
},
operationName = "archiveRepository"
};

var response = await _client.PostAsync(url, payload);
var data = JObject.Parse(response);

CheckForErrors(data);
}

public virtual async Task<bool> IsRepoArchived(string org, string repo)
{
var url = $"{_apiUrl}/graphql";

var payload = new
{
// TODO: this is super ugly, need to find a graphql library to make this code nicer
query = "query repository($owner: String!, $name: String!) { repository(name: $name, owner: $owner) { isArchived } }",
variables = new { owner = org, name = repo }
};

var response = await _client.PostAsync(url, payload);
var data = JObject.Parse(response);

CheckForErrors(data);

return (bool)data["data"]!["repository"]!["isArchived"];
}

protected void CheckForErrors(JObject responseJObject)
{
if (responseJObject == null)
{
throw new OctoshiftCliException("Response from API was not valid");
}

responseJObject.TryGetValue("errors", StringComparison.InvariantCultureIgnoreCase, out var errors);

if (errors == null)
{
return;
}

var errorMessageStringBuilder = new StringBuilder();

var messages = errors
.Where(error => error["message"] != null)
.Select(error => error["message"])
.ToList();

foreach (var message in messages)
{
errorMessageStringBuilder.AppendLine(message.ToString());

Check notice

Code scanning / CodeQL

Redundant ToString() call

Redundant call to 'ToString'.
}

throw new OctoshiftCliException(errorMessageStringBuilder.ToString());
}
}
}
32 changes: 32 additions & 0 deletions src/OctoshiftCLI.IntegrationTests/GithubToGithub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,38 @@ public GithubToGithub(ITestOutputHelper output)
_helper = new TestHelper(_output, _githubApi, _githubClient);
}

[Fact]
public async Task ArchiveSourceGhecRepo()
{
var githubSourceOrg = $"e2e-testing-source-{TestHelper.GetOsName()}";
var githubTargetOrg = $"e2e-testing-{TestHelper.GetOsName()}";
var repo1 = "repo-1";
var repo2 = "repo-2";

await _helper.ResetGithubTestEnvironment(githubSourceOrg);
await _helper.ResetGithubTestEnvironment(githubTargetOrg);

await _helper.CreateGithubRepo(githubSourceOrg, repo1);
await _helper.CreateGithubRepo(githubSourceOrg, repo2);

await _helper.RunGeiCliMigration($"generate-script --github-source-org {githubSourceOrg} --github-target-org {githubTargetOrg} --archive-source-gh-repos --download-migration-logs", _tokens);

await _helper.AssertGithubRepoExists(githubTargetOrg, repo1);
await _helper.AssertGithubRepoExists(githubTargetOrg, repo2);

await _helper.AssertGithubRepoInitialized(githubTargetOrg, repo1);
await _helper.AssertGithubRepoInitialized(githubTargetOrg, repo2);

_helper.AssertMigrationLogFileExists(githubTargetOrg, repo1);
_helper.AssertMigrationLogFileExists(githubTargetOrg, repo2);

// Doing this last ensures enough time has passed since
// the generate-script command started to allow for the
// archive repo API call to run and complete
await _helper.AssertGithubRepoIsArchived(githubSourceOrg, repo1);
await _helper.AssertGithubRepoIsArchived(githubSourceOrg, repo2);
}

[Fact]
public async Task Basic()
{
Expand Down
9 changes: 9 additions & 0 deletions src/OctoshiftCLI.IntegrationTests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,15 @@ public void AssertMigrationLogFileExists(string githubOrg, string repo)
File.Exists(migrationLogFile).Should().BeTrue();
}

public async Task AssertGithubRepoIsArchived(string githubOrg, string repo)
{
_output.WriteLine("Checking that the repo is archived...");

var isRepoArchived = await _githubApi.IsRepoArchived(githubOrg, repo);

isRepoArchived.Should().BeTrue();
}

public async Task ResetBlobContainers()
{
_output.WriteLine($"Deleting all blob containers...");
Expand Down
114 changes: 114 additions & 0 deletions src/OctoshiftCLI.Tests/gei/Commands/ArchiveGitHubRepoCommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using OctoshiftCLI.GithubEnterpriseImporter;
using OctoshiftCLI.GithubEnterpriseImporter.Commands;
using Xunit;

// ReSharper disable once CheckNamespace
namespace OctoshiftCLI.Tests.GithubEnterpriseImporter.Commands;

public class ArchiveGitHubRepoCommandTests
{
public ArchiveGitHubRepoCommandTests() =>
_command = new ArchiveGitHubRepoCommand(
_mockOctoLogger.Object,
_mockSourceGithubApiFactory.Object,
_mockEnvironmentVariableProvider.Object);

private readonly Mock<GithubApi> _mockGithubApi = TestHelpers.CreateMock<GithubApi>();
private readonly Mock<ISourceGithubApiFactory> _mockSourceGithubApiFactory = new();
private readonly Mock<EnvironmentVariableProvider> _mockEnvironmentVariableProvider = TestHelpers.CreateMock<EnvironmentVariableProvider>();
private readonly Mock<OctoLogger> _mockOctoLogger = TestHelpers.CreateMock<OctoLogger>();
private readonly ArchiveGitHubRepoCommand _command;

[Fact]
public async Task Happy_Path_For_Ghec()
{
var sourceOrg = Guid.NewGuid().ToString();
var sourceRepo = Guid.NewGuid().ToString();
var sourceGithubPat = Guid.NewGuid().ToString();

_mockGithubApi.Setup(_ => _.ArchiveRepository(sourceOrg, sourceRepo)).Verifiable("`ArchiveRepository` call did not match specifications");

_mockSourceGithubApiFactory
.Setup(_ => _.Create(null, sourceGithubPat))
.Returns(_mockGithubApi.Object);

var args = new ArchiveGitHubRepoCommandArgs
{
GithubSourceOrg = sourceOrg,
GithubSourcePat = sourceGithubPat,
SourceRepo = sourceRepo
};

await _command.Invoke(args);
}

[Fact]
public async Task Happy_Path_For_Ghes()
{
var ghesApiUrl = "https://myghes.local/api/v3";
var sourceOrg = Guid.NewGuid().ToString();
var sourceRepo = Guid.NewGuid().ToString();
var sourceGithubPat = Guid.NewGuid().ToString();

_mockGithubApi.Setup(_ => _.ArchiveRepository(sourceOrg, sourceRepo)).Verifiable("`ArchiveRepository` call did not match specifications");

_mockSourceGithubApiFactory
.Setup(_ => _.Create(ghesApiUrl, sourceGithubPat))
.Returns(_mockGithubApi.Object);

var args = new ArchiveGitHubRepoCommandArgs
{
GhesApiUrl = ghesApiUrl,
GithubSourceOrg = sourceOrg,
GithubSourcePat = sourceGithubPat,
SourceRepo = sourceRepo
};

await _command.Invoke(args);
}

[Fact]
public async Task Happy_Path_For_Ghes_With_No_Ssl_Verify()
{
var ghesApiUrl = "https://myghes.local/api/v3";
var sourceOrg = Guid.NewGuid().ToString();
var sourceRepo = Guid.NewGuid().ToString();
var sourceGithubPat = Guid.NewGuid().ToString();

_mockGithubApi.Setup(_ => _.ArchiveRepository(sourceOrg, sourceRepo)).Verifiable("`ArchiveRepository` call did not match specifications");

_mockSourceGithubApiFactory
.Setup(_ => _.CreateClientNoSsl(ghesApiUrl, sourceGithubPat))
.Returns(_mockGithubApi.Object);

var args = new ArchiveGitHubRepoCommandArgs
{
GhesApiUrl = ghesApiUrl,
GithubSourceOrg = sourceOrg,
GithubSourcePat = sourceGithubPat,
SourceRepo = sourceRepo,
NoSslVerify = true
};

await _command.Invoke(args);
}

[Fact]
public void Should_Have_Options()
{
_command.Should().NotBeNull();
_command.Name.Should().Be("archive-gh-repo");
_command.Options.Count.Should().Be(6);

TestHelpers.VerifyCommandOption(_command.Options, "github-source-org", true);
TestHelpers.VerifyCommandOption(_command.Options, "github-source-pat", false);
TestHelpers.VerifyCommandOption(_command.Options, "source-repo", true);
TestHelpers.VerifyCommandOption(_command.Options, "ghes-api-url", false);
TestHelpers.VerifyCommandOption(_command.Options, "no-ssl-verify", false);
TestHelpers.VerifyCommandOption(_command.Options, "verbose", false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void Should_Have_Options()
{
_command.Should().NotBeNull();
_command.Name.Should().Be("generate-script");
_command.Options.Count.Should().Be(16);
_command.Options.Count.Should().Be(17);

TestHelpers.VerifyCommandOption(_command.Options, "github-source-org", false);
TestHelpers.VerifyCommandOption(_command.Options, "ado-server-url", false, true);
Expand All @@ -70,6 +70,7 @@ public void Should_Have_Options()
TestHelpers.VerifyCommandOption(_command.Options, "github-source-pat", false);
TestHelpers.VerifyCommandOption(_command.Options, "ado-pat", false);
TestHelpers.VerifyCommandOption(_command.Options, "verbose", false);
TestHelpers.VerifyCommandOption(_command.Options, "archive-source-gh-repos", false);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ public async Task Ghes_AzureConnectionString_Uses_Env_When_Option_Empty()
authenticatedMetadataArchiveUrl.ToString(),
false).Result)
.Returns(migrationId);

_mockGithubApi.Setup(x => x.GetMigration(migrationId).Result).Returns((State: RepositoryMigrationStatus.Succeeded, TARGET_REPO, null));

_mockGithubApi.Setup(x => x.StartGitArchiveGeneration(SOURCE_ORG, SOURCE_REPO).Result).Returns(gitArchiveId);
Expand Down Expand Up @@ -673,6 +674,7 @@ public async Task Ghes_With_NoSslVerify_Uses_NoSsl_Client()
authenticatedMetadataArchiveUrl.ToString(),
false).Result)
.Returns(migrationId);

_mockGithubApi.Setup(x => x.GetMigration(migrationId).Result).Returns((State: RepositoryMigrationStatus.Succeeded, TARGET_REPO, null));

_mockGithubApi.Setup(x => x.StartGitArchiveGeneration(SOURCE_ORG, SOURCE_REPO).Result).Returns(gitArchiveId);
Expand Down
Loading