Manage PRs using the GitHub CLI client.
Comment on a particular line in a PR with Python.
import requests
# Replace the following placeholders with your actual values:
owner = "repository_owner_username"
repo = "repository_name"
pull_number = "pull_request_number"
commit_id = "commit_id"
path = "file_path"
line = "line_number"
body = "your_comment"
token = "your_github_personal_access_token"
url = f"https://api.github.com/repos/{owner}/{repo}/pulls/{pull_number}/comments"
headers = {
"Authorization": f"token {token}",
"Accept": "application/vnd.github+json"
}
data = {
"commit_id": commit_id,
"path": path,
"position": line,
"body": body
}
response = requests.post(url, json=data, headers=headers)
if response.status_code == 201:
print("Comment posted successfully")
else:
print(f"Error posting comment: {response.status_code} - {response.text}")
Do the same thing, but with curl.
curl -X POST \
-H "Authorization: token :your_token" \
-H "Accept: application/vnd.github+json" \
-d '{ "commit_id": ":commit_id", "path": ":path", "position": :line, "body": ":body" }' \
"https://api.github.com/repos/:owner/:repo/pulls/:pull_number/comments"