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
Community Members
Community Events
Community Groups

How to create attachment in Bitbucket Server by Java API

In a Bitbucket Server add-on I like to create pull request comments of more than 32kB length. But the length is limited to that.

My idea is to create a text-file and attach it by Java API to that pull request.

There is the class com.atlassian.stash.internal.attach.AttachmentService in bitbucket-service-api-4.14.7.jar, but it seems to be internal only and not reachable at runtime from within my add-on.

Any ideas how to create an attachment? Or in general, how to add that information to a pull request?

Kind regards, Johannes

1 answer

Relevant for those who are below 7.x, above packages changed. and the type that is returned from attachmentService is now different. But that code should be the inspiration 

import com.atlassian.stash.internal.attach.AttachmentService
import com.atlassian.stash.internal.attach.AttachmentSupplier
import java.nio.charset.StandardCharsets
import com.atlassian.bitbucket.comment.AddCommentRequest
import com.atlassian.bitbucket.comment.AddCommentRequest.Builder

def PullRequest epr = event.getPullRequest();
def fromRefId = epr.fromRef.getId();
def toRefId = epr.toRef.getId();
def mergeCommit = event.commit.displayId

def StringBuilder sb = new StringBuilder();

epr.changesets.each{changeset -> changeset.changes.values.each{ change -> sb.append( change.getType().name() + " " + change.getPath() + "\n") } }

def att =, new AttachmentSupplier(){

public String getName(){
return "hoc.txt";

public long getSize(){
return sb.length();

public InputStream open(){
return new ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8));


def AddCommentRequest commentRequest = new AddCommentRequest.Builder(epr, "[hoc.txt](attachment:192/" + att +")").build();


Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Bitbucket

Git push size limits are coming to Bitbucket Cloud starting April 4th, 2022

Beginning on April 4th, we will be implementing push limits. This means that your push cannot be completed if it is over 3.5 GB. If you do attempt to complete a push that is over 3.5 GB, it will fail...

2,170 views 2 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you