- Notifications
You must be signed in to change notification settings - Fork673
feat(api): add support for the Draft notes API#2728
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsdocs/api-objects.rst
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
58 changes: 58 additions & 0 deletionsdocs/gl_objects/draft_notes.rst
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,58 @@ | ||
.. _draft-notes: | ||
########### | ||
Draft Notes | ||
########### | ||
Draft notes are pending, unpublished comments on merge requests. | ||
They can be either start a discussion, or be associated with an existing discussion as a reply. | ||
They are viewable only by the author until they are published. | ||
Reference | ||
--------- | ||
* v4 API: | ||
+ :class:`gitlab.v4.objects.ProjectMergeRequestDraftNote` | ||
+ :class:`gitlab.v4.objects.ProjectMergeRequestDraftNoteManager` | ||
+ :attr:`gitlab.v4.objects.ProjectMergeRequest.draft_notes` | ||
* GitLab API: https://docs.gitlab.com/ee/api/draft_notes.html | ||
Examples | ||
-------- | ||
List all draft notes for a merge request:: | ||
draft_notes = merge_request.draft_notes.list() | ||
Get a draft note for a merge request by ID:: | ||
draft_note = merge_request.draft_notes.get(note_id) | ||
.. warning:: | ||
When creating or updating draft notes, you can provide a complex nested ``position`` argument as a dictionary. | ||
Please consult the upstream API documentation linked above for the exact up-to-date attributes. | ||
Create a draft note for a merge request:: | ||
draft_note = merge_request.draft_notes.create({'note': 'note content'}) | ||
Update an existing draft note:: | ||
draft_note.note = 'updated note content' | ||
draft_note.save() | ||
Delete an existing draft note:: | ||
draft_note.delete() | ||
Publish an existing draft note:: | ||
draft_note.publish() | ||
Publish all existing draft notes for a merge request in bulk:: | ||
merge_request.draft_notes.bulk_publish() |
2 changes: 2 additions & 0 deletionsgitlab/client.py
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
1 change: 1 addition & 0 deletionsgitlab/v4/objects/__init__.py
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
43 changes: 43 additions & 0 deletionsgitlab/v4/objects/draft_notes.py
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,43 @@ | ||
from typing import Any, cast, Union | ||
from gitlab.base import RESTManager, RESTObject | ||
from gitlab.mixins import CRUDMixin, ObjectDeleteMixin, SaveMixin | ||
from gitlab.types import RequiredOptional | ||
__all__ = [ | ||
"ProjectMergeRequestDraftNote", | ||
"ProjectMergeRequestDraftNoteManager", | ||
] | ||
class ProjectMergeRequestDraftNote(ObjectDeleteMixin, SaveMixin, RESTObject): | ||
def publish(self, **kwargs: Any) -> None: | ||
path = f"{self.manager.path}/{self.encoded_id}/publish" | ||
self.manager.gitlab.http_put(path, **kwargs) | ||
class ProjectMergeRequestDraftNoteManager(CRUDMixin, RESTManager): | ||
_path = "/projects/{project_id}/merge_requests/{mr_iid}/draft_notes" | ||
_obj_cls = ProjectMergeRequestDraftNote | ||
_from_parent_attrs = {"project_id": "project_id", "mr_iid": "iid"} | ||
_create_attrs = RequiredOptional( | ||
required=("note",), | ||
optional=( | ||
"commit_id", | ||
"in_reply_to_discussion_id", | ||
"position", | ||
"resolve_discussion", | ||
), | ||
) | ||
_update_attrs = RequiredOptional(optional=("position",)) | ||
def get( | ||
self, id: Union[str, int], lazy: bool = False, **kwargs: Any | ||
) -> ProjectMergeRequestDraftNote: | ||
return cast( | ||
ProjectMergeRequestDraftNote, super().get(id=id, lazy=lazy, **kwargs) | ||
) | ||
def bulk_publish(self, **kwargs: Any) -> None: | ||
path = f"{self.path}/bulk_publish" | ||
self.gitlab.http_post(path, **kwargs) |
2 changes: 2 additions & 0 deletionsgitlab/v4/objects/merge_requests.py
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
175 changes: 175 additions & 0 deletionstests/unit/objects/test_draft_notes.py
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,175 @@ | ||
""" | ||
GitLab API: https://docs.gitlab.com/ee/api/draft_notes.html | ||
""" | ||
from copy import deepcopy | ||
import pytest | ||
import responses | ||
from gitlab.v4.objects import ProjectMergeRequestDraftNote | ||
draft_note_content = { | ||
"id": 1, | ||
"author_id": 23, | ||
"merge_request_id": 1, | ||
"resolve_discussion": False, | ||
"discussion_id": None, | ||
"note": "Example title", | ||
"commit_id": None, | ||
"line_code": None, | ||
"position": { | ||
"base_sha": None, | ||
"start_sha": None, | ||
"head_sha": None, | ||
"old_path": None, | ||
"new_path": None, | ||
"position_type": "text", | ||
"old_line": None, | ||
"new_line": None, | ||
"line_range": None, | ||
}, | ||
} | ||
@pytest.fixture() | ||
def resp_list_merge_request_draft_notes(): | ||
with responses.RequestsMock() as rsps: | ||
rsps.add( | ||
method=responses.GET, | ||
url="http://localhost/api/v4/projects/1/merge_requests/1/draft_notes", | ||
json=[draft_note_content], | ||
content_type="application/json", | ||
status=200, | ||
) | ||
yield rsps | ||
@pytest.fixture() | ||
def resp_get_merge_request_draft_note(): | ||
with responses.RequestsMock() as rsps: | ||
rsps.add( | ||
method=responses.GET, | ||
url="http://localhost/api/v4/projects/1/merge_requests/1/draft_notes/1", | ||
json=draft_note_content, | ||
content_type="application/json", | ||
status=200, | ||
) | ||
yield rsps | ||
@pytest.fixture() | ||
def resp_create_merge_request_draft_note(): | ||
with responses.RequestsMock() as rsps: | ||
rsps.add( | ||
method=responses.POST, | ||
url="http://localhost/api/v4/projects/1/merge_requests/1/draft_notes", | ||
json=draft_note_content, | ||
content_type="application/json", | ||
status=201, | ||
) | ||
yield rsps | ||
@pytest.fixture() | ||
def resp_update_merge_request_draft_note(): | ||
updated_content = deepcopy(draft_note_content) | ||
updated_content["note"] = "New title" | ||
with responses.RequestsMock() as rsps: | ||
rsps.add( | ||
method=responses.PUT, | ||
url="http://localhost/api/v4/projects/1/merge_requests/1/draft_notes/1", | ||
json=updated_content, | ||
content_type="application/json", | ||
status=201, | ||
) | ||
yield rsps | ||
@pytest.fixture() | ||
def resp_delete_merge_request_draft_note(): | ||
with responses.RequestsMock() as rsps: | ||
rsps.add( | ||
method=responses.DELETE, | ||
url="http://localhost/api/v4/projects/1/merge_requests/1/draft_notes/1", | ||
json=draft_note_content, | ||
content_type="application/json", | ||
status=201, | ||
) | ||
yield rsps | ||
@pytest.fixture() | ||
def resp_publish_merge_request_draft_note(): | ||
with responses.RequestsMock() as rsps: | ||
rsps.add( | ||
method=responses.PUT, | ||
url="http://localhost/api/v4/projects/1/merge_requests/1/draft_notes/1/publish", | ||
status=204, | ||
) | ||
yield rsps | ||
@pytest.fixture() | ||
def resp_bulk_publish_merge_request_draft_notes(): | ||
with responses.RequestsMock() as rsps: | ||
rsps.add( | ||
method=responses.POST, | ||
url="http://localhost/api/v4/projects/1/merge_requests/1/draft_notes/bulk_publish", | ||
status=204, | ||
) | ||
yield rsps | ||
def test_list_merge_requests_draft_notes( | ||
project_merge_request, resp_list_merge_request_draft_notes | ||
): | ||
draft_notes = project_merge_request.draft_notes.list() | ||
assert len(draft_notes) == 1 | ||
assert isinstance(draft_notes[0], ProjectMergeRequestDraftNote) | ||
assert draft_notes[0].note == draft_note_content["note"] | ||
def test_get_merge_requests_draft_note( | ||
project_merge_request, resp_get_merge_request_draft_note | ||
): | ||
draft_note = project_merge_request.draft_notes.get(1) | ||
assert isinstance(draft_note, ProjectMergeRequestDraftNote) | ||
assert draft_note.note == draft_note_content["note"] | ||
def test_create_merge_requests_draft_note( | ||
project_merge_request, resp_create_merge_request_draft_note | ||
): | ||
draft_note = project_merge_request.draft_notes.create({"note": "Example title"}) | ||
assert isinstance(draft_note, ProjectMergeRequestDraftNote) | ||
assert draft_note.note == draft_note_content["note"] | ||
def test_update_merge_requests_draft_note( | ||
project_merge_request, resp_update_merge_request_draft_note | ||
): | ||
draft_note = project_merge_request.draft_notes.get(1, lazy=True) | ||
draft_note.note = "New title" | ||
draft_note.save() | ||
assert draft_note.note == "New title" | ||
def test_delete_merge_requests_draft_note( | ||
project_merge_request, resp_delete_merge_request_draft_note | ||
): | ||
draft_note = project_merge_request.draft_notes.get(1, lazy=True) | ||
draft_note.delete() | ||
def test_publish_merge_requests_draft_note( | ||
project_merge_request, resp_publish_merge_request_draft_note | ||
): | ||
draft_note = project_merge_request.draft_notes.get(1, lazy=True) | ||
draft_note.publish() | ||
def test_bulk_publish_merge_requests_draft_notes( | ||
project_merge_request, resp_bulk_publish_merge_request_draft_notes | ||
): | ||
project_merge_request.draft_notes.bulk_publish() |
15 changes: 15 additions & 0 deletionstests/unit/test_gitlab_http_methods.py
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
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.