Skip to content

Commit

Permalink
Merge pull request #1191 from github/feature/abort-migration
Browse files Browse the repository at this point in the history
Feature/abort migration
  • Loading branch information
begonaguereca authored Dec 15, 2023
2 parents 142c9f8 + b463368 commit fffd8f8
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 33 deletions.
2 changes: 2 additions & 0 deletions releasenotes/v1.7.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Release Notes:
- Added `abort-migration` command to `gh ado2gh`, `gh bbs2gh` and `gh gei` that will abort a migration that is queued or in-progress.
66 changes: 36 additions & 30 deletions src/Octoshift/Services/GithubApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,42 @@ public virtual async Task<string> GetEnterpriseServerVersion()
return (string)data["installed_version"];
}

public virtual async Task<bool> AbortMigration(string migrationId)
{
var url = $"{_apiUrl}/graphql";

var query = @"
mutation abortRepositoryMigration(
$migrationId: ID!,
)";
var gql = @"
abortRepositoryMigration(
input: {
migrationId: $migrationId
})
{ success }";

var payload = new
{
query = $"{query} {{ {gql} }}",
variables = new
{
migrationId,
},
operationName = "abortRepositoryMigration"
};

try
{
var data = await _client.PostGraphQLAsync(url, payload);
return (bool)data["data"]["abortRepositoryMigration"]["success"];
}
catch (OctoshiftCliException ex) when (ex.Message.Contains("Could not resolve to a node", StringComparison.OrdinalIgnoreCase))
{
throw new OctoshiftCliException($"Invalid migration id: {migrationId}", ex);
}
}

