Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions .github/workflows/cleanup_cron_job.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Cleanup Successful and Cancelled Workflow Runs

on:
schedule:
- cron: "0 7 * * 0"

permissions:
actions: write
contents: read
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
pull-requests: read

concurrency:
group: ${{ github.workflow }}
cancel-in-progress: false

jobs:
cleanup:
name: Cleanup successful and cancelled runs
runs-on: ubuntu-latest
steps:
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
- name: Delete successful and cancelled workflow runs
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -euo pipefail
REPO="$GITHUB_REPOSITORY"
RETENTION_DAYS=14
CUTOFF_DATE=$(date -u -d "$RETENTION_DAYS days ago" +%Y-%m-%dT%H:%M:%SZ)
echo "Target repository: $REPO"
echo "Deleting successful and cancelled runs older than $RETENTION_DAYS days (before $CUTOFF_DATE)"

gh api --paginate "repos/$REPO/actions/runs?status=completed&per_page=100" \
--jq '.workflow_runs[] | select((.conclusion == "success") or (.conclusion == "cancelled")) | {id: .id, event: .event, created_at: .created_at, prs: [.pull_requests[]?.number]}' |
jq -c --arg cutoff "$CUTOFF_DATE" 'select(.created_at < $cutoff)' |
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
while read -r run; do
RUN_ID=$(echo "$run" | jq -r '.id')
EVENT=$(echo "$run" | jq -r '.event')

DELETE_RUN=true
if [[ "$EVENT" == "pull_request" ]]; then
PR_NUMBERS=$(echo "$run" | jq -r '.prs[]')

for PR_NUMBER in $PR_NUMBERS; do

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the workflow around the referenced lines.
file=".github/workflows/cleanup_cron_job.yml"
wc -l "$file"
sed -n '1,220p' "$file"

# Find where EVENT and prs are populated/used in this workflow.
rg -n '\bEVENT\b|\.prs\b|pull_request|pull_request_target|gh pr view|DELETE_RUN' "$file"

Repository: jbosstm/lra

Length of output: 3116


🌐 Web query:

GitHub Actions workflow runs API pull_requests field pull_request_target event docs

💡 Result:

In the GitHub Actions REST API, the workflow_runs object includes a pull_requests field, which is defined as an array of minimal pull request objects associated with that specific workflow run [1][2]. However, technical documentation and community reports indicate that this field can be unreliable or empty when a workflow is triggered by the pull_request_target event [3]. While the API schema formally requires the pull_requests array [1][2], users have reported that for runs triggered by pull_request_target, the field often returns an empty array even when the run is clearly associated with an open pull request in the GitHub UI [3]. Regarding the pull_request_target event specifically: 1. Nature of the Event: The pull_request_target event is designed to run workflows in the context of the base repository's default branch, rather than the pull request's head branch [4]. This allows the workflow to securely access repository secrets and write permissions because it executes trusted code from the base branch, even when triggered by a pull request from a fork [4][5]. 2. Context Access: Within the workflow environment, you can access information about the triggering pull request using the github context (e.g., ${{ github.event.pull_request.number }}) [6]. Because the github context mirrors the webhook payload for the event, it is the primary and recommended method for accessing pull request details during the workflow execution, rather than relying on the workflow_runs REST API endpoint [6][7]. 3. Security Warning: Because pull_request_target runs code from the base branch, it is considered safe for secrets. However, if you attempt to check out the pull request's head code (e.g., using actions/checkout with a PR ref), you may expose the workflow to malicious code from a fork [4]. GitHub has implemented protections in newer versions of actions/checkout to prevent unsafe checkouts by default when using this event [4]. If you require the pull request details via the API for external tooling, it is generally more reliable to use the Pulls API (e.g., GET /repos/{owner}/{repo}/pulls/{pull_number}) using the pull request number obtained from the workflow's event payload, rather than relying on the pull_requests field within the workflow_runs API response [5].

Citations:


Use PR association instead of gating on pull_request only. .prs is already available here, but if [[ "$EVENT" == "pull_request" ]] skips other PR-associated runs such as pull_request_target, so an open PR can still lose its run entry. Check any non-empty .prs, and only fall back to the event name to fail closed when a PR-like run has no associated PR number.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In @.github/workflows/cleanup_cron_job.yml around lines 40 - 43, The cleanup job
is gating run handling only on the EVENT being pull_request, which misses other
PR-associated runs even when .prs is present. Update the logic around the
PR_NUMBERS lookup to use the existing .prs association as the primary check, and
only fall back to the event name for fail-closed behavior when no PR number is
associated. Keep the fix localized to the event/PR handling block in the
workflow so it covers pull_request_target and similar PR-linked runs.

STATE=$(gh pr view "$PR_NUMBER" \
--repo "$REPO" \
--json state \
-q '.state') || {
echo "Failed to view PR #$PR_NUMBER; skipping run $RUN_ID" >&2
DELETE_RUN=false
break
}

STATE=$(echo "$STATE" | tr '[:upper:]' '[:lower:]')

if [[ "$STATE" == "open" ]]; then
DELETE_RUN=false
echo "Skipping run $RUN_ID (PR #$PR_NUMBER is still open)"
break
fi
done
fi

if [[ "$DELETE_RUN" == true ]]; then
echo "Deleting run ID: $RUN_ID"
gh run delete "$RUN_ID" --repo "$REPO" || echo "Failed to delete run $RUN_ID" >&2
sleep 0.5
fi
done
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved