Skip to content
Merged
Changes from all 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
46 changes: 5 additions & 41 deletions .github/scripts/pr-triage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,10 @@ process_pr() {
local PR_NUMBER=$1
echo "🔄 Processing PR #${PR_NUMBER}"

# Get PR body with error handling
local PR_BODY
if ! PR_BODY=$(gh pr view "${PR_NUMBER}" --repo "${GITHUB_REPOSITORY}" --json body -q .body 2>/dev/null); then
echo " ⚠️ Could not fetch PR #${PR_NUMBER} details"
return 1
fi

# Look for issue references using multiple patterns
local ISSUE_NUMBER=""

# Pattern 1: Direct reference like #123
if [[ -z "${ISSUE_NUMBER}" ]]; then
ISSUE_NUMBER=$(echo "${PR_BODY}" | grep -oE '#[0-9]+' | head -1 | sed 's/#//' 2>/dev/null || echo "")
fi

# Pattern 2: Closes/Fixes/Resolves patterns (case-insensitive)
if [[ -z "${ISSUE_NUMBER}" ]]; then
ISSUE_NUMBER=$(echo "${PR_BODY}" | grep -iE '(closes?|fixes?|resolves?) #[0-9]+' | grep -oE '#[0-9]+' | head -1 | sed 's/#//' 2>/dev/null || echo "")
# Get closing issue number with error handling
local ISSUE_NUMBER
if ! ISSUE_NUMBER=$(gh pr view "${PR_NUMBER}" --repo "${GITHUB_REPOSITORY}" --json closingIssuesReferences -q '.closingIssuesReferences.nodes[0].number' 2>/dev/null); then
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The gh command with the jq query can return the string "null" if the queried field exists but its value is null. This would cause the script to proceed with ISSUE_NUMBER="null", which is not a valid issue number. You can use jq's alternative operator // to default to an empty string in this case, which will be correctly handled by the subsequent check for an empty string.

Suggested change
if ! ISSUE_NUMBER=$(gh pr view "${PR_NUMBER}" --repo "${GITHUB_REPOSITORY}" --json closingIssuesReferences -q '.closingIssuesReferences.nodes[0].number' 2>/dev/null); then
if ! ISSUE_NUMBER=$(gh pr view "${PR_NUMBER}" --repo "${GITHUB_REPOSITORY}" --json closingIssuesReferences -q '(.closingIssuesReferences.nodes[0].number // "")' 2>/dev/null); then

echo " ⚠️ Could not fetch closing issue for PR #${PR_NUMBER}"
fi

if [[ -z "${ISSUE_NUMBER}" ]]; then
Expand Down Expand Up @@ -95,21 +81,6 @@ process_pr() {
fi
done

# Find labels to remove (on PR but not on issue)
local LABELS_TO_REMOVE=""
for label in "${PR_LABEL_ARRAY[@]}"; do
if [[ -n "${label}" ]] && [[ " ${ISSUE_LABEL_ARRAY[*]} " != *" ${label} "* ]]; then
# Don't remove status/need-issue since we already handled it
if [[ "${label}" != "status/need-issue" ]]; then
if [[ -z "${LABELS_TO_REMOVE}" ]]; then
LABELS_TO_REMOVE="${label}"
else
LABELS_TO_REMOVE="${LABELS_TO_REMOVE},${label}"
fi
fi
fi
done

# Apply label changes
if [[ -n "${LABELS_TO_ADD}" ]]; then
echo "➕ Adding labels: ${LABELS_TO_ADD}"
Expand All @@ -118,14 +89,7 @@ process_pr() {
fi
fi

if [[ -n "${LABELS_TO_REMOVE}" ]]; then
echo "➖ Removing labels: ${LABELS_TO_REMOVE}"
if ! gh pr edit "${PR_NUMBER}" --repo "${GITHUB_REPOSITORY}" --remove-label "${LABELS_TO_REMOVE}" 2>/dev/null; then
echo " ⚠️ Failed to remove some labels"
fi
fi

if [[ -z "${LABELS_TO_ADD}" ]] && [[ -z "${LABELS_TO_REMOVE}" ]]; then
if [[ -z "${LABELS_TO_ADD}" ]]; then
echo "✅ Labels already synchronized"
fi
echo "needs_comment=false" >> "${GITHUB_OUTPUT}"
Expand Down
Loading