git hub에서 생성한 Issue가 PR시 종료되게 하려면close ${이슈번호}
를 붙이면 자동으로 Close되지만 이것은 default(main) 브랜치에서만 적용이 가능했다. 이런 문제를 해결하고 싶어 방법을 찾다가 git action을 사용해서 자동화 하는 방법이 있어 소개해보려고 한다.
name: Close associated issue
on:
pull_request:
branches:
- dev
types:
- closed
jobs:
close-issue:
runs-on: ubuntu-latest
steps:
- name: Close associated issue
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
PR_URL="https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER"
PR_BODY=$(curl -s -H "Authorization: token ${{ secrets.ACTION_TOKEN }}" $PR_URL | jq -r '.body')
ISSUE_NUMBER=$(echo $PR_BODY | grep -oE "close #[0-9]+" | tr -d 'close #')
if [[ ! -z "$ISSUE_NUMBER" ]]; then
curl -s -H "Authorization: token ${{ secrets.ACTION_TOKEN }}" -X PATCH "https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER" -d '{"state": "closed"}'
fi
shell: bash
Git hub api를 사용해 PR 이슈 번호를 가져온다.
PR 내용을 가져온다
PR 내용에서 Body값을 가져온다
정규 표현식을 사용해 body에서 close #${이슈번호}를 가져온다
이슈 번호가 있는 경우 이슈의 상태를 close한다.
Git secrets token이 필요하다.
워크 플로우 생성시 Marketplace에서 Automation Issue Closed 검색