GitHub を Backlog の課題と連携する GitHub Action を公開しました

はじめに

プッシュされたコミットメッセージをBacklog課題のコメントに追加するGitHub Actionを公開しました。ぜひご利用ください。

GitHub Action

GitHub Actionとは、GitHub内で完結するCI / CDツールです。個人開発や仕事で大変お世話になっています。
Actions | GitHub
https://github.co.jp/features/actions
そのうち、仕事で活用できたパッケージを1つ公開し、GitHub Marketplaceに公開しています。どうぞご利用ください。

Backlog Notify

プッシュされたコミットメッセージをBacklog課題のコメントに追加するGitHub Actionです。キーワードによる課題の状態変更も可能です。
Backlog Notify · Actions · GitHub Marketplace
https://github.com/marketplace/actions/backlog-notify

使用方法

Backlog の Gitと同様です。ただし、今の所は先頭にある1つ目のキーしか認識しません。
付加機能として、コミットログで課題を操作できます。
  • #fix #fixes #fixed のどれかで処理済み
  • #close #closes #closed のどれかで完了
例えば下記のようにコミットメッセージを設定してください。
PROJECT-123 不具合修正 #fix
使用例です。
コミット21eaec3: PROJECT_2-5 仕様ですので修正しません #close コミットb9c827b: PROJECT_2-4 検索条件の修正 #fix
コミット例 GitHub のスクリーンショット
コミット21eaec3は、下記のようにコミットメッセージとともに、自動で完了にステータスを変更します。空コミットすることで、Backlogを開かずともコメントを残すことができます。また、コミットログやBacklogにも同じ情報を残せます。
Backlog.com の PROJECT_2-5 課題 スクリーンショット
コミットb9c827bは、下記のようにコミットメッセージとともに、自動で処理済みにステータスを変更します。GitHubのコミット差分にリンクされているので、Backlogのコメントから簡単に差分を確認できます。
Backlog.com の PROJECT_2-4 課題 スクリーンショット
GitHub Actionsは処理時間で課金されるためできるだけ実行時間を減らすよう設計しています。
ncc を活用し、単一のファイルにコンパイルしたNode.jsのプログラムとなっており、数秒で実行が完了します。2コミットでは3秒でした。とてもエコです。
GitHub Actions の実行履歴スクリーンショット

設定方法

BOT ユーザーの作成

  1. Backlogのプロジェクトに移動します。
  2. プロジェクト設定 → 参加ユーザー → 新しいユーザの追加はこちらからを選択します。
  3. クラシックプランの場合は 一般ユーザ 、新プランの場合は ゲスト を選択します。
  4. 登録します。
Backlog.com のスクリーンショット 「プロジェクト設定」→「参加ユーザー」→「新しいユーザーの追加はこちら」を選択する

Backlog API キーの取得

  1. 登録したBOTアカウントにログインします。
  2. 個人設定 → API → 登録でAPIキーを発行します。
Backlog.com のスクリーンショット 「ユーザー設定」→「API設定」からAPIを発行します

API キーを GitHub に登録

  1. GitHubのリポジトリページに移動します。
  2. Setting → Secrets → Add a new secretを選択します。
  3. Nameは BACKLOG_API_KEY とし、 ValueにAPIキーをそのまま貼り付けます。
  4. 登録します。
GitHubのプロジェクトスクリーンショット リポジトリの「Settings」→「Secrets」→「Add a new secret」 を選択して、 BACKLOG_API_KEY を追加する

collaborator による workflow の実行を制限

プライベートリポジトリの場合は下記の操作する必要はありません。
パブリックリポジトリの場合は、collaboratorからのworkflowの実行を制限してください。
  1. Setting → Actions → Fork pull request workflows from outside collaboratorsを開きます。
  2. Require approval for all outside collaborators を選択します。
GitHubのプロジェクトスクリーンショット リポジトリの「Settings」→「Actions」→「Fork pull request workflows from outside collaborators」

Workflow の作成

GitHub Actions workflowを作成します (例: .github/workflows/backlog-notify.yml )。 下記のような形式である必要があります。
name: Backlog Notify

on: push

jobs:
  notify:
    runs-on: ubuntu-latest

    steps:
      - name: Backlog Notify
        uses: bicstone/backlog-notify@v2
        with:
          # The following are required settings
          project_key: PROJECT_KEY
          api_host: example.backlog.jp
          api_key: ${{ secrets.BACKLOG_API_KEY }}

          # The following are optional settings
          fix_keywords: |-
            #fix
            #fixes
            #fixed
          close_keywords: |-
            #close
            #closes
            #closed
          push_comment_template: |-
            <%= commits[0].author.name %>さんがプッシュしました
            <% commits.forEach(commit=>{ %>
            + <%= commit.comment %> ([<% print(commit.id.slice(0, 7)) %>](<%= commit.url %>))<% }); %>
          commit_message_reg_template: "\
            ^\
            (<%= projectKey %>\\-\\d+)\\s?\
            (.*?)?\\s?\
            (<% print(fixKeywords.join('|')) %>|<% print(closeKeywords.join('|')) %>)?\
            $\
            "
          fix_status_id: 3
          close_status_id: 4

設定一覧

設定名説明
project_keyBacklog プロジェクトキー (必須)
api_hostBacklog のホスト (必須)
api_keyBacklog API キー (必須)
fix_keywords処理済みにするキーワード
close_keywords完了にするキーワード
push_comment_templateプッシュ時のコメント雛形
commit_message_reg_templateコミットメッセージ解析の正規表現雛形
fix_status_id処理済みの 状態 ID
close_status_id完了の 状態 ID

push_comment_template

プッシュ時のコメントの雛形を変更できます。
構文については lodash/template をご参照ください。
** 使用可能な変数 **
変数名
idstring
tree_idstring
distinctboolean
messagestring
timestampstring
urlstring
authorCommitter
committerCommitter
addedstring[]
modifiedstring[]
removedstring[]
issueKeystring
commentstring
keywordsstring
isFixboolean
isCloseboolean
Committer
変数名
namestring
emailstring &#124; null
datestring &#124; undefined
usernamestring &#124; undefined

commit_message_reg_template

コミットメッセージ解析の正規表現雛形を変更できます。
構文については lodash/template をご参照ください。
** 使用可能な変数 **
変数名
projectKeystring
fixKeywordsstring[]
closeKeywordsstring[]

まとめ

もし、便利だと思ったらスター頂ければ幸いです。反響があると、モチベーションに繋がります。
よろしくお願いいたします。
Backlog Notify · Actions · GitHub Marketplace
https://github.com/marketplace/actions/backlog-notify

ホームプロフィール外部リンクのため、別ウインドウで開きますプライバシーポリシー

時間のないサイト運営者リング外部リンクのため、別ウインドウで開きます

© 2024 Oishi Takanori / Made with Gatsby.js