You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.