Skip to content

Commit db967e9

Browse files
fix: add explicit GITHUB_TOKEN permissions to workflows (#74)
* fix: add explicit GITHUB_TOKEN permissions to workflows (closes #73) Lock down GITHUB_TOKEN to minimum required permissions per job, following principle of least privilege (CodeQL alerts #1, #2, #3). ci.yml: contents: read, pull-requests: write preview.yml: deploy_preview: contents: read + pull-requests: write close_preview: pull-requests: write only (no checkout) deploy.yml already had explicit permissions and is unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix: skip coverage comment steps when no coverage files are generated When a PR only changes non-code files (e.g. workflow YAML), nx affected runs no tests so no coverage XML is produced and code-coverage-results.md is never created. The sticky-pull-request-comment step then fails with 'Either message or path input is required'. - Add continue-on-error to CodeCoverageSummary (graceful no-op) - Add a Check step that sets coverage.outputs.exists - Gate annotate, summary, and comment steps on that output Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0b81370 commit db967e9

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

.github/workflows/ci.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ concurrency:
99
group: ${{ github.workflow }}-${{ github.ref }}
1010
cancel-in-progress: true
1111

12+
permissions:
13+
contents: read
14+
pull-requests: write # sticky-pull-request-comment
15+
1216
jobs:
1317
ci:
1418
runs-on: ubuntu-latest
@@ -83,6 +87,7 @@ jobs:
8387

8488
- name: Coverage summary
8589
if: always()
90+
continue-on-error: true # no coverage files on PRs with no code changes
8691
uses: irongut/CodeCoverageSummary@v1.3.0
8792
with:
8893
filename: coverage/**/cobertura-coverage.xml
@@ -92,8 +97,14 @@ jobs:
9297
output: both
9398
thresholds: '60 80'
9499

100+
- name: Check for coverage results
101+
id: coverage
102+
if: always()
103+
run: |
104+
[ -f code-coverage-results.md ] && echo "exists=true" >> $GITHUB_OUTPUT || echo "exists=false" >> $GITHUB_OUTPUT
105+
95106
- name: Annotate coverage report
96-
if: always() && github.event_name == 'pull_request'
107+
if: always() && github.event_name == 'pull_request' && steps.coverage.outputs.exists == 'true'
97108
run: |
98109
echo "> [!NOTE]" > coverage-note.md
99110
echo "> Coverage shown for **affected projects only**. Per-file thresholds (80%) are enforced by vitest." >> coverage-note.md
@@ -102,11 +113,11 @@ jobs:
102113
mv coverage-note.md code-coverage-results.md
103114
104115
- name: Write coverage to job summary
105-
if: always()
116+
if: always() && steps.coverage.outputs.exists == 'true'
106117
run: cat code-coverage-results.md >> $GITHUB_STEP_SUMMARY
107118

108119
- name: Add coverage PR comment
109-
if: always() && github.event_name == 'pull_request'
120+
if: always() && github.event_name == 'pull_request' && steps.coverage.outputs.exists == 'true'
110121
uses: marocchino/sticky-pull-request-comment@v2
111122
with:
112123
recreate: true

.github/workflows/preview.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ jobs:
1414
if: github.event.action != 'closed'
1515
name: Deploy Preview
1616
runs-on: ubuntu-latest
17+
permissions:
18+
contents: read
19+
pull-requests: write # SWA deploy posts preview URL comment
1720
steps:
1821
- uses: actions/checkout@v4
1922

@@ -49,6 +52,8 @@ jobs:
4952
if: github.event.action == 'closed'
5053
name: Close Preview
5154
runs-on: ubuntu-latest
55+
permissions:
56+
pull-requests: write # SWA close action updates PR status
5257
steps:
5358
- name: Close staging environment
5459
uses: Azure/static-web-apps-deploy@v1

0 commit comments

Comments
 (0)