Skip to content

Commit 66fde68

Browse files
authored
Merge pull request #1294 from github/add-git-archive-file-git-metadata-file-options
Add --git-archive-path and --metadata-archive-path options
2 parents 236b3fe + c6d693a commit 66fde68

File tree

11 files changed

+623
-514
lines changed

11 files changed

+623
-514
lines changed

RELEASENOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
- Adds `--git-archive-path` and `--metadata-archive-path` options to `gh gei migrate-repo` for uploading (to selected storage) and migrating

src/Octoshift/Extensions/EnumerableExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Text;
45
using System.Threading.Tasks;
56

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

2526
public static IEnumerable<T> ToEmptyEnumerableIfNull<T>(this IEnumerable<T> enumerable) => enumerable ?? Enumerable.Empty<T>();
27+
28+
public static string GetString(this byte[] bytes) => Encoding.UTF8.GetString(bytes.ToArray());
2629
}
2730
}

src/Octoshift/Extensions/StringExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ public static class StringExtensions
2424
public static string ToUnixPath(this string path) => path?.Replace("\\", "/");
2525

2626
public static string EscapeDataString(this string value) => Uri.EscapeDataString(value);
27+
28+
public static byte[] ToBytes(this string s) => Encoding.UTF8.GetBytes(s);
2729
}
2830
}

src/Octoshift/Services/FileSystemProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class FileSystemProvider
1515

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

18-
public virtual FileStream OpenRead(string path) => File.OpenRead(path);
18+
public virtual Stream OpenRead(string path) => File.OpenRead(path);
1919

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

src/OctoshiftCLI.Tests/bbs2gh/Commands/MigrateRepo/MigrateRepoCommandHandlerTests.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Moq;
77
using OctoshiftCLI.BbsToGithub.Commands.MigrateRepo;
88
using OctoshiftCLI.BbsToGithub.Services;
9+
using OctoshiftCLI.Extensions;
910
using OctoshiftCLI.Services;
1011
using Xunit;
1112

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

354-
await File.WriteAllTextAsync(gitArchiveFilePath, "I am git archive");
355-
await using var gitContentStream = File.OpenRead(gitArchiveFilePath);
356+
await using var gitContentStream = new MemoryStream(gitArchiveContents.ToBytes());
356357

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

359360
_mockGithubApi.Setup(x => x.GetOrganizationId(GITHUB_ORG).Result).Returns(GITHUB_ORG_ID);
360361
_mockGithubApi.Setup(x => x.CreateBbsMigrationSource(GITHUB_ORG_ID).Result).Returns(MIGRATION_SOURCE_ID);
361362
_mockGithubApi.Setup(x => x.GetOrganizationDatabaseId(GITHUB_ORG).Result).Returns(githubOrgDatabaseId);
362363
_mockGithubApi
363-
.Setup(x => x.UploadArchiveToGithubStorage(githubOrgDatabaseId, It.IsAny<string>(), gitContentStream).Result)
364+
.Setup(x => x.UploadArchiveToGithubStorage(
365+
githubOrgDatabaseId,
366+
It.IsAny<string>(),
367+
It.Is<Stream>(s => (s as MemoryStream).ToArray().GetString() == gitArchiveContents)).Result)
364368
.Returns(gitArchiveUrl);
365369

366370
// Act

src/OctoshiftCLI.Tests/gei/Commands/MigrateRepo/MigrateRepoCommandArgsTests.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public class MigrateRepoCommandArgsTests
1616
private const string GITHUB_TARGET_PAT = "github-target-pat";
1717
private const string AWS_BUCKET_NAME = "aws-bucket-name";
1818
private const string GHES_API_URL = "foo-ghes-api.com";
19+
private const string GIT_ARCHIVE_URL = "http://host/git-archive.tar.gz";
20+
private const string METADATA_ARCHIVE_URL = "http://host/metadata-archive.tar.gz";
21+
private const string GIT_ARCHIVE_PATH = "./git-archive.tar.gz";
22+
private const string METADATA_ARCHIVE_PATH = "./metadata-archive.tar.gz";
1923

