How to enclose file to pull request comment via REST API

Still need help?

The Atlassian Community is here for you.

Ask the community

Background

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.

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:

Sample response
{
	"attachments": [
		{
			"id": "5b3728a433/foo.txt",
			"url": "http://localhost:7990/projects/{projectKey}/repos/{repositorySlug}/attachments/5b3728a433/foo.txt",
			"links": {
				"self": {
					"href": "http://localhost:7990/projects/{projectKey}/repos/{repositorySlug}/attachments/5b3728a433/foo.txt"
				},
				"attachment": {
					"href": "attachment:1/5b3728a433%2Ffoo.txt"
				}
			}
		}
	]
}

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

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](http://localhost:7990/projects/{projectKey}/repos/{repositorySlug}/attachments/5b3728a433/foo.txt)"}'

As a response, you should expect:

Sample response
{
	"properties": {
		"repositoryId": 1
	},
	"id": 45,
	"version": 0,
	"text": "From Local Drive - [foo.txt](http://localhost:7990/projects/{projectKey}/repos/{repositorySlug}/attachments/5b3728a433/foo.txt)",
	"author": {
		"name": "admin",
		"emailAddress": "admin@local.host",
		"id": 1,
		"displayName": "Admin Istrator",
		"active": true,
		"slug": "admin",
		"type": "NORMAL",
		"links": {
			"self": [
				{
					"href": "http://localhost:7990/users/admin"
				}
			]
		}
	},
	"createdDate": 1528687290320,
	"updatedDate": 1528687290320,
	"comments": [

	],
	"tasks": [

	],
	"permittedOperations": {
		"editable": true,
		"deletable": true
	}
}

Reference

Further information on https://docs.atlassian.com/bitbucket-server/rest/<bitbucket-server-version>/bitbucket-rest.html


Last modified on Jun 18, 2018

Was this helpful?

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