4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GithubActionsを使ってIssueのタイトルを元にラベルを貼ってみる!

Posted at

はじめに

こんにちは!
iOSエンジニアになることを目指して、Swiftの勉強しておりますそこら辺の大学2年生です!
最近、個人開発に取り入れようとGithubActionsの勉強しているので、それについてアウトプットできたらなと思います!
早速本題に移りたいと思います!

Issueのタイトルを取得する

色々なやり方でIssueのタイトルは取得できると思いますが、今回は「コンテキスト」を使って取得してみたいと思います!

下記の内容をymlファイルに定義します。

# workflowの名前 
name: Get Issue Contents

# トリガー条件を指定する
on:
  issues:
    types: [opened]

# jobを定義する -workflowの実行単位となる
jobs:
  get-issue-contents:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Get issue title
        run: |
          echo "Issue Title: ${{ github.event.issue.title }}"
          # 今回の本題ではないが、issueの内容もとることができる! ⬇️
          echo "Issue Body: ${{ github.event.issue.body }}"

簡単に説明

GithubActionsでは、「コンテキスト」というワークフローの実行やジョブ、ステップなどに関する情報にアクセスする方法が定義されています!
下記のような書き方で実際にコンテキストにアクセスすることができます!
${{ <context> }}
詳しくは下記リンクを参照ください🙏

また、環境変数を用意してあげて取得することもできます!

  - name: Get issue title
        id: issue
        run: |
          echo "issue_title=$ISSUE_TITLE"
        env: 
          ISSUE_TITLE: ${{ github.event.issue.title }}

実際にこれで、issueを作成してみると、タイトルが取得できると思います!

スクリーンショット 2023-05-28 20.39.17.png

続いてこのまま、取得したタイトルを元にラベルを貼ってみたいと思います!

受け取ったタイトルを別のアクションでも使えるようにする

実は前回の定義のままでは、取得したissueのタイトルを他のステップで参照することができません...
そのため、他のステップでも参照できるように下記のようにGithub Actionsが用意してくれているGITHUB_ENVという環境変数を使って、他のアクションでも使えるようにしてあげましょう。
以下のように修正します。

  - name: Get issue title
        id: issue
        run: |
          echo "issue_title=$ISSUE_TITLE"
          echo "ISSUE_TITLE=$ISSUE_TITLE" >> $GITHUB_ENV
        env: 
          ISSUE_TITLE: ${{ github.event.issue.title }}

これで他のステップissueのタイトルを渡すことができるようになりました!

ラベルを貼る

渡されたタイトルを元に、ラベルを貼り分けていきましょう!
これも色々なやり方でラベルを貼ることができると思いますが、「REST API」を使ってやりたいと思います!
REST APIについては下記のリンクを見ていただけるとわかると思います!


  - name: Add labels based on issue title
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    run: |
      title="$ISSUE_TITLE"
      if [[ $title == *"CI"* ]]; then
        echo "Adding 'CI' label to the issue"
        curl -X POST \
          -H "Authorization: Bearer $GITHUB_TOKEN" \
          -H "Accept: application/vnd.github.v3+json" \
          -d '{"labels": ["CI"]}' \
          "https://api.github.com/repos/アカウント名/リポジトリ名/issues/${{ github.event.issue.number }}/labels"
      elif [[ $title == *"Wontfix"* ]]; then
        echo "Adding 'Wontfix' label to the issue"
        curl -X POST \
          -H "Authorization: Bearer $GITHUB_TOKEN" \
          -H "Accept: application/vnd.github.v3+json" \
          -d '{"labels": ["Wontfix"]}' \
          "https://api.github.com/repos/アカウント名/リポジトリ名/issues/${{ github.event.issue.number }}/labels"
      else
        echo "No label to add"
      fi

手順

  1. curlコマンドを用いて、実際にGitHubのAPIにリクエストを送ります。
  2. ヘッダーの設定を行います
    ※ここではGithub Actionsが提供しているGITHUB_TOKENという特別な環境変数を使うことで、ワークフロー実行の認証を簡単に行うことができます。
  3. APIのバージョンを指定します。
  4. リクエストのボディに含まれるデータを指定します。
  5. 最後にラベルを追加するためのエンドポイントを記述すれば終わりです!!

あとはこれを実行すれば、ymlファイルに定義した条件式に応じてラベルを貼ることができるようになります!

以下はymlファイルの全体のコードとなります。

name: Add Labels to Issues
on:
  issues:
    types: [opened, reopened]

jobs:
  add-labels:
    name: Add Labels
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Get issue title
        id: issue
        run: |
          echo "issue_title=$ISSUE_TITLE"
          echo "ISSUE_TITLE=$ISSUE_TITLE" >> $GITHUB_ENV
        env: 
          ISSUE_TITLE: ${{ github.event.issue.title }}

      - name: Add labels based on issue title
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          title="$ISSUE_TITLE}"
          if [[ $title == *"CI"* ]]; then
            echo "Adding 'CI' label to the issue"
            curl -X POST \
              -H "Authorization: Bearer $GITHUB_TOKEN" \
              -H "Accept: application/vnd.github.v3+json" \
              -d '{"labels": ["CI"]}' \
              "https://api.github.com/repos/KantaSwift/WantListApp/issues/${{ github.event.issue.number }}/labels"
          elif [[ $title == *"Wontfix"* ]]; then
            echo "Adding 'Wontfix' label to the issue"
            curl -X POST \
              -H "Authorization: Bearer $GITHUB_TOKEN" \
              -H "Accept: application/vnd.github.v3+json" \
              -d '{"labels": ["Wontfix"]}' \
              "https://api.github.com/repos/KantaSwift/WantListApp/issues/${{ github.event.issue.number }}/labels"
          else
            echo "No label to add"
          fi

最後に

Github Actionsの勉強をはじめたのですが、初めて聞くような用語もたくさんあり、個人的にはかなり難しいです。
今回のやつもあまり実用性はないですね笑
深く探っていきたいと思います。

以下の記事を参考にさせていただきました🙇

4
0
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
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?