diff --git a/.github/workflows/commit-check.yml b/.github/workflows/commit-check.yml index 46de0b0..28615d0 100644 --- a/.github/workflows/commit-check.yml +++ b/.github/workflows/commit-check.yml @@ -10,11 +10,13 @@ jobs: steps: - name: Checkout 喵喵代码 uses: actions/checkout@v2 + with: + fetch-depth: 0 - name: 获取提交信息 id: get_commits run: | - echo "喵~ 正在获取提交信息呢~" + echo "正在获取提交信息" git fetch --no-tags origin ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} COMMITS=$(git log --pretty=format:"%s" ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }}) echo "找到以下提交:" @@ -25,30 +27,48 @@ jobs: id: check_commits continue-on-error: true run: | - echo "喵~ 开始检查提交信息啦~" + echo "开始检查提交信息" PATTERN='^(feat|fix|docs|style|refactor|perf|test|chore|ci)(\([a-zA-Z0-9\-]+\))?: .+' FAIL=0 + FAIL_MESSAGES="" while IFS= read -r line; do if [[ ! $line =~ $PATTERN ]]; then echo "❌ 无效的提交信息: $line" FAIL=1 + FAIL_MESSAGES="${FAIL_MESSAGES}\n❌ 无效的提交信息: $line" fi done <<< "${{ steps.get_commits.outputs.messages }}" if [ $FAIL -eq 1 ]; then echo "::set-output name=result::fail" + echo "::set-output name=failing_commits::$FAIL_MESSAGES" exit 1 else echo "::set-output name=result::pass" + echo "::set-output name=failing_commits::" fi + - name: 添加 PR 评论 + if: steps.check_commits.outputs.result == 'fail' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + echo "添加 PR 评论,通知主人哪些提交不符合规范" + FAILING_MESSAGES="${{ steps.check_commits.outputs.failing_commits }}" + COMMENT_BODY="检测到以下提交不符合 Angular 提交信息规范:${FAILING_MESSAGES}" + PR_NUMBER=${{ github.event.pull_request.number }} + curl -s -H "Authorization: token $GITHUB_TOKEN" \ + -H "Content-Type: application/json" \ + -d "{\"body\": \"$COMMENT_BODY\"}" \ + https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/comments + - name: 如果提交不符合规范,则自动关闭 PR if: steps.check_commits.outputs.result == 'fail' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - echo "喵~ 检测到有无效的提交信息,正在关闭该 PR 呀~" + echo "检测到有无效的提交信息,正在关闭该 PR" PR_NUMBER=${{ github.event.pull_request.number }} - curl -X PATCH \ + curl -s -X PATCH \ -H "Authorization: token $GITHUB_TOKEN" \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER \