mirror of
https://github.com/Lissy93/awesome-privacy.git
synced 2026-03-11 08:55:33 +00:00
Simplifies comment logic
This commit is contained in:
parent
512299c71d
commit
acd153fd22
8 changed files with 32 additions and 41 deletions
58
.github/workflows/pr-comment.yml
vendored
58
.github/workflows/pr-comment.yml
vendored
|
|
@ -26,7 +26,7 @@ jobs:
|
|||
run-id: ${{ github.event.workflow_run.id }}
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Post or update comment
|
||||
- name: Post comment
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
github-token: ${{ secrets.BOT_TOKEN || secrets.GITHUB_TOKEN }}
|
||||
|
|
@ -34,12 +34,20 @@ jobs:
|
|||
const fs = require('fs');
|
||||
const marker = '<!-- pr-check-bot -->';
|
||||
|
||||
// Check if there are findings to post
|
||||
const commentFile = 'pr-meta/comment.md';
|
||||
if (!fs.existsSync(commentFile)) {
|
||||
console.log('No findings to post — skipping.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Determine the PR number
|
||||
let prNumber;
|
||||
const numberFile = 'pr-meta/number.txt';
|
||||
if (fs.existsSync(numberFile)) {
|
||||
prNumber = parseInt(fs.readFileSync(numberFile, 'utf8').trim());
|
||||
} else {
|
||||
}
|
||||
if (!prNumber) {
|
||||
// workflow_run.pull_requests is empty for fork PRs, so
|
||||
// fall back to searching by head SHA if needed
|
||||
const prs = context.payload.workflow_run.pull_requests;
|
||||
|
|
@ -53,48 +61,34 @@ jobs:
|
|||
state: 'open',
|
||||
sort: 'updated',
|
||||
direction: 'desc',
|
||||
per_page: 10,
|
||||
per_page: 100,
|
||||
});
|
||||
const match = prList.find(pr => pr.head.sha === headSha);
|
||||
if (!match) {
|
||||
console.log(`No open PR found for SHA ${headSha} — skipping comment.`);
|
||||
console.log(`No open PR found for SHA ${headSha} — skipping.`);
|
||||
return;
|
||||
}
|
||||
prNumber = match.number;
|
||||
}
|
||||
}
|
||||
|
||||
// Find existing bot comment
|
||||
// Skip if we already commented on this PR
|
||||
const { data: comments } = await github.rest.issues.listComments({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
issue_number: prNumber,
|
||||
per_page: 100,
|
||||
});
|
||||
const existing = comments.find(c => c.body.includes(marker));
|
||||
|
||||
const commentFile = 'pr-meta/comment.md';
|
||||
if (fs.existsSync(commentFile)) {
|
||||
const body = fs.readFileSync(commentFile, 'utf8').trim();
|
||||
if (existing) {
|
||||
await github.rest.issues.updateComment({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
comment_id: existing.id,
|
||||
body,
|
||||
});
|
||||
} else {
|
||||
await github.rest.issues.createComment({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
issue_number: prNumber,
|
||||
body,
|
||||
});
|
||||
}
|
||||
} else if (existing) {
|
||||
// No findings — remove stale comment
|
||||
await github.rest.issues.deleteComment({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
comment_id: existing.id,
|
||||
});
|
||||
if (comments.some(c => c.body.includes(marker))) {
|
||||
console.log('Bot comment already exists — skipping.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Post the comment
|
||||
const body = fs.readFileSync(commentFile, 'utf8').trim();
|
||||
await github.rest.issues.createComment({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
issue_number: prNumber,
|
||||
body,
|
||||
});
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -86,12 +86,13 @@ def check_required_fields(diff, head):
|
|||
for svc in diff.get("services", {}).get("modified", []):
|
||||
if not head:
|
||||
continue
|
||||
changed = svc.get("changed_fields", [])
|
||||
fields = find_service_fields(
|
||||
head, svc["category"], svc["section"], svc["service"]
|
||||
)
|
||||
if fields:
|
||||
for f in REQUIRED_FIELDS:
|
||||
if not fields.get(f):
|
||||
if f in changed and not fields.get(f):
|
||||
missing.add(f)
|
||||
if missing:
|
||||
names = ", ".join(f"`{f}`" for f in sorted(missing))
|
||||
|
|
@ -120,16 +121,12 @@ def check_open_source(diff):
|
|||
|
||||
|
||||
def check_single_entry(diff):
|
||||
"""Return a finding if the diff contains multiple service or section changes."""
|
||||
"""Return a finding if the diff adds multiple new services or sections."""
|
||||
services = diff.get("services", {})
|
||||
svc_count = (
|
||||
len(services.get("added", []))
|
||||
+ len(services.get("removed", []))
|
||||
+ len(services.get("modified", []))
|
||||
)
|
||||
if svc_count > 1:
|
||||
added_count = len(services.get("added", []))
|
||||
if added_count > 1:
|
||||
return MULTIPLE_MSG
|
||||
if svc_count == 0:
|
||||
if added_count == 0:
|
||||
sec_count = len(diff.get("sections", []))
|
||||
if sec_count > 1:
|
||||
return MULTIPLE_MSG
|
||||
|
|
|
|||
Loading…
Reference in a new issue