refactor ♻️: Refactor PR check workflow by consolidating steps and removing redundant code
This refactoring consolidates the steps in the PR check workflow, reducing redundancy and improving efficiency.
This commit is contained in:
@@ -66,99 +66,99 @@ jobs:
|
||||
run: |
|
||||
ansible-lint
|
||||
|
||||
handle_failures:
|
||||
runs-on: ubuntu-latest
|
||||
needs: [leak_test, lint_test]
|
||||
if: needs.leak_test.result != 'success' || needs.lint_test.result != 'success'
|
||||
handle_failures:
|
||||
runs-on: ubuntu-latest
|
||||
needs: [leak_test, lint_test]
|
||||
if: needs.leak_test.result != 'success' || needs.lint_test.result != 'success'
|
||||
|
||||
steps:
|
||||
- name: Comment, label, and close PR
|
||||
run: |
|
||||
API="${{ vars.GIT_SERVER_URL }}/api/v1/repos/${{ github.repository }}"
|
||||
PR="${{ github.event.pull_request.number }}"
|
||||
TOKEN="${{ secrets.GITEA_TOKEN }}"
|
||||
steps:
|
||||
- name: Comment, label, and close PR
|
||||
run: |
|
||||
API="${{ vars.GIT_SERVER_URL }}/api/v1/repos/${{ github.repository }}"
|
||||
PR="${{ github.event.pull_request.number }}"
|
||||
TOKEN="${{ secrets.GITEA_TOKEN }}"
|
||||
|
||||
COMMENT_BODY="❌ CI checks failed.\n\nLeak test: ${{ needs.leak_test.result }}\nLint: ${{ needs.lint_test.result }}"
|
||||
COMMENT_BODY="❌ CI checks failed.\n\nLeak test: ${{ needs.leak_test.result }}\nLint: ${{ needs.lint_test.result }}"
|
||||
|
||||
# Find existing comment
|
||||
EXISTING_COMMENT_ID=$(curl -s -H "Authorization: token $TOKEN" \
|
||||
"$API/issues/$PR/comments" \
|
||||
| jq -r '.[] | select(.body | test("<!--ci-failed-comment-->")) | .id')
|
||||
# Find existing comment
|
||||
EXISTING_COMMENT_ID=$(curl -s -H "Authorization: token $TOKEN" \
|
||||
"$API/issues/$PR/comments" \
|
||||
| jq -r '.[] | select(.body | test("<!--ci-failed-comment-->")) | .id')
|
||||
|
||||
# Update or create comment
|
||||
if [ -n "$EXISTING_COMMENT_ID" ]; then
|
||||
# Update or create comment
|
||||
if [ -n "$EXISTING_COMMENT_ID" ]; then
|
||||
curl -s -X PATCH \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"body\":\"<!--ci-failed-comment-->\n$COMMENT_BODY\"}" \
|
||||
"$API/issues/$PR/comments/$EXISTING_COMMENT_ID"
|
||||
else
|
||||
curl -s -X POST \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"body\":\"<!--ci-failed-comment-->\n$COMMENT_BODY\"}" \
|
||||
"$API/issues/$PR/comments"
|
||||
fi
|
||||
|
||||
# Add label if missing
|
||||
LABELS=$(curl -s -H "Authorization: token $TOKEN" "$API/issues/$PR/labels" | jq -r '.[] | .name')
|
||||
if ! echo "$LABELS" | grep -q "^ci-failed$"; then
|
||||
curl -s -X POST \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '["ci-failed"]' \
|
||||
"$API/issues/$PR/labels"
|
||||
fi
|
||||
|
||||
# Close PR
|
||||
curl -s -X PATCH \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"body\":\"<!--ci-failed-comment-->\n$COMMENT_BODY\"}" \
|
||||
"$API/issues/$PR/comments/$EXISTING_COMMENT_ID"
|
||||
else
|
||||
curl -s -X POST \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"body\":\"<!--ci-failed-comment-->\n$COMMENT_BODY\"}" \
|
||||
"$API/issues/$PR/comments"
|
||||
fi
|
||||
|
||||
# Add label if missing
|
||||
LABELS=$(curl -s -H "Authorization: token $TOKEN" "$API/issues/$PR/labels" | jq -r '.[] | .name')
|
||||
if ! echo "$LABELS" | grep -q "^ci-failed$"; then
|
||||
curl -s -X POST \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '["ci-failed"]' \
|
||||
"$API/issues/$PR/labels"
|
||||
fi
|
||||
|
||||
# Close PR
|
||||
curl -s -X PATCH \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"state":"closed"}' \
|
||||
"$API/pulls/$PR"
|
||||
|
||||
handle_success:
|
||||
runs-on: ubuntu-latest
|
||||
needs: [leak_test, lint_test]
|
||||
if: needs.leak_test.result == 'success' && needs.lint_test.result == 'success'
|
||||
|
||||
steps:
|
||||
- name: Update comment, remove label, reopen PR
|
||||
run: |
|
||||
API="${{ vars.GIT_SERVER_URL }}/api/v1/repos/${{ github.repository }}"
|
||||
PR="${{ github.event.pull_request.number }}"
|
||||
TOKEN="${{ secrets.GITEA_TOKEN }}"
|
||||
|
||||
COMMENT_BODY="✅ All CI checks passed.\n\nLeak test: ${{ needs.leak_test.result }}\nLint: ${{ needs.lint_test.result }}"
|
||||
|
||||
# Find existing comment
|
||||
EXISTING_COMMENT_ID=$(curl -s -H "Authorization: token $TOKEN" \
|
||||
"$API/issues/$PR/comments" \
|
||||
| jq -r '.[] | select(.body | test("<!--ci-failed-comment-->")) | .id')
|
||||
|
||||
# Update comment if exists
|
||||
if [ -n "$EXISTING_COMMENT_ID" ]; then
|
||||
curl -s -X PATCH \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"body\":\"<!--ci-failed-comment-->\n$COMMENT_BODY\"}" \
|
||||
"$API/issues/$PR/comments/$EXISTING_COMMENT_ID"
|
||||
fi
|
||||
|
||||
# Remove label if exists
|
||||
LABELS=$(curl -s -H "Authorization: token $TOKEN" "$API/issues/$PR/labels" | jq -r '.[] | .name')
|
||||
if echo "$LABELS" | grep -q "^ci-failed$"; then
|
||||
curl -s -X DELETE \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
"$API/issues/$PR/labels/ci-failed"
|
||||
fi
|
||||
|
||||
# Reopen PR if closed
|
||||
PR_STATE=$(curl -s -H "Authorization: token $TOKEN" "$API/pulls/$PR" | jq -r '.state')
|
||||
if [ "$PR_STATE" = "closed" ]; then
|
||||
curl -s -X PATCH \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"state":"open"}' \
|
||||
-d '{"state":"closed"}' \
|
||||
"$API/pulls/$PR"
|
||||
fi
|
||||
|
||||
handle_success:
|
||||
runs-on: ubuntu-latest
|
||||
needs: [leak_test, lint_test]
|
||||
if: needs.leak_test.result == 'success' && needs.lint_test.result == 'success'
|
||||
|
||||
steps:
|
||||
- name: Update comment, remove label, reopen PR
|
||||
run: |
|
||||
API="${{ vars.GIT_SERVER_URL }}/api/v1/repos/${{ github.repository }}"
|
||||
PR="${{ github.event.pull_request.number }}"
|
||||
TOKEN="${{ secrets.GITEA_TOKEN }}"
|
||||
|
||||
COMMENT_BODY="✅ All CI checks passed.\n\nLeak test: ${{ needs.leak_test.result }}\nLint: ${{ needs.lint_test.result }}"
|
||||
|
||||
# Find existing comment
|
||||
EXISTING_COMMENT_ID=$(curl -s -H "Authorization: token $TOKEN" \
|
||||
"$API/issues/$PR/comments" \
|
||||
| jq -r '.[] | select(.body | test("<!--ci-failed-comment-->")) | .id')
|
||||
|
||||
# Update comment if exists
|
||||
if [ -n "$EXISTING_COMMENT_ID" ]; then
|
||||
curl -s -X PATCH \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"body\":\"<!--ci-failed-comment-->\n$COMMENT_BODY\"}" \
|
||||
"$API/issues/$PR/comments/$EXISTING_COMMENT_ID"
|
||||
fi
|
||||
|
||||
# Remove label if exists
|
||||
LABELS=$(curl -s -H "Authorization: token $TOKEN" "$API/issues/$PR/labels" | jq -r '.[] | .name')
|
||||
if echo "$LABELS" | grep -q "^ci-failed$"; then
|
||||
curl -s -X DELETE \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
"$API/issues/$PR/labels/ci-failed"
|
||||
fi
|
||||
|
||||
# Reopen PR if closed
|
||||
PR_STATE=$(curl -s -H "Authorization: token $TOKEN" "$API/pulls/$PR" | jq -r '.state')
|
||||
if [ "$PR_STATE" = "closed" ]; then
|
||||
curl -s -X PATCH \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"state":"open"}' \
|
||||
"$API/pulls/$PR"
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user