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,294,702
Community Members
 
Community Events
165
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: 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

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?

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

 

Pre_Hooks.png

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

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
TAGS
Community showcase
Published in Bitbucket

Git push size limits are coming to Bitbucket Cloud starting April 4th, 2022

Beginning on April 4th, we will be implementing push limits. This means that your push cannot be completed if it is over 3.5 GB. If you do attempt to complete a push that is over 3.5 GB, it will fail...

2,159 views 2 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you