如何在Bash脚本中提取JIRA发行票?

发布于 2025-01-20 18:58:37 字数 1571 浏览 1 评论 0原文

我正在尝试使用 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-modMR2-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 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

长亭外,古道边 2025-01-27 18:58:37

项目密钥是大写字母和数字(我假设它必须以字母开头),因此您可能需要

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

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
不知所踪 2025-01-27 18:58:37

使用sed

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

Using sed

$ sed 's|[^/]*/\([^-]*-[^-]*\).*|\1|' <<< "$string"
MR2-38
MR2-39
MR2-17
MR2-34
送舟行 2025-01-27 18:58:37

我最近为此编写了一个函数。它可能不是最漂亮/最理想的 bash,但它确实适用于相当多的情况。

需要注意的是 - 如果票证 ID 不存在,则此函数不会以非零值退出。它旨在从分支名称中提取信息,并对票据所在位置做出一些假设。票证 ID 的大小写或是否包含数字并不重要。

#!/usr/bin/env bash

# function to extract ticket id from branch name
function extract_ticket_id() {
  # handles extraction for branches that contain a forward slash
  # example: feat/TICKET-200-other-desciptors -> TICKET-200
  if [[ $1 == *"/"* ]]; then
    {
      echo "$1" | cut -d/ -f 2 | cut -d- -f 1,2
    }

  # handles ticket extraction from commitizen syntax (assumes ticket number is subject)
  # example: feat(TICKET-ID): add thing
  elif [[ $1 == *"("* ]]; then
  {
    echo "$1" | cut -d\( -f2 | cut -d\) -f1
  }
  # handles cases where branches use dashes to delimit terms
  # example: TICKET-200-other-desciptors -> TIC-200
  elif [[ $1 == *"-"* ]]; then
  {
    echo "$1" | cut -d- -f 1,2
  }

  fi
}

export -f extract_ticket_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.

#!/usr/bin/env bash

# function to extract ticket id from branch name
function extract_ticket_id() {
  # handles extraction for branches that contain a forward slash
  # example: feat/TICKET-200-other-desciptors -> TICKET-200
  if [[ $1 == *"/"* ]]; then
    {
      echo "$1" | cut -d/ -f 2 | cut -d- -f 1,2
    }

  # handles ticket extraction from commitizen syntax (assumes ticket number is subject)
  # example: feat(TICKET-ID): add thing
  elif [[ $1 == *"("* ]]; then
  {
    echo "$1" | cut -d\( -f2 | cut -d\) -f1
  }
  # handles cases where branches use dashes to delimit terms
  # example: TICKET-200-other-desciptors -> TIC-200
  elif [[ $1 == *"-"* ]]; then
  {
    echo "$1" | cut -d- -f 1,2
  }

  fi
}

export -f extract_ticket_id
暗藏城府 2025-01-27 18:58:37

您可以使用此 sed:

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

示例:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文