Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Add a Post-Merge Hook to Repository

tscullyncu May 5, 2015

Hello,

I would like to be able to add a post-merge like hook that would ssh into a production web server and pull changes from a repository that has had a pull request merged into it. I've read all the other posts on this and I see that a webhook is the most recommended method. However I am not comfortable with publishing a page that executes code on my public-facing server. Is there another way to use a webhook that I'm unaware of that would prevent potential unauthorized code execution on my public-facing server?

I am quite surprised that this isn't a feature that has been made more accessible (i.e. without having to write my own add-on) to paying customers. It seems like this should be a standard part of the hooks available to Stash.

Anyway, any insight would be greatly appreciated.

-Tim.

9 answers

1 accepted

0 votes
Answer accepted
Felipe Kraemer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 22, 2015

Hi Tim,

Stash Pull Requests use 'git merge' command for sure.

Kind regards,
Felipe Kraemer

0 votes
Aurélien Vallée May 3, 2019

Sorry to wake up an old thread, but I had the exact same need (perform automated tasks after a PR merge), and had the same struggles, so I figure I would post my solution here.

I spent quite some time trying to make BB work with git server-side hooks, but eventually forgot the idea. For some reason, BB does not trigger post-merge hooks when a PR is merged. My guess is that BB is using its own git implementation that does $

I thought of writing a BB plugin, but honestly I don't want to deal with Java, maven, XML... And even if I did that, maintaining the build and deployment stack of that plugin would have been a nightmare.

So instead, I found "ScriptRunner". It's a BB server plugin that basically allows you to run scripts whenever some event is triggered.

There are quite a lot of events, (PullRequestMergedEvent being one of them), and basically you can provide a groovy script to run when this even is triggered. This groovy script has access to the whole BB server API, which means retrieving meta-inf$

It's of course annoying to have to learn Groovy, but is 200% easier than rolling your own plugin. Here is what my "script" triggered on "PullRequestMergedEvent" looks like:

import com.atlassian.bitbucket.event.pull.PullRequestMergedEvent
import com.atlassian.bitbucket.repository.RepositoryService
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bitbucket.repository.RepositoryCloneLinksRequest
import com.atlassian.bitbucket.repository.RepositoryCloneLinksRequest.Builder

PullRequestMergedEvent event = (PullRequestMergedEvent)event

// The repository service handles generating clone URLs for repositories
def repository_service = ComponentLocator.getComponent(RepositoryService)

// Retrieve the target repository of the pull request
def repository = event.getPullRequest().getToRef().getRepository()

// Retrieve the SSH clone URL for this repository
def repository_clone_request = new RepositoryCloneLinksRequest.Builder()
.protocol('ssh')
.repository(repository)
.build()

def repository_clone_url = repository_service.getCloneLinks(repository_clone_request)
.getAt(0).getHref()

// Source branch of the pull request
def source = event.getPullRequest().getFromRef().getId()

// Target branch of the pull request
def destination = event.getPullRequest().getToRef().getId()

// Only run this script when merging from "release/*" to "master"
def is_release = (destination == 'refs/heads/master') && source.startsWith('refs/heads/release/')

if (is_release) {
// <Perform the release>
}
Raghav Vaidhyanathan May 18, 2020

Thanks Aurélien Vallée .. this is similar to what i was looking for .. in Post receive hook scriptrunner, how do you get the event object binding ? When i use the above script under Post receive hook it errors saying event is not a local variable.  

0 votes
Aurélien Vallée May 3, 2019

Sorry to wake up an old thread, but I had the exact same need (perform automated tasks after a PR merge), and had the same struggles, so I figure I would post my solution here.

I spent quite some time trying to make BB work with git server-side hooks, but eventually forgot the idea. For some reason, BB does not trigger post-merge hooks when a PR is merged. My guess is that BB is using its own git implementation that does $
I thought of writing a BB plugin, but honestly I don't want to deal with Java, maven, XML... And even if I did that, maintaining the build and deployment stack of that plugin would have been a nightmare.

So instead, I found "ScriptRunner". It's a BB server plugin that basically allows you to run scripts whenever some event is triggered.

There are quite a lot of events, (PullRequestMergedEvent being one of them), and basically you can provide a groovy script to run when this even is triggered. This groovy script has access to the whole BB server API, which means retrieving meta-inf$

It's of course annoying to have to learn Groovy, but is 200% easier than rolling your own plugin. Here is what my "script" triggered on "PullRequestMergedEvent" looks like:

import com.atlassian.bitbucket.event.pull.PullRequestMergedEvent
import com.atlassian.bitbucket.repository.RepositoryService
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bitbucket.repository.RepositoryCloneLinksRequest
import com.atlassian.bitbucket.repository.RepositoryCloneLinksRequest.Builder

PullRequestMergedEvent event = (PullRequestMergedEvent)event

// The repository service handles generating clone URLs for repositories
def repository_service = ComponentLocator.getComponent(RepositoryService)

// Retrieve the target repository of the pull request
def repository = event.getPullRequest().getToRef().getRepository()

// Retrieve the SSH clone URL for this repository
def repository_clone_request = new RepositoryCloneLinksRequest.Builder()
.protocol('ssh')
.repository(repository)
.build()

def repository_clone_url = repository_service.getCloneLinks(repository_clone_request)
. getAt(0).getHref()

// Source branch of the pull request
def source = event.getPullRequest().getFromRef().getId()

// Target branch of the pull request
def destination = event.getPullRequest().getToRef().getId()

// Only run this script when merging from "release/*" to "master"
def is_release = (destination == 'refs/heads/master') && source.startsWith('refs/heads/release/')

if (is_release) {
// <Perform the release>
}

 

 

 

0 votes
Aditya Sharma June 12, 2018

Hey Tim, were your worked around the sitisitua?

Please suggest your followed procedure if it was successful. Also will be able to do same after a merge also?

Thanks.

0 votes
tscullyncu June 22, 2015

Interesting, I'll have to play around with this. Thanks for your insight!

 

-Tim.

0 votes
tscullyncu June 1, 2015

That's not a bad idea. I had the same thought, but I wasn't sure I could do that without interfering with Stash's operations. Also, I've read elsewhere that a merge from a Stash Pull Request doesn't actually use the 'merge' command. Can you shed a little more light on this?

 

Thanks!

-Tim.

0 votes
Felipe Kraemer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 20, 2015

Hi Tim,

Maybe you don't need to write a plugin, but configure a Git post-merge hook?

http://git-scm.com/docs/githooks

If this answer was helpful, please allow me to ask you to mark my answer as accepted in order to have it in the top of the thread, also helping other customers.

Thanks in advance!

Kind regards,
Felipe Kraemer

0 votes
tscullyncu May 11, 2015

As I alluded to in my original post, I was hoping to avoid having to learn Java, learn the Atlassian libraries, and then write my own plugin. I just don't have the time at the moment.

As a workaround, I was able to get a webhook add-on to do what I needed by locking down a specific port on my webserver to only allow traffic from my development server's ip address.

But like I said, this sort of functionality seems like it should just be part of the out-of-box functionality.

0 votes
Balázs Szakmáry
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 5, 2015

You should write a plugin that has a listener to PullRequestMergedEvent and does the SSH/deployment you need in the event handler.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events