Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,557,507
Community Members
 
Community Events
184
Community Groups

Push content of a JSON file as a PR Comment

I am trying to post a comment with the content of JSON file(example: cat somefile.json)  as a PR comment via rest API 2.0.

 

Any help can be appreciated

 

Thanks,

Srinivas Aluka

2 answers

0 votes
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
Nov 02, 2022

Hi @Srinivas Aluka,

You will need to pass the content of the file in the data of the request and any new lines should be escaped.

For example, if the content of the JSON file is the following:

{
"a": "a",
"b": "b"
}

You can post it as a PR comment via API with a call like the following:

curl -u BitbucketUsername:AppPassword \
-X POST "https://api.bitbucket.org/2.0/repositories/workspace-id/repo-slug/pullrequests/pr-id/comments" \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{"content":{"raw": "{ \n\"a\": \"a\", \n \"b\": \"b\" \n}"}}'

where

BitbucketUsername is the username of a Bitbucket account with access to the repo
AppPassword is an app password for the account
workspace-id is the workspace id of the workspace that owns the repo
pr-id is the id of the PR where you want to post the comment

Please feel free to let me know if that works for you and if you have any questions

Kind regards,
Theodora

A way to skirt around the comments issue is to add data to your JSON file that function as comments.

Let’s go through an example, starting with this information in our JSON file:

{
"sport": "basketball",
"coach": "Joe Smith",
"wins": 15,
"losses": 5
}

Now let’s add another key-value pair to serve as our comment, which you can see in the first line in the code below:

{
"_comment1": "this is my comment",
"sport": "basketball",
"coach": "Joe Smith",
"wins": 15,
"losses": 5
}
Here’s another example. This time, we use two underscores at the start and end of the key:

"__comment2__": "this is another comment",
The underscores help to differentiate the comment from the rest of the data in our file.

A Word of Caution
There’s an important detail to keep in mind.

The comments we added to our JSON file are included in the JSON object. In other words, the comments are treated as data.

Here’s what we mean.

This is the code in our file, data.json:

{
"_comment1": "this is my comment",
"sport": "basketball",
"coach": "Joe Smith",
"wins": 15,
"losses": 5
}
Now we’re going to read that data from the file, read_comments.py:

import json
with open("data.json", mode="r") as j_object:
data = json.load(j_object)
print(data)
The result includes our comment:

{'_comment1': 'this is my comment', 'sport': 'basketball', 'coach': 'Joe Smith', 'wins': 15, 'losses': 5}
We can even extract the comment's value from the JSON object: this is my comment:

import json

with open("data.json", mode="r") as j_object:
data = json.load(j_object)
print(data["_comment1"])
Keep in mind that the comment is only a comment in the eyes of the developer—not the computer.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events