Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --git-archive-path and --metadata-archive-path options #1294

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/Octoshift/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OctoshiftCLI.Extensions
Expand All @@ -23,5 +24,7 @@ public static async Task<int> Sum<T>(this IEnumerable<T> list, Func<T, Task<int>
}

public static IEnumerable<T> ToEmptyEnumerableIfNull<T>(this IEnumerable<T> enumerable) => enumerable ?? Enumerable.Empty<T>();

public static string GetString(this byte[] bytes) => Encoding.UTF8.GetString(bytes.ToArray());
}
}
2 changes: 2 additions & 0 deletions src/Octoshift/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ public static class StringExtensions
public static string ToUnixPath(this string path) => path?.Replace("\\", "/");

public static string EscapeDataString(this string value) => Uri.EscapeDataString(value);

public static byte[] ToBytes(this string s) => Encoding.UTF8.GetBytes(s);
}
}
2 changes: 1 addition & 1 deletion src/Octoshift/Services/FileSystemProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class FileSystemProvider

public virtual FileStream Open(string path, FileMode mode) => File.Open(path, mode);

public virtual FileStream OpenRead(string path) => File.OpenRead(path);
public virtual Stream OpenRead(string path) => File.OpenRead(path);

public virtual async Task WriteAllTextAsync(string path, string contents) => await File.WriteAllTextAsync(path, contents);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Moq;
using OctoshiftCLI.BbsToGithub.Commands.MigrateRepo;
using OctoshiftCLI.BbsToGithub.Services;
using OctoshiftCLI.Extensions;
using OctoshiftCLI.Services;
using Xunit;

Expand Down Expand Up @@ -350,17 +351,20 @@ public async Task Happy_Path_Uploads_To_Github_Storage()
var githubOrgDatabaseId = Guid.NewGuid().ToString();
const string gitArchiveFilePath = "./gitdata_archive";
const string gitArchiveUrl = "gei://archive/1";
const string gitArchiveContents = "I am git archive";

await File.WriteAllTextAsync(gitArchiveFilePath, "I am git archive");
await using var gitContentStream = File.OpenRead(gitArchiveFilePath);
await using var gitContentStream = new MemoryStream(gitArchiveContents.ToBytes());

_mockFileSystemProvider.Setup(m => m.OpenRead(gitArchiveFilePath)).Returns(gitContentStream);

_mockGithubApi.Setup(x => x.GetOrganizationId(GITHUB_ORG).Result).Returns(GITHUB_ORG_ID);
_mockGithubApi.Setup(x => x.CreateBbsMigrationSource(GITHUB_ORG_ID).Result).Returns(MIGRATION_SOURCE_ID);
_mockGithubApi.Setup(x => x.GetOrganizationDatabaseId(GITHUB_ORG).Result).Returns(githubOrgDatabaseId);
_mockGithubApi
.Setup(x => x.UploadArchiveToGithubStorage(githubOrgDatabaseId, It.IsAny<string>(), gitContentStream).Result)
.Setup(x => x.UploadArchiveToGithubStorage(
githubOrgDatabaseId,
It.IsAny<string>(),
It.Is<Stream>(s => (s as MemoryStream).ToArray().GetString() == gitArchiveContents)).Result)
.Returns(gitArchiveUrl);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public class MigrateRepoCommandArgsTests
private const string GITHUB_TARGET_PAT = "github-target-pat";
private const string AWS_BUCKET_NAME = "aws-bucket-name";
private const string GHES_API_URL = "foo-ghes-api.com";
private const string GIT_ARCHIVE_URL = "http://host/git-archive.tar.gz";
private const string METADATA_ARCHIVE_URL = "http://host/metadata-archive.tar.gz";
private const string GIT_ARCHIVE_PATH = "./git-archive.tar.gz";
private const string METADATA_ARCHIVE_PATH = "./metadata-archive.tar.gz";

[Fact]
public void Defaults_TargetRepo_To_SourceRepo()
Expand Down Expand Up @@ -124,6 +128,7 @@ public void It_Throws_When_Aws_Bucket_Name_Provided_With_AzureStorageConnectionS
.ThrowExactly<OctoshiftCliException>()
.WithMessage("*--use-github-storage flag*");
}

[Fact]
public void No_Ssl_Verify_Without_Ghes_Api_Url_Throws()
{
Expand Down Expand Up @@ -159,5 +164,79 @@ public void Keep_Archive_Without_Ghes_Api_Url_Throws()
.ThrowExactly<OctoshiftCliException>()
.WithMessage("*--keep-archive*");
}

[Fact]
public void GitArchivePath_Without_MetadataArchivePath_Throws()
{
var args = new MigrateRepoCommandArgs
{
SourceRepo = SOURCE_REPO,
GithubSourceOrg = SOURCE_ORG,
GithubTargetOrg = TARGET_ORG,
TargetRepo = TARGET_REPO,
GitArchivePath = GIT_ARCHIVE_PATH
};

FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
.Should()
.ThrowExactly<OctoshiftCliException>()
.WithMessage("*you must provide both --git-archive-path --metadata-archive-path*");
}

[Fact]
public void MetadataArchivePath_Without_GitArchivePath_Throws()
{
var args = new MigrateRepoCommandArgs
{
SourceRepo = SOURCE_REPO,
GithubSourceOrg = SOURCE_ORG,
GithubTargetOrg = TARGET_ORG,
TargetRepo = TARGET_REPO,
MetadataArchivePath = METADATA_ARCHIVE_PATH
};

FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
.Should()
.ThrowExactly<OctoshiftCliException>()
.WithMessage("*you must provide both --git-archive-path --metadata-archive-path*");
}

[Fact]
public void GitArchiveUrl_With_GitArchivePath_Throws()
{
var args = new MigrateRepoCommandArgs
{
SourceRepo = SOURCE_REPO,
GithubSourceOrg = SOURCE_ORG,
GithubTargetOrg = TARGET_ORG,
TargetRepo = TARGET_REPO,
GitArchiveUrl = GIT_ARCHIVE_URL,
GitArchivePath = GIT_ARCHIVE_PATH
};

FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
.Should()
.ThrowExactly<OctoshiftCliException>()
.WithMessage("*--git-archive-url and --git-archive-path may not be used together*");
}

[Fact]
public void MetadataArchiveUrl_With_MetadataArchivePath_Throws()
{
var args = new MigrateRepoCommandArgs
{
SourceRepo = SOURCE_REPO,
GithubSourceOrg = SOURCE_ORG,
GithubTargetOrg = TARGET_ORG,
TargetRepo = TARGET_REPO,
MetadataArchiveUrl = METADATA_ARCHIVE_URL,
MetadataArchivePath = METADATA_ARCHIVE_PATH
};

FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
.Should()
.ThrowExactly<OctoshiftCliException>()
.WithMessage("*--metadata-archive-url and --metadata-archive-path may not be used together*");
}
}
}
Loading
Loading