-
Notifications
You must be signed in to change notification settings - Fork 86
Add UnapproveMergeRequest, GetDiffsAsync with unidiff, and draft notes API #1123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
louis-z
wants to merge
4
commits into
main
Choose a base branch
from
add-unapprove-unidiff-draft-notes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using NGitLab.Models; | ||
|
|
||
| namespace NGitLab.Mock.Clients; | ||
|
|
||
| internal sealed class MergeRequestDraftNoteClient : ClientBase, IMergeRequestDraftNoteClient | ||
| { | ||
| public MergeRequestDraftNoteClient(ClientContext context) | ||
| : base(context) | ||
| { | ||
| } | ||
|
|
||
| public IEnumerable<DraftNote> All => throw new NotImplementedException(); | ||
|
|
||
| public Task<DraftNote> CreateAsync(DraftNoteCreate draftNote, CancellationToken cancellationToken = default) | ||
| => throw new NotImplementedException(); | ||
|
|
||
| public Task PublishAllAsync(CancellationToken cancellationToken = default) | ||
| => throw new NotImplementedException(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
NGitLab.Tests/MergeRequest/MergeRequestApprovalClientTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using NGitLab.Tests.Docker; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace NGitLab.Tests; | ||
|
|
||
| public class MergeRequestApprovalClientTests | ||
| { | ||
| [Test] | ||
| [NGitLabRetry] | ||
| public async Task ApproveMergeRequest_and_UnapproveMergeRequest_roundtrip() | ||
| { | ||
| using var context = await GitLabTestContext.CreateAsync(); | ||
| var (project, mergeRequest) = context.CreateMergeRequest(); | ||
| var mrClient = context.Client.GetMergeRequest(project.Id); | ||
| var approvalClient = mrClient.ApprovalClient(mergeRequest.Iid); | ||
|
|
||
| // Approve | ||
| var approvals = approvalClient.ApproveMergeRequest(); | ||
| Assert.That(approvals, Is.Not.Null); | ||
| Assert.That(approvals.Approved, Is.True, "MR should be marked as approved after ApproveMergeRequest"); | ||
|
|
||
| // Unapprove — should not throw | ||
| Assert.DoesNotThrow((Action)(() => approvalClient.UnapproveMergeRequest())); | ||
|
|
||
| // After unapproval the approval state should show no approved-by entries | ||
| var state = approvalClient.Approvals; | ||
| Assert.That(state.ApprovedBy, Is.Empty.Or.Null, "No approvers should remain after unapproving"); | ||
| } | ||
|
|
||
| [Test] | ||
| [NGitLabRetry] | ||
| public async Task UnapproveMergeRequest_on_unapproved_mr_throws() | ||
| { | ||
| // Arrange | ||
| using var context = await GitLabTestContext.CreateAsync(); | ||
| var (project, mergeRequest) = context.CreateMergeRequest(); | ||
| var approvalClient = context.Client.GetMergeRequest(project.Id).ApprovalClient(mergeRequest.Iid); | ||
|
|
||
| // Act/Assert | ||
| Assert.That((Action)(() => approvalClient.UnapproveMergeRequest()), Throws.TypeOf<GitLabException>(), | ||
| "Unapproving an unapproved MR should throw a GitLabException"); | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
NGitLab.Tests/MergeRequest/MergeRequestDiffsClientTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using NGitLab.Models; | ||
| using NGitLab.Tests.Docker; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace NGitLab.Tests; | ||
|
|
||
| public class MergeRequestDiffsClientTests | ||
| { | ||
| [Test] | ||
| [NGitLabRetry] | ||
| public async Task GetDiffsAsync_returns_changed_files() | ||
| { | ||
| using var context = await GitLabTestContext.CreateAsync(); | ||
| var (project, mergeRequest) = context.CreateMergeRequest(); | ||
| var mrClient = context.Client.GetMergeRequest(project.Id); | ||
|
|
||
| var diffs = await mrClient.GetDiffsAsync(mergeRequest.Iid, query: null).ToListAsync(); | ||
|
|
||
| Assert.That(diffs, Has.Count.GreaterThan(0), "At least one diff should be returned"); | ||
| Assert.That(diffs[0].OldPath, Is.Not.Null.And.Not.Empty); | ||
| Assert.That(diffs[0].NewPath, Is.Not.Null.And.Not.Empty); | ||
| } | ||
|
|
||
| [Test] | ||
| [NGitLabRetry] | ||
| public async Task GetDiffsAsync_with_unidiff_returns_changed_files() | ||
| { | ||
| using var context = await GitLabTestContext.CreateAsync(); | ||
| var (project, mergeRequest) = context.CreateMergeRequest(); | ||
| var mrClient = context.Client.GetMergeRequest(project.Id); | ||
|
|
||
| var query = new MergeRequestDiffQuery { Unidiff = true }; | ||
| var diffs = await mrClient.GetDiffsAsync(mergeRequest.Iid, query).ToListAsync(); | ||
|
|
||
| Assert.That(diffs, Has.Count.GreaterThan(0), "At least one diff should be returned with unidiff=true"); | ||
| Assert.That(diffs[0].OldPath, Is.Not.Null.And.Not.Empty); | ||
| Assert.That(diffs[0].NewPath, Is.Not.Null.And.Not.Empty); | ||
| } | ||
|
|
||
| [Test] | ||
| [NGitLabRetry] | ||
| public async Task GetDiffsAsync_unidiff_format_starts_with_unified_diff_header() | ||
| { | ||
| using var context = await GitLabTestContext.CreateAsync(); | ||
| var (project, mergeRequest) = context.CreateMergeRequest(); | ||
| var mrClient = context.Client.GetMergeRequest(project.Id); | ||
|
|
||
| var plainDiffs = await mrClient.GetDiffsAsync(mergeRequest.Iid, query: null).ToListAsync(); | ||
| var unifiedDiffs = await mrClient.GetDiffsAsync(mergeRequest.Iid, new MergeRequestDiffQuery { Unidiff = true }).ToListAsync(); | ||
|
|
||
| Assert.That(plainDiffs, Has.Count.EqualTo(unifiedDiffs.Count), "Both queries should return the same number of files"); | ||
|
|
||
| // Unified diff format uses "--- a/..." / "+++ b/..." headers; the plain GitLab format does not. | ||
| var firstUnified = unifiedDiffs[0].Difference; | ||
| Assert.That(firstUnified, Does.StartWith("---").Or.StartWith("diff --git"), | ||
| "Unified diff content should start with standard unified-diff markers"); | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
56
NGitLab.Tests/MergeRequest/MergeRequestDraftNoteClientTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using NGitLab.Models; | ||
| using NGitLab.Tests.Docker; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace NGitLab.Tests; | ||
|
|
||
| public class MergeRequestDraftNoteClientTests | ||
| { | ||
| [Test] | ||
| [NGitLabRetry] | ||
| public async Task CreateAsync_adds_draft_note_visible_in_All() | ||
| { | ||
| using var context = await GitLabTestContext.CreateAsync(); | ||
| var (project, mergeRequest) = context.CreateMergeRequest(); | ||
| var draftNoteClient = context.Client.GetMergeRequest(project.Id).DraftNotes(mergeRequest.Iid); | ||
|
|
||
| var created = await draftNoteClient.CreateAsync(new DraftNoteCreate { Note = "Draft review comment" }); | ||
|
|
||
| Assert.That(created, Is.Not.Null); | ||
| Assert.That(created.Note, Is.EqualTo("Draft review comment")); | ||
| Assert.That(created.Id, Is.GreaterThan(0)); | ||
|
|
||
| var all = draftNoteClient.All.ToList(); | ||
| Assert.That(all, Has.Count.EqualTo(1), "The created draft note should appear in All"); | ||
| Assert.That(all[0].Id, Is.EqualTo(created.Id)); | ||
| } | ||
|
|
||
| [Test] | ||
| [NGitLabRetry] | ||
| public async Task PublishAllAsync_publishes_all_draft_notes() | ||
| { | ||
| using var context = await GitLabTestContext.CreateAsync(); | ||
| var (project, mergeRequest) = context.CreateMergeRequest(); | ||
| var mrClient = context.Client.GetMergeRequest(project.Id); | ||
| var draftNoteClient = mrClient.DraftNotes(mergeRequest.Iid); | ||
|
|
||
| await draftNoteClient.CreateAsync(new DraftNoteCreate { Note = "First draft" }); | ||
| await draftNoteClient.CreateAsync(new DraftNoteCreate { Note = "Second draft" }); | ||
|
|
||
| Assert.That(draftNoteClient.All.Count(), Is.EqualTo(2), "Two draft notes should exist before publishing"); | ||
|
|
||
| await draftNoteClient.PublishAllAsync(); | ||
|
|
||
| // After publishing, draft notes should be cleared | ||
| var remaining = draftNoteClient.All.ToList(); | ||
| Assert.That(remaining, Is.Empty, "Draft notes should be gone after PublishAllAsync"); | ||
|
|
||
| // The notes should now appear as regular MR notes/discussions | ||
| var notes = mrClient.Comments(mergeRequest.Iid).All.ToList(); | ||
| Assert.That(notes.Any(n => string.Equals(n.Body, "First draft", System.StringComparison.Ordinal) | ||
| || string.Equals(n.Body, "Second draft", System.StringComparison.Ordinal)), Is.True, | ||
| "Published draft notes should appear as regular comments"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using NGitLab.Models; | ||
|
|
||
| namespace NGitLab; | ||
|
|
||
| /// <summary> | ||
| /// Client for the GitLab draft notes API: | ||
| /// <c>GET/POST /projects/:id/merge_requests/:iid/draft_notes</c> and | ||
| /// <c>POST /projects/:id/merge_requests/:iid/draft_notes/bulk_publish</c>. | ||
| /// </summary> | ||
| public interface IMergeRequestDraftNoteClient | ||
| { | ||
| IEnumerable<DraftNote> All { get; } | ||
|
|
||
| Task<DraftNote> CreateAsync(DraftNoteCreate draftNote, CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Publishes all draft notes for the merge request in a single request. | ||
| /// </summary> | ||
| Task PublishAllAsync(CancellationToken cancellationToken = default); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using NGitLab.Models; | ||
|
|
||
| namespace NGitLab.Impl; | ||
|
|
||
| public class MergeRequestDraftNoteClient : IMergeRequestDraftNoteClient | ||
| { | ||
| private readonly API _api; | ||
| private readonly string _draftNotesPath; | ||
| private readonly string _bulkPublishPath; | ||
|
|
||
| public MergeRequestDraftNoteClient(API api, string projectPath, long mergeRequestIid) | ||
| { | ||
| _api = api; | ||
| var iid = mergeRequestIid.ToString(CultureInfo.InvariantCulture); | ||
| _draftNotesPath = projectPath + "/merge_requests/" + iid + "/draft_notes"; | ||
| _bulkPublishPath = _draftNotesPath + "/bulk_publish"; | ||
| } | ||
|
|
||
| public IEnumerable<DraftNote> All => _api.Get().GetAll<DraftNote>(_draftNotesPath); | ||
|
|
||
| public Task<DraftNote> CreateAsync(DraftNoteCreate draftNote, CancellationToken cancellationToken = default) | ||
| => _api.Post().With(draftNote).ToAsync<DraftNote>(_draftNotesPath, cancellationToken); | ||
|
|
||
| public Task PublishAllAsync(CancellationToken cancellationToken = default) | ||
| => _api.Post().ExecuteAsync(_bulkPublishPath, cancellationToken); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.