Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Creating a PR comment via REST API

anton.merzliakov July 3, 2023

Greetings.

I am trying to add a comment to my PR. I tried both the method from the documntation and from Google. Both return required key not provided and do not create a comment. As you can see, the error message doesn't tell me which required key is not provided.

The json from documentation is a long one

{
"type": "pullrequest_comment",

"id": 2154,
"created_on": "2023-06-28T12:44:36.726033+00:00",
"updated_on": "2023-06-28T12:44:36.726033+00:00",
"content": {
"raw": "test text",
"markup": "plaintext",
"html": ""
},
"user": {
"type": "user"
},
"deleted": False,
"inline": {
"from": 57,
"to": 122,
"path": ""
},
"pullrequest": {
"type": "pullrequest"
},
"resolution": {
"type": "<string>",
"user": {
"type": "user"
},
"created_on": "2023-06-28T12:44:36.726033+00:00"
}

As I can see from the documentation, this json has a required parameter resolution->type, but the documentation doesn't mention what are allowed values for this resolution->type. There's also inline->path that is also required and I also have no idea what to write as a path. The most stupid part is that the key content is not marked as required -- why would I create a comment with no content? But a random key resolution is required?

The simpler json I tried is just
{
"content": {
"raw": "first!"
}
}
I found it both on Stackoverflow and Atlassian Community. I have no idea why it worked for those people because this json clearly doesn't have a bunch of required parameters.

1 answer

1 accepted

1 vote
Answer accepted
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 4, 2023

Hello @anton.merzliakov and welcome to the Community!

You should be able to create a new comment in a Pull Request just providing the raw content, as in your example. However, it's important to include the header Content-Type: application/json in the request, otherwise it might fail with a bad request error as you received.

You can use the following example as a base to make your request : 

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"}}'

With this command, you should be able to successfully create a comment in the Pull Request ID provided in the URL.

Hope that helps! Let us know in case you run into any issues.

Thank you, @anton.merzliakov !

Patrik S

anton.merzliakov July 4, 2023

It actually worker! Really thank you a lot, Patrik!

(It would also be nice if the documentation had this example.)

Maybe you have any idea what is happening with Atlassian's Python library too? https://community.atlassian.com/t5/Bitbucket-questions/Create-a-PR-comment-using-Python/qaq-p/2407830#M95382

Sunny Ape
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 5, 2023

This is the risk you take when using third party libraries to 'short cut' your development work. It may or may not be maintained it to the exact extent needed.

If in doubt, always use the native functionality in your development language to interact with the REST APIs, and always use a test tool like Postman to validate your query before assuming your code is faulty.

PS. Atlassian, the company, doesn't own or maintain that python library. It's a public library that is maintained by volunteers. It's just called the 'Atlassian' library because it's used with Atlassian's products. If you have found a real bug with it, log it on its GitHub page.

anton.merzliakov July 6, 2023

Well, I did try the official way first. It was nice that Patrik understood what was wrong with my request. The documentation can definitely be improved.

muhammad mudassir March 1, 2025

@Patrik S I can't see a comment on my PR, although I am getting 201

Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 4, 2025

Hey @muhammad mudassir ,

could you share the exact command you're using to make the request so we can help investigate why it's not showing?

Please make sure to hide any sensitive/personal information when sharing the command as this is a public forum.

Like muhammad mudassir likes this
muhammad mudassir March 4, 2025

Thanks @Patrik S this issue is resolved but now I want to know if I can comment as a bot in PRs with my connect application, I am calling comment API and it simply comments on behalf of the user, I want to see my application name with [bot] tag there can you help me with this.

const jwtToken = atlassianJwt.encodeSymmetric(
    {
      iss: 'application-name',
      iat: now.unix(),
      exp: now.add(5, 'minutes').unix(),
      qsh: atlassianJwt.createQueryStringHash(
        atlassianJwt.fromMethodAndUrl(method, resource),
      ),
      sub: data.clientKey,
    },
    installation.sharedSecret
  );

 

Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 4, 2025

Hello @muhammad mudassir 

Thank you for sharing the command.

For development-related inquiries, I recommend posting your question on our developer-focused community:

That community allows you to collaborate with other developers, and the Atlassian engineering team is also active in responding to questions.

Patrik S

Like muhammad mudassir likes this
muhammad mudassir March 4, 2025

Thanks Mann!!

Suggest an answer

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

Atlassian Community Events