Skip to content

Commit

Permalink
Implements SHA and Format parameters for RepositoryClient.GetArchive
Browse files Browse the repository at this point in the history
  • Loading branch information
steve85 committed Apr 11, 2024
1 parent 87fed0b commit b8d48da
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 9 deletions.
4 changes: 2 additions & 2 deletions NGitLab.Mock/Clients/RepositoryClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -80,7 +80,7 @@ public void GetRawBlob(string sha, Action<Stream> parser)
throw new NotImplementedException();
}

public void GetArchive(Action<Stream> parser)
public void GetArchive(Action<Stream> parser, string sha = null, string format = null)
{
throw new NotImplementedException();
}
Expand Down
97 changes: 97 additions & 0 deletions NGitLab.Tests/RepositoryClient/RepositoryClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,101 @@ public async Task GetCommitRefs(CommitRefType type)
Assert.That(commitRefs, Is.Not.Empty);
}
}

[Test]
[NGitLabRetry]
public async Task GetArchiveWithoutOptionalParameters()
{
// Arrange
using var context = await RepositoryClientTestsContext.CreateAsync(commitCount: 2);

// Act
context.RepositoryClient.GetArchive((stream) => { });

// Assert
var requestPathAndQuery = context.Context.LastRequest.RequestUri.PathAndQuery;

Assert.Multiple(() =>
{
Assert.That(requestPathAndQuery, Is.Not.Null);
Assert.That(requestPathAndQuery.EndsWith("/archive", StringComparison.OrdinalIgnoreCase), Is.True);
});
}

[Test]
[NGitLabRetry]
public async Task GetArchiveAcceptsShaParameter()
{
// Arrange
using var context = await RepositoryClientTestsContext.CreateAsync(commitCount: 2);
var firstCommitId = context.Commits[0].Id.ToString();

// Act
context.RepositoryClient.GetArchive((stream) => { }, sha: firstCommitId);

// Assert
var requestPathAndQuery = context.Context.LastRequest.RequestUri.PathAndQuery;

Assert.Multiple(() =>
{
Assert.That(requestPathAndQuery, Is.Not.Null);
Assert.That(requestPathAndQuery.Contains($"?sha={firstCommitId}", StringComparison.OrdinalIgnoreCase), Is.True);
});
}

[Test]
[NGitLabRetry]
public async Task GetArchiveAcceptsFormatParameter()
{
// Arrange
using var context = await RepositoryClientTestsContext.CreateAsync(commitCount: 2);
var format = ".zip";

// Act
context.RepositoryClient.GetArchive((stream) => { }, format: format);

// Assert
var requestPathAndQuery = context.Context.LastRequest.RequestUri.PathAndQuery;

Assert.Multiple(() =>
{
Assert.That(requestPathAndQuery, Is.Not.Null);
Assert.That(requestPathAndQuery.Contains($"/archive{format}", StringComparison.OrdinalIgnoreCase), Is.True);
});
}

[Test]
[NGitLabRetry]
public async Task GetArchiveAcceptsShaAndFormatParametersTogether()
{
// Arrange
using var context = await RepositoryClientTestsContext.CreateAsync(commitCount: 2);
var format = ".zip";
var firstCommitId = context.Commits[0].Id.ToString();

// Act
context.RepositoryClient.GetArchive((stream) => { }, sha: firstCommitId, format: format);

// Assert
var requestPathAndQuery = context.Context.LastRequest.RequestUri.PathAndQuery;

Assert.Multiple(() =>
{
Assert.That(requestPathAndQuery, Is.Not.Null);
Assert.That(requestPathAndQuery.Contains($"/archive{format}", StringComparison.OrdinalIgnoreCase), Is.True);
Assert.That(requestPathAndQuery.Contains($"?sha={firstCommitId}", StringComparison.OrdinalIgnoreCase), Is.True);
});
}

[Test]
[NGitLabRetry]
public async Task GetArchiveThrowsExceptionWhenFormatDoesNotStartWithDot()
{
// Arrange
using var context = await RepositoryClientTestsContext.CreateAsync(commitCount: 2);
var format = "zip";

// Act and Assert
Assert.Throws<ArgumentException>(() => context.RepositoryClient.GetArchive((stream) => { }, format: format));
}
}
4 changes: 2 additions & 2 deletions NGitLab/IRepositoryClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using NGitLab.Models;
Expand All @@ -21,7 +21,7 @@ public interface IRepositoryClient

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

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

IEnumerable<Commit> Commits { get; }

Expand Down
14 changes: 11 additions & 3 deletions NGitLab/Impl/RepositoryClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
Expand Down Expand Up @@ -55,9 +55,17 @@ public void GetRawBlob(string sha, Action<Stream> parser)
_api.Get().Stream(_repoPath + "/raw_blobs/" + sha, parser);
}

public void GetArchive(Action<Stream> parser)
public void GetArchive(Action<Stream> parser, string sha = null, string format = null)
{
_api.Get().Stream(_repoPath + "/archive", parser);
if (!string.IsNullOrEmpty(format) && !format.StartsWith(".", StringComparison.Ordinal))
throw new ArgumentException($"Format must include the '.' as part of extension", nameof(format));

var relativePath = $"/archive{format}";

if (!string.IsNullOrEmpty(sha))
relativePath += $"?sha={sha}";

_api.Get().Stream(_repoPath + relativePath, parser);
}

public IEnumerable<Commit> Commits => _api.Get().GetAll<Commit>(_repoPath + $"/commits?per_page={GetCommitsRequest.DefaultPerPage}");
Expand Down
4 changes: 2 additions & 2 deletions NGitLab/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ NGitLab.Impl.RepositoryClient.Commits.get -> System.Collections.Generic.IEnumera
NGitLab.Impl.RepositoryClient.Compare(NGitLab.Models.CompareQuery query) -> NGitLab.Models.CompareResults
NGitLab.Impl.RepositoryClient.Contributors.get -> NGitLab.IContributorClient
NGitLab.Impl.RepositoryClient.Files.get -> NGitLab.IFilesClient
NGitLab.Impl.RepositoryClient.GetArchive(System.Action<System.IO.Stream> parser) -> void
NGitLab.Impl.RepositoryClient.GetArchive(System.Action<System.IO.Stream> parser, string sha = null, string format = null) -> void
NGitLab.Impl.RepositoryClient.GetCommit(NGitLab.Sha1 sha) -> NGitLab.Models.Commit
NGitLab.Impl.RepositoryClient.GetCommitDiff(NGitLab.Sha1 sha) -> System.Collections.Generic.IEnumerable<NGitLab.Models.Diff>
NGitLab.Impl.RepositoryClient.GetCommitRefs(NGitLab.Sha1 sha, NGitLab.Models.CommitRefType type = NGitLab.Models.CommitRefType.All) -> System.Collections.Generic.IEnumerable<NGitLab.Models.Ref>
Expand Down Expand Up @@ -1086,7 +1086,7 @@ NGitLab.IRepositoryClient.Commits.get -> System.Collections.Generic.IEnumerable<
NGitLab.IRepositoryClient.Compare(NGitLab.Models.CompareQuery query) -> NGitLab.Models.CompareResults
NGitLab.IRepositoryClient.Contributors.get -> NGitLab.IContributorClient
NGitLab.IRepositoryClient.Files.get -> NGitLab.IFilesClient
NGitLab.IRepositoryClient.GetArchive(System.Action<System.IO.Stream> parser) -> void
NGitLab.IRepositoryClient.GetArchive(System.Action<System.IO.Stream> parser, string sha = null, string format = null) -> void
NGitLab.IRepositoryClient.GetCommit(NGitLab.Sha1 sha) -> NGitLab.Models.Commit
NGitLab.IRepositoryClient.GetCommitDiff(NGitLab.Sha1 sha) -> System.Collections.Generic.IEnumerable<NGitLab.Models.Diff>
NGitLab.IRepositoryClient.GetCommitRefs(NGitLab.Sha1 sha, NGitLab.Models.CommitRefType type = NGitLab.Models.CommitRefType.All) -> System.Collections.Generic.IEnumerable<NGitLab.Models.Ref>
Expand Down

0 comments on commit b8d48da

Please sign in to comment.