Skip to content

Commit b8d48da

Browse files
committed
Implements SHA and Format parameters for RepositoryClient.GetArchive
1 parent 87fed0b commit b8d48da

File tree

5 files changed

+114
-9
lines changed

5 files changed

+114
-9
lines changed

NGitLab.Mock/Clients/RepositoryClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
@@ -80,7 +80,7 @@ public void GetRawBlob(string sha, Action<Stream> parser)
8080
throw new NotImplementedException();
8181
}
8282

83-
public void GetArchive(Action<Stream> parser)
83+
public void GetArchive(Action<Stream> parser, string sha = null, string format = null)
8484
{
8585
throw new NotImplementedException();
8686
}

NGitLab.Tests/RepositoryClient/RepositoryClientTests.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,101 @@ public async Task GetCommitRefs(CommitRefType type)
353353
Assert.That(commitRefs, Is.Not.Empty);
354354
}
355355
}
356+
357+
[Test]
358+
[NGitLabRetry]
359+
public async Task GetArchiveWithoutOptionalParameters()
360+
{
361+
// Arrange
362+
using var context = await RepositoryClientTestsContext.CreateAsync(commitCount: 2);
363+
364+
// Act
365+
context.RepositoryClient.GetArchive((stream) => { });
366+
367+
// Assert
368+
var requestPathAndQuery = context.Context.LastRequest.RequestUri.PathAndQuery;
369+
370+
Assert.Multiple(() =>
371+
{
372+
Assert.That(requestPathAndQuery, Is.Not.Null);
373+
Assert.That(requestPathAndQuery.EndsWith("/archive", StringComparison.OrdinalIgnoreCase), Is.True);
374+
});
375+
}
376+
377+
[Test]
378+
[NGitLabRetry]
379+
public async Task GetArchiveAcceptsShaParameter()
380+
{
381+
// Arrange
382+
using var context = await RepositoryClientTestsContext.CreateAsync(commitCount: 2);
383+
var firstCommitId = context.Commits[0].Id.ToString();
384+
385+
// Act
386+
context.RepositoryClient.GetArchive((stream) => { }, sha: firstCommitId);
387+
388+
// Assert
389+
var requestPathAndQuery = context.Context.LastRequest.RequestUri.PathAndQuery;
390+
391+
Assert.Multiple(() =>
392+
{
393+
Assert.That(requestPathAndQuery, Is.Not.Null);
394+
Assert.That(requestPathAndQuery.Contains($"?sha={firstCommitId}", StringComparison.OrdinalIgnoreCase), Is.True);
395+
});
396+
}
397+
398+
[Test]
399+
[NGitLabRetry]
400+
public async Task GetArchiveAcceptsFormatParameter()
401+
{
402+
// Arrange
403+
using var context = await RepositoryClientTestsContext.CreateAsync(commitCount: 2);
404+
var format = ".zip";
405+
406+
// Act
407+
context.RepositoryClient.GetArchive((stream) => { }, format: format);
408+
409+
// Assert
410+
var requestPathAndQuery = context.Context.LastRequest.RequestUri.PathAndQuery;
411+
412+
Assert.Multiple(() =>
413+
{
414+
Assert.That(requestPathAndQuery, Is.Not.Null);
415+
Assert.That(requestPathAndQuery.Contains($"/archive{format}", StringComparison.OrdinalIgnoreCase), Is.True);
416+
});
417+
}
418+
419+
[Test]
420+
[NGitLabRetry]
421+
public async Task GetArchiveAcceptsShaAndFormatParametersTogether()
422+
{
423+
// Arrange
424+
using var context = await RepositoryClientTestsContext.CreateAsync(commitCount: 2);
425+
var format = ".zip";
426+
var firstCommitId = context.Commits[0].Id.ToString();
427+
428+
// Act
429+
context.RepositoryClient.GetArchive((stream) => { }, sha: firstCommitId, format: format);
430+
431+
// Assert
432+
var requestPathAndQuery = context.Context.LastRequest.RequestUri.PathAndQuery;
433+
434+
Assert.Multiple(() =>
435+
{
436+
Assert.That(requestPathAndQuery, Is.Not.Null);
437+
Assert.That(requestPathAndQuery.Contains($"/archive{format}", StringComparison.OrdinalIgnoreCase), Is.True);
438+
Assert.That(requestPathAndQuery.Contains($"?sha={firstCommitId}", StringComparison.OrdinalIgnoreCase), Is.True);
439+
});
440+
}
441+
442+
[Test]
443+
[NGitLabRetry]
444+
public async Task GetArchiveThrowsExceptionWhenFormatDoesNotStartWithDot()
445+
{
446+
// Arrange
447+
using var context = await RepositoryClientTestsContext.CreateAsync(commitCount: 2);
448+
var format = "zip";
449+
450+
// Act and Assert
451+
Assert.Throws<ArgumentException>(() => context.RepositoryClient.GetArchive((stream) => { }, format: format));
452+
}
356453
}

