aimdevel’s blog

勉強したことを書きます

Issue作成をトリガーにしてPull Requestを作成する

概要

ソースコードの特定の文字列を置き換えるような修正、例えばバージョン番号の変更など、を手動で行うのは手間もかかるし作業ミス発生の可能性もあると思います。
そこで、今回は以下が自動で行われるGithub Actionsのworkflowを作成しました。
Issueのdescriptionに更新情報を記入し、その情報を元にファイルの更新を行い、Pull Requestを発行する使い方を想定しています。

1. Issueをトリガにして動作開始
2. 対象のリポジトリをcheckoutする
3. ファイルを更新する
4. 変更をcommitする
5. Pull Requestを作成する

ソースコード

作成したworkflowは以下です。

name: make pull request from issue

on:
  issues:
    types: [labeled]

jobs:
  sync:
    if: github.event.label.name == 'enhancement'
    name: Run test flow
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v3
      - name: git setting 
        run : |
          git config --local user.email "github-actions@users.noreply.github.com"
          git config --local user.name "github-actions"
      - name: file update
        run : echo "${{github.event.issue.body}}" >> test_files/test_file
      - name: commit
        run : |
          git stage test_files/test_file
          git commit -m "test commit!"
      - name: pull request
        uses: peter-evans/create-pull-request@v4
        with : 
          token: ${{secrets.GITHUB_TOKEN }}
          branch: "issue#${{github.event.issue.number}}_enhancement"
          delete-branch: true
          title: "issue#${{github.event.issue.number}} ${{github.event.issue.title}}"

リポジトリは以下です。forkすれば使用できるはずです。

GitHub - aimdevel/my_github_actions_workflow

使用手順

  1. このworkflowをissueを作成できるgithubリポジトリに格納する。
  2. issuesタブのNew issueをクリックしissueを作成する。その際にはラベルにenhancementを指定する。
  3. issueを作成するとActionsタブでworkflowが実行されていることを確認できる。
  4. 数秒後にPull requestタブに新しいPull Requestが作成される。

次はworkflowの内容の説明をしていきます。

1. Issueをトリガにして動作開始

Issueにラベルが付いた時に実行されるようになっています。 github.event.label.nameにラベル名が格納されていますので、今回はifを使用してenhancementタグが付いている場合のみ処理するようにしています。
ただし、この方法だとラベルの付け直しでも処理が行われてしまうので、別のトリガにした方がいいかもしれないです。

on:
  issues:
    types: [labeled]

jobs:
  sync:
    if: github.event.label.name == 'enhancement'

2. 対象のリポジトリをcheckoutする

workflowの実行されているリポジトリをcheckoutします。

 steps:
      - name: checkout
        uses: actions/checkout@v3

3. ファイルを更新する

echoで既存のファイルにissueで指定したdescriptionを文字列で書き込んでいます。
github.event.issue.bodyにissueのdescriptionが格納されています。 今回はechoだけですが、ここに目的の処理を記述すればある程度複雑な更新も行えると思います。

     - name: file update
        run : echo "${{github.event.issue.body}}" >> test_files/test_file

4. 変更をcommitする

一般的なcommitコマンドです。

      - name: commit
        run : |
          git stage test_files/test_file
          git commit -m "test commit!"

5. Pull Requestを作成する

Pull Requestの作成には、マーケットプレイスのaction「create-pull-request」を使用します。
いくつかの変数を設定すると簡単にPull Requestを作成することができます。 今回は作成するbranchとPull Requestのタイトルにissueの番号やissueのタイトルを含めるようにしています。

      - name: pull request
        uses: peter-evans/create-pull-request@v4
        with : 
          token: ${{secrets.GITHUB_TOKEN }}
          branch: "issue#${{github.event.issue.number}}_enhancement"
          delete-branch: true
          title: "issue#${{github.event.issue.number}} ${{github.event.issue.title}}"

まとめ

Issue作成をトリガーにしてPull Requestを作成するworkflowを作成しました。
今回Github Actionsを触る前は、workflowの書き方が独特でとっつきにくい印象でしたが、 実際にはそこまででもないというと感じることができました。
今後も少しずつGithub Actionsを触ってみようと思います。

今後

  • 更新処理をより複雑なものに差し替えて動作を確認する
  • 自動ビルドを試す

参考資料

Create Pull Request · Actions · GitHub Marketplace · GitHub