Skip to content

Commit

Permalink
Fix #705
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-z committed Dec 20, 2024
1 parent d5740e3 commit 4500aa6
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 0 deletions.
5 changes: 5 additions & 0 deletions NGitLab.Mock/Clients/CommitClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ public JobStatus GetJobStatus(string branchName)
return GitLabCollectionResponse.Create(relatedMerqueRequests);
}
}

public Commit Revert(CommitRevert revert)
{
throw new NotImplementedException();
}
}
48 changes: 48 additions & 0 deletions NGitLab.Tests/CommitsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,54 @@ public async Task Test_can_cherry_pick_commit()
Assert.That(latestCommit.Id, Is.EqualTo(cherryPickedCommit.Id));
}

[Test]
[NGitLabRetry]
public async Task Test_can_revert_commit()
{
// Arrange
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject();

var testBranchName = "revert-test";

var repositoryClient = context.Client.GetRepository(project.Id);
repositoryClient.Branches.Create(new BranchCreate
{
Name = testBranchName,
Ref = project.DefaultBranch,
});

var commitClient = context.Client.GetCommits(project.Id);
var testCommit = commitClient.Create(new CommitCreate
{
Branch = testBranchName,
CommitMessage = "This commit will be reverted",
Actions =
[
new()
{
Action = "update",
Content = "Testing commit revert",
FilePath = "README.md",
},
],
});

var compareResults = repositoryClient.Compare(new CompareQuery(project.DefaultBranch, testBranchName));
Assert.That(compareResults.Diff, Has.Length.EqualTo(1));

// Act
var revertedCommit = commitClient.Revert(new CommitRevert
{
Branch = testBranchName,
Sha = testCommit.Id,
});

// Assert
compareResults = repositoryClient.Compare(new CompareQuery(project.DefaultBranch, testBranchName));
Assert.That(compareResults.Diff, Is.Empty);
}

[TestCase(false)]
[TestCase(true)]
[NGitLabRetry]
Expand Down
6 changes: 6 additions & 0 deletions NGitLab/ICommitClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public interface ICommitClient
/// </summary>
Commit CherryPick(CommitCherryPick cherryPick);


/// <summary>
/// Reverts a specific branch commit
/// </summary>
Commit Revert(CommitRevert revert);

/// <summary>
/// Get merge requests related to a commit
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions NGitLab/Impl/CommitClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ public GitLabCollectionResponse<MergeRequest> GetRelatedMergeRequestsAsync(Relat
{
return _api.Get().GetAllAsync<MergeRequest>(_repoPath + $"/commits/{query.Sha}/merge_requests");
}

public Commit Revert(CommitRevert revert)
{
return _api.Post().With(revert).To<Commit>($"{_repoPath}/commits/{revert.Sha}/revert");
}
}
18 changes: 18 additions & 0 deletions NGitLab/Models/CommitRevert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace NGitLab.Models;

public class CommitRevert
{
[Required]
[JsonIgnore]
public Sha1 Sha { get; set; }

[Required]
[JsonPropertyName("branch")]
public string Branch { get; set; }

[JsonPropertyName("dry_run")]
public bool? DryRun { get; set; }
}
10 changes: 10 additions & 0 deletions NGitLab/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ NGitLab.ICommitClient.Create(NGitLab.Models.CommitCreate commit) -> NGitLab.Mode
NGitLab.ICommitClient.GetCommit(string ref) -> NGitLab.Models.Commit
NGitLab.ICommitClient.GetJobStatus(string branchName) -> NGitLab.JobStatus
NGitLab.ICommitClient.GetRelatedMergeRequestsAsync(NGitLab.Models.RelatedMergeRequestsQuery query) -> NGitLab.GitLabCollectionResponse<NGitLab.Models.MergeRequest>
NGitLab.ICommitClient.Revert(NGitLab.Models.CommitRevert revert) -> NGitLab.Models.Commit
NGitLab.ICommitStatusClient
NGitLab.ICommitStatusClient.AddOrUpdate(NGitLab.Models.CommitStatusCreate status) -> NGitLab.Models.CommitStatusCreate
NGitLab.ICommitStatusClient.AllBySha(string commitSha) -> System.Collections.Generic.IEnumerable<NGitLab.Models.CommitStatus>
Expand Down Expand Up @@ -475,6 +476,7 @@ NGitLab.Impl.CommitClient.Create(NGitLab.Models.CommitCreate commit) -> NGitLab.
NGitLab.Impl.CommitClient.GetCommit(string ref) -> NGitLab.Models.Commit
NGitLab.Impl.CommitClient.GetJobStatus(string branchName) -> NGitLab.JobStatus
NGitLab.Impl.CommitClient.GetRelatedMergeRequestsAsync(NGitLab.Models.RelatedMergeRequestsQuery query) -> NGitLab.GitLabCollectionResponse<NGitLab.Models.MergeRequest>
NGitLab.Impl.CommitClient.Revert(NGitLab.Models.CommitRevert revert) -> NGitLab.Models.Commit
NGitLab.Impl.CommitStatusClient
NGitLab.Impl.CommitStatusClient.AddOrUpdate(NGitLab.Models.CommitStatusCreate status) -> NGitLab.Models.CommitStatusCreate
NGitLab.Impl.CommitStatusClient.AllBySha(string commitSha) -> System.Collections.Generic.IEnumerable<NGitLab.Models.CommitStatus>
Expand Down Expand Up @@ -1530,6 +1532,14 @@ NGitLab.Models.CommitRefType
NGitLab.Models.CommitRefType.All = 0 -> NGitLab.Models.CommitRefType
NGitLab.Models.CommitRefType.Branch = 1 -> NGitLab.Models.CommitRefType
NGitLab.Models.CommitRefType.Tag = 2 -> NGitLab.Models.CommitRefType
NGitLab.Models.CommitRevert
NGitLab.Models.CommitRevert.Branch.get -> string
NGitLab.Models.CommitRevert.Branch.set -> void
NGitLab.Models.CommitRevert.CommitRevert() -> void
NGitLab.Models.CommitRevert.DryRun.get -> bool?
NGitLab.Models.CommitRevert.DryRun.set -> void
NGitLab.Models.CommitRevert.Sha.get -> NGitLab.Sha1
NGitLab.Models.CommitRevert.Sha.set -> void
NGitLab.Models.CommitStats
NGitLab.Models.CommitStats.Additions.get -> int
NGitLab.Models.CommitStats.Additions.set -> void
Expand Down

0 comments on commit 4500aa6

Please sign in to comment.