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_<VARIABLE_NAME> 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 += "<div align=`"right`" markdown=`"1`">`n`n" + $text += "_Link issues :rocket: powered by [``awesome_bot``](https://github.com/dkhamsing/awesome_bot)_." + $text += "`n`n</div>" + } + + # 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 = '(?<Num>\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 }}