How to create attachment in Bitbucket Server by Java API

Johannes Heger August 10, 2017

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

0 votes
dmitry August 9, 2021

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 = attachmentService.save(event.repository, 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();
commentService.addComment(commentRequest); 

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events