From 57cc3173da1376b661c6f4e8104d21953671bdff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Ord=C3=A1s?= <3125580+davorpa@users.noreply.github.com> Date: Wed, 31 Aug 2022 17:53:22 +0200 Subject: [PATCH] ci(check-urls): fix push crash on new branch creation + render awesome_bot report (#7046) * format: expand events * format: add blank lines between steps of check job * format: to run steps using multiline format * feat: group for each output * fix: solve crash on first push (trilom 2 tj action) * chore: upload ab-results as `awesomebot-results.zip` artifact * feat: changed files as workflow output * feat: Generate GitHub Summary using `ab-results-*.json`s * security: limit access rights with `contents: read` Seen at PR #7043 Cherry picked from 50300ca119a11292f6b27e6c33699b19fc7458d2 Co-authored-by: Alex <93376818+sashashura@users.noreply.github.com> * chore: setup concurrency policy * chore: modularize using composite actions Co-authored-by: Alex <93376818+sashashura@users.noreply.github.com> --- .../awesomebot-gh-summary-action/action.yml | 94 +++++++++++++++++++ .github/workflows/check-urls.yml | 79 +++++++++++++--- 2 files changed, 162 insertions(+), 11 deletions(-) create mode 100644 .github/actions/awesomebot-gh-summary-action/action.yml diff --git a/.github/actions/awesomebot-gh-summary-action/action.yml b/.github/actions/awesomebot-gh-summary-action/action.yml new file mode 100644 index 000000000..629ecd3ce --- /dev/null +++ b/.github/actions/awesomebot-gh-summary-action/action.yml @@ -0,0 +1,94 @@ +name: 'AwesomeBot Markdown Summary Report' +description: 'Composes the summary report using JSON results of any AwesomeBot execution' + +inputs: + ab-root: + description: 'Path where AwesomeBot result files are written.' + required: true + files: + description: 'A delimited string containing the filenames to process.' + required: true + separator: + description: 'Token used to delimit each filename. Default: " ".' + required: false + default: ' ' + append-heading: + description: 'When should append report heading.' + required: false + default: "false" + write: + description: 'When should append the report to GITHUB_STEP_SUMMARY file descriptor.' + required: false + default: "true" + +outputs: + text: + description: Generated Markdown text. + value: ${{ steps.generate.outputs.text }} + +runs: + using: "composite" + + steps: + + - name: Generate markdown + id: generate + # Using PowerShell + shell: pwsh + # sec: sanatize inputs using environment variables + env: + GITHUB_ACTION_PATH: ${{ github.action_path }} + GITHUB_WORKSPACE: ${{ github.workspace }} + # INPUT_ is not available in Composite run steps + # https://github.community/t/input-variable-name-is-not-available-in-composite-run-steps/127611 + INPUT_AB_ROOT: ${{ inputs.ab-root }} + INPUT_FILES: ${{ inputs.files }} + INPUT_SEPARATOR: ${{ inputs.separator }} + INPUT_APPEND_HEADING: ${{ inputs.append-heading }} + run: | + $text = "" + + # Handle optional heading + if ("true" -eq $env:INPUT_APPEND_HEADING) { + $text += "### Report of Checked URLs!" + $text += "`n`n" + $text += "
`n`n" + $text += "_Link issues :rocket: powered by [``awesome_bot``](https://github.com/dkhamsing/awesome_bot)_." + $text += "`n`n
" + } + + # Loop ForEach files + $env:INPUT_FILES -split $env:INPUT_SEPARATOR | ForEach { + $file = $_ + $abr_file = $env:INPUT_AB_ROOT + "/ab-results-" + ($file -replace "[/\\]","-") + "-markdown-table.json" + $json = Get-Content $abr_file | ConvertFrom-Json + + $text += "`n`n" + if ("true" -eq $json.error) { + # Highlighting issues counter + $SearchExp = '(?\d+)' + $ReplaceExp = '**${Num}**' + $text += "`:page_facing_up: File: ``" + $file + "`` (:warning: " + ($json.title -replace $SearchExp,$ReplaceExp) + ")" + # removing where ab attribution lives (moved to report heading) + $text += $json.message -replace "####.*?\n","`n" + } else { + $text += ":page_facing_up: File: ``" + $file + "`` (:ok: **No issues**)" + } + } + + # HACK to single line strings (https://trstringer.com/github-actions-multiline-strings/) + $text = $text -replace "`%","%25" + $text = $text -replace "`n","%0A" + $text = $text -replace "`r","%25" + # set output + echo "::set-output name=text::$text" + + + - name: Write output + if: ${{ fromJson(inputs.write) }} + shell: bash + env: + INPUT_TEXT: ${{ steps.generate.outputs.text }} + INPUT_WRITE: ${{ inputs.write }} + run: | + echo "$INPUT_TEXT" >> $GITHUB_STEP_SUMMARY diff --git a/.github/workflows/check-urls.yml b/.github/workflows/check-urls.yml index 3f7eff9dc..6e1defeb2 100644 --- a/.github/workflows/check-urls.yml +++ b/.github/workflows/check-urls.yml @@ -1,24 +1,81 @@ name: Check URLs from changed files -on: [push, pull_request] + +on: + push: + pull_request: + permissions: contents: read + +# This allows a subsequently queued workflow run to interrupt previous runs +concurrency: + group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref || github.run_id }}' + cancel-in-progress: true + jobs: - job: - permissions: - # Needed for the 'trilom/file-changes-action' action - pull-requests: read + check-urls: runs-on: ubuntu-latest + + outputs: + changed-files: ${{ steps.changed-files.outputs.all_changed_files }} + steps: + +# NOTE: tj-actions/changed-files. +# For push events you need to include fetch-depth: 0 | 2 depending on your use case. +# 0: retrieve all history for all branches and tags +# 1: retrieve current commit (by default) +# 2: retrieve the preceding commit + - name: Determine workflow parameters + id: init-params + run: | + echo "::set-output name=fetch_depth::0"; + if [ "${{ github.event_name }}" == "pull_request" ]; then + echo "::set-output name=fetch_depth::1"; + fi + - uses: actions/checkout@v3 - - uses: trilom/file-changes-action@v1.2.4 - id: file_changes with: - output: '' + fetch-depth: ${{ steps.init-params.outputs.fetch_depth }} + + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v29.0.1 + with: + separator: " " + - uses: ruby/setup-ruby@v1 with: ruby-version: 2.6 - - run: gem install awesome_bot - - run: for i in ${{ steps.file_changes.outputs.files_modified }}; do echo; echo "processing $i"; awesome_bot $i --allow-redirect --allow-dupe --allow-ssl || true; done + + - run: | + gem install awesome_bot + + - name: Check each changed file + run: | + # Set field separator + IFS=$' '; + + # Processing loop + for file in ${{ steps.changed-files.outputs.all_changed_files }}; do + echo; + echo "::group::Processing file... $file"; + awesome_bot "$file" --allow-redirect --allow-dupe --allow-ssl || true; + echo "::endgroup::"; + done + + # Unset field separator + unset IFS; + - uses: actions/upload-artifact@v3 with: - path: ${{ github.workspace }}/*.json + name: awesomebot-results + path: ${{ github.workspace }}/ab-results-*.json + + - name: Generate Summary Report using AwesomeBot results + uses: ./.github/actions/awesomebot-gh-summary-action + with: + ab-root: ${{ github.workspace }} + files: ${{ steps.changed-files.outputs.all_changed_files }} + separator: " " + append-heading: ${{ true }}