Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,369,943
Community Members
 
Community Events
168
Community Groups

Trying to automate the creation of a pull request and a merge

Our branching approach is to have a master and production branch. Changes committed to our production branch are pushed to our repository nightly, via a 'git push' on our production server. Upon completion of the push, we would like an automated process that would:

1. Create a pull request to merge changes from production branch to the master branch

2. Auto approve the pull request

3. Auto-merge the pull request from production branch to master branch

4. Send email to predefined BitBucket Admin user, if any of the previous steps failed.

I reviewed the tutorials and examples from ScriptRunner, but it does not appear to offer this type of 'post-hook' process. Is this type of automation possible? How would we accomplish this?

Thank you!

1 answer

1 accepted

0 votes
Answer accepted

Hi @rdennisj,

Thank you for your question.

I am the Product Manager for ScriptRunner for Bitbucket.

I would look at the following in order to answer your requirements using ScriptRunner for Bitbucket.

1. Create a pull request to merge changes from production branch to the master branch

For this you could use a Custom Post Hook (https://scriptrunner.adaptavist.com/latest/bitbucket/PostReceiveHooks.html#_custom_post_receive_hooks), where the custom code for the Post Hook programatically creates the Pull Request using the Bitbucket API.

However, as an alternative, I believe you could also use the same Custom Post Hook to programatically run a merge commit onto the master branch without the need for a Pull Request. Thus removing requirements 2 and 3 altogether.

2. Auto approve the pull request

Given that you also want to auto-merge a Pull Request, I don't believe this is actually required, as the Pull Request could be created with no approvers on it.

 3. Auto-merge the pull request from production branch to master branch

We do have a built-in Event Handler that allows for auto-merging of Pull Requests (https://scriptrunner.adaptavist.com/latest/bitbucket/StashEventHandlers.html#_auto_merge_pull_request).

However, I would recommend a Custom Event Handler that responded to a Pull Request Created Event, because of requirement number 4. Again, the Custom Event Handler could auto-merge the Pull Request using the Bitbucket API.

Here is an example Custom Event Handler (albeit that updates JIRA issues upon Pull Request creation): https://scriptrunner.adaptavist.com/latest/bitbucket/StashEventHandlers.html#_update_all_related_jira_issues_when_pull_request_opened

4. Send email to predefined BitBucket Admin user, if any of the previous steps failed.

We do again have built-in Post Hook's and Event Handlers that can send emails:

https://scriptrunner.adaptavist.com/latest/bitbucket/PostReceiveHooks.html#_send_mail

https://scriptrunner.adaptavist.com/latest/bitbucket/StashEventHandlers.html#_send_mail

However, if you are using the Custom Post Hook and Custom Event Handler suggests listed in sections 1 and 3 above, then you can include error handling in your custom scripts.

Here is an example Custom Event Handler, which posts custom Slack messages, when a Repo is forked for example: https://scriptrunner.adaptavist.com/latest/bitbucket/StashEventHandlers.html#_post_to_em_slack_em_when_a_repository_is_forked

Finally, this looks like an interesting use case of ScriptRunner for Bitbucket and I will look to see if we could include this functionality as a Built-in Script inside the product, in the future.

Please let me know if you require any additional help?

Kind regards,

Robert Giddings,

Product Manager, ScriptRunner for Bitbucket

Robert, Thank you for the detailed reply!  We will definitely pursue your suggestion in step 1 to run a merge commit, instead of creating a pull request. Our Production branch is the ONLY branch in our implementation that will apply updates to the Master branch, and will be done ONLY via nightly merges, so there is no change there will be any code conflicts, and no reason to go thru any code reviews or approvals. 

I will update this reply after we have completed/tested the Custom Post Hook.

Thank you again!

I would like this thread to be re-initiated, we are a corporate user of Atlassian products, we are looking for a way to 'lint' the PRs and auto-approve & merge based on the kind of changes done.

We dont have hooks enabled by our admin team, what options do i have to make this happen?

 

Appreciate your time and guidance.

Hi @suryachandra27 ,

Are you a ScriptRunner for Bitbucket customer?

As the ScriptRunner for Bitbucket team might be able to help you.

Kind regards,

Robert Giddings,

Product Manager, ScriptRunner for Bitbucket

Hi @Robert Giddings [Adaptavist] 

Is there a way for the listeners to work together with the merge checks, you see we are trying to perform an automatic merge from the pull request, but we are using the merge checks to perform certain validations, is this possible?

Hi @Jose Wannan ,

Yes, this is possible.

I am assuming you are using ScriptRunner's Built-In Listener "Auto-Merge Pull Requests on Approval", documented here: https://docs.adaptavist.com/sr4bib/latest/features/listeners/built-in-listeners/auto-merge-pull-requests-on-approval

In that documentation it says "It will only do this if can be merged with no conflicts, and no merge checks veto the merge."

Please let me know if you are experiencing any issues with this? And the auto-merge listener is not performing as expected?

Kind regards,

Robert Giddings,

Product Manager, ScriptRunner for Bitbucket

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events