프론트엔드

[github-action] PR 생성한 branch name에 있는 Issue Number를 통해 Issue ticket에 label 추가/삭제하기

veganee 2023. 5. 16. 17:12
name: QA PR branch name에 있는 Issue Number card에 label 추가/삭제하기
run-name: QA PR Issue card labeling (${{ github.actor}})
on:
  pull_request:
    types: [opened, closed]

permissions:
  issues: write

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

jobs:
  job_opened:
    if: startsWith(github.event.pull_request.title,'[QA]') && github.event.action == 'opened'
    runs-on: ubuntu-latest

    steps:
      - name: check BRANCH_NAME
        run: |
          echo "BRANCH_NAME : $BRANCH_NAME"

      - name: Add 'qc-local-ing' label
        uses: actions/github-script@v6
        with:
          script: |
            try{
               const { BRANCH_NAME } = process.env
               const issue_num = String(BRANCH_NAME).split('-')[2]

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

  job_closed:
    if: startsWith(github.event.pull_request.title,'[QA]') && github.event.action == 'closed'
    runs-on: ubuntu-latest
    steps:
      - name: check BRANCH_NAME
        run: |
          echo "BRANCH_NAME: $BRANCH_NAME"

      - name: Change label 'qc-local-ing' to 'qc-local-done'
        uses: actions/github-script@v6
        with:
          script: |
            try{
              const { BRANCH_NAME } = process.env
              const issue_num = BRANCH_NAME.split('-')[2]

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

Use case

  1. PR 생성 시 branch 이름에 연관된 issue number 넣기 
    • github.head_ref = feature/client-hub-1372-github-action-test
  2. PR 생성 시 title 앞에 [QA]를 붙이기
    • [QA]를 붙이지 않으면 skip됨

  3. PR opened 시 PR opened한 branch name에 있는 issue number를 사용해 해당 issue에 qc-local-ing 추가됨
    • 예를 들어, branch name = feature/client-hub-1372-github-action-test 라면 issue number = 1372
  4. PR closed 시 qc-local-done label 추가 후 qc-local-ing label 삭제됨