Skip to content
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

Allow multiline comments in PullRequest #2540

Merged
merged 27 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e3efd64
fix(PullRequest.create_comment): add multiline comment
heitorpolidoro May 31, 2023
b4f6dae
fix(PullRequest.create_comment): fix and create tests
heitorpolidoro May 31, 2023
71ce75e
fix(PullRequest.create_comment): lint
heitorpolidoro May 31, 2023
84bd203
fix(PullRequest.create_comment): lint
heitorpolidoro May 31, 2023
d559f6d
Update github/PullRequest.py
heitorpolidoro Jun 14, 2023
bd00108
Update github/PullRequest.py
heitorpolidoro Jun 14, 2023
28eb82b
Fix broken urls in docstrings (#2393)
Muscaw Jun 1, 2023
1bc6179
Add `name`, `display_title` and `path` attributes to `WorkflowRun` (#…
nuang-ee Jun 1, 2023
2d5933f
Add `name` filter to `Repository.get_artifacts()` (#2459)
trim21 Jun 1, 2023
f00b0be
Merging 1.58.x patch release notes into master (#2525)
EnricoMi Jun 1, 2023
fe2525a
Add sort order and direction for getting comments (#2544)
EnricoMi Jun 7, 2023
8b27cc5
Add authentication classes, move auth logic there (#2528)
EnricoMi Jun 8, 2023
14e902e
Make `MainClass.get_app` return completed `GithubApp` when slug is gi…
EnricoMi Jun 8, 2023
cb13705
Add support for new RepositoryAdvisories API :tada: (#2483)
JLLeitschuh Jun 9, 2023
1e16ae0
Fix auth issues with `Installation.get_repos` (#2547)
EnricoMi Jun 13, 2023
6a41b40
Make Requester.__createException robust against missing message and b…
EnricoMi Jun 13, 2023
cc6a627
Add support for environments (#2223)
alson Jun 13, 2023
c504fb8
Implement `AppUserAuth` for Github App user tokens (#2546)
EnricoMi Jun 14, 2023
41c79dd
feat(PullRequest.create_review_comment): new parameters and better te…
heitorpolidoro Jun 14, 2023
7543301
Merge branch 'master' into fix_create_comment_issues
heitorpolidoro Jun 14, 2023
18a9e57
style(PullRequest.create_review_comment): fixing lint errors
heitorpolidoro Jun 14, 2023
69f85a3
Merge branch 'master' into fix_create_comment_issues
heitorpolidoro Jun 14, 2023
7feb50c
Update github/PullRequest.py
heitorpolidoro Jun 15, 2023
10254c2
fix(PullRequest.create_review_comment): Change default from None to N…
heitorpolidoro Jun 15, 2023
f9196de
fix(PullRequest.create_review_comment): Change default from None to N…
heitorpolidoro Jun 15, 2023
6453e32
style(PullRequest.create_review_comment): minor changes
heitorpolidoro Jun 15, 2023
50f5398
style(PullRequest.create_review_comment): lint
heitorpolidoro Jun 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 60 additions & 10 deletions github/PullRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,36 +398,86 @@ def as_issue(self):
headers, data = self._requester.requestJsonAndCheck("GET", self.issue_url)
return github.Issue.Issue(self._requester, headers, data, completed=True)

def create_comment(self, body, commit_id, path, position):
def create_comment(self, body, commit, path, position):
"""
:calls: `POST /repos/{owner}/{repo}/pulls/{number}/comments <https://docs.github.com/en/rest/reference/pulls#review-comments>`_
:param body: string
:param commit_id: :class:`github.Commit.Commit`
:param commit: :class:`github.Commit.Commit`
:param path: string
:param position: integer
:rtype: :class:`github.PullRequestComment.PullRequestComment`
"""
return self.create_review_comment(body, commit_id, path, position)
return self.create_review_comment(body, commit, path, position)

def create_review_comment(self, body, commit_id, path, position):
def create_review_comment(
self,
body,
commit,
path,
# line replaces deprecated position argument, so we put it between path and side
line=github.GithubObject.NotSet,
side=github.GithubObject.NotSet,
start_line=github.GithubObject.NotSet,
start_side=github.GithubObject.NotSet,
in_reply_to=github.GithubObject.NotSet,
subject_type=github.GithubObject.NotSet,
as_suggestion=False,
):
"""
:calls: `POST /repos/{owner}/{repo}/pulls/{number}/comments <https://docs.github.com/en/rest/reference/pulls#review-comments>`_
:param body: string
:param commit_id: :class:`github.Commit.Commit`
:param commit: :class:`github.Commit.Commit`
heitorpolidoro marked this conversation as resolved.
Show resolved Hide resolved
:param path: string
:param position: integer
:param line: integer
:param side: string
:param start_line: integer
:param start_side: string
:param in_reply_to: integer
:param subject_type: string
:param as_suggestion: bool interprets the body as suggested code and modifies it accordingly
:rtype: :class:`github.PullRequestComment.PullRequestComment`
"""
assert isinstance(body, str), body
assert isinstance(commit_id, github.Commit.Commit), commit_id
assert isinstance(commit, github.Commit.Commit), commit
assert isinstance(path, str), path
assert isinstance(position, int), position
assert line is github.GithubObject.NotSet or isinstance(line, int), line
assert side is github.GithubObject.NotSet or side in ["LEFT", "RIGHT"], side
assert start_line is github.GithubObject.NotSet or isinstance(
start_line, int
), start_line
assert start_side is github.GithubObject.NotSet or start_side in [
"LEFT",
"RIGHT",
], start_side
assert in_reply_to is github.GithubObject.NotSet or isinstance(
in_reply_to, int
), in_reply_to
assert subject_type is github.GithubObject.NotSet or subject_type in [
"LINE",
"FILE",
"side",
], subject_type
assert isinstance(as_suggestion, bool), as_suggestion

if as_suggestion:
body = f"```suggestion\n{body}\n```"
post_parameters = {
"body": body,
"commit_id": commit_id._identity,
"commit_id": commit._identity,
"path": path,
"position": position,
}
if line is not github.GithubObject.NotSet:
post_parameters["line"] = line
if side is not github.GithubObject.NotSet:
post_parameters["side"] = side
if start_line is not github.GithubObject.NotSet:
post_parameters["start_line"] = start_line
if start_side is not github.GithubObject.NotSet:
post_parameters["start_side"] = start_side
if in_reply_to is not github.GithubObject.NotSet:
post_parameters["in_reply_to"] = in_reply_to
if subject_type is not github.GithubObject.NotSet:
post_parameters["subject_type"] = subject_type
headers, data = self._requester.requestJsonAndCheck(
"POST", f"{self.url}/comments", input=post_parameters
)
Expand Down
61 changes: 61 additions & 0 deletions tests/PullRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,67 @@ def testCreateComment(self):
)
self.assertEqual(comment.id, 886298)

def testCreateReviewCommentInReplyTo(self):
commit = self.repo.get_commit("8a4f306d4b223682dd19410d4a9150636ebe4206")
comment = self.pull.create_review_comment(
"Comment created by PyGithub",
commit,
"src/github/Issue.py",
5,
in_reply_to=42,
)
self.assertEqual(comment.id, 886298)

def testCreateReviewCommentSubjectType(self):
commit = self.repo.get_commit("8a4f306d4b223682dd19410d4a9150636ebe4206")
comment = self.pull.create_review_comment(
"Comment created by PyGithub",
commit,
"src/github/Issue.py",
5,
subject_type="FILE",
)
self.assertEqual(comment.id, 886298)

def testCreateMultilineReviewComment(self):
commit = self.repo.get_commit("8a4f306d4b223682dd19410d4a9150636ebe4206")
comment = self.pull.create_review_comment(
"Comment created by PyGithub",
commit,
"src/github/Issue.py",
10,
start_line=5,
)
self.assertEqual(comment.id, 886298)

def testCreateMultilineReviewCommentAsSuggestion(self):
commit = self.repo.get_commit("8a4f306d4b223682dd19410d4a9150636ebe4206")
comment = self.pull.create_review_comment(
"Comment created by PyGithub",
commit,
"src/github/Issue.py",
10,
start_line=5,
as_suggestion=True,
)
self.assertEqual(comment.id, 886298)
self.assertEqual(
comment.body, "```suggestion\nComment created by PyGithub\n```"
)

def testCreateMultilineReviewCommentChoosingSide(self):
commit = self.repo.get_commit("8a4f306d4b223682dd19410d4a9150636ebe4206")
comment = self.pull.create_review_comment(
"Comment created by PyGithub",
commit,
"src/github/Issue.py",
10,
start_line=5,
side="RIGHT",
start_side="RIGHT",
)
self.assertEqual(comment.id, 886298)

heitorpolidoro marked this conversation as resolved.
Show resolved Hide resolved
def testGetComments(self):
epoch = datetime.datetime(1970, 1, 1, 0, 0)
comments = self.pull.get_comments(sort="updated", direction="desc", since=epoch)
Expand Down
4 changes: 2 additions & 2 deletions tests/ReplayData/PullRequest.testCreateComment.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ api.github.com
None
/repos/PyGithub/PyGithub/pulls/31/comments
{'Content-Type': 'application/json', 'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
{"body": "Comment created by PyGithub", "commit_id": "8a4f306d4b223682dd19410d4a9150636ebe4206", "position": 5, "path": "src/github/Issue.py"}
{"body": "Comment created by PyGithub", "commit_id": "8a4f306d4b223682dd19410d4a9150636ebe4206", "line": 5, "path": "src/github/Issue.py"}
201
[('status', '201 Created'), ('x-ratelimit-remaining', '4953'), ('content-length', '937'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"7ca841d45253326d61bee20c809872dc"'), ('date', 'Sun, 27 May 2012 09:40:12 GMT'), ('content-type', 'application/json; charset=utf-8'), ('location', 'https://api.github.com/repos/PyGithub/PyGithub/pulls/comments/886298')]
{"updated_at":"2012-05-27T09:40:12Z","position":5,"original_position":5,"body":"Comment created by PyGithub","url":"https://api.github.com/repos/PyGithub/PyGithub/pulls/comments/886298","commit_id":"8a4f306d4b223682dd19410d4a9150636ebe4206","created_at":"2012-05-27T09:40:12Z","user":{"url":"https://api.github.com/users/jacquev6","avatar_url":"https://secure.gravatar.com/avatar/b68de5ae38616c296fa345d2b9df2225?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"b68de5ae38616c296fa345d2b9df2225","login":"jacquev6","id":327146},"original_commit_id":"8a4f306d4b223682dd19410d4a9150636ebe4206","path":"src/github/Issue.py","id":886298,"_links":{"self":{"href":"https://api.github.com/repos/PyGithub/PyGithub/pulls/comments/886298"},"html":{"href":"https://github.com/PyGithub/PyGithub/pull/31#r886298"},"pull_request":{"href":"https://api.github.com/repos/PyGithub/PyGithub/pulls/31"}}}
{"updated_at":"2012-05-27T09:40:12Z","line":5,"original_position":5,"body":"Comment created by PyGithub","url":"https://api.github.com/repos/PyGithub/PyGithub/pulls/comments/886298","commit_id":"8a4f306d4b223682dd19410d4a9150636ebe4206","created_at":"2012-05-27T09:40:12Z","user":{"url":"https://api.github.com/users/jacquev6","avatar_url":"https://secure.gravatar.com/avatar/b68de5ae38616c296fa345d2b9df2225?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"b68de5ae38616c296fa345d2b9df2225","login":"jacquev6","id":327146},"original_commit_id":"8a4f306d4b223682dd19410d4a9150636ebe4206","path":"src/github/Issue.py","id":886298,"_links":{"self":{"href":"https://api.github.com/repos/PyGithub/PyGithub/pulls/comments/886298"},"html":{"href":"https://github.com/PyGithub/PyGithub/pull/31#r886298"},"pull_request":{"href":"https://api.github.com/repos/PyGithub/PyGithub/pulls/31"}}}

22 changes: 22 additions & 0 deletions tests/ReplayData/PullRequest.testCreateMultilineReviewComment.txt

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions tests/ReplayData/PullRequest.testCreateReviewCommentInReplyTo.txt

Large diffs are not rendered by default.

Large diffs are not rendered by default.