From 957c6c8e224e4a9cfc542f711ac2b0a067976ccf Mon Sep 17 00:00:00 2001 From: OnkarRuikar <87750369+OnkarRuikar@users.noreply.github.com> Date: Thu, 3 Aug 2023 10:25:36 +0530 Subject: [PATCH] feat(ci): provide automated PR reviews for linting related changes --- .github/workflows/pr-check-lint_content.yml | 141 ++++++++++++++++++++ .github/workflows/pr-check_markdownlint.yml | 50 ------- 2 files changed, 141 insertions(+), 50 deletions(-) create mode 100644 .github/workflows/pr-check-lint_content.yml delete mode 100644 .github/workflows/pr-check_markdownlint.yml diff --git a/.github/workflows/pr-check-lint_content.yml b/.github/workflows/pr-check-lint_content.yml new file mode 100644 index 000000000000000..fa89b08f1c60880 --- /dev/null +++ b/.github/workflows/pr-check-lint_content.yml @@ -0,0 +1,141 @@ +name: Lint and review content files + +on: + pull_request_target: + branches: + - main + paths: + - .nvmrc + - "*.md" + - "files/**/*.md" + +permissions: + pull-requests: write + +jobs: + lint-and-review-docs: + runs-on: ubuntu-latest + + steps: + - name: Checkout BASE + uses: actions/checkout@v3 + + - name: Checkout HEAD + uses: actions/checkout@v3 + with: + ref: ${{ github.event.pull_request.head.sha }} + path: pr_head + + - name: Get changed content from HEAD + run: | + git config --global user.email "108879845+mdn-bot@users.noreply.github.com" + git config --global user.name "mdn-bot" + + rm -r files *.md + mv pr_head/files pr_head/*.md . + rm -r pr_head + git commit -am "Code from PR head" + + - name: Get changed files + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + BASE_SHA: ${{ github.event.pull_request.base.sha }} + HEAD_SHA: ${{ github.event.pull_request.head.sha }} + run: | + # Use the GitHub API to get the list of changed files + # documentation: https://docs.github.com/rest/commits/commits#compare-two-commits + DIFF_DOCUMENTS=$(gh api repos/{owner}/{repo}/compare/${{ env.BASE_SHA }}...${{ env.HEAD_SHA }} \ + --jq '.files | .[] | select(.status|IN("added", "modified", "renamed", "copied", "changed")) | .filename') + # filter out files that are not markdown + DIFF_DOCUMENTS=$(echo "${DIFF_DOCUMENTS}" | egrep -i "^files/.*\.md$" | xargs) + echo "DIFF_DOCUMENTS=${DIFF_DOCUMENTS}" >> $GITHUB_ENV + + - name: Setup Node.js environment + uses: actions/setup-node@v3 + with: + node-version-file: ".nvmrc" + cache: yarn + + - name: Install all yarn packages + run: yarn --frozen-lockfile + env: + # https://github.com/microsoft/vscode-ripgrep#github-api-limit-note + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Lint and format markdown files + run: | + # Generate random delimiter + # https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings + EOF="$(openssl rand -hex 8)" + + files_to_lint="${{ env.DIFF_DOCUMENTS }}" + + echo "Running markdownlint --fix" + MD_LINT_FAILED=false + MD_LINT_LOG=$(yarn markdownlint-cli2 --fix ${files_to_lint} 2>&1) || MD_LINT_FAILED=true + echo "MD_LINT_LOG<<${EOF}" >> $GITHUB_ENV + echo "${MD_LINT_LOG}" >> $GITHUB_ENV + echo "${EOF}" >> $GITHUB_ENV + echo "MD_LINT_FAILED=${MD_LINT_FAILED}" >> $GITHUB_ENV + + echo "Linting front-matter" + FM_LINT_FAILED=false + FM_LINT_LOG=$(node scripts/front-matter_linter.js --fix true ${files_to_lint} 2>&1) || FM_LINT_FAILED=true + echo "FM_LINT_LOG<<${EOF}" >> $GITHUB_ENV + echo "${FM_LINT_LOG}" >> $GITHUB_ENV + echo "${EOF}" >> $GITHUB_ENV + echo "FM_LINT_FAILED=${FM_LINT_FAILED}" >> $GITHUB_ENV + + echo "Running Prettier" + yarn prettier -w ${files_to_lint} + + if [[ -n $(git diff) ]]; then + echo "FILES_MODIFIED=true" >> $GITHUB_ENV + fi + + # info for troubleshooting + echo MD_LINT_FAILED=${MD_LINT_FAILED} + echo FM_LINT_FAILED=${FM_LINT_FAILED} + git diff + + - name: Setup reviewdog + uses: reviewdog/action-setup@v1 + with: + reviewdog_version: latest + + - name: Suggest changes using diff + if: env.FILES_MODIFIED == 'true' + env: + REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + TMPFILE=$(mktemp) + git diff >"${TMPFILE}" + git stash -u && git stash drop + reviewdog \ + -name="mdn-linter" \ + -f=diff \ + -f.diff.strip=1 \ + -reporter=github-pr-review < "${TMPFILE}" + + - name: Add reviews for markdownlint errors + if: env.MD_LINT_FAILED == 'true' + env: + REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + echo "${{ env.MD_LINT_LOG }}" | \ + reviewdog \ + -efm="%f:%l:%c %m" \ + -efm="%f:%l %m" \ + -name="markdownlint" \ + -diff="git diff" \ + -reporter="github-pr-review" + + - name: Fail if any issues pending + if: env.FILES_MODIFIED == 'true' || env.MD_LINT_FAILED == 'true' || env.FM_LINT_FAILED == 'true' + run: | + echo -e "\nLogs from markdownlint:" + echo "${{ env.MD_LINT_LOG }}" + echo -e "\nLogs from front-matter linter:" + echo "${{ env.FM_LINT_LOG }}" + echo -e "\nPlease fix all the linting issues mentioned in above logs and in the review comments." + exit 1 diff --git a/.github/workflows/pr-check_markdownlint.yml b/.github/workflows/pr-check_markdownlint.yml deleted file mode 100644 index abdbff3e6766866..000000000000000 --- a/.github/workflows/pr-check_markdownlint.yml +++ /dev/null @@ -1,50 +0,0 @@ -name: Markdownlint (PR files) - -on: - pull_request: - branches: - - main - paths: - - .nvmrc - - "**/*.md" - -jobs: - lint-docs: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - - name: Get changed files - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - BASE_SHA: ${{ github.event.pull_request.base.sha }} - HEAD_SHA: ${{ github.event.pull_request.head.sha }} - run: | - # Use the GitHub API to get the list of changed files - # documenation: https://docs.github.com/rest/commits/commits#compare-two-commits - DIFF_DOCUMENTS=$(gh api repos/{owner}/{repo}/compare/${{ env.BASE_SHA }}...${{ env.HEAD_SHA }} \ - --jq '.files | .[] | select(.status|IN("added", "modified", "renamed", "copied", "changed")) | .filename') - # filter out files that are not markdown - DIFF_DOCUMENTS=$(echo "${DIFF_DOCUMENTS}" | egrep -i "^files/.*\.md$" | xargs) - echo "DIFF_DOCUMENTS=${DIFF_DOCUMENTS}" >> $GITHUB_ENV - - - name: Setup Node.js environment - uses: actions/setup-node@v3 - with: - node-version-file: ".nvmrc" - cache: yarn - - - name: Install all yarn packages - run: yarn --frozen-lockfile - env: - # https://github.com/microsoft/vscode-ripgrep#github-api-limit-note - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Lint markdown files - run: | - echo "::add-matcher::.github/workflows/markdownlint-problem-matcher.json" - files_to_lint="${{ env.DIFF_DOCUMENTS }}" - yarn markdownlint-cli2 ${files_to_lint} - echo "Linting front-matter" - node scripts/front-matter_linter.js ${files_to_lint}