2024
[Fact]
2125
public void Defaults_TargetRepo_To_SourceRepo()
@@ -124,6 +128,7 @@ public void It_Throws_When_Aws_Bucket_Name_Provided_With_AzureStorageConnectionS
124128
.ThrowExactly<OctoshiftCliException>()
125129
.WithMessage("*--use-github-storage flag*");
126130
}
131+
127132
[Fact]
128133
public void No_Ssl_Verify_Without_Ghes_Api_Url_Throws()
129134
{
@@ -159,5 +164,79 @@ public void Keep_Archive_Without_Ghes_Api_Url_Throws()
159164
.ThrowExactly<OctoshiftCliException>()
160165
.WithMessage("*--keep-archive*");
161166
}
167+
168+
[Fact]
169+
public void GitArchivePath_Without_MetadataArchivePath_Throws()
170+
{
171+
var args = new MigrateRepoCommandArgs
172+
{
173+
SourceRepo = SOURCE_REPO,
174+
GithubSourceOrg = SOURCE_ORG,
175+
GithubTargetOrg = TARGET_ORG,
176+
TargetRepo = TARGET_REPO,
177+
GitArchivePath = GIT_ARCHIVE_PATH
178+
};
179+
180+
FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
181+
.Should()
182+
.ThrowExactly<OctoshiftCliException>()
183+
.WithMessage("*you must provide both --git-archive-path --metadata-archive-path*");
184+
}
185+
186+
[Fact]
187+
public void MetadataArchivePath_Without_GitArchivePath_Throws()
188+
{
189+
var args = new MigrateRepoCommandArgs
190+
{
191+
SourceRepo = SOURCE_REPO,
192+
GithubSourceOrg = SOURCE_ORG,
193+
GithubTargetOrg = TARGET_ORG,
194+
TargetRepo = TARGET_REPO,
195+
MetadataArchivePath = METADATA_ARCHIVE_PATH
196+
};
197+
198+
FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
199+
.Should()
200+
.ThrowExactly<OctoshiftCliException>()
201+
.WithMessage("*you must provide both --git-archive-path --metadata-archive-path*");
202+
}
203+
204+
[Fact]
205+
public void GitArchiveUrl_With_GitArchivePath_Throws()
206+
{
207+
var args = new MigrateRepoCommandArgs
208+
{
209+
SourceRepo = SOURCE_REPO,
210+
GithubSourceOrg = SOURCE_ORG,
211+
GithubTargetOrg = TARGET_ORG,
212+
TargetRepo = TARGET_REPO,
213+
GitArchiveUrl = GIT_ARCHIVE_URL,
214+
GitArchivePath = GIT_ARCHIVE_PATH
215+
};
216+
217+
FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
218+
.Should()
219+
.ThrowExactly<OctoshiftCliException>()
220+
.WithMessage("*--git-archive-url and --git-archive-path may not be used together*");
221+
}
222+
223+
[Fact]
224+
public void MetadataArchiveUrl_With_MetadataArchivePath_Throws()
225+
{
226+
var args = new MigrateRepoCommandArgs
227+
{
228+
SourceRepo = SOURCE_REPO,
229+
GithubSourceOrg = SOURCE_ORG,
230+
GithubTargetOrg = TARGET_ORG,
231+
TargetRepo = TARGET_REPO,
232+
MetadataArchiveUrl = METADATA_ARCHIVE_URL,
233+
MetadataArchivePath = METADATA_ARCHIVE_PATH
234+
};
235+
236+
FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
237+
.Should()
238+
.ThrowExactly<OctoshiftCliException>()
239+
.WithMessage("*--metadata-archive-url and --metadata-archive-path may not be used together*");
240+
}
162241
}
163242
}

0 commit comments

Comments
 (0)