Skip to content

Commit 57cc317

Browse files
davorpasashashura
andauthored
ci(check-urls): fix push crash on new branch creation + render awesome_bot report (EbookFoundation#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 EbookFoundation#7043 Cherry picked from 50300ca Co-authored-by: Alex <[email protected]> * chore: setup concurrency policy * chore: modularize using composite actions Co-authored-by: Alex <[email protected]>
1 parent a5c106c commit 57cc317

File tree

2 files changed

+162
-11
lines changed

2 files changed

+162
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
name: 'AwesomeBot Markdown Summary Report'
2+
description: 'Composes the summary report using JSON results of any AwesomeBot execution'
3+
4+
inputs:
5+
ab-root:
6+
description: 'Path where AwesomeBot result files are written.'
7+
required: true
8+
files:
9+
description: 'A delimited string containing the filenames to process.'
10+
required: true
11+
separator:
12+
description: 'Token used to delimit each filename. Default: " ".'
13+
required: false
14+
default: ' '
15+
append-heading:
16+
description: 'When should append report heading.'
17+
required: false
18+
default: "false"
19+
write:
20+
description: 'When should append the report to GITHUB_STEP_SUMMARY file descriptor.'
21+
required: false
22+
default: "true"
23+
24+
outputs:
25+
text:
26+
description: Generated Markdown text.
27+
value: ${{ steps.generate.outputs.text }}
28+
29+
runs:
30+
using: "composite"
31+
32+
steps:
33+
34+
- name: Generate markdown
35+
id: generate
36+
# Using PowerShell
37+
shell: pwsh
38+
# sec: sanatize inputs using environment variables
39+
env:
40+
GITHUB_ACTION_PATH: ${{ github.action_path }}
41+
GITHUB_WORKSPACE: ${{ github.workspace }}
42+
# INPUT_<VARIABLE_NAME> is not available in Composite run steps
43+
# https://github.community/t/input-variable-name-is-not-available-in-composite-run-steps/127611
44+
INPUT_AB_ROOT: ${{ inputs.ab-root }}
45+
INPUT_FILES: ${{ inputs.files }}
46+
INPUT_SEPARATOR: ${{ inputs.separator }}
47+
INPUT_APPEND_HEADING: ${{ inputs.append-heading }}
48+
run: |
49+
$text = ""
50+
51+
# Handle optional heading
52+
if ("true" -eq $env:INPUT_APPEND_HEADING) {
53+
$text += "### Report of Checked URLs!"
54+
$text += "`n`n"
55+
$text += "<div align=`"right`" markdown=`"1`">`n`n"
56+
$text += "_Link issues :rocket: powered by [``awesome_bot``](https://github.com/dkhamsing/awesome_bot)_."
57+
$text += "`n`n</div>"
58+
}
59+
60+
# Loop ForEach files
61+
$env:INPUT_FILES -split $env:INPUT_SEPARATOR | ForEach {
62+
$file = $_
63+
$abr_file = $env:INPUT_AB_ROOT + "/ab-results-" + ($file -replace "[/\\]","-") + "-markdown-table.json"
64+
$json = Get-Content $abr_file | ConvertFrom-Json
65+
66+
$text += "`n`n"
67+
if ("true" -eq $json.error) {
68+
# Highlighting issues counter
69+
$SearchExp = '(?<Num>\d+)'
70+
$ReplaceExp = '**${Num}**'
71+
$text += "`:page_facing_up: File: ``" + $file + "`` (:warning: " + ($json.title -replace $SearchExp,$ReplaceExp) + ")"
72+
# removing where ab attribution lives (moved to report heading)
73+
$text += $json.message -replace "####.*?\n","`n"
74+
} else {
75+
$text += ":page_facing_up: File: ``" + $file + "`` (:ok: **No issues**)"
76+
}
77+
}
78+
79+
# HACK to single line strings (https://trstringer.com/github-actions-multiline-strings/)
80+
$text = $text -replace "`%","%25"
81+
$text = $text -replace "`n","%0A"
82+
$text = $text -replace "`r","%25"
83+
# set output
84+
echo "::set-output name=text::$text"
85+
86+
87+
- name: Write output
88+
if: ${{ fromJson(inputs.write) }}
89+
shell: bash
90+
env:
91+
INPUT_TEXT: ${{ steps.generate.outputs.text }}
92+
INPUT_WRITE: ${{ inputs.write }}
93+
run: |
94+
echo "$INPUT_TEXT" >> $GITHUB_STEP_SUMMARY

.github/workflows/check-urls.yml

+68-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,81 @@
11
name: Check URLs from changed files
2-
on: [push, pull_request]
2+
3+
on:
4+
push:
5+
pull_request:
6+
37
permissions:
48
contents: read
9+
10+
# This allows a subsequently queued workflow run to interrupt previous runs
11+
concurrency:
12+
group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref || github.run_id }}'
13+
cancel-in-progress: true
14+
515
jobs:
6-
job:
7-
permissions:
8-
# Needed for the 'trilom/file-changes-action' action
9-
pull-requests: read
16+
check-urls:
1017
runs-on: ubuntu-latest
18+
19+
outputs:
20+
changed-files: ${{ steps.changed-files.outputs.all_changed_files }}
21+
1122
steps:
23+
24+
# NOTE: tj-actions/changed-files.
25+
# For push events you need to include fetch-depth: 0 | 2 depending on your use case.
26+
# 0: retrieve all history for all branches and tags
27+
# 1: retrieve current commit (by default)
28+
# 2: retrieve the preceding commit
29+
- name: Determine workflow parameters
30+
id: init-params
31+
run: |
32+
echo "::set-output name=fetch_depth::0";
33+
if [ "${{ github.event_name }}" == "pull_request" ]; then
34+
echo "::set-output name=fetch_depth::1";
35+
fi
36+
1237
- uses: actions/checkout@v3
13-
- uses: trilom/[email protected]
14-
id: file_changes
1538
with:
16-
output: ''
39+
fetch-depth: ${{ steps.init-params.outputs.fetch_depth }}
40+
41+
- name: Get changed files
42+
id: changed-files
43+
uses: tj-actions/[email protected]
44+
with:
45+
separator: " "
46+
1747
- uses: ruby/setup-ruby@v1
1848
with:
1949
ruby-version: 2.6
20-
- run: gem install awesome_bot
21-
- 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
50+
51+
- run: |
52+
gem install awesome_bot
53+
54+
- name: Check each changed file
55+
run: |
56+
# Set field separator
57+
IFS=$' ';
58+
59+
# Processing loop
60+
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
61+
echo;
62+
echo "::group::Processing file... $file";
63+
awesome_bot "$file" --allow-redirect --allow-dupe --allow-ssl || true;
64+
echo "::endgroup::";
65+
done
66+
67+
# Unset field separator
68+
unset IFS;
69+
2270
- uses: actions/upload-artifact@v3
2371
with:
24-
path: ${{ github.workspace }}/*.json
72+
name: awesomebot-results
73+
path: ${{ github.workspace }}/ab-results-*.json
74+
75+
- name: Generate Summary Report using AwesomeBot results
76+
uses: ./.github/actions/awesomebot-gh-summary-action
77+
with:
78+
ab-root: ${{ github.workspace }}
79+
files: ${{ steps.changed-files.outputs.all_changed_files }}
80+
separator: " "
81+
append-heading: ${{ true }}

0 commit comments

Comments
 (0)