Skip to content

Commit c1a40e7

Browse files
committed
ci(workflows): suggest lint fixes via reviewdog
1 parent 0d8796c commit c1a40e7

File tree

2 files changed

+148
-1
lines changed

2 files changed

+148
-1
lines changed

.github/workflows/pr-reviewdog.yml

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
name: PR Reviewdog
2+
3+
on:
4+
workflow_run:
5+
workflows:
6+
- "Test"
7+
types:
8+
- completed
9+
10+
permissions:
11+
# Download artifact from lint workflow.
12+
actions: read
13+
# Post inline review comments via reviewdog.
14+
pull-requests: write
15+
# Report commit status.
16+
statuses: write
17+
18+
jobs:
19+
reviewdog:
20+
runs-on: ubuntu-latest
21+
env:
22+
STATUS_PATH: repos/${{ github.repository }}/statuses/${{ github.event.workflow_run.head_sha }}
23+
STATUS_CONTEXT: ${{ github.workflow }}
24+
STATUS_TARGET: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
25+
steps:
26+
- name: Mark status as pending
27+
env:
28+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
29+
run: |
30+
gh api "$STATUS_PATH" \
31+
-f state=pending \
32+
-f context="$STATUS_CONTEXT" \
33+
-f description='Running' \
34+
-f target_url="$STATUS_TARGET"
35+
36+
- name: Identify PR
37+
id: identify-pr
38+
env:
39+
BASE_REPO: ${{ github.repository }}
40+
GITHUB_TOKEN: ${{ github.token }}
41+
HEAD_REPO: ${{ github.event.workflow_run.head_repository.full_name }}
42+
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
43+
run: |
44+
PR_NUMBER=$(gh api "repos/$HEAD_REPO/commits/$HEAD_SHA/pulls" \
45+
--jq ".[] | select(.base.repo.full_name == \"$BASE_REPO\") | .number")
46+
echo "number=$PR_NUMBER" >> $GITHUB_OUTPUT
47+
48+
- name: Download artifact
49+
if: steps.identify-pr.outputs.number
50+
continue-on-error: true
51+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
52+
with:
53+
name: lint-results
54+
path: lint-results
55+
github-token: ${{ secrets.GITHUB_TOKEN }}
56+
run-id: ${{ github.event.workflow_run.id }}
57+
58+
- name: Check for artifact
59+
id: check
60+
if: steps.identify-pr.outputs.number && hashFiles('lint-results/') != ''
61+
run: echo "HAS_ARTIFACT=true" >> "$GITHUB_OUTPUT"
62+
63+
- name: Checkout
64+
if: steps.identify-pr.outputs.number
65+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
66+
with:
67+
path: browser-compat-data
68+
ref: main
69+
persist-credentials: false
70+
71+
- name: Setup reviewdog
72+
if: steps.check.outputs.HAS_ARTIFACT == 'true'
73+
uses: reviewdog/action-setup@d8a7baabd7f3e8544ee4dbde3ee41d0011c3a93f # v1.5.0
74+
with:
75+
reviewdog_version: latest
76+
77+
- name: Add PR review with suggested changes using `git diff` output
78+
if: steps.check.outputs.HAS_ARTIFACT == 'true' && hashFiles('lint-results/lint.diff') != ''
79+
working-directory: browser-compat-data
80+
env:
81+
CI_PULL_REQUEST: ${{ steps.identify-pr.outputs.number }}
82+
CI_COMMIT: ${{ github.event.workflow_run.head_sha }}
83+
CI_REPO_OWNER: ${{ github.repository_owner }}
84+
CI_REPO_NAME: ${{ github.event.repository.name }}
85+
CI_BRANCH: ${{ github.event.workflow_run.head_branch }}
86+
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
87+
run: |
88+
env -u GITHUB_ACTIONS reviewdog \
89+
-guess \
90+
-name="bcd-linter" \
91+
-f=diff \
92+
-f.diff.strip=1 \
93+
-filter-mode=diff_context \
94+
-reporter=github-pr-review < ../lint-results/lint.diff
95+
96+
- name: Mark status as success
97+
if: success()
98+
env:
99+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
100+
run: |
101+
gh api "$STATUS_PATH" \
102+
-f state=success \
103+
-f context="$STATUS_CONTEXT" \
104+
-f description='Successful' \
105+
-f target_url="$STATUS_TARGET"
106+
107+
- name: Mark status as failure
108+
if: failure()
109+
env:
110+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
111+
run: |
112+
gh api "$STATUS_PATH" \
113+
-f state=failure \
114+
-f context="$STATUS_CONTEXT" \
115+
-f description='Failing' \
116+
-f target_url="$STATUS_TARGET"

.github/workflows/test.yml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,38 @@ jobs:
5555

5656
- run: npm ci
5757

58-
- run: npm run lint -- --fail-on-warnings
58+
- name: Lint
59+
id: lint
60+
run: |
61+
LINT_FAILED=false
62+
npm run lint -- --fail-on-warnings || LINT_FAILED=true
63+
echo "failed=$LINT_FAILED" >> "$GITHUB_OUTPUT"
64+
65+
- name: Fix lint issues
66+
id: lint-fix
67+
if: steps.lint.outputs.failed == 'true'
68+
run: |
69+
npm run lint:fix
70+
if [[ -n $(git diff) ]]; then
71+
echo "files_modified=true" >> "$GITHUB_OUTPUT"
72+
fi
73+
74+
- name: Collect artifact files
75+
if: steps.lint-fix.outputs.files_modified == 'true'
76+
run: |
77+
mkdir -p lint-results
78+
git diff > lint-results/lint.diff
79+
80+
- name: Upload artifact
81+
if: steps.lint-fix.outputs.files_modified == 'true'
82+
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
83+
with:
84+
name: lint-results
85+
path: lint-results
86+
87+
- name: Fail if lint failed
88+
if: steps.lint.outputs.failed == 'true'
89+
run: exit 1
5990

6091
test:
6192
runs-on: ubuntu-latest

0 commit comments

Comments
 (0)