How to test for regex pattern in Java files pre-commit using Scriptrunner
Asked by M Bleyer:

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?


Answers:
author: Robert Giddings [Adaptavist]
body: Hi [@M Bleyer](/t5/user/viewprofilepage/user-id/1106154) ,

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: <https://docs.adaptavist.com/sr4bib/latest/features/script-pre-receive-hooks/built-in-pre-receive-hooks/protect-git-refs>

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

comments: author: M Bleyer
body: Hi,

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?


author: Robert Giddings [Adaptavist]
body: Hi [@M Bleyer](/t5/user/viewprofilepage/user-id/1106154) ,

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


author: M Bleyer
body: 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

![Pre_Hooks.png](https://community.atlassian.com/t5/image/serverpage/image-id/172723iF63BCF8D9B318FDD/image-size/large?v=v2&px=999 "Pre_Hooks.png")


author: Robert Giddings [Adaptavist]
body: Hi [@M Bleyer](/t5/user/viewprofilepage/user-id/1106154) ,

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


author: Robert Giddings [Adaptavist]
body: Hi [@M Bleyer](/t5/user/viewprofilepage/user-id/1106154) ,

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: <https://library.adaptavist.com/entity/block-commits-for-files-containing-content-that-doesnt-match-regex>

Kind regards,

Robert Giddings,

Product Manager, ScriptRunner for Bitbucket


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


Tags: addon-com.onresolve.stash.groovy.groovyrunner

bitbucket-server

scriptrunner

server