I want to create a small bash script that checks whether a pull request is started from the correct branches, or exits on error if it is not.
This was my final attempt/iteration before I posted this question:
if [[ -n $(System.PullRequest.TargetBranch) ]]&&[[ "$(System.PullRequest.TargetBranch)"!="refs/heads/master" ]];
then
echo "$(System.PullRequest.TargetBranch)"!="refs/heads/master"
echo "Branch $(System.PullRequest.TargetBranch) is not master, proceed."
exit 0
fi
if [ -n $(system.pullRequest.sourceBranch) ];
then
if [ "$(system.pullRequest.sourceBranch)"!="refs/heads/hotfixes/"* ]&&[ "$(system.pullRequest.sourceBranch)"!="refs/heads/develop" ];
then
echo "Only hotfixes and develop are allowed to be pulled to the master branch"
exit 1
else
echo "$(system.pullRequest.sourceBranch) is allowed to be pulled to the master branch"
fi
else
echo "variable does not exists"
fi
Currently I am testing/building this script and I expect an exit 1 in my test case. But I am getting the following output:
refs/heads/master!=refs/heads/master
Branch refs/heads/master is not master, proceed.
I am guessing that I am doing something wrong with the string comparison statements (not yet to mention the hotfixes startswith check later on). But I can't figure out what I am doing wrong. I tried multiple variations, some give other errors. I have no idea where to be going from here.
As can be seen from the output, the two strings that are compared are in fact the same. So I am not sure where this is going wrong.
You need spaces between the variables and the operator. And you don't need to double the brackets for this comparison.
Instead of
Try