Create a PR comment using Python

anton.merzliakov July 4, 2023

I tried to use Atlassian Python library to create a comment for my PR and failed.

from atlassian import Bitbucket

bb = Bitbucket(
url='https://api.bitbucket.org',
username='anton_merzliakov',
password='<app-password>')

resp = bb.add_pull_request_comment('<project>', '<repo>', '<pr-id>', 'new comment')

The authentication definitely works correctly because some API calls work, just not the add_pull_request_comment

Using a debugger I found out what happened. The server returned:

{
"type": "error",
"error": {
"message": "Bad request",
"fields": {
"text": "extra keys not allowed",
"content": "required key not provided"
}
}
}

This is the very same error message I was getting trying to create a comment manually: https://community.atlassian.com/t5/Bitbucket-questions/Creating-a-PR-comment-via-REST-API/qaq-p/2406796 If the library itself doesn't know what keys to use, I definitely don't.

So, maybe I'm using an old version of the Python library? Maybe there's a workaround? Does Atlassian even support its Python library?

1 answer

1 accepted

0 votes
Answer accepted
anton.merzliakov July 5, 2023

Patrik S told me the correct command, but I'm still curious why the Python library cannot do the same, or how to make it use this very command.

The correct manual command is

curl -X POST -u USERNAME:APP_PASSWORD 'https://api.bitbucket.org/2.0/repositories/<workspace>/<Repository>/pullrequests/<Pull Request ID>/comments' --header 'Content-Type: application/json' --data '{"content":{"raw":"testing one comment"}}'

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events