private static object GetMannequinsPayload(string orgId)
{
var query = "query($id: ID!, $first: Int, $after: String)";
Expand Down Expand Up @@ -1142,34 +1178,4 @@ private static CodeScanningAlertInstance BuildCodeScanningAlertInstance(JToken s
StartColumn = (int)scanningAlertInstance["location"]["start_column"],
EndColumn = (int)scanningAlertInstance["location"]["end_column"]
};

public virtual async Task<bool> AbortMigration(string migrationSourceId)
{
var url = $"{_apiUrl}/graphql";

var query = @"
mutation abortRepositoryMigration(
$migrationId: ID!,
)";
var gql = @"
abortRepositoryMigration(
input: {
migrationId: $migrationId
})
{ success }";

var payload = new
{
query = $"{query} {{ {gql} }}",
variables = new
{
migrationId = migrationSourceId,
},
operationName = "abortRepositoryMigration"
};

var data = await _client.PostGraphQLAsync(url, payload);

return (bool)data["data"]["abortRepositoryMigration"]["success"];
}
}
24 changes: 21 additions & 3 deletions src/OctoshiftCLI.Tests/Octoshift/Services/GithubApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3328,7 +3328,7 @@ public async Task AbortMigration_Returns_True_On_Success()
{
// Arrange
const string url = "https://api.github.com/graphql";
const string migrationSourceId = "MIGRATION_SOURCE_ID";
const string migrationId = "MIGRATION_ID";

const string query = @"
mutation abortRepositoryMigration(
Expand All @@ -3346,7 +3346,7 @@ mutation abortRepositoryMigration(
query = $"{query} {{ {gql} }}",
variables = new
{
migrationId = migrationSourceId,
migrationId,
},
operationName = "abortRepositoryMigration"
};
Expand All @@ -3368,12 +3368,30 @@ mutation abortRepositoryMigration(
.ReturnsAsync(response);

// Act
var expectedBooleanResponse = await _githubApi.AbortMigration(migrationSourceId);
var expectedBooleanResponse = await _githubApi.AbortMigration(migrationId);

// Assert
expectedBooleanResponse.Should().Be(actualBooleanResponse);
}

[Fact]
public async Task AbortMigration_Surfaces_Error_With_Incorrect_Migration_ID()
{
// Arrange
const string migrationId = "1234";
const string expectedErrorMessage = $"Invalid migration id: {migrationId}";

_githubClientMock
.Setup(m => m.PostGraphQLAsync(It.IsAny<string>(), It.IsAny<object>(), null))
.ThrowsAsync(new OctoshiftCliException("Could not resolve to a node"));

// Act, Assert
await _githubApi.Invoking(api => api.AbortMigration(migrationId))
.Should()
.ThrowExactlyAsync<OctoshiftCliException>()
.WithMessage(expectedErrorMessage);
}

private string Compact(string source) =>
source
.Replace("\r", "")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using FluentAssertions;
using OctoshiftCLI.AdoToGithub.Commands.AbortMigration;
using Xunit;

namespace OctoshiftCLI.Tests.AdoToGithub.Commands.AbortMigration;

public class AbortMigrationCommandTests
{
[Fact]
public void Should_Have_Options()
{
var command = new AbortMigrationCommand();
command.Should().NotBeNull();
command.Name.Should().Be("abort-migration");
command.Options.Count.Should().Be(3);

TestHelpers.VerifyCommandOption(command.Options, "migration-id", true);
TestHelpers.VerifyCommandOption(command.Options, "github-pat", false);
TestHelpers.VerifyCommandOption(command.Options, "verbose", false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using FluentAssertions;
using OctoshiftCLI.BbsToGithub.Commands.AbortMigration;
using Xunit;

namespace OctoshiftCLI.Tests.BbsToGithub.Commands.AbortMigration;

public class AbortMigrationCommandTests
{
[Fact]
public void Should_Have_Options()
{
var command = new AbortMigrationCommand();
command.Should().NotBeNull();
command.Name.Should().Be("abort-migration");
command.Options.Count.Should().Be(3);

TestHelpers.VerifyCommandOption(command.Options, "migration-id", true);
TestHelpers.VerifyCommandOption(command.Options, "github-pat", false);
TestHelpers.VerifyCommandOption(command.Options, "verbose", false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using FluentAssertions;
using OctoshiftCLI.GithubEnterpriseImporter.Commands.AbortMigration;
using Xunit;

namespace OctoshiftCLI.Tests.GithubEnterpriseImporter.Commands.AbortMigration;

public class AbortMigrationCommandTests
{
[Fact]
public void Should_Have_Options()
{
var command = new AbortMigrationCommand();
command.Should().NotBeNull();
command.Name.Should().Be("abort-migration");
command.Options.Count.Should().Be(3);

TestHelpers.VerifyCommandOption(command.Options, "migration-id", true);
TestHelpers.VerifyCommandOption(command.Options, "github-pat", false);
TestHelpers.VerifyCommandOption(command.Options, "verbose", false);
}
}
8 changes: 8 additions & 0 deletions src/ado2gh/Commands/AbortMigration/AbortMigrationCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using OctoshiftCLI.Commands.AbortMigration;

namespace OctoshiftCLI.AdoToGithub.Commands.AbortMigration;

public sealed class AbortMigrationCommand : AbortMigrationCommandBase
{
public AbortMigrationCommand() : base() => AddOptions();
}
8 changes: 8 additions & 0 deletions src/bbs2gh/Commands/AbortMigration/AbortMigrationCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using OctoshiftCLI.Commands.AbortMigration;

namespace OctoshiftCLI.BbsToGithub.Commands.AbortMigration;

public sealed class AbortMigrationCommand : AbortMigrationCommandBase
{
public AbortMigrationCommand() : base() => AddOptions();
}
8 changes: 8 additions & 0 deletions src/gei/Commands/AbortMigration/AbortMigrationCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using OctoshiftCLI.Commands.AbortMigration;

namespace OctoshiftCLI.GithubEnterpriseImporter.Commands.AbortMigration;

public sealed class AbortMigrationCommand : AbortMigrationCommandBase
{
public AbortMigrationCommand() : base() => AddOptions();
}

0 comments on commit fffd8f8

Please sign in to comment.