Git Pre -recieve hook to validate JIRA ID status

Hi

I am not able to figure out what I am doing WRONG.Need help.Basically when JIRA id is not valid the push should fail from local to remote Bitbucket repository.But when I enable the below script for pre receive logic even the correct JIRA id commit is failing and I am not able to push any commits from local repository to Bitbucket repository.Any help is greatly appreciated

REGEX=[BM]±[0-9]
ERROR_MSG="[POLICY] The commit doesn’t reference a JIRA issue"
while read OLDREV NEWREV REFNAME ; do
for COMMIT in git rev-list $OLDREV..$NEWREV;
do
MESSAGE=git cat-file commit $COMMIT | sed '1,/^$/d'|sed ‘1,/^$/d’|grep -o -E “[BM]±[0-9]+”|head -n1 // git cat-file commit df0965c4d564b02d116bb4aadc16a0d267e389c6
if ! echo $MESSAGE | grep -iqE “$REGEX”; then
echo “$ERROR_MSG: $MESSAGE” >&2
exit 1
fi
JSON_RETURN=curl -s http://hcs431brgppa911:8080/rest/api/latest/issue/$MESSAGE?fields=status -u username:password|egrep -o “Open|inprogress|Assigned|errorMessage”
if [-z “$JSON_RETURN”]
then
echo “The specified Jira Issue “$JIRAID” was not found in Jira server.” 1>&2
exit 1
fi
if ["$JSON_RETURN"=“errorMessage”]
then
echo “The specified Jira Issue “$JIRAID” was not found in Jira server.” 1>&2
exit 1
fi
done
done
exit 0

Hi @cpotti,

I’ve moved your question to our Bitbucket Server development category as this will probably be a better place for it.

Can you please also clean up your example code using the formatting tools? Currently, it’s quite difficult to read. That can help all of us to find your problem sooner.

One thing I already noticed is that your REGEX will never match a Jira issue key. If you want it to match these kinds of issue keys: BM-1, BM-33434,… (I’m just assuming this based on the information you’ve provided) You’ll need to use the following regex: BM-[0-9]* .

You can validate your regular expression here: https://regex101.com/