Skip to main content

Adding labels to issues

You can use GitHub Actions to automatically label issues.

Introduction

This tutorial demonstrates how to use the actions/github-script action in a workflow to label newly opened or reopened issues. For example, you can add the triage label every time an issue is opened or reopened. Then, you can see all issues that need to be triaged by filtering for issues with the triage label.

The actions/github-script action allows you to easily use the GitHub API in a workflow.

In the tutorial, you will first make a workflow file that uses the actions/github-script action. Then, you will customize the workflow to suit your needs.

Creating the workflow

  1. このプロジェクト管理ワークフローを適用したいリポジトリを選択してください。 書き込みアクセス権を持つ既存のリポジトリを利用することも、新しいリポジトリを作成することもできます。 リポジトリの作成の詳細については、「新しいリポジトリの作成」を参照してください。

  2. リポジトリに .github/workflows/YOUR_WORKFLOW.yml というファイルを作成します (YOUR_WORKFLOW は任意の名前に置き換えます)。 これがワークフローファイルです。 GitHub での新しいファイルの作成の詳細については、「新しいファイルの作成」を参照してください。

  3. Copy the following YAML contents into your workflow file.

    YAML
    name: Label issues
    on:
      issues:
        types:
          - reopened
          - opened
    jobs:
      label_issues:
        runs-on: ubuntu-latest
        permissions:
          issues: write
        steps:
          - uses: actions/github-script@v5
            with:
              script: |
                github.rest.issues.addLabels({
                  issue_number: context.issue.number,
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  labels: ["triage"]
                })
  4. Customize the script parameter in your workflow file:

    • The issue_number, owner, and repo values are automatically set using the context object. You do not need to change these.
    • Change the value for labels to the list of labels that you want to add to the issue. Separate multiple labels with commas. For example, ["help wanted", "good first issue"]. For more information about labels, see "Managing labels."
  5. ワークフローファイルを、リポジトリのデフォルトブランチにコミットしてください。 詳細については、「新しいファイルの作成」を参照してください。

Testing the workflow

Every time an issue in your repository is opened or reopened, this workflow will add the labels that you specified to the issue.

Test out your workflow by creating an issue in your repository.

  1. Create an issue in your repository. For more information, see "Creating an issue."
  2. To see the workflow run that was triggered by creating the issue, view the history of your workflow runs. For more information, see "Viewing workflow run history."
  3. When the workflow completes, the issue that you created should have the specified labels added.

Next steps