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?
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.