Skip to content

Commit

Permalink
Add ability to do a code compare. (#444)
Browse files Browse the repository at this point in the history
* Add ability to do a code compare.

* Rename classes, methods, add tests

* Update NGitLab.Tests/CompareTests.cs

Co-authored-by: Louis Zanella <[email protected]>

* Update NGitLab.Tests/CompareTests.cs

Co-authored-by: Louis Zanella <[email protected]>

* Update NGitLab/Impl/RepositoryClient.cs

Co-authored-by: Louis Zanella <[email protected]>

* Fix spelling

* Fixed tests

* removed unnecessary variable

---------

Co-authored-by: Markus Stein <[email protected]>
Co-authored-by: Louis Zanella <[email protected]>
  • Loading branch information
3 people authored Nov 17, 2023
1 parent 97a0852 commit 613bee0
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 0 deletions.
5 changes: 5 additions & 0 deletions NGitLab.Mock/Clients/RepositoryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,10 @@ private static Commit ConvertToNGitLabCommit(LibGit2Sharp.Commit commit, Project
{
return commit.ToCommitClient(project);
}

public CompareResults Compare(CompareQuery query)
{
throw new NotImplementedException();
}
}
}
70 changes: 70 additions & 0 deletions NGitLab.Tests/CompareTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Threading.Tasks;
using NGitLab.Models;
using NGitLab.Tests.Docker;
using NUnit.Framework;

namespace NGitLab.Tests
{
public class CompareTests
{
[Test]
[NGitLabRetry]
public async Task Test_compare_equal()
{
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject(initializeWithCommits: true);
var compareResults = context.Client.GetRepository(project.Id).Compare(new CompareQuery(project.DefaultBranch, project.DefaultBranch));

Assert.IsNotNull(compareResults);
Assert.AreEqual(0, compareResults.Commits.Length);
}

[Test]
[NGitLabRetry]
public async Task Test_compare()
{
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject(initializeWithCommits: true);

var devTestBranchCreate = new BranchCreate();
devTestBranchCreate.Ref = project.DefaultBranch;
devTestBranchCreate.Name = "devtest";

context.Client.GetRepository(project.Id).Branches.Create(devTestBranchCreate);

context.Client.GetRepository(project.Id).Files.Create(new FileUpsert
{
Branch = "devtest",
CommitMessage = "file to be compared",
Path = "compare1.txt",
RawContent = "compare me",
});

context.Client.GetRepository(project.Id).Files.Create(new FileUpsert
{
Branch = "devtest",
CommitMessage = "file to be compared, too",
Path = "compare2.txt",
RawContent = "compare me now",
});

var compareResults = context.Client.GetRepository(project.Id).Compare(new CompareQuery(project.DefaultBranch, "devtest"));

Assert.IsNotNull(compareResults);
Assert.AreEqual(2, compareResults.Commits.Length);
}

[Test]
[NGitLabRetry]
public async Task Test_compare_invalid()
{
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject(initializeWithCommits: true);

Assert.Catch< GitLabException>(() =>
{
context.Client.GetRepository(project.Id).Compare(new CompareQuery(project.DefaultBranch, "testblub"));
}, "404 Ref Not Found", null);
}
}
}
5 changes: 5 additions & 0 deletions NGitLab/IRepositoryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@ public interface IRepositoryClient
IBranchClient Branches { get; }

IProjectHooksClient ProjectHooks { get; }

/// <summary>
/// Compare two branches/tags/commit hashes
/// </summary>
CompareResults Compare(CompareQuery query);
}
}
5 changes: 5 additions & 0 deletions NGitLab/Impl/RepositoryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ public IEnumerable<Commit> GetCommits(GetCommitsRequest request)
return allCommits.Take(request.MaxResults);
}

public CompareResults Compare(CompareQuery query)
{
return _api.Get().To<CompareResults>(_repoPath + $@"/compare?from={query.Source}&to={query.Target}");
}

public Commit GetCommit(Sha1 sha) => _api.Get().To<Commit>(_repoPath + "/commits/" + sha);

