如何在Bash脚本中提取JIRA发行票?
我正在尝试使用 bash 函数原子化我的 git 提交,以便在检测到自己位于带有 jira 票证的分支上时,在每次提交之前添加文本。
例如 我正在研究一个新问题,该分支称为 错误修复/MR2-71-sidebar-modification
其中 MR2-71 是 jira 票号。我想找到一种通用方法来找到尽可能多的案例。
我的 bash 函数如下,
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
}
但 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
应返回的结果(每行一个):
MR2-38
MR2-39
MR2-17
MR2-34
知道如何提取问题编号并使该函数在任何情况下都能工作吗?
例如像 [aA-zZZ]-[0-9]
(抱歉我对正则表达式几乎一无所知)
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 calledbugfix/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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
项目密钥是大写字母和数字(我假设它必须以字母开头),因此您可能需要
演示:
我们也可以仅使用 bash 来完成此操作
A project key is capital letters and numbers (I assume it has to start with a letter), so you might want
Demo:
We can do this with just bash as well
使用
sed
Using
sed
我最近为此编写了一个函数。它可能不是最漂亮/最理想的 bash,但它确实适用于相当多的情况。
需要注意的是 - 如果票证 ID 不存在,则此函数不会以非零值退出。它旨在从分支名称中提取信息,并对票据所在位置做出一些假设。票证 ID 的大小写或是否包含数字并不重要。
I recently wrote a function for this. It may not be the prettiest / most idomatic bash, but it does work for a decent amount of cases.
One caveat - this function doesn't exit non-zero if a ticket ID doesn't exist. It's designed to pull from the branch name, and lays out some assumptions on where the ticket would be. It does not matter what case the ticket ID is in, or whether it contains numbers.
您可以使用此 sed:
示例:
You can use this sed:
Example: