Skip to content

Commit 59c0d84

Browse files
committed
Add PR comment
1 parent c15736f commit 59c0d84

2 files changed

Lines changed: 34 additions & 16 deletions

File tree

.github/workflows/check-project-actions.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ jobs:
6565
runs-on: ubuntu-latest
6666
permissions:
6767
contents: read
68+
pull-requests: write
6869
steps:
6970
- name: "Checkout apache/infrastructure-actions"
7071
uses: actions/checkout@v2
@@ -91,3 +92,10 @@ jobs:
9192
9293
import check_repository_actions as c
9394
c.check_project_actions('./repository-to-be-checked', './approved_patterns.yml')
95+
96+
- name: Comment on PR
97+
if: failure() && github.event_name == 'pull_request'
98+
env:
99+
GH_TOKEN: ${{ github.token }}
100+
run:
101+
gh pr --repo ${{ github.repository }} comment ${{ github.event.pull_request.number }} --body-file step-summary-output.txt

gateway/check_repository_actions.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,33 @@ def check_project_actions(repository: str | os.PathLike, approved_patterns_file:
159159
failures.append(f"❌ {relative_path} {yaml_path}: '{uses_value}' is not approved")
160160

161161
if on_gha():
162+
summary_lines: list[str] = [
163+
"# GitHub Actions verification result",
164+
"",
165+
"For more information visit the [ASF Infrastructure GitHub Actions Policy](https://infra.apache.org/github-actions-policy.html) page",
166+
"and the [ASF Infrastructure Actions](https://github.com/apache/infrastructure-actions) repository.",
167+
]
168+
169+
if len(failures) > 0:
170+
summary_lines.extend(["", f"## Failures ({len(failures)})"])
171+
for msg in failures:
172+
summary_lines.extend([msg, ""])
173+
174+
if len(warnings) > 0:
175+
summary_lines.extend(["", f"## Warnings ({len(warnings)})"])
176+
for msg in warnings:
177+
summary_lines.extend([msg, ""])
178+
179+
if len(failures) == 0:
180+
summary_lines.append("✅ Success, all action usages match the currently approved patterns.")
181+
182+
summary_text = "\n".join(summary_lines).rstrip() + "\n"
183+
162184
with open(os.environ["GITHUB_STEP_SUMMARY"], "a") as f:
163-
f.write(f"# GitHub Actions verification result\n")
164-
f.write("\n")
165-
f.write("For more information visit the [ASF Infrastructure GitHub Actions Policy](https://infra.apache.org/github-actions-policy.html) page\n")
166-
f.write("and the [ASF Infrastructure Actions](https://github.com/apache/infrastructure-actions) repository.\n")
167-
if len(failures) > 0:
168-
f.write("\n")
169-
f.write(f"## Failures ({len(failures)})\n")
170-
for msg in failures:
171-
f.write(f"{msg}\n\n")
172-
if len(warnings) > 0:
173-
f.write("\n")
174-
f.write(f"## Warnings ({len(warnings)})\n")
175-
for msg in warnings:
176-
f.write(f"{msg}\n\n")
177-
if len(failures) == 0:
178-
f.write(f"✅ Success, all action usages match the currently approved patterns.\n")
185+
f.write(summary_text)
186+
# This file is used in the workflow to post a comment on a pull request.
187+
with open("step-summary-output.txt", "a") as f:
188+
f.write(summary_text)
179189

180190
if len(failures) > 0:
181191
raise Exception(f"One or more action references are not approved or explicitly blocked:\n{"\n".join(failures)}")

0 commit comments

Comments
 (0)