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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root


1 badge earned


Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!


Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.


Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!


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
Community Members
Community Events
Community Groups

How to test for regex pattern in Java files pre-commit using Scriptrunner

We're running Bitbucket and Scriptrunner (hosted).


When my developers commit multiple files, some of which match a list of defined file types (lets say Java or JS file)

I want to test these file contents for a regex and if present or missing I want to block/reject the commit.

Ideally Serverside using Scriptrunner with a pre-commit hook.


How can this be done?


1 answer

Hi @M Bleyer ,

Thank you for your question regarding this.

In ScriptRunner for Bitbucket we have an example Snippet showing how to load the contents of a JSON file.

The Snippet can be found on the Snippets of the Protect Git Refs pre-hook:

We are also planning to make examples around file reading more accessible to users, as currently it only exists as a Snippet example from within the product itself.

Please let me know if this helps solve what you wanted to achieve?

Kind regards,

Robert Giddings


thanks for the suggestion. I checked that page but it does not mention anything loading contents of a JSON file.

Basically I need to check the contents of all the files that were modified as part of a commit - and reject the commit if the files do not contain certain string patterns.

So how can I scan source code files for presence of a string (only those that are part of the commit) in a pre-hook?

Hi @M Bleyer ,

Thank you for your message.

Sorry for the confusion. The example code is not currently in the docs, but as a "Snippet" example inside the app itself.

If you navigate to the Protect Git Refs pre-hook inside ScriptRunner for Bitbucket and select the "Show snippets" under the "Condition" field, you should see a snippet titled "Load content of JSON file from commits". (You might need to scroll down the list).

Please let me know that you have found the snippet example OK, from within ScriptRunner for Bitbucket?

Kind regards,

Robert Giddings,

Product Manager, ScriptRunner for Bitbucket

Ah, thank you - now we're getting closer.

I found the snippets, but the mentioned one (Load content...) is not in the list.


Is there something my Bitbucket admins need to do? I am only Project admin.

Also, we're on version 6.31 only



Hi @M Bleyer ,

Thank you for your reply.

Currently the example snippet is only available for global admin users to use in global hooks.

This is because it makes use of a ContentService class that has access to all repositories.

So, for now, I would suggest asking a global administrator to create the hook for you, using the example snippet available to them. And then setting the hook to only be active for your repository.

Please let me know how you get on?

Kind regards,

Robert Giddings,

Product Manager, ScriptRunner for Bitbucket

Hi @M Bleyer ,

Further to my previous answer, we have now released a more comprehensive example of checking file content in a Script to our Adaptavist Library.

The Script can be found here:

Kind regards,

Robert Giddings,

Product Manager, ScriptRunner for Bitbucket

Like M Bleyer likes this

Great, thanks for following up! We'll give this a try right away. :-)

Suggest an answer

Log in or Sign up to answer

Atlassian Community Events