0

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.

1 Answer 1

1

You need spaces between the variables and the operator. And you don't need to double the brackets for this comparison.

Instead of

if [[ -n $(System.PullRequest.TargetBranch) ]]&&[[ "$(System.PullRequest.TargetBranch)"!="refs/heads/master" ]]; 

Try

target_branch=$(System.PullRequest.TargetBranch) # to simplify the next lines if [ -n "$target_branch" ] && [ "$target_branch" != "refs/heads/master" ]; then # ... 
1
  • Well... it works, I thought I had tried it with spaces... without spaces, with quotes, without quotes.. Apparently I did not try this... Thanks! Commented Sep 26, 2019 at 15:49

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.