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

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

M Bleyer August 11, 2021

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

0 votes
Robert Giddings [Adaptavist]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 26, 2021

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

M Bleyer December 2, 2021

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?

Robert Giddings [Adaptavist]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 7, 2021

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

M Bleyer December 7, 2021

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

Robert Giddings [Adaptavist]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 8, 2021

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

Robert Giddings [Adaptavist]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 19, 2022

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
M Bleyer January 20, 2022

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events