【问题标题】:Pre-commit hook to check for Jira issue key预提交挂钩以检查 Jira 问题密钥
【发布时间】:2018-06-18 15:27:31
【问题描述】:

我正在寻找一些帮助来在 Windows 上编写一个预提交挂钩,以便在提交时检查 Jira 问题密钥。如果 Jira 密钥不存在,则不应允许提交。我找不到任何方法。我是脚本新手。任何帮助将不胜感激。

【问题讨论】:

    标签: windows svn pre-commit-hook


    【解决方案1】:

    我假设您说的是 Git 存储库中的挂钩。

    • 导航到本地 Git 存储库并进入文件夹 .git\hooks
    • 创建一个名为 commit-msg 的文件
    • 插入以下内容(不知道如何正确格式化)
    #!/bin/bash
    # The script below adds the branch name automatically to
    # every one of your commit messages. The regular expression
    # below searches for JIRA issue key's. The issue key will
    # be extracted out of your branch name
    
    REGEX_ISSUE_ID="[a-zA-Z0-9,\.\_\-]+-[0-9]+"
    
    # Find current branch name
    BRANCH_NAME=$(git symbolic-ref --short HEAD)
    
    if [[ -z "$BRANCH_NAME" ]]; then
        echo "No branch name... "; exit 1
    fi
    
    # Extract issue id from branch name
    ISSUE_ID=$(echo "$BRANCH_NAME" | grep -o -E "$REGEX_ISSUE_ID")
    
    echo "$ISSUE_ID"': '$(cat "$1") > "$1"
    

    如果您现在有一个名为 feature/MYKEY-1234-That-a-branch-name 的分支 并添加为提交消息“添加新功能” 您的最终提交消息将如下所示 MYKEY-1234: Add a new feature

    您可以在使用 Git 2.9 时全局放置钩子。 请在此处找到更多有用信息:

    https://andy-carter.com/blog/automating-git-commit-messages-with-git-hooks

    Git hooks : applying `git config core.hooksPath`

    【讨论】:

    • 很好的答案!我唯一的建议是改进 REGEXP 以在 grep 上使用环视和 PCRE 标志使用更好的 REGEXP(当前一个匹配一些误报)。还检查未找到处理提交消息的无效问题 ID。
    • 我借用了这个解决方案的大部分内容发表了一篇文章:mobeigi.com/blog/programming/… 非常适合我!
    【解决方案2】:

    您可以使用 git 服务器端预接收挂钩。 https://git-scm.com/docs/git-receive-pack

    在下面的代码中要成功推送,您必须在提交的注释中指定 Jira 问题密钥。

    #!/bin/bash
        #
        # check commit messages for JIRA issue numbers
    
        # This file must be named pre-receive, and be saved in the hook directory in a bare git repository.
        # Run "chmod +x pre-receive" to make it executable.
        #
        # Don't forget to change
        # - Jira id regex
    
        jiraIdRegex="\[JIRA\-[0-9]*\]"
    
        error_msg="[POLICY] The commit doesn't reference a JIRA issue"
    
        while read oldrev newrev refname
        do
          for sha1Commit in $(git rev-list $oldrev..$newrev);
          do
            echo "sha1 : $sha1Commit";
            commitMessage=$(git log --format=%B -n 1 $sha1Commit)
    
    
            jiraIds=$(echo $commitMessage | grep -Pqo $jiraIdRegex)
    
            if ! jiraIds; then
              echo "$error_msg: $commitMessage" >&2
              exit 1
            fi
          done
        done
        exit 0
    

    【讨论】:

      【解决方案3】:

      您必须将以下脚本放入本地 Git 存储库中的 .git/hooks/prepare-commit-msg。这将在您添加新提交时运行。

      #!/bin/bash
      
      # get current branch
      branchName=`git rev-parse --abbrev-ref HEAD`
      
      # search jira issue id in pattern
      jiraId=$(echo $branchName | sed -nr 's,[a-z]*\/*([A-Z]+-[0-9]+)-.+,\1,p') 
      
      # only prepare commit message if pattern matched and jiraId was found
      if [[ ! -z $jiraId ]]; then
       # $1 is the name of the file containing the commit message
       sed -i.bak -e "1s/^/\n\n$jiraId: /" $1
      fi
      

      首先,我们获取分支名称,例如 feature/JIRA-2393-add-max-character-limit。

      接下来,我们提取密钥,去除前缀特征。

      生成的提交消息将以“JIRA-2393:”为前缀

      当没有前缀时,该脚本也可以工作,例如没有功能/,错误修复/等。

      【讨论】:

      • 我很难在 Windows 上实现这一目标。它根本不起作用。
      猜你喜欢
      • 2021-03-24
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      相关资源
      最近更新 更多