Git/action

AutoAssign

Hamp 2025. 6. 11. 22:19
반응형

👋 들어가기 전

이번 내용은 프로젝트 협업에 유용한 custom workflow를 가져왔다.

바로 AutoAssign이다.

 

AutoAssign은 PR에서 담당자를 자동으로 지정해주는 workflow다.

 

 

매번 이렇게 수동으로 해주는 것도 좋지만, PR올리는 유저 = Assigne인 경우가 거의 대부분이기 때문에

 

이런 특징을 이용하면, Assign지정을 PR올리는 유저로 자동으로 지정해버리면 편할 것 같다.

🏁 학습할 내용

  • 전체 코드
  • 분석

📋 전체코드

먼저 오늘 분석할 workflow 코드를 살펴보자.

name: Auto assign PR author

on:
  pull_request:
    types:
      - opened
      - reopened

jobs:
  assign-pr-author:
    runs-on: ubuntu-latest

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

      - name: Get PR author
        id: get-pr-author
        run: echo "author=${{ github.event.pull_request.user.login }}" >> $GITHUB_OUTPUT

      - name: Assign PR author
        run: gh pr edit ${{ github.event.number }} --add-assignee ${{ steps.get-pr-author.outputs.author }}
        env:
          GH_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}

      - name: Comment success result to PR
        uses: mshick/add-pr-comment@v2
        if: ${{ success() }}
        with:
          message: |
            ## ✅ Assign 자동 지정을 성공했어요!
            @${{ steps.get-pr-author.outputs.author }}
          allow-repeats: true
          repo-token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}

      - name: Comment failure result to PR
        uses: mshick/add-pr-comment@v2
        if: ${{ failure() }}
        with:
          message: "## ❌ PR의 Assign 자동 지정을 실패했어요."
          allow-repeats: true
          repo-token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}

📊 분석

위 코드는 크게 3가지 부분으로 나눌 수 있다.

  • action trigger 설정
  • Assign 지정
  • 결과 코멘트 추가

🔫 Trigger 지정

name: Auto assign PR author

on:
  pull_request:
    types:
      - opened 
      - reopened

PR이 최초로 열리거나, 다시 열리면 실행한다.

✅ Assgin 지정

jobs:
  assign-pr-author:
    runs-on: ubuntu-latest

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

      - name: Get PR author
        id: get-pr-author
        run: echo "author=${{ github.event.pull_request.user.login }}" >> $GITHUB_OUTPUT

      - name: Assign PR author
        run: gh pr edit ${{ github.event.number }} --add-assignee ${{ steps.get-pr-author.outputs.author }}
        env:
          GH_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}

 

1) Get PR author step 부터 알아보자.

 

${{ github.event.pull_request.user.login }} 결과는 현재 PR 작성자의 ID를 얻는다.

그값을 steps.get-pr-author(step_id).outputs.author(variable_name)으로 얻을 수 있다.

 

2) Assign PR author

 

gh pr edit ${{ github.event.number }} --add-assignee ${{ steps.get-pr-author.outputs.author }}

 

현재 event번호(현재 이벤트 번호 =  PR번호)에 해당하는 PR을 수정한다.

 

이때 assignee을 위에서 구한, 현재 PR 작성자 ID로 추가한다.

 

💡 토큰을 사용한 이유는, PR 수정은 인증된 멤버만 할 수 있어야한다.

 

🫧 결과 코멘트 지정

      - name: Comment success result to PR
        uses: mshick/add-pr-comment@v2
        if: ${{ success() }}
        with:
          message: |
            ## ✅ Assign 자동 지정을 성공했어요!
            @${{ steps.get-pr-author.outputs.author }}
          allow-repeats: true
          repo-token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}

      - name: Comment failure result to PR
        uses: mshick/add-pr-comment@v2
        if: ${{ failure() }}
        with:
          message: "## ❌ PR의 Assign 자동 지정을 실패했어요."
          allow-repeats: true
          repo-token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}

 

https://github.com/mshick/add-pr-comment 를 사용하면 PR에 Comment를 손쉽게 추가할 수 있다.

 

${{success()}} 의 결과는 앞선 steps에서 실패한 내용이 없으면 true로 받는다.

반대로 ${{failure()}} 은 steps에서 실패한 내용이 있으면 true를 받는다.

 

assign 지정이 성공했을 경우 

 @${{ steps.get-pr-author.outputs.author }} 로 Comment가 달린다.

 

결과는 아래와 같다.

반응형