🙌

ワーキングブランチ名生成CIの作り方

に公開

正直内容は簡単なのですが、個人的にブランチ名を考えるストレスから解放されて負担が減った気がするのでGitHub ActionsのCIでワーキングブランチ名を生成する方法を紹介します。

動き

issueを作成した際に、指定した文字列 + issue番号 の形式でブランチ名をコメントするアクションです。


とりあえず使ってみたい方はこちらのリポジトリを参照してください。
https://github.com/SEKI-YUTA/action-comment-working-branch-name

ワークフローの実装

name: 'Comment Working Branch Name'
description: 'Adds a comment with a suggested branch name based on issue number and app name'
author: 'SEKI-YUTA'

inputs:
  app-name:
    description: 'The name of your application, which will be used in the branch name'
    required: true
    default: 'app'

runs:
  using: 'composite'
  steps:
    - name: Process branch name
      id: process-name
      shell: bash
      run: |
        PROCESSED_NAME=$(echo "${{ inputs.app-name }}" | sed "s/'/-/g" | sed "s/ /-/g" | sed "s/[^a-zA-Z0-9-]//g")
        echo "processed_name=$PROCESSED_NAME" >> $GITHUB_OUTPUT

    - name: Add comment
      shell: bash
      run: gh issue comment "$NUMBER" --body "$BODY"
      env:
        GH_TOKEN: ${{ github.token }}
        GH_REPO: ${{ github.repository }}
        NUMBER: ${{ github.event.issue.number }}
        BODY: |
          This issue's work branch is "${{ steps.process-name.outputs.processed_name }}-${{github.event.issue.number}}".
          branch name:
          ```
          ${{ steps.process-name.outputs.processed_name }}-${{github.event.issue.number}}
          ```
          To checkout this branch, run the following command:
          ```
          git checkout -b ${{ steps.process-name.outputs.processed_name }}-${{github.event.issue.number}}
          ```

やっていることの流れは以下です。

  1. 引数として渡された文字列を整形して変数に格納
  2. issueにコメントを追加

1でどのように整形しているかというと、'- に変換して、スペースを - に変換して、英数字以外の文字を削除しています。これはブランチ名として使える文字列に変換するためです。

おわりに

課題だと思っているのが、現状だとissue作成した時点でコメントが追加されるので、問題の報告共有として作成したIssueの場合にもコメントが追加されてしまいます。そのため、[Work]などのプレフィックスがタイトルについていた場合にコメントが追加されないようにするなどの改善をすることを考えています。
このアクションを使うことで、ブランチ名を考えるストレスから解放されることができると思います。ぜひ使ってみてください。

https://github.com/SEKI-YUTA/action-comment-working-branch-name

Discussion