How to enclose file to pull request comment via REST API in Bitbucket Server/DataCenter

Still need help?

The Atlassian Community is here for you.

Ask the community

For Atlassian eyes only

This article is Not Validated and cannot be shared with customers.

Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Summary

Bitbucket Server pull request comments allow to enclose file(s) from local drive (via Bitbucket Server UI) and in this Knowledge Base Article we will be demonstrating how to enclose file to pull request comment via REST API.

Environment

Bitbucket Server and DataCenter version 7.16+

Solution

Create a file

foo.txt
foo A
foo B
foo C

Add file as attachment

curl -k -u admin:admin \
     -X POST 'http://localhost:7990/projects/{projectKey}/repos/{repositorySlug}/attachments' \
     -H 'Content-Type: multipart/form-data' \
     -F 'files=@foo.txt'

As a response, you should expect below. Please take note of the attachment value in the response text as you will need it for the next API command. For example, the attachment value for the response text below is 1/3.

Sample response
{"attachments":
 [
  {
    "id":"3",
    "url":"http://localhost:7990/bitbucket/projects/TEST/repos/attachments/attachments/3",
    "links":{
        "self":{"href":"http://localhost:7990/bitbucket/projects/TEST/repos/attachments/attachments/3"},
        "attachment":{"href":"attachment:1/3"}
    	 }
   }
 ]
}

Add comment to pull-request, referring attachment in comment's message

Execute another API command to add a comment (with attachment) to a pull-request with the attachment value received from the previous API command. For example, the attachment value to add is 1/3.

curl -k -u admin:admin \
     -H 'Content-type: application/json' \
     -X POST 'http://localhost:7990/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/pull-requests/{pullRequestId}/comments' \
     -d '{"text": "From Local Drive - [foo.txt](attachment:1/3)"}'

As a response, you should expect:

Sample response
{
	"properties":{
		"repositoryId":1
	},
	"id":1,
	"version":0,
	"text":"From Local Drive - [foo.txt](attachment:1/1)",
	"author":{"name":"admin",
	"emailAddress":"admin@local.host",
	"id":2,
	"displayName":"admin",
	"active":true,
	"slug":"admin",
	"type":"NORMAL",
	"links":{
		"self":[
			{
				"href":"http://localhost:7990/users/admin"
			}
		]
	}
},
	"createdDate":1634907380937,
	"updatedDate":1634907380937,
	"comments":[

	],
	"tasks":[

	],
	"severity":"NORMAL",
	"state":"OPEN",
	"permittedOperations":{
		"editable":true,
		"transitionable":true,
		"deletable":true
	}
}

If you find any issue with the API call, please reach out to Bitbucket support.


Last modified on Oct 7, 2022

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.