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.