Reputation: 1534
following is my Gitlab CI code:-
stages:
- check
variables:
JIRA_HEADER: "Accept: application/json"
jira:
stage: check
before_script:
#- apk add jq curl
- apk add --no-cache bash jq curl
image: python:3.7.4-alpine3.9
script:
- export MERGE_REQUEST_JIRA_ID=$(echo ${CI_MERGE_REQUEST_TITLE} | sed -r "s/^([A-Za-z][A-Za-z0-9]+-[0-9]+).*/\1/")
- echo $CI_MERGE_REQUEST_TITLE
- export JIRA_DETAIL=$(curl -u ${JIRA_USERNAME}:${JIRA_PASSWORD} -H "${JIRA_HEADER}" -X GET https://${JIRA_SERVER}/rest/api/2/issue/${MERGE_REQUEST_JIRA_ID}?fields=status)
- echo $JIRA_DETAIL
# extract the JIRA key id, this also validates JIRA issue referenced is valid
- export JIRA_KEY_ID=$(echo ${JIRA_DETAIL} | jq -e '.key')
- echo $JIRA_KEY_ID
# extract the JIRA status
- export JIRA_STATUS=$(echo ${JIRA_DETAIL} | jq '.fields.status.name')
- echo $JIRA_STATUS
- |
if [[ "$JIRA_STATUS" == "^(Done|Completed|Closed)$" ]]
then
echo "Invalid JIRA (Done/Completed/Closed) found!"
exit 1
else echo "Valid JIRA Id found!"
fi
only:
- merge_requests
I'm trying to validate the JIRA status by calling its API after retrieving Jira id from title of Merge Request. There is a problem in the If
condition below if [[ "$JIRA_STATUS" == "^(Done|Completed|Closed)$" ]]
as it is not validating it properly. Every time, the else condition is getting executed and printing the message as Valid JIRA Id found!
I would really appreciate if someone can help me to fix this minor issue. I want to gracefully exit the job with this message in the if
block as Invalid JIRA (Done/Completed/Closed) found! whenever the Jira status found to be in any of the given values as Done, Completed or Closed.
Upvotes: 2
Views: 196
Reputation: 1534
I'm finally able to resolve this issue by modifying the code like below:-
- |
if test -z "$(echo ${JIRA_STATUS} | sed -r "s/\"(Done|Completed|Closed)\"//")"
then
echo "Not a valid Jira (Done/Completed/Closed)"; exit 1
else
echo "Valid Jira found!"; echo $?
fi
I had used the test command along with if-else condition in Linux to make it work
Upvotes: 2