如何在 bash 脚本中提取 jira 问题单?
How to extract jira issue ticket in bash script?
我正在尝试使用 bash 函数原子化我的 git 提交,以便他们在每次提交之前添加一个文本,如果他们检测到他们在带有 jira 票证的分支上。
例如
我正在处理一个新问题,该分支称为
bugfix/MR2-71-sidebar-modification
其中 MR2-71
是 jira 票号。我想找到一个通用的方法来找到尽可能多的案例。
我的bash函数如下
function getIssueName() {
local string="$(git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)//')"
# 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\/\(.*\)-.*//' -e 's/bugfix\/\(.*\)-.*//' -e 's/hotfix\/\(.*\)-.*//' -e 's/release\/\(.*\)-.*//' -e 's/\(.*\)-.*//')"
# if issueName is empty or equal to string then return empty
if [ -z "$issueName" ] || [ "$issueName" = "$string" ]; then
echo ""
else
echo "$issueName"
fi
}
但 issueName
变量似乎没有正确的票号,因为它有时会带来额外的字符串。
例如 MR2-71-sidebar-mod
或 MR2-75-table
string
变量的示例内容(每行一个):
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
函数getIssueName
应该return的结果示例(每行一个):
MR2-38
MR2-39
MR2-17
MR2-34
知道如何提取问题编号并使该功能在任何情况下都能正常工作吗?
例如 [aA-zZZ]-[0-9]
(抱歉,我对正则表达式几乎一无所知)
你可以使用这个 sed:
getIssueName() {
git branch --no-color | sed -E 's~^[^/]+/([^-]+-[^-]+).*~~'
}
示例:
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~^[^/]+/([^-]+-[^-]+).*~~' file
MR2-38
MR2-39
MR2-17
MR2-34
使用sed
$ sed 's|[^/]*/\([^-]*-[^-]*\).*||' <<< "$string"
MR2-38
MR2-39
MR2-17
MR2-34
项目密钥是大写字母和数字(我假设它必须以字母开头),所以您可能需要
grep -E -o '[A-Z][A-Z0-9]+-[0-9]+'
演示:
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
我们也可以用 bash 做到这一点
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
我正在尝试使用 bash 函数原子化我的 git 提交,以便他们在每次提交之前添加一个文本,如果他们检测到他们在带有 jira 票证的分支上。
例如
我正在处理一个新问题,该分支称为
bugfix/MR2-71-sidebar-modification
其中 MR2-71
是 jira 票号。我想找到一个通用的方法来找到尽可能多的案例。
我的bash函数如下
function getIssueName() {
local string="$(git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)//')"
# 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\/\(.*\)-.*//' -e 's/bugfix\/\(.*\)-.*//' -e 's/hotfix\/\(.*\)-.*//' -e 's/release\/\(.*\)-.*//' -e 's/\(.*\)-.*//')"
# if issueName is empty or equal to string then return empty
if [ -z "$issueName" ] || [ "$issueName" = "$string" ]; then
echo ""
else
echo "$issueName"
fi
}
但 issueName
变量似乎没有正确的票号,因为它有时会带来额外的字符串。
例如 MR2-71-sidebar-mod
或 MR2-75-table
string
变量的示例内容(每行一个):
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
函数getIssueName
应该return的结果示例(每行一个):
MR2-38
MR2-39
MR2-17
MR2-34
知道如何提取问题编号并使该功能在任何情况下都能正常工作吗?
例如 [aA-zZZ]-[0-9]
(抱歉,我对正则表达式几乎一无所知)
你可以使用这个 sed:
getIssueName() {
git branch --no-color | sed -E 's~^[^/]+/([^-]+-[^-]+).*~~'
}
示例:
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~^[^/]+/([^-]+-[^-]+).*~~' file
MR2-38
MR2-39
MR2-17
MR2-34
使用sed
$ sed 's|[^/]*/\([^-]*-[^-]*\).*||' <<< "$string"
MR2-38
MR2-39
MR2-17
MR2-34
项目密钥是大写字母和数字(我假设它必须以字母开头),所以您可能需要
grep -E -o '[A-Z][A-Z0-9]+-[0-9]+'
演示:
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
我们也可以用 bash 做到这一点
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