LoginSignup
1
1

GitHub issue へのラベル付けを強化する

Last updated at Posted at 2024-05-14

はじめに

皆さんはissueテンプレート1を使っていますか?
テンプレートには幾つかのフォーム要素があります。具体的な要素としてmarkdown, checkboxes, textareaなど色々とあります。
この中でもdropdownタイプは環境報告(例えば OSの選択など)等、色々な所で使われることがあると思います。そしてdropdownで選択されたもののラベルを付与したいと思ったことがないでしょうか?
今回はAdvanced Issue Labelerを使って自動的にラベル付けさせてみたいと思います。

本記事で作成したものは以下のリポジトリより確認できます。
動作が見たい方はissueを実際に作ってみてください。

issueテンプレートを作成

今回はバグ報告のissueを想定して入力フォームを作成していきます。

.github/ISSUE_TEMPLATE/bug_report.yml
(略)
  - type: dropdown
    id: operating-system
    attributes:
      label: Operating System
      description: The operating system where the bug occurred.
      options:
        - Windows
        - macOS
        - Linux
        - Other
    validations:
      required: true

  - type: dropdown
    id: browser
    attributes:
      label: Browser
      description: The browser where the bug occurred.
      options:
        - Chrome
        - Firefox
        - Safari
        - Edge
        - Other
    validations:
      required: true

ここではOSとブラウザの種類を選択できるドロップダウンを用意しました。

GitHub Actionの設定

workflowsの設定

.github/workflows/issue_labeler.yml
name: Issue labeler
on:
  issues:
    types: [ opened ]

permissions:
  contents: read

jobs:
  label-component:
    runs-on: ubuntu-22.04

    permissions:
      issues: write
      
    strategy:
      matrix:
        template: [ bug_report.yml ]

    steps:
      - uses: actions/checkout@v3

      - name: Parse issue form
        uses: stefanbuck/github-issue-parser@v3
        id: issue-parser
        with:
          template-path: .github/ISSUE_TEMPLATE/${{ matrix.template }}

      - name: Set labels based on component field
        uses: redhat-plumbers-in-action/advanced-issue-labeler@v3
        with:
          issue-form: ${{ steps.issue-parser.outputs.jsonString }}
          template: ${{ matrix.template }}
          token: ${{ secrets.GITHUB_TOKEN }}

もしプライベートリポジトリで実行する場合は以下のコードを追加してください。

    permissions:
      issues: write
+     # only required for workflows in private repositories
+     actions: read
+     contents: read

ラベルの設定

.github/advanced-issue-labeler.yml
policy:
  - template: ["bug_report.yml"]
    section:
      - id: ["operating-system"]
        block-list: []
        label:
          - name: os:windows
            keys: ["Windows"]
          - name: os:macos
            keys: ["macOS"]
          - name: os:linux
            keys: ["Linux"]
          - name: os:other
            keys: ["Other"]
      - id: ["browser"]
        block-list: []
        label:
          - name: browser:chrome
            keys: ["Chrome"]
          - name: browser:firefox
            keys: ["Firefox"]
          - name: browser:safari
            keys: ["Safari"]
          - name: browser:edge
            keys: ["Edge"]
          - name: browser:other
            keys: ["Other"]

ここではパースされた結果とラベルの対応関係を設定します。idが一致するフォーを対象にして key がフォームの選択肢でそれが設定された時は name にあるラベルを付与するように。
例えば以下のようにすれば特定の機能が有効な場合のみラベルを付けるといったこともできます。

bug_report.yml
  - type: dropdown
    id: encryption
    attributes:
      label: Encryption
      description: The encryption method used.
      options:
        - Yes
        - No
advanced-issue-labeler.yml
      - id: ["encryption"]
        block-list: ["None", "No"]
        label:
          - name: Encrypted
            keys: ["Yes"]

動作

以上、作成したもので実際にissueを立ててみましょう。

まずはissueテンプレートを使ってissueを作成します。
issue-template-select-dropdown.png
OSとブラウザの種類を選択し、issueを作成するとGitHub Actionsが走り、以下のようにラベルが自動付与されます。
issues-result.png

まとめ

今回は入力内容に応じて自動的にラベル付けするということをしてみました。
内容自体は公式のページを見ればできるかもしれませんが、日本語で検索かけたときに理想としている動作を実現している記事が見かけなかったので紹介の意味も含めて本記事を作成しました。

この機能が必要となるのは相当な数のissueが立てられるOSSなので使いどころは悩む所がありますが、その様なものを見かけた時にそっと追加してあげるのもいいかもしれません。私はKawaiiLogosにPRを送ったらそのまま採用されました。(コミュニティに迷惑は掛けないようにしましょう)

最後まで読んで頂きありがとうございました!!

  1. https://docs.github.com/en/communities/using-templates-to-encourage-useful-issues-and-pull-requests/configuring-issue-templates-for-your-repository

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1