bitbucket api PostRepositoryHook is not invoked on pull request merged

天涯浪子 提交于 2019-12-23 04:52:07

问题


I am using PostRepositoryHook to develop plugin to listen for all the pushes made by developer. During testing I realized that it does work when I test it using command line to run git push command. However it doesn't work when I do PR and merge my PR.

Following are code details.

// LoggingPostRepositoryHook.java

import com.atlassian.bitbucket.hook.repository.PostRepositoryHook;
import com.atlassian.bitbucket.hook.repository.PostRepositoryHookContext;
import com.atlassian.bitbucket.hook.repository.RepositoryHookRequest;
import com.atlassian.bitbucket.hook.repository.SynchronousPreferred;


import javax.annotation.Nonnull;

/**
 * Example hook that logs what changes have been made to a set of refs
 */
@SynchronousPreferred(asyncSupported = false)
public class LoggingPostRepositoryHook implements PostRepositoryHook<RepositoryHookRequest> {

    @Override
    public void postUpdate(@Nonnull PostRepositoryHookContext context,
                           @Nonnull RepositoryHookRequest hookRequest) {

        String message = hookRequest.getRepository().getProject()+" "+ hookRequest.getRepository().getName();
        PostMessage postMessage = new PostMessage();
        postMessage.send(message);
        hookRequest.getScmHookDetails().ifPresent(scmDetails -> {
            hookRequest.getRefChanges().forEach(refChange -> {
                scmDetails.out().println("Thank you for pusing code! "+ message);
            });
        });


    }
}

// atlassian-plugin.xml
<repository-hook key="logging-hook" name="Logging Post Hook"
                   i18n-name-key="hook.guide.logginghook.name"
                   configurable="false"
                   class="com.myapp.impl.LoggingPostRepositoryHook">
    <description key="hook.guide.logginghook.description" />
  </repository-hook>


// PostMessage.java
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;

public class PostMessage {
    public void send(String message) {

        HttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead

        try {

            HttpPost request = new HttpPost("http://localhost:3008/git-hooks");
            StringEntity params =new StringEntity("details={\"message\":\""+message+"\"} ");
            request.addHeader("content-type", "application/x-www-form-urlencoded");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);

            //handle response here...

        }catch (Exception ex) {

            //handle exception here
            System.out.println(ex);

        }
    }
}

Please help.


回答1:


After reading more on bitbucket api, I realized that @SynchronousPreferred(asyncSupported = false) was unnecessary, once I removed it, now RepositoryHookRequest works for all three categories

  1. Push to repo
  2. Online edit
  3. PR merged >> This trigger two, one for PR merge and second for push on the target repository

Following is code for reference.

import com.atlassian.bitbucket.hook.repository.PostRepositoryHook;
import com.atlassian.bitbucket.hook.repository.PostRepositoryHookContext;
import com.atlassian.bitbucket.hook.repository.RepositoryHookRequest;

import javax.annotation.Nonnull;

public class PostCommitGlobalHook implements PostRepositoryHook<RepositoryHookRequest> {
    @Override
    public void postUpdate(@Nonnull PostRepositoryHookContext context,
                           @Nonnull RepositoryHookRequest hookRequest) {
        // Pass request to handler
        PostHookHandler handler = new PostHookHandler();
        handler.handleRequest(hookRequest);
    }
}


来源:https://stackoverflow.com/questions/50165483/bitbucket-api-postrepositoryhook-is-not-invoked-on-pull-request-merged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!