NGitLab/IRepositoryClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using NGitLab.Models;
@@ -21,7 +21,7 @@ public interface IRepositoryClient
2121

2222
void GetRawBlob(string sha, Action<Stream> parser);
2323

24-
void GetArchive(Action<Stream> parser);
24+
void GetArchive(Action<Stream> parser, string sha = null, string format = null);
2525

2626
IEnumerable<Commit> Commits { get; }
2727

NGitLab/Impl/RepositoryClient.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
44
using System.Globalization;
@@ -55,9 +55,17 @@ public void GetRawBlob(string sha, Action<Stream> parser)
5555
_api.Get().Stream(_repoPath + "/raw_blobs/" + sha, parser);
5656
}
5757

58-
public void GetArchive(Action<Stream> parser)
58+
public void GetArchive(Action<Stream> parser, string sha = null, string format = null)
5959
{
60-
_api.Get().Stream(_repoPath + "/archive", parser);
60+
if (!string.IsNullOrEmpty(format) && !format.StartsWith(".", StringComparison.Ordinal))
61+
throw new ArgumentException($"Format must include the '.' as part of extension", nameof(format));
62+
63+
var relativePath = $"/archive{format}";
64+
65+
if (!string.IsNullOrEmpty(sha))
66+
relativePath += $"?sha={sha}";
67+
68+
_api.Get().Stream(_repoPath + relativePath, parser);
6169
}
6270

6371
public IEnumerable<Commit> Commits => _api.Get().GetAll<Commit>(_repoPath + $"/commits?per_page={GetCommitsRequest.DefaultPerPage}");

NGitLab/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ NGitLab.Impl.RepositoryClient.Commits.get -> System.Collections.Generic.IEnumera
869869
NGitLab.Impl.RepositoryClient.Compare(NGitLab.Models.CompareQuery query) -> NGitLab.Models.CompareResults
870870
NGitLab.Impl.RepositoryClient.Contributors.get -> NGitLab.IContributorClient
871871
NGitLab.Impl.RepositoryClient.Files.get -> NGitLab.IFilesClient
872-
NGitLab.Impl.RepositoryClient.GetArchive(System.Action<System.IO.Stream> parser) -> void
872+
NGitLab.Impl.RepositoryClient.GetArchive(System.Action<System.IO.Stream> parser, string sha = null, string format = null) -> void
873873
NGitLab.Impl.RepositoryClient.GetCommit(NGitLab.Sha1 sha) -> NGitLab.Models.Commit
874874
NGitLab.Impl.RepositoryClient.GetCommitDiff(NGitLab.Sha1 sha) -> System.Collections.Generic.IEnumerable<NGitLab.Models.Diff>
875875
NGitLab.Impl.RepositoryClient.GetCommitRefs(NGitLab.Sha1 sha, NGitLab.Models.CommitRefType type = NGitLab.Models.CommitRefType.All) -> System.Collections.Generic.IEnumerable<NGitLab.Models.Ref>
@@ -1086,7 +1086,7 @@ NGitLab.IRepositoryClient.Commits.get -> System.Collections.Generic.IEnumerable<
10861086
NGitLab.IRepositoryClient.Compare(NGitLab.Models.CompareQuery query) -> NGitLab.Models.CompareResults
10871087
NGitLab.IRepositoryClient.Contributors.get -> NGitLab.IContributorClient
10881088
NGitLab.IRepositoryClient.Files.get -> NGitLab.IFilesClient
1089-
NGitLab.IRepositoryClient.GetArchive(System.Action<System.IO.Stream> parser) -> void
1089+
NGitLab.IRepositoryClient.GetArchive(System.Action<System.IO.Stream> parser, string sha = null, string format = null) -> void
10901090
NGitLab.IRepositoryClient.GetCommit(NGitLab.Sha1 sha) -> NGitLab.Models.Commit
10911091
NGitLab.IRepositoryClient.GetCommitDiff(NGitLab.Sha1 sha) -> System.Collections.Generic.IEnumerable<NGitLab.Models.Diff>
10921092
NGitLab.IRepositoryClient.GetCommitRefs(NGitLab.Sha1 sha, NGitLab.Models.CommitRefType type = NGitLab.Models.CommitRefType.All) -> System.Collections.Generic.IEnumerable<NGitLab.Models.Ref>

0 commit comments

Comments
 (0)