Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Add comment to pull request in a custom plugin with Java

maweeks
Contributor
June 11, 2018

Hello,

I am trying to write a bitbucket module that adds a comment to a pull request upon creation. I have searched a lot and found that the latest way to do it seems to be using ComponentLocator but didn't manage to make it work. I have also tried using ComponentImport.

In my atlassian-plugin.xml I only have plugin-info at the moment.

Is ComponentLocator the correct way to do it? If so how would I modify the code below to make it work so that the commentService can be used?

 

import com.atlassian.bitbucket.event.pull.PullRequestOpenRequestedEvent;
import com.atlassian.bitbucket.pull.PullRequest;
import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.plugin.spring.scanner.annotation.export.ExportAsService;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.sal.api.ApplicationProperties;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.Calendar;

@ExportAsService({MyPluginComponent.class})
@Named("myPluginComponent")
public class MyPluginComponentImpl implements MyPluginComponent, InitializingBean, DisposableBean {
@ComponentImport
private final ApplicationProperties applicationProperties;

@ComponentImport
private final EventPublisher eventPublisher;

@Inject
public MyPluginComponentImpl(final ApplicationProperties applicationProperties,
final EventPublisher eventPublisher)
{
this.applicationProperties = applicationProperties;
this.eventPublisher = eventPublisher;
}
@EventListener
public void OnPullRequestOpen(PullRequestOpenRequestedEvent pullRequestOpenRequestedEvent) {
PullRequest pullRequest = pullRequestOpenRequestedEvent.getPullRequest();
String repoProject = pullRequest.getToRef().getRepository().getProject().getKey();
String repoSlug = pullRequest.getToRef().getRepository().getSlug();

System.out.println("****** *** **** " + getCurrentTime() + " PullRequestOpen requested");
System.out.println("****** *** **** " + getCurrentTime() + " Project: " + repoProject);
System.out.println("****** *** **** " + getCurrentTime() + " Repo: " + repoSlug);

// AddCommentRequest addCommentRequest = new AddCommentRequest.Builder(pullRequest, "comment").build();
// Comment newComment = commentService.addComment(addCommentRequest);
}

public String getName()
{
if(null != applicationProperties)
{
return "myComponent:" + applicationProperties.getDisplayName();
}
return "myComponent";
}

@Override
public void destroy() throws Exception {
System.out.println("***** Destroying myPluginComponent");
this.eventPublisher.unregister(this);
}

@Override
public void afterPropertiesSet() throws Exception {
this.eventPublisher.register(this);
}

private String getCurrentTime() {
return Calendar.getInstance().getTime().toGMTString();
}
}

 

 

2 answers

1 accepted

0 votes
Answer accepted
maweeks
Contributor
June 12, 2018

Issue was on the following line:

public void OnPullRequestOpen(PullRequestOpenRequestedEvent pullRequestOpenRequestedEvent) {

Using the wrong Event, should be

public void OnPullRequestOpen(PullRequestOpenedEvent pullRequestOpenedEvent) {

 

And add the CommentService in exactly the same way that the EventPublisher is added.

0 votes
Piyush Khetan
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 19, 2018

I have a doubt here. I am creating a plugin which will check before merge if some files are not correct then need to add a comment on pull request. I used above code for making comments. But its not working. Kindly suggest what is wrong.




import com.atlassian.bitbucket.comment.CommentService;
import com.atlassian.bitbucket.content.ContentService;
import com.atlassian.bitbucket.hook.repository.PreRepositoryHookContext;
import com.atlassian.bitbucket.hook.repository.PullRequestMergeHookRequest;
import com.atlassian.bitbucket.hook.repository.RepositoryHookResult;
import com.atlassian.bitbucket.hook.repository.RepositoryMergeCheck;
import com.atlassian.bitbucket.pull.PullRequestService;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;

@Component("P2MergeCheck")
public class P2MergeCheck implements RepositoryMergeCheck {
private final PullRequestService pullRequestService;
private final ContentService contentService;
private final CommentService commentService;

@Autowired
public P2MergeCheck(@ComponentImport PullRequestService pullRequestService, @ComponentImport ContentService contentService, @ComponentImport CommentService commentService) {
this.pullRequestService = pullRequestService;
this.contentService = contentService;
this.commentService = commentService;
}

@Nonnull
@Override
public RepositoryHookResult preUpdate(@Nonnull PreRepositoryHookContext preRepositoryHookContext, @Nonnull PullRequestMergeHookRequest pullRequestMergeHookRequest) {
MergeCheckMigration mergeCheckMigration = new MergeCheckMigration(pullRequestService, contentService,commentService);

/*This should add comments on pull request. But not working. What is it I am missing here*/

AddCommentRequest addCommentRequest = new AddCommentRequest.Builder(pullRequestMergeHookRequest.getPullRequest(), "comment").build();

Comment newComment  = commentService.addComment(addCommentRequest);


return mergeCheckMigration.check(pullRequestMergeHookRequest.getPullRequest());
}
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events