From 6ff27295e6223a76fc28131a519dc8027d0a661e Mon Sep 17 00:00:00 2001 From: Jaybe Date: Fri, 14 Nov 2025 22:33:43 +0900 Subject: [PATCH] =?UTF-8?q?chore(ci):=20prevent=20duplicate=20'automerge'?= =?UTF-8?q?=20creation=20=E2=80=94=20use=20existing=20label=20id=20only=20?= =?UTF-8?q?(auto-label/auto-pr)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/auto-label.yml | 19 ++++++------------- .gitea/workflows/auto-pr.yml | 19 ++++++------------- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/.gitea/workflows/auto-label.yml b/.gitea/workflows/auto-label.yml index 723c48d..f66113a 100644 --- a/.gitea/workflows/auto-label.yml +++ b/.gitea/workflows/auto-label.yml @@ -20,22 +20,15 @@ jobs: echo "CI_TOKEN not set; skipping labeling"; exit 0; fi OWNER=$(echo "$REPO" | cut -d'/' -f1) NAME=$(echo "$REPO" | cut -d'/' -f2) - echo "Resolving label id for 'automerge' in $OWNER/$NAME" + echo "Resolving existing label id for 'automerge' in $OWNER/$NAME (no creation)" curl -sS -o /tmp/labels.json -H "Authorization: token ${CI_TOKEN}" \ "${GITEA_BASE_URL}/api/v1/repos/${OWNER}/${NAME}/labels?limit=1000" || true - # Robust parse without jq: find the id preceding name "automerge" - LID=$(tr '\n' ' ' < /tmp/labels.json | grep -o '"id":[0-9][0-9]*[^\}]*"name":"automerge"' | head -n1 | sed -E 's/.*"id":([0-9]+).*/\1/' || true) + # Extract all ids whose name == automerge, prefer the smallest id (oldest) + LID=$(tr '\n' ' ' < /tmp/labels.json | \ + grep -o '"id":[0-9][0-9]*[^\}]*"name":"automerge"' | \ + sed -E 's/.*"id":([0-9]+).*/\1/' | sort -n | head -n1 || true) if [ -z "${LID:-}" ]; then - echo "Label 'automerge' not found. Creating..." - curl -sS -o /tmp/create_label.json -X POST \ - -H "Content-Type: application/json" \ - -H "Authorization: token ${CI_TOKEN}" \ - "${GITEA_BASE_URL}/api/v1/repos/${OWNER}/${NAME}/labels" \ - -d '{"name":"automerge","color":"#0ea5e9"}' - LID=$(tr '\n' ' ' < /tmp/create_label.json | sed -E 's/.*"id":([0-9]+).*/\1/' | head -n1 || true) - fi - if [ -z "${LID:-}" ]; then - echo "Failed to resolve or create label id; skipping"; exit 0; fi + echo "No existing 'automerge' label found; skipping labeling"; exit 0; fi echo "Labeling PR #$PR_NUMBER with id $LID" curl -sS -X POST \ -H "Content-Type: application/json" \ diff --git a/.gitea/workflows/auto-pr.yml b/.gitea/workflows/auto-pr.yml index 87d8fe2..b9d12bd 100644 --- a/.gitea/workflows/auto-pr.yml +++ b/.gitea/workflows/auto-pr.yml @@ -55,19 +55,12 @@ jobs: done if [ -n "${PR_NUMBER:-}" ]; then - echo "Resolving label id for 'automerge' in $OWNER/$NAME" + echo "Resolving existing label id for 'automerge' (no creation)" curl -sS -o /tmp/labels.json -H "Authorization: token ${CI_TOKEN}" \ - "${GITEA_BASE_URL}/api/v1/repos/${OWNER}/${NAME}/labels" - LID=$(awk 'BEGIN{RS=","} /"name":"automerge"/ {if (match($0,/"id":([0-9]+)/,m)) {print m[1]; exit}}' /tmp/labels.json || true) - if [ -z "${LID:-}" ]; then - echo "Label 'automerge' not found. Creating..." - curl -sS -o /tmp/create_label.json -X POST \ - -H "Content-Type: application/json" \ - -H "Authorization: token ${CI_TOKEN}" \ - "${GITEA_BASE_URL}/api/v1/repos/${OWNER}/${NAME}/labels" \ - -d '{"name":"automerge","color":"#0ea5e9"}' - LID=$(sed -n 's/.*"id":\([0-9]*\).*/\1/p' /tmp/create_label.json | head -n1 || true) - fi + "${GITEA_BASE_URL}/api/v1/repos/${OWNER}/${NAME}/labels?limit=1000" || true + LID=$(tr '\n' ' ' < /tmp/labels.json | \ + grep -o '"id":[0-9][0-9]*[^\}]*"name":"automerge"' | \ + sed -E 's/.*"id":([0-9]+).*/\1/' | sort -n | head -n1 || true) if [ -n "${LID:-}" ]; then echo "Adding 'automerge' (id=$LID) label to PR #$PR_NUMBER" curl -sS -X POST \ @@ -76,7 +69,7 @@ jobs: "${GITEA_BASE_URL}/api/v1/repos/${OWNER}/${NAME}/issues/${PR_NUMBER}/labels" \ -d '{"labels":['"${LID}"']}' || true else - echo "Failed to resolve label id; skipping label add" + echo "No existing 'automerge' label found; skipping label add" fi else echo "PR number could not be determined; skipping label add"