Skip to content

280+ free n8n automation templates — ready-to-use workflows for Gmail, Telegram, Slack, Discord, WhatsApp, Google Drive, Notion, OpenAI, and more. AI agents, RAG chatbots, email automation, social media, DevOps, and document processing. The largest open-source n8n template collection.

pr review workflow

enescingoz0e8ddf47d8b2
diff --git a/.github/workflows/pr-review.yml b/.github/workflows/pr-review.yml
index ff14324..fc80b3f 100644
--- a/.github/workflows/pr-review.yml
+++ b/.github/workflows/pr-review.yml
@@ -45,6 +45,10 @@ on:
pull_request_target:
types: [opened, reopened, synchronize]
+ # Hourly safety net: retry PRs deferred by rate limits & catch any missed changes
+ schedule:
+ - cron: '17 * * * *'
+
permissions:
pull-requests: write
contents: read
@@ -141,7 +145,9 @@ jobs:
local pr="$1"
local body="$2"
local verdict
- if printf '%s' "${body}" | grep -qF "NEEDS CHANGES"; then
+ if printf '%s' "${body}" | grep -qiE "review deferred|rate limit reached"; then
+ verdict="auto-review: deferred"
+ elif printf '%s' "${body}" | grep -qF "NEEDS CHANGES"; then
verdict="auto-review: needs-changes"
elif printf '%s' "${body}" | grep -qF "NOT A TEMPLATE"; then
verdict="auto-review: not-a-template"
@@ -156,6 +162,7 @@ jobs:
gh label create "auto-review: needs-changes" --color d93f0b --force --repo "${REPO}" >/dev/null 2>&1 || true
gh label create "auto-review: not-a-template" --color ededed --force --repo "${REPO}" >/dev/null 2>&1 || true
gh label create "auto-review: error" --color b60205 --force --repo "${REPO}" >/dev/null 2>&1 || true
+ gh label create "auto-review: deferred" --color fbca04 --force --repo "${REPO}" >/dev/null 2>&1 || true
echo "Applying verdict label '${verdict}' to PR #${pr}."
# Only remove the OTHER auto-review labels that are actually present,
@@ -165,7 +172,7 @@ jobs:
local current edit_args=()
current=$(gh pr view "${pr}" --repo "${REPO}" --json labels --jq '.labels[].name' 2>/dev/null || true)
local L
- for L in "auto-review: safe" "auto-review: needs-changes" "auto-review: not-a-template" "auto-review: error"; do
+ for L in "auto-review: safe" "auto-review: needs-changes" "auto-review: not-a-template" "auto-review: error" "auto-review: deferred"; do
if [ "${L}" = "${verdict}" ]; then
if ! printf '%s\n' "${current}" | grep -qxF "${L}"; then
edit_args+=(--add-label "${L}")
@@ -189,11 +196,16 @@ jobs:
local model="$1"
local prompt="$2"
local attempt http_code response body
+ local RL_MAX_WAIT=300 # cap for a SINGLE 429 wait (per-minute window)
+ local RL_TOTAL_BUDGET=600 # cumulative 429 wait budget for this invocation
+ local rl_waited=0
- for attempt in 1 2 3 4 5; do
+ for attempt in $(seq 1 12); do
- # Capture body + HTTP status in one call
- response=$(curl -sS -w "\n%{http_code}" \
+ local hdr_file retry_after_hdr
+ hdr_file="$(mktemp)"
+ # Capture body + HTTP status; dump response headers for Retry-After.
+ response=$(curl -sS -D "${hdr_file}" -w "\n%{http_code}" \
-X POST "${MODELS_ENDPOINT}" \
-H "Authorization: Bearer ${GH_TOKEN}" \
-H "Content-Type: application/json" \
@@ -209,6 +221,8 @@ jobs:
http_code=$(printf '%s' "${response}" | tail -n1)
body=$(printf '%s' "${response}" | head -n -1)
+ retry_after_hdr=$(grep -i '^retry-after:' "${hdr_file}" | head -n1 | awk '{print $2}' | tr -d '\r\n' || true)
+ rm -f "${hdr_file}"
echo " [attempt ${attempt}] model=${model} http=${http_code}"
@@ -225,25 +239,43 @@ jobs:
echo " Payload too large (413) — diff exceeds model context. Skipping retries and fallback."
return 2
elif [ "${http_code}" = "429" ]; then
- local retry_after
- retry_after=$(printf '%s' "${body}" | jq -r '.error.retry_after // empty' 2>/dev/null || true)
- if [ -n "${retry_after}" ]; then
- echo " Rate limited; waiting ${retry_after}s (retry-after header)..."
- sleep "${retry_after}"
+ # Log any API error message for diagnostics.
+ if printf '%s' "${body}" | jq -e '.error' >/dev/null 2>&1; then
+ echo " API error detail: $(printf '%s' "${body}" | jq -r '.error.message // .error')"
+ fi
+ # Wait time by priority: Retry-After header > body.retry_after >
+ # "wait N seconds" in the message > exponential backoff.
+ local wait_secs="" wait_src="" body_retry msg_retry
+ if printf '%s' "${retry_after_hdr}" | grep -qE '^[0-9]+$'; then
+ wait_secs=$(( retry_after_hdr + 3 )); wait_src="header"
else
- local wait_secs
- if [ $(( 10 * attempt )) -lt 60 ]; then
- wait_secs=$(( 10 * attempt ))
+ body_retry=$(printf '%s' "${body}" | jq -r '.error.retry_after // empty' 2>/dev/null || true)
+ if printf '%s' "${body_retry}" | grep -qE '^[0-9]+$'; then
+ wait_secs=$(( body_retry + 3 )); wait_src="body"
else
- wait_secs=60
+ msg_retry=$(printf '%s' "${body}" | grep -oiE 'wait[^0-9]*[0-9]+[[:space:]]*second' | grep -oE '[0-9]+' | head -n1 || true)
+ if printf '%s' "${msg_retry}" | grep -qE '^[0-9]+$'; then
+ wait_secs=$(( msg_retry + 3 )); wait_src="message"
+ elif [ $(( 10 * attempt )) -lt 60 ]; then
+ wait_secs=$(( 10 * attempt )); wait_src="backoff"
+ else
+ wait_secs=60; wait_src="backoff"
+ fi
fi
- echo " Rate limited (429); waiting ${wait_secs}s (attempt ${attempt}/5)..."
- sleep "${wait_secs}"
fi
- # Log any API error message for diagnostics
- if printf '%s' "${body}" | jq -e '.error' >/dev/null 2>&1; then
- echo " API error detail: $(printf '%s' "${body}" | jq -r '.error.message // .error')"
+ # A long wait means a daily/long quota — don't hang the job; defer.
+ if [ "${wait_secs}" -gt "${RL_MAX_WAIT}" ]; then
+ echo " Rate limit needs ${wait_secs}s (> ${RL_MAX_WAIT}s cap) — likely a daily/long quota. Deferring."
+ return 3
+ fi
+ if [ $(( rl_waited + wait_secs )) -gt "${RL_TOTAL_BUDGET}" ]; then
+ echo " Cumulative 429 wait budget (${RL_TOTAL_BUDGET}s) would be exceeded — deferring."
+ return 3
fi
+ echo " Rate limited (429); waiting ${wait_secs}s (source: ${wait_src}, attempt ${attempt})..."
+ sleep "${wait_secs}"
+ rl_waited=$(( rl_waited + wait_secs ))
+ continue
elif [ "${http_code}" -ge 500 ]; then
local delay
delay=$(( 2 ** attempt )) # 2s, 4s, 8s
@@ -429,6 +461,7 @@ jobs:
# ── call GitHub Models with fallback ─────────────
REVIEW_BODY=""
USED_MODEL=""
+ DEFERRED="no"
echo "Trying primary model: ${PRIMARY_MODEL}"
set +e
@@ -461,6 +494,12 @@ jobs:
REVIEW_BODY="Automated review skipped — this PR's diff exceeds the GitHub Models free-tier 8K-token context window. Please review manually."
USED_MODEL="(skipped: payload too large)"
;;
+ 3)
+ echo "Fallback also rate-limited — deferring review."
+ REVIEW_BODY="⏳ Automated review deferred — GitHub Models rate limit reached. This PR will be re-reviewed automatically on the next push or the next scheduled sweep."
+ USED_MODEL="(rate-limited — deferred)"
+ DEFERRED="yes"
+ ;;
*)
echo "Both models failed. Posting fallback comment."
REVIEW_BODY="Automated review unavailable — please review manually."
@@ -479,7 +518,12 @@ jobs:
<sub>Reviewed by \`${USED_MODEL}\` via GitHub Models · [Workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})</sub>"
COMMENT=$(printf '%s' "${COMMENT}" | sed 's/^ //')
- post_review "${PR_NUMBER}" "${COMMENT}" "${HEAD_SHA}"
+ if [ "${DEFERRED}" = "yes" ]; then
+ # Stamp a non-SHA so needs_review re-reviews this PR on the next sweep.
+ post_review "${PR_NUMBER}" "${COMMENT}" "deferred"
+ else
+ post_review "${PR_NUMBER}" "${COMMENT}" "${HEAD_SHA}"
+ fi
apply_verdict_label "${PR_NUMBER}" "${REVIEW_BODY}"
echo "Review posted on PR #${PR_NUMBER} (model: ${USED_MODEL})."
@@ -487,18 +531,19 @@ jobs:
# MANUAL: backlog sweep or single-PR review
# ──────────────────────────────────────────────────────────
review-pr-manual:
- name: Review PR (manual — ${{ inputs.mode }})
- if: github.event_name == 'workflow_dispatch' && github.actor == github.repository_owner
+ name: Review PR (manual/scheduled)
+ if: (github.event_name == 'workflow_dispatch' && github.actor == github.repository_owner) || github.event_name == 'schedule'
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- - name: Run review (${{ inputs.mode }})
+ - name: Run review
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
- MODE: ${{ inputs.mode }}
+ # On schedule, inputs.* are empty → default to a backlog sweep.
+ MODE: ${{ inputs.mode || (github.event_name == 'schedule' && 'backlog' || 'latest') }}
INPUT_PR_NUMBER: ${{ inputs.pr_number }}
- FORCE: ${{ inputs.force }}
+ FORCE: ${{ inputs.force || false }}
run: |
set -euo pipefail
@@ -565,7 +610,9 @@ jobs:
local pr="$1"
local body="$2"
local verdict
- if printf '%s' "${body}" | grep -qF "NEEDS CHANGES"; then
+ if printf '%s' "${body}" | grep -qiE "review deferred|rate limit reached"; then
+ verdict="auto-review: deferred"
+ elif printf '%s' "${body}" | grep -qF "NEEDS CHANGES"; then
verdict="auto-review: needs-changes"
elif printf '%s' "${body}" | grep -qF "NOT A TEMPLATE"; then
verdict="auto-review: not-a-template"
@@ -579,14 +626,29 @@ jobs:
gh label create "auto-review: needs-changes" --color d93f0b --force --repo "${REPO}" >/dev/null 2>&1 || true
gh label create "auto-review: not-a-template" --color ededed --force --repo "${REPO}" >/dev/null 2>&1 || true
gh label create "auto-review: error" --color b60205 --force --repo "${REPO}" >/dev/null 2>&1 || true
+ gh label create "auto-review: deferred" --color fbca04 --force --repo "${REPO}" >/dev/null 2>&1 || true
echo "Applying verdict label '${verdict}' to PR #${pr}."
- gh pr edit "${pr}" --repo "${REPO}" \
- --remove-label "auto-review: safe" \
- --remove-label "auto-review: needs-changes" \
- --remove-label "auto-review: not-a-template" \
- --remove-label "auto-review: error" \
- --add-label "${verdict}" || true
+ # Only remove the OTHER auto-review labels that are actually present,
+ # and add the target only if missing — avoids "added X / removed X"
+ # timeline spam from removing and re-adding the same label.
+ local current edit_args=()
+ current=$(gh pr view "${pr}" --repo "${REPO}" --json labels --jq '.labels[].name' 2>/dev/null || true)
+ local L
+ for L in "auto-review: safe" "auto-review: needs-changes" "auto-review: not-a-template" "auto-review: error" "auto-review: deferred"; do
+ if [ "${L}" = "${verdict}" ]; then
+ if ! printf '%s\n' "${current}" | grep -qxF "${L}"; then
+ edit_args+=(--add-label "${L}")
+ fi
+ elif printf '%s\n' "${current}" | grep -qxF "${L}"; then
+ edit_args+=(--remove-label "${L}")
+ fi
+ done
+ if [ "${#edit_args[@]}" -gt 0 ]; then
+ gh pr edit "${pr}" --repo "${REPO}" "${edit_args[@]}" || true
+ else
+ echo "Label '${verdict}' already correct on PR #${pr}; no change."
+ fi
}
# call_model <model> <prompt>
@@ -597,11 +659,16 @@ jobs:
local model="$1"
local prompt="$2"
local attempt http_code response body
+ local RL_MAX_WAIT=300 # cap for a SINGLE 429 wait (per-minute window)
+ local RL_TOTAL_BUDGET=600 # cumulative 429 wait budget for this invocation
+ local rl_waited=0
- for attempt in 1 2 3 4 5; do
+ for attempt in $(seq 1 12); do
- # Capture body + HTTP status in one call
- response=$(curl -sS -w "\n%{http_code}" \
+ local hdr_file retry_after_hdr
+ hdr_file="$(mktemp)"
+ # Capture body + HTTP status; dump response headers for Retry-After.
+ response=$(curl -sS -D "${hdr_file}" -w "\n%{http_code}" \
-X POST "${MODELS_ENDPOINT}" \
-H "Authorization: Bearer ${GH_TOKEN}" \
-H "Content-Type: application/json" \
@@ -617,6 +684,8 @@ jobs:
http_code=$(printf '%s' "${response}" | tail -n1)
body=$(printf '%s' "${response}" | head -n -1)
+ retry_after_hdr=$(grep -i '^retry-after:' "${hdr_file}" | head -n1 | awk '{print $2}' | tr -d '\r\n' || true)
+ rm -f "${hdr_file}"
echo " [attempt ${attempt}] model=${model} http=${http_code}"
@@ -633,25 +702,43 @@ jobs:
echo " Payload too large (413) — diff exceeds model context. Skipping retries and fallback."
return 2
elif [ "${http_code}" = "429" ]; then
- local retry_after
- retry_after=$(printf '%s' "${body}" | jq -r '.error.retry_after // empty' 2>/dev/null || true)
- if [ -n "${retry_after}" ]; then
- echo " Rate limited; waiting ${retry_after}s (retry-after header)..."
- sleep "${retry_after}"
+ # Log any API error message for diagnostics.
+ if printf '%s' "${body}" | jq -e '.error' >/dev/null 2>&1; then
+ echo " API error detail: $(printf '%s' "${body}" | jq -r '.error.message // .error')"
+ fi
+ # Wait time by priority: Retry-After header > body.retry_after >
+ # "wait N seconds" in the message > exponential backoff.
+ local wait_secs="" wait_src="" body_retry msg_retry
+ if printf '%s' "${retry_after_hdr}" | grep -qE '^[0-9]+$'; then
+ wait_secs=$(( retry_after_hdr + 3 )); wait_src="header"
else
- local wait_secs
- if [ $(( 10 * attempt )) -lt 60 ]; then
- wait_secs=$(( 10 * attempt ))
+ body_retry=$(printf '%s' "${body}" | jq -r '.error.retry_after // empty' 2>/dev/null || true)
+ if printf '%s' "${body_retry}" | grep -qE '^[0-9]+$'; then
+ wait_secs=$(( body_retry + 3 )); wait_src="body"
else
- wait_secs=60
+ msg_retry=$(printf '%s' "${body}" | grep -oiE 'wait[^0-9]*[0-9]+[[:space:]]*second' | grep -oE '[0-9]+' | head -n1 || true)
+ if printf '%s' "${msg_retry}" | grep -qE '^[0-9]+$'; then
+ wait_secs=$(( msg_retry + 3 )); wait_src="message"
+ elif [ $(( 10 * attempt )) -lt 60 ]; then
+ wait_secs=$(( 10 * attempt )); wait_src="backoff"
+ else
+ wait_secs=60; wait_src="backoff"
+ fi
fi
- echo " Rate limited (429); waiting ${wait_secs}s (attempt ${attempt}/5)..."
- sleep "${wait_secs}"
fi
- # Log any API error message for diagnostics
- if printf '%s' "${body}" | jq -e '.error' >/dev/null 2>&1; then
- echo " API error detail: $(printf '%s' "${body}" | jq -r '.error.message // .error')"
+ # A long wait means a daily/long quota — don't hang the job; defer.
+ if [ "${wait_secs}" -gt "${RL_MAX_WAIT}" ]; then
+ echo " Rate limit needs ${wait_secs}s (> ${RL_MAX_WAIT}s cap) — likely a daily/long quota. Deferring."
+ return 3
fi
+ if [ $(( rl_waited + wait_secs )) -gt "${RL_TOTAL_BUDGET}" ]; then
+ echo " Cumulative 429 wait budget (${RL_TOTAL_BUDGET}s) would be exceeded — deferring."
+ return 3
+ fi
+ echo " Rate limited (429); waiting ${wait_secs}s (source: ${wait_src}, attempt ${attempt})..."
+ sleep "${wait_secs}"
+ rl_waited=$(( rl_waited + wait_secs ))
+ continue
elif [ "${http_code}" -ge 500 ]; then
local delay
delay=$(( 2 ** attempt )) # 2s, 4s, 8s
@@ -831,6 +918,7 @@ jobs:
REVIEW_BODY=""
USED_MODEL=""
+ DEFERRED="no"
echo "Trying primary model: ${PRIMARY_MODEL}"
set +e
@@ -863,6 +951,12 @@ jobs:
REVIEW_BODY="Automated review skipped — this PR's diff exceeds the GitHub Models free-tier 8K-token context window. Please review manually."
USED_MODEL="(skipped: payload too large)"
;;
+ 3)
+ echo "Fallback also rate-limited — deferring review."
+ REVIEW_BODY="⏳ Automated review deferred — GitHub Models rate limit reached. This PR will be re-reviewed automatically on the next push or the next scheduled sweep."
+ USED_MODEL="(rate-limited — deferred)"
+ DEFERRED="yes"
+ ;;
*)
echo "Both models failed. Posting fallback comment."
REVIEW_BODY="Automated review unavailable — please review manually."
@@ -880,7 +974,12 @@ jobs:
<sub>Reviewed by \`${USED_MODEL}\` via GitHub Models · [Workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})</sub>"
COMMENT=$(printf '%s' "${COMMENT}" | sed 's/^ //')
- post_review "${PR_NUMBER}" "${COMMENT}" "${HEAD_SHA}"
+ if [ "${DEFERRED}" = "yes" ]; then
+ # Stamp a non-SHA so needs_review re-reviews this PR on the next sweep.
+ post_review "${PR_NUMBER}" "${COMMENT}" "deferred"
+ else
+ post_review "${PR_NUMBER}" "${COMMENT}" "${HEAD_SHA}"
+ fi
apply_verdict_label "${PR_NUMBER}" "${REVIEW_BODY}"
echo "Review posted on PR #${PR_NUMBER} (model: ${USED_MODEL})."
}
@@ -939,8 +1038,8 @@ jobs:
exit 0
fi
review_one_pr "${PR_NUM}"
- # Small delay between actual reviews to avoid rate limits.
- sleep 10
+ # Pace requests under the free-tier RPM cap.
+ sleep 30
) || echo "PR #${PR_NUM}: processing failed — logged and continuing to next PR."
done
echo "Backlog sweep complete."