public IEnumerable<Diff> GetCommitDiff(Sha1 sha) => _api.Get().GetAll<Diff>(_repoPath + "/commits/" + sha + "/diff");
Expand Down
24 changes: 24 additions & 0 deletions NGitLab/Models/CompareQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace NGitLab.Models
{
/// <summary>
/// Query details for comparison of branches/tags/commit hashes
/// </summary>
public class CompareQuery
{
/// <summary>
/// The source for comparison, can be a branch, tag or a commit hash.
/// </summary>
public string Source { get; set; }

/// <summary>
/// The target for comparison, can be a branch, tag or a commit hash.
/// </summary>
public string Target { get; set; }

public CompareQuery(string source, string target)
{
Source = source;
Target = target;
}
}
}
22 changes: 22 additions & 0 deletions NGitLab/Models/CompareResults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Text.Json.Serialization;

namespace NGitLab.Models
{
public class CompareResults
{
[JsonPropertyName("commit")]
public Commit Commit { get; set; }

[JsonPropertyName("commits")]
public Commit[] Commits { get; set; }

[JsonPropertyName("diffs")]
public Diff[] Diff { get; set; }

[JsonPropertyName("compare_timeout")]
public bool CompareTimeout { get; set; }

[JsonPropertyName("compare_same_ref")]
public bool CompareSameRefs { get; set; }
}
}
20 changes: 20 additions & 0 deletions NGitLab/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ NGitLab.Impl.ProjectLevelApprovalRulesClient.UpdateProjectLevelApprovalRule(int
NGitLab.Impl.RepositoryClient
NGitLab.Impl.RepositoryClient.Branches.get -> NGitLab.IBranchClient
NGitLab.Impl.RepositoryClient.Commits.get -> System.Collections.Generic.IEnumerable<NGitLab.Models.Commit>
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
Expand Down Expand Up @@ -1004,6 +1005,7 @@ NGitLab.IReleaseLinkClient.Update(int id, NGitLab.Models.ReleaseLinkUpdate data)
NGitLab.IRepositoryClient
NGitLab.IRepositoryClient.Branches.get -> NGitLab.IBranchClient
NGitLab.IRepositoryClient.Commits.get -> System.Collections.Generic.IEnumerable<NGitLab.Models.Commit>
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
Expand Down Expand Up @@ -1394,6 +1396,24 @@ NGitLab.Models.CommitStatusCreate.Ref -> string
NGitLab.Models.CommitStatusCreate.State -> string
NGitLab.Models.CommitStatusCreate.Status -> string
NGitLab.Models.CommitStatusCreate.TargetUrl -> string
NGitLab.Models.CompareQuery
NGitLab.Models.CompareQuery.CompareQuery(string source, string target) -> void
NGitLab.Models.CompareQuery.Source.get -> string
NGitLab.Models.CompareQuery.Source.set -> void
NGitLab.Models.CompareQuery.Target.get -> string
NGitLab.Models.CompareQuery.Target.set -> void
NGitLab.Models.CompareResults
NGitLab.Models.CompareResults.Commit.get -> NGitLab.Models.Commit
NGitLab.Models.CompareResults.Commit.set -> void
NGitLab.Models.CompareResults.Commits.get -> NGitLab.Models.Commit[]
NGitLab.Models.CompareResults.Commits.set -> void
NGitLab.Models.CompareResults.CompareResults() -> void
NGitLab.Models.CompareResults.CompareSameRefs.get -> bool
NGitLab.Models.CompareResults.CompareSameRefs.set -> void
NGitLab.Models.CompareResults.CompareTimeout.get -> bool
NGitLab.Models.CompareResults.CompareTimeout.set -> void
NGitLab.Models.CompareResults.Diff.get -> NGitLab.Models.Diff[]
NGitLab.Models.CompareResults.Diff.set -> void
NGitLab.Models.Contributor
NGitLab.Models.Contributor.Addition -> int
NGitLab.Models.Contributor.Commits -> int
Expand Down

0 comments on commit 613bee0

Please sign in to comment.