본문 바로가기
프론트엔드

[github-action] PR에 연결된 issues에 특정 라벨이 있는지 확인하기

by veganee 2023. 5. 30.

1. issue 생성 시 QA 라벨을 붙인다.

 

2. PR opened 시 PR branch name에 있는 issue number를 issue_number 변수에 할당한다.

const { BRANCH_NAME } = process.env
const issue_number = String(BRANCH_NAME).split(‘-’)[2]

 

3. 추후 'Add ‘qc-local-ing’ label' step과 pr_merged job에서 사용하므로 output 변수에 등록한다.

 core.setOutput('ISSUE_NUMBER', issue_number)

 

4. actions/github-script@v6 library에서 제공하는 github.rest.issues.get API request body에 issue_number, owner, repo를 추가한다.

const { data: issueData } = await github.rest.issues.get({
      issue_number: issue_number,
      owner: context.repo.owner,
      repo: context.repo.repo,
    })

 

5. response에서 labels 정보가 array type으로 온다.

  "labels": [
    {
      "id": 0010101194,
      "node_id": "abc",
      "url": "https://api.github.com/repos/github_repo_path/labels/QA",
      "name": "QA",
      "color": "b52b05",
      "default": false,
      "description": "QA팀 등록 이슈 또는 검증이 필요한 티켓"
    }
  ],

해당 이슈에 'QA'라벨이 있는지 여부를 response에서 labels array값을 filter로 확인한다.

const isQALabelExist = issueData.labels.filter(({ name }) => name === 'QA').length === 1
core.setOutput('IS_QA_LABEL_EXIST', isQALabelExist)

해당 이슈에 'QA'라벨이 있는지 여부는 마찬가지로 추후 'Add ‘qc-local-ing’ label' step과 pr_merged job에서 사용하므로 output 변수에 등록한다.

 

6. ISSUE_NUMBER를 사용하는 방법은 같은 qa_pr_opened job에 있는 step의 경우, steps변수에 id(예시에서는 step1)를 통해 접근해 가져올 수 있다.

${{ steps.step1.outputs.ISSUE_NUMBER }}

다른 job(예시코드에서는 pr_merged)에서 사용하는 경우는, needs변수에 job name(qa_pr_opened)을 통해 접근할 수 있다.

if: ${{ needs.qa_pr_opened.outputs.IS_QA_LABEL_EXIST && github.event.pull_request.merged == true }}

물론 pr_opened에서 outputs 변수에 다른 job과 공유하고자하는 변수를 미리 등록해두어야 한다.

  pr_opened:
    runs-on: ubuntu-latest
    outputs:
      IS_QA_LABEL_EXIST: ${{ steps.step1.outputs.IS_QA_LABEL_EXIST }}
      ISSUE_NUMBER: ${{ steps.step1.outputs.ISSUE_NUMBER }}
 

전체 코드는 다음과 같다.

name: QA PR branch name에 있는 Issue Number card에 label 추가/삭제하기
run-name: QA PR Issue card labeling (${{ github.actor}})
on:
  pull_request:
    branches:
      - develop
      - releases/**
      - feature/**
    types: [opened, closed]

permissions:
  issues: write

env:
  BRANCH_NAME: ${{ github.head_ref || github.ref_name }}

jobs:
  qa_pr_opened:
    runs-on: ubuntu-latest
    outputs:
      IS_QA_LABEL_EXIST: ${{ steps.step1.outputs.IS_QA_LABEL_EXIST }}
      ISSUE_NUMBER: ${{ steps.step1.outputs.ISSUE_NUMBER }}
    steps:
      - name: check QA label exist
        id: step1
        uses: actions/github-script@v6
        with:
          script: |
            try {
              const { BRANCH_NAME } = process.env
              const issue_number = String(BRANCH_NAME).split('-')[2]      
              core.setOutput('ISSUE_NUMBER', issue_number)

              const { data: issueData } = await github.rest.issues.get({
                issue_number: issue_number,
                owner: context.repo.owner,
                repo: context.repo.repo,
              })

              const isQALabelExist = issueData.labels.filter(({ name }) => name === 'QA').length === 1
              core.setOutput('IS_QA_LABEL_EXIST', isQALabelExist)
              } catch (error){
              console.log('[get labels at issues error]', error)
            }

      - name: check qa label exist
        run: echo "${{ steps.step1.outputs.IS_QA_LABEL_EXIST }}"

      - name: Add 'qc-local-ing' label
        if: ${{ steps.step1.outputs.IS_QA_LABEL_EXIST && github.event.action == 'opened' }}
        uses: actions/github-script@v6
        with:
          script: |
            try {
              if(!${{ steps.step1.outputs.ISSUE_NUMBER }}){
                return;
              }

               github.rest.issues.addLabels({
                 issue_number: ${{ steps.step1.outputs.ISSUE_NUMBER }},
                 owner: context.repo.owner,
                 repo: context.repo.repo,
                 labels: ["qc-local-ing"]
               })
             } catch (error){
               console.log('[add label error]', error)
             }

  qa_pr_closed:
    runs-on: ubuntu-latest
    needs: qa_pr_opened
    if: ${{ needs.qa_pr_opened.outputs.IS_QA_LABEL_EXIST && github.event.pull_request.merged == true }}
    env:
      ISSUE_NUMBER: ${{ needs.qa_pr_opened.outputs.ISSUE_NUMBER }}
    steps:
      - name: Change 'qc-local-ing' label to 'qc-local-done' label
        uses: actions/github-script@v6
        with:
          script: |
            try {
              const { ISSUE_NUMBER } = process.env

              const { data: issueData } = await github.rest.issues.get({
                issue_number: ISSUE_NUMBER,
                owner: context.repo.owner,
                repo: context.repo.repo,
              })

              const isLocalQcIngLabelExist = issueData.labels.filter(({ name }) => name === 'qc-local-ing').length === 1
              console.log("isLocalQcIngLabelExist",isLocalQcIngLabelExist)

              if(!isLocalQcIngLabelExist){
                return;
              }

              github.rest.issues.addLabels({
                issue_number: ISSUE_NUMBER,
                owner: context.repo.owner,
                repo: context.repo.repo,
                labels: ["qc-local-done"]
              })
              github.rest.issues.removeLabel({
                issue_number: ISSUE_NUMBER,
                owner: context.repo.owner,
                repo: context.repo.repo,
                name: "qc-local-ing"
              })
            } catch (error){
              console.log('[change label error]', error)
            }