Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

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

Pre receive hooks to reject push on file that has blacklisted keywords

Linson Joseph
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 1, 2021

Hi,

We are trying to implement a pre receive hook at repository level. We want to reject push that contains sql files having blacklisted keywords. Currently don't see an option to enable/add custom pre-receive hook via Bitbucket UI.

Following the shell script we used as pre-commit hook in local repo.

########################

#!/bin/bash

LIST="DROP|COMMIT|TRUNCATE|ROLLBACK"

if git rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

for FILE in `git diff-index -name-status --cached $against – | cut -c3 | grep -E 'dbsql'` ; do

#Check if the file contains one of the words in LIST
if grep -iwE $LIST $FILE; then
echo $FILE." has one of the following blacklisted keywords you don't want to commit. Please remove it"
echo "Blacklisted keywords : DROP|COMMIT|TRUNCATE|ROLLBACK"
exit 1
fi
done
exit

##########################################

We have few questions:

a) Is it possible to add custom pre-receive hooks via bitbucket UI and provide the content as shell script?

b) Is it mandatory that content of pre-receive hook should be written in groovy scripting language?

Current setup:

Current pre hook settings in UI are below

image.png

When we click add hook, it is re-directing to market place.

 

Our use case : We want developers to block pushing the sql files having blacklisted keywords like DROP,COMMIT etc at bitbucket server side.

 

1 answer

0 votes
Evan Slaughter
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 3, 2021

Hi Linson,

There are a few different ways of going about creating your own custom logic for use with pre-receive hooks within Bitbucket - and ultimately all of our recommended options for doing so are going to be found in the following document:

I'd recommend going through the various solutions outlined in that article, and seeing which of the options there would likely work best for your team. And although we can't help with creating the hook itself, if you have any questions regarding this document as a whole you're always welcome to shoot us a ticket over at Atlassian Support!

Thanks again, and I hope you and your team have a great weekend! 😀

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events