-
Notifications
You must be signed in to change notification settings - Fork 38
178 lines (152 loc) · 6.84 KB
/
Copy pathcodeql-comment.yml
File metadata and controls
178 lines (152 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Fork pull requests receive a read-only GITHUB_TOKEN, so the CodeQL scan can
# upload results but cannot post its summary comment. This workflow runs after
# a successful fork scan in the base repository context, where it can create or
# update that comment from the uploaded SARIF artifacts. Same-repository pull
# requests comment directly from codeql.yml and do not need this workflow.
name: "CodeQL: Comment"
on:
workflow_run:
workflows: ["codeql"]
types: [completed]
permissions:
actions: read
pull-requests: write
jobs:
resolve-pr:
# Same-repository PRs are commented on directly by the CodeQL workflow.
if: >-
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.head_repository.full_name != github.repository
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
pr_number: ${{ steps.validate.outputs.pr_number }}
steps:
- name: Download fork PR metadata
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: codeql-pr-metadata
path: codeql-pr-metadata
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Validate pull request metadata
id: validate
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
HEAD_REPOSITORY: ${{ github.event.workflow_run.head_repository.full_name }}
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
run: |
set -euo pipefail
PR_NUMBER=$(tr -d '[:space:]' < codeql-pr-metadata/pr-number)
if ! [[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ ]]; then
echo "::error::Invalid pull request number in CodeQL metadata artifact"
exit 1
fi
PR_DATA=$(gh api "repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}")
if ! jq -e \
--arg head_repository "$HEAD_REPOSITORY" \
--arg head_branch "$HEAD_BRANCH" \
--arg head_sha "$HEAD_SHA" \
'.state == "open" and
.head.repo.full_name == $head_repository and
.head.ref == $head_branch and
.head.sha == $head_sha' <<< "$PR_DATA" > /dev/null; then
echo "::error::Pull request ${PR_NUMBER} does not match the CodeQL workflow run"
exit 1
fi
echo "pr_number=${PR_NUMBER}" >> "$GITHUB_OUTPUT"
comment:
needs: resolve-pr
runs-on: ubuntu-latest
timeout-minutes: 5
concurrency:
group: codeql-comment-pr-${{ needs.resolve-pr.outputs.pr_number }}
cancel-in-progress: true
steps:
- name: Download CodeQL results
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
pattern: codeql-pr-results-*
path: codeql-results
merge-multiple: true
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Post or update CodeQL results comment
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
PR_NUMBER: ${{ needs.resolve-pr.outputs.pr_number }}
RUN_URL: ${{ github.event.workflow_run.html_url }}
run: |
set -euo pipefail
mapfile -d '' SARIF_FILES < <(find codeql-results -type f -name '*.sarif' -print0)
if [ "${#SARIF_FILES[@]}" -eq 0 ]; then
echo "::error::No SARIF files were downloaded from ${RUN_URL}"
exit 1
fi
COMMENT_MARKER="<!-- dsx-codeql-scan -->"
TOTAL_ISSUES=$(jq -s '[.[].runs[]?.results[]?] | length' "${SARIF_FILES[@]}")
SHORT_SHA=${HEAD_SHA:0:7}
FOOTER="🔗 [View full details in Security tab](https://github.com/${GITHUB_REPOSITORY}/security/code-scanning)"
if [ "$TOTAL_ISSUES" -eq 0 ]; then
REPORT="${COMMENT_MARKER}
## 🛡️ CodeQL Analysis
✅ No security issues found!
$FOOTER
<sub>🕐 Last updated: $(date -u '+%Y-%m-%d %H:%M:%S UTC') | Commit: ${SHORT_SHA}</sub>"
else
ERRORS=$(jq -s '[.[].runs[]?.results[]? | select(.level == "error")] | length' "${SARIF_FILES[@]}")
WARNINGS=$(jq -s '[.[].runs[]?.results[]? | select(.level == "warning")] | length' "${SARIF_FILES[@]}")
NOTES=$(jq -s '[.[].runs[]?.results[]? | select(.level == "note")] | length' "${SARIF_FILES[@]}")
# Match the shared action's same-repository comment format.
TOP_ISSUES=$(jq -rs '
[.[].runs[]?.results[]?
| select(.level == "error" or .level == "warning")][0:5][]
| "- **\(.ruleId // "unknown")**: "
+ "\(.message.text // "No description") "
+ "(\(.locations[0].physicalLocation.artifactLocation.uri // "unknown"):"
+ "\(.locations[0].physicalLocation.region.startLine // "?"))"
' "${SARIF_FILES[@]}")
REPORT="${COMMENT_MARKER}
## 🛡️ CodeQL Analysis
🚨 Found **${TOTAL_ISSUES}** issue(s)
**Severity Breakdown:**
- 🔴 Errors: ${ERRORS}
- 🟡 Warnings: ${WARNINGS}
- 🔵 Notes: ${NOTES}
<details>
<summary>📋 Top Issues</summary>
${TOP_ISSUES}
</details>
$FOOTER
<sub>🕐 Last updated: $(date -u '+%Y-%m-%d %H:%M:%S UTC') | Commit: ${SHORT_SHA}</sub>"
fi
COMMENT_ID=$(gh api --paginate \
"repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" \
--jq '.[] | select(.user.login == "github-actions[bot]") | select(.body | contains("<!-- dsx-codeql-scan -->")) | .id' |
sed -n '1p')
if [ -n "$COMMENT_ID" ]; then
gh api "repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}" \
--method PATCH \
-f body="$REPORT"
else
gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" \
--method POST \
-f body="$REPORT"
fi