-
Notifications
You must be signed in to change notification settings - Fork 7
t2800: fix pulse idempotency guard for external-contributor label check #2801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -79,18 +79,22 @@ perm=$(echo "$response" | tail -1 | jq -r '.permission // empty' 2>/dev/null) | |||||
|
|
||||||
| ```bash | ||||||
| # Idempotency guard: skip if already labelled OR already commented (belt-and-suspenders). | ||||||
| # IMPORTANT: Do NOT suppress stderr on the label check — a failed API call must not | ||||||
| # silently bypass the guard (root cause of duplicate comments in #2795). | ||||||
| labels=$(gh pr view <number> --repo <slug> --json labels --jq '.labels[].name' 2>&1) || true | ||||||
| if echo "$labels" | grep -q '^external-contributor$'; then | ||||||
| # Uses jq any() for a clean boolean result — avoids grep anchor issues with multi-line | ||||||
| # output that caused duplicate comments (#2795, #2800). | ||||||
| has_label=$(gh pr view <number> --repo <slug> --json labels --jq 'any(.labels[]; .name == "external-contributor")' 2>&1) || true | ||||||
| if [ "$has_label" = "true" ]; then | ||||||
| : # Already labelled — skip | ||||||
| elif gh pr view <number> --repo <slug> --json comments --jq '.comments[].body' 2>&1 | grep -qiF 'external contributor'; then | ||||||
| # Comment already exists but label is missing — re-add the label only | ||||||
| gh api "repos/<slug>/issues/<number>/labels" -X POST -f 'labels[]=external-contributor' 2>/dev/null || true | ||||||
| else | ||||||
| # Neither label nor comment exists — post comment and add label atomically | ||||||
| gh pr comment <number> --repo <slug> --body "This PR is from an external contributor (@<author>). Auto-merge is disabled for external PRs — a maintainer must review and merge manually." \ | ||||||
| && gh api "repos/<slug>/issues/<number>/labels" -X POST -f 'labels[]=external-contributor' 2>/dev/null || true | ||||||
| # Label not found (or API failed) — check for existing comment as fallback | ||||||
| existing_comment=$(gh pr view <number> --repo <slug> --json comments --jq '[.comments[].body | select(test("external contributor"; "i"))] | length' 2>&1) || true | ||||||
| if [ "$existing_comment" != "0" ] && [ -n "$existing_comment" ]; then | ||||||
| # Comment already exists but label is missing — re-add the label only | ||||||
| gh api "repos/<slug>/issues/<number>/labels" -X POST -f 'labels[]=external-contributor' 2>/dev/null || true | ||||||
| else | ||||||
| # Neither label nor comment exists — post comment and add label atomically | ||||||
| gh pr comment <number> --repo <slug> --body "This PR is from an external contributor (@<author>). Auto-merge is disabled for external PRs — a maintainer must review and merge manually." \ | ||||||
| && gh api "repos/<slug>/issues/<number>/labels" -X POST -f 'labels[]=external-contributor' 2>/dev/null || true | ||||||
| fi | ||||||
| fi | ||||||
| ``` | ||||||
|
|
||||||
|
|
@@ -100,9 +104,9 @@ Then skip to the next PR. Do NOT dispatch workers to fix failing CI on external | |||||
|
|
||||||
| ```bash | ||||||
| # Only comment once — check for existing permission-failure comment. | ||||||
| # Do NOT suppress stderr — a failed API call must not bypass the guard. | ||||||
| comments=$(gh pr view <number> --repo <slug> --json comments --jq '.comments[].body' 2>&1) || true | ||||||
| if ! echo "$comments" | grep -qF 'Permission check failed'; then | ||||||
| # Uses jq select+test for robust matching — avoids grep issues with multi-line output. | ||||||
| perm_comment_count=$(gh pr view <number> --repo <slug> --json comments --jq '[.comments[].body | select(test("Permission check failed"))] | length' 2>&1) || true | ||||||
| if [ "$perm_comment_count" = "0" ] || [ -z "$perm_comment_count" ]; then | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For improved robustness and to prevent duplicate comments, this check can be simplified. The By only checking
Suggested change
|
||||||
| gh pr comment <number> --repo <slug> --body "Permission check failed for this PR (HTTP $http_status from collaborator permission API). Unable to determine if @<author> is a maintainer or external contributor. **A maintainer must review and merge this PR manually.** This is a fail-closed safety measure — the pulse will not auto-merge until the permission API succeeds." | ||||||
| fi | ||||||
| ``` | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic for checking
existing_commentcan be made more robust to prevent posting duplicate comments. The current condition[ "$existing_comment" != "0" ] && [ -n "$existing_comment" ]will evaluate to false if$existing_commentis an empty string (e.g., if theghcommand fails and produces no output), causing a new comment to be posted and potentially creating a duplicate.A safer approach is to invert the logic and only post a comment if you are certain there are none. By checking
[ "$existing_comment" = "0" ], you ensure that a new comment is posted only when the count is definitively zero. In all other cases (comment exists, or the check failed), the script will attempt the safer, idempotent action of re-adding the label.Additionally,
2>/dev/nullshould be avoided to ensure error messages are visible for debugging. The|| trueis sufficient to prevent script termination on failure.References