From e3c492d93d0c89adbd1c4a6a35b045f2c0ea9b24 Mon Sep 17 00:00:00 2001 From: Saptarshi Sarkar Date: Fri, 12 Jan 2024 16:53:41 +0530 Subject: [PATCH] feat(CI): Add GitHub Markdown summary for linter CI (#412) * test: Added a comment to check for failed lint check * fix: Set continue on error option to true to upload artifacts * test: Added a wrong package name to check for failed linter workflow * test: Added a wrong package name to check for failed linter workflow * feat(CI): Added Workflow summary generation support to linter CI * revert: Reverted back the changes made solely for testing CI * fix: added ansi-remover * fix: added ansi-remover * feat(CI): Added Workflow summary generation support to linter CI * feat(CI): Added Workflow summary generation support to linter CI * feat(CI): Added Workflow summary generation support to linter CI * fix(CI): Fixed extra newlines in workflow summary * fix(CI): Fixed extra newlines in workflow summary * feat(CI): Added PR comment code * feat(CI): Added PR comment code * feat(CI): Added PR comment code * feat(CI): Added PR comment code * feat(CI): Added PR comment code * feat(CI): Added PR comment code * feat(CI): Added PR comment code * feat(CI): Added PR comment code * feat(CI): Added PR comment code * feat(CI): Added PR comment permission * feat(CI): Added PR comment permission * feat(CI): Added PR comment permission * feat(CI): Added PR comment permission * feat(CI): Added PR comment permission * feat(CI): Added PR comment permission * fix(CI): Fixed gh pr comment formatting issue * fix(CI): Fixed gh pr comment formatting issue * fix(CI): Fixed gh pr comment formatting issue * fix(CI): Fixed gh pr comment formatting issue * fix(CI): Fixed gh pr comment formatting issue * fix(CI): Fixed gh pr comment formatting issue * fix(CI): Fixed gh pr comment formatting issue * fix(CI): Fixed gh pr comment formatting issue * fix(CI): Fixed gh pr comment formatting issue * fix(CI): Fixed gh pr comment formatting issue * fix(CI): Fixed gh pr comment formatting issue * feat(CI): Added limit to run only the latest trigger * feat(CI): Added limit to run only the latest trigger * feat(CI): Added limit to run only the latest trigger * revert: Reverted back the changes made solely for testing CI * chore: Changed the summary for successful run of linter * fix: Fixed failure in running Build CI in source branch if it is triggered in PR --- .github/workflows/build.yml | 2 +- .github/workflows/linter.yml | 48 +++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0322a8082..0f66159e8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,7 +19,7 @@ on: type: boolean concurrency: - group: ${{ github.ref }} + group: ${{ github.event.pull_request.number || github.ref }} cancel-in-progress: true jobs: diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index a2becdbe1..12536edfe 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -4,14 +4,20 @@ on: push: pull_request: +concurrency: + group: ${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + jobs: build: name: Lint Code Base runs-on: ubuntu-latest permissions: - contents: read - packages: read + contents: write + pull-requests: write + issues: write statuses: write + packages: read steps: - name: Checkout Code uses: actions/checkout@v4 @@ -20,7 +26,9 @@ jobs: # list of changed files within `super-linter` fetch-depth: 0 - name: Lint Code Base + id: lint uses: super-linter/super-linter/slim@v5 + continue-on-error: true env: DEFAULT_BRANCH: master GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -39,4 +47,38 @@ jobs: uses: actions/upload-artifact@v4 with: name: linter-result - path: super-linter.log \ No newline at end of file + path: super-linter.log + - name: Generate Workflow Summary for successful run + if: ${{ steps.lint.outcome == 'success' }} + run: | + echo "## :ballot_box_with_check: Linting passed!" >> $GITHUB_STEP_SUMMARY + - name: Generate Workflow Summary for failed run + if: ${{ steps.lint.outcome == 'failure' }} + run: | + echo "## :x: Linting errors found!" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + cat super-linter.log | grep "\[ERROR]" | sed 's/[0-9A-Z:.-]\+ [0-9:]\+ //' | sed 's/\[ERROR]//' | sed 's/ //' | sed 's/Found errors in \[[A-Za-z]\+\] linter!//' | sed 's/Error code: 1. Command output://' | sed 's/ //' | sed 's/ERRORSFOUND//' | sed 's/ in [A-Z:]\+//' | sed 's/[[0-9]\+]//' | sed -r "s/\x1B\[(([0-9]+)(;[0-9]+)*)?[m,K,H,f,J]//g" | sed '1,2d' | tr -s '\n' '\n' | sed 's/\/github\/workspace\///' >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + - name: Comment on PR for failed run + if: ${{ steps.lint.outcome == 'failure' && github.event_name == 'pull_request' }} + shell: bash + run: | + COMMENT_INITIAL=$(echo -e "## :x: Linting errors found!\n") + COMMENT_MIDDLE=$(cat super-linter.log | grep "\[ERROR]" | sed 's/[0-9A-Z:.-]\+ [0-9:]\+ //' | sed 's/\[ERROR]//' | sed 's/ //' | sed 's/Found errors in \[[A-Za-z]\+\] linter!//' | sed 's/Error code: 1. Command output://' | sed 's/ //' | sed 's/ERRORSFOUND//' | sed 's/ in [A-Z:]\+//' | sed 's/[[0-9]\+]//' | sed -r "s/\x1B\[(([0-9]+)(;[0-9]+)*)?[m,K,H,f,J]//g" | sed '1,2d' | tr -s '\n' '\n' | sed 's/\/github\/workspace\///') + COMMENT_BODY=$(echo -e "$COMMENT_INITIAL \n @${{ github.actor }} Please fix the following errors:\n\n\`\`\`\n$COMMENT_MIDDLE\n\`\`\`") + COMMENT=$(echo "$COMMENT_BODY") + gh api \ + --method POST \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + /repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments \ + -f body="$COMMENT" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Fail Workflow for failure in linting + if: ${{ steps.lint.outcome == 'failure' }} + uses: actions/github-script@v7.0.1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + core.setFailed("Linting errors found! Please check workflow summary for details.") \ No newline at end of file