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

コミット 21eaec3 は、下記のようにコミットメッセージとともに、自動で完了にステータスを変更します。空コミットすることで、Backlog を開かずともコメントを残すことができます。また、コミットログや Backlog にも同じ情報を残せます。

コミット b9c827b は、下記のようにコミットメッセージとともに、自動で処理済みにステータスを変更します。GitHub のコミット差分にリンクされているので、Backlog のコメントから簡単に差分を確認できます。

GitHub Actions は処理時間で課金されるためできるだけ実行時間を減らすよう設計しています。
ncc を活用し、単一のファイルにコンパイルした Node.js のプログラムとなっており、数秒で実行が完了します。2 コミットでは 3 秒でした。とてもエコです。

設定方法
BOT ユーザーの作成
- Backlog のプロジェクトに移動します。
- プロジェクト設定 → 参加ユーザー → 新しいユーザの追加はこちらからを選択します。
- クラシックプランの場合は 一般ユーザ 、新プランの場合は ゲスト を選択します。
- 登録します。

Backlog API キーの取得
- 登録した BOT アカウントにログインします。
- 個人設定 → API → 登録で API キーを発行します。

API キーを GitHub に登録
- GitHub のリポジトリページに移動します。
- Setting → Secrets → Add a new secret を選択します。
- Name は BACKLOG_API_KEY とし、 Value に API キーをそのまま貼り付けます。
- 登録します。

collaborator による workflow の実行を制限
プライベートリポジトリの場合は下記の操作する必要はありません。
パブリックリポジトリの場合は、collaborator からの workflow の実行を制限してください。
パブリックリポジトリの場合は、collaborator からの workflow の実行を制限してください。
- Setting → Actions → Fork pull request workflows from outside collaborators を開きます。
- Require approval for all 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_key | Backlog プロジェクトキー (必須) |
api_host | Backlog のホスト (必須) |
api_key | Backlog 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 をご参照ください。
構文については lodash/template をご参照ください。
** 使用可能な変数 **
変数名 | 型 |
---|---|
id | string |
tree_id | string |
distinct | boolean |
message | string |
timestamp | string |
url | string |
author | Committer |
committer | Committer |
added | string[] |
modified | string[] |
removed | string[] |
issueKey | string |
comment | string |
keywords | string |
isFix | boolean |
isClose | boolean |
Committer
変数名 | 型 |
---|---|
name | string |
string | null | |
date | string | undefined |
username | string | undefined |
commit_message_reg_template
コミットメッセージ解析の正規表現雛形を変更できます。
構文については lodash/template をご参照ください。
構文については lodash/template をご参照ください。
** 使用可能な変数 **
変数名 | 型 |
---|---|
projectKey | string |
fixKeywords | string[] |
closeKeywords | string[] |
まとめ
もし、便利だと思ったらスター頂ければ幸いです。反響があると、モチベーションに繋がります。
よろしくお願いいたします。
Backlog Notify · Actions · GitHub Marketplace
https://github.com/marketplace/actions/backlog-notify