Home > Mobile >  How to extract jira issue ticket in bash script?
How to extract jira issue ticket in bash script?

Time:04-13

I am trying to atomatize my git commits with bash functions so that they add a text before each commit if they detect that they are on a branch with a jira ticket.

For example I am working on a new issue and the branch is called bugfix/MR2-71-sidebar-modification where MR2-71 is the jira ticket number. I would like to find a general way to find as many cases as possible.

The bash functions I have are as follows

function getIssueName() {
    local string="$(git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')"

    # Extract the issue name from the branch name with regex without perl
    # For example extract MR2-16 of the string feature/MR2-16-requests-list
    # string variable will be feature/MR2-16-requests-list
    # issueName variable will be MR2-123 only
    local issueName="$(echo "$string" | sed -e 's/feature\/\(.*\)-.*/\1/' -e 's/bugfix\/\(.*\)-.*/\1/' -e 's/hotfix\/\(.*\)-.*/\1/' -e 's/release\/\(.*\)-.*/\1/' -e 's/\(.*\)-.*/\1/')"

    # if issueName is empty or equal to string then return empty
    if [ -z "$issueName" ] || [ "$issueName" = "$string" ]; then
        echo ""
    else
        echo "$issueName"
    fi
}

but issueName variable does not seem to have the ticket number correctly, as it sometimes brings extra string. for example MR2-71-sidebar-mod or MR2-75-table

Example content of the string variable (one for each line):

bugfix/MR2-38-refactor-routes-for-requests-list
bugfix/MR2-39-default-sorting-order-in-requests-list
feature/MR2-17-feature-clients-list
feature/MR2-34-laravel-9-upgrade

Example of the result that the function getIssueName should return (one for each line):

MR2-38
MR2-39
MR2-17
MR2-34

Any idea how to extract the issue number and make the function work in any case?

For example something like [aA-zZZ]-[0-9] (sorry I know almost nothing about regex)

CodePudding user response:

Using sed

$ sed 's|[^/]*/\([^-]*-[^-]*\).*|\1|' <<< "$string"
MR2-38
MR2-39
MR2-17
MR2-34

CodePudding user response:

You can use this sed:

getIssueName() {
   git branch --no-color | sed -E 's~^[^/] /([^-] -[^-] ).*~\1~'
}

Example:

cat file

bugfix/MR2-38-refactor-routes-for-requests-list
bugfix/MR2-39-default-sorting-order-in-requests-list
feature/MR2-17-feature-clients-list
feature/MR2-34-laravel-9-upgrade

sed -E 's~^[^/] /([^-] -[^-] ).*~\1~' file

MR2-38
MR2-39
MR2-17
MR2-34

CodePudding user response:

A project key is capital letters and numbers (I assume it has to start with a letter), so you might want

grep -E -o '[A-Z][A-Z0-9] -[0-9] '

Demo:

strings=(
    bugfix/MR2-38-refactor-routes-for-requests-list
    bugfix/MR2-39-default-sorting-order-in-requests-list
    feature/MR2-17-feature-clients-list
    feature/MR2-34-laravel-9-upgrade
)
for string in "${strings[@]}"; do
  grep -Eo '[A-Z][A-Z0-9] -[0-9] ' <<< "$string"
done
MR2-38
MR2-39
MR2-17
MR2-34

We can do this with just bash as well

if [[ $string =~ ([A-Z][A-Z0-9] -[0-9] ) ]]; then
  issue=${BASH_REMATCH[1]}
else
  echo "no issue number found in string '$string'" >&2
fi
echo $issue   # => MR2-34
  • Related