fix: fall back to create+delete when a draft tag rename hits a duplicate-key 500 #49

Merged
robin.wieske merged 1 commit from fix/47-draft-tag-rename-conflict into main 2026-10-02 17:53:09 +02:00
Owner

Summary

Fixes #47.

Since #44, create_or_update_release() always PATCHes the existing draft with the freshly computed tag, including a changed tag_name when the bump type escalates (e.g. patch → minor). Forgejo can answer that rename with an HTTP 500 unique-constraint violation on (repo_id, tag_name) even when no release or git tag with that name is visible through the API. Every subsequent run then fails identically — the action wedges.

Root-cause verification

Confirmed against Forgejo's own source (services/release/release.go, models/repo/release.go), not just the issue's hypothesis:

// createTag(): only touches the git tag ref when publishing
if !rel.IsDraft {
    if !gitRepo.IsTagExist(rel.TagName) { ... creates the git ref ... }
}

// repo_model.UpdateRelease(): called unconditionally after createTag()
_, err := db.GetEngine(ctx).ID(rel.ID).AllCols().Update(rel)

For a draft, createTag()'s whole body is skipped (no git ref is touched), but UpdateRelease() still does AllCols().Update(rel), writing the new tag_name straight to the DB regardless. The Release struct has TagName string xorm:"INDEX UNIQUE(n)"`` on (RepoID, TagName) — that's UQE_release_n, matching the error ('35-v0.1.0') exactly.

So: the web UI's edit-release form never exposes a tag field on edit, which is why nobody hits this by hand — but the API's PATCH has no such guard and applies a changed tag_name blind. This is a server-side gap exercised only by direct API clients like this action.

This also explains the "invisible conflicting row": Release has an IsTag bool field — "true only if the record is a tag and has no related releases" — i.e. Forgejo keeps bookkeeping rows for raw git tags in the same table, sharing the same unique index. GET /releases filters is_tag=false and GET /tags reads live git refs; neither surfaces an orphaned is_tag=true row with no corresponding ref. That's consistent with everything observed on the reported repo (Aevana/aevana, read-only):

  • GET /repos/Aevana/aevana/releases (unfiltered) and ?draft=true: only v0.0.1...v0.0.8 (published) and v0.0.9 (the one draft, id 594). No v0.1.0 anywhere.
  • GET /repos/Aevana/aevana/releases/tags/v0.1.0: 404.
  • fj tag list / fj tag view v0.1.0: no v0.1.0 git tag exists.

I did not write-probe that repo (e.g. a throwaway POST .../releases with tag_name=v0.1.0) to empirically confirm the fallback's POST succeeds, since it isn't mine. Given the source-level confirmation above — CreateRelease checks IsReleaseExist (which itself likely filters is_tag, same asymmetry) rather than blindly inserting — a fresh POST for the new tag should succeed where the PATCH-rename of a different row id cannot. Happy to run the probe with your go-ahead, or confirm on the next real run.

Changes

  • check_for_conflicting_tag(): before a tag rename, checks both /releases/tags/{tag} and /tags/{tag} and logs what's found — including explicitly logging when neither shows anything, since that absence is itself the diagnostic signal for this failure mode.
  • On 409/422/500 from a tag-renaming PATCH, fall back to create_then_delete_old_draft(): POST a new draft for the new tag, then DELETE the stale draft by id. Retrying the identical PATCH would just reproduce the same deterministic conflict, so no retry loop was added.
  • If the fallback's DELETE fails, raise ReleaseLookupError instead of logging and continuing — otherwise a duplicate draft could accumulate silently across runs.
  • ReleaseLookupError messages now include the method, URL, and a (body-truncated, to keep the "duplicate" case from dumping the entire rendered release notes) request payload, so a failure is reproducible from the logs alone.

Intentionally not implemented from the issue's suggestions: a blind retry-once-for-5xx — retrying a deterministic unique-constraint violation just reproduces the same error.

Test plan

  • python3 -m ast syntax check passes.
  • Confirmed the reported repo's release/tag state as described above.
  • No test harness exists in this repo. Recommend watching the next merge on Aevana/aevana (or a deliberate patch→minor escalation on a repo with an existing draft) to confirm the fallback actually fires and succeeds.

🤖 Generated with Claude Code

## Summary Fixes #47. Since #44, `create_or_update_release()` always `PATCH`es the existing draft with the freshly computed tag, including a changed `tag_name` when the bump type escalates (e.g. patch → minor). Forgejo can answer that rename with an HTTP 500 unique-constraint violation on `(repo_id, tag_name)` even when no release or git tag with that name is visible through the API. Every subsequent run then fails identically — the action wedges. ## Root-cause verification **Confirmed against Forgejo's own source** (`services/release/release.go`, `models/repo/release.go`), not just the issue's hypothesis: ```go // createTag(): only touches the git tag ref when publishing if !rel.IsDraft { if !gitRepo.IsTagExist(rel.TagName) { ... creates the git ref ... } } // repo_model.UpdateRelease(): called unconditionally after createTag() _, err := db.GetEngine(ctx).ID(rel.ID).AllCols().Update(rel) ``` For a draft, `createTag()`'s whole body is skipped (no git ref is touched), but `UpdateRelease()` still does `AllCols().Update(rel)`, writing the new `tag_name` straight to the DB regardless. The `Release` struct has `TagName string `xorm:"INDEX UNIQUE(n)"`` on `(RepoID, TagName)` — that's `UQE_release_n`, matching the error (`'35-v0.1.0'`) exactly. So: the web UI's edit-release form never exposes a tag field on edit, which is why nobody hits this by hand — but the API's `PATCH` has no such guard and applies a changed `tag_name` blind. This is a server-side gap exercised only by direct API clients like this action. This also explains the "invisible conflicting row": `Release` has an `IsTag bool` field — "true only if the record is a tag and has no related releases" — i.e. Forgejo keeps bookkeeping rows for raw git tags in the *same* table, sharing the same unique index. `GET /releases` filters `is_tag=false` and `GET /tags` reads live git refs; neither surfaces an orphaned `is_tag=true` row with no corresponding ref. That's consistent with everything observed on the reported repo (`Aevana/aevana`, read-only): - `GET /repos/Aevana/aevana/releases` (unfiltered) and `?draft=true`: only `v0.0.1`...`v0.0.8` (published) and `v0.0.9` (the one draft, id 594). No `v0.1.0` anywhere. - `GET /repos/Aevana/aevana/releases/tags/v0.1.0`: 404. - `fj tag list` / `fj tag view v0.1.0`: no `v0.1.0` git tag exists. I did not write-probe that repo (e.g. a throwaway `POST .../releases` with `tag_name=v0.1.0`) to empirically confirm the fallback's `POST` succeeds, since it isn't mine. Given the source-level confirmation above — `CreateRelease` checks `IsReleaseExist` (which itself likely filters `is_tag`, same asymmetry) rather than blindly inserting — a fresh `POST` for the new tag should succeed where the `PATCH`-rename of a different row id cannot. Happy to run the probe with your go-ahead, or confirm on the next real run. ## Changes - `check_for_conflicting_tag()`: before a tag rename, checks both `/releases/tags/{tag}` and `/tags/{tag}` and logs what's found — including explicitly logging when *neither* shows anything, since that absence is itself the diagnostic signal for this failure mode. - On `409`/`422`/`500` from a tag-renaming `PATCH`, fall back to `create_then_delete_old_draft()`: `POST` a new draft for the new tag, then `DELETE` the stale draft by id. Retrying the identical `PATCH` would just reproduce the same deterministic conflict, so no retry loop was added. - If the fallback's `DELETE` fails, raise `ReleaseLookupError` instead of logging and continuing — otherwise a duplicate draft could accumulate silently across runs. - `ReleaseLookupError` messages now include the method, URL, and a (body-truncated, to keep the "duplicate" case from dumping the entire rendered release notes) request payload, so a failure is reproducible from the logs alone. Intentionally not implemented from the issue's suggestions: a blind retry-once-for-5xx — retrying a deterministic unique-constraint violation just reproduces the same error. ## Test plan - [x] `python3 -m ast` syntax check passes. - [x] Confirmed the reported repo's release/tag state as described above. - [ ] No test harness exists in this repo. Recommend watching the next merge on `Aevana/aevana` (or a deliberate patch→minor escalation on a repo with an existing draft) to confirm the fallback actually fires and succeeds. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
fix: fall back to create+delete when a draft tag rename hits a duplicate-key 500
All checks were successful
Tag Version / update_tags (pull_request) Successful in 14s
c57a9ddccc
Since #44, create_or_update_release() always PATCHes the existing draft
with the freshly computed tag, including a changed tag_name when the
bump type escalates (e.g. patch -> minor). Forgejo can answer that
rename with an HTTP 500 unique-constraint violation on
(repo_id, tag_name) even when no release or git tag with that name is
visible through the API -- the conflicting row isn't exposed by either
/releases or /tags, so there's nothing the action can clean up directly.
Every subsequent run then fails identically, wedging the workflow.

- Before a rename, check both /releases/tags/{tag} and /tags/{tag} and
  log what's found (or that nothing is visible, which is itself the
  useful signal here).
- On 409/422/500 from a tag-renaming PATCH, fall back to POSTing a new
  draft for the new tag and deleting the stale draft by id, instead of
  retrying the same deterministic conflict.
- Raise loudly (not log-and-continue) if the fallback's delete fails,
  so a leftover duplicate draft doesn't go unnoticed.
- Include method, URL and a (body-truncated) request payload in
  ReleaseLookupError messages so a failure is reproducible from the
  logs alone.

Fixes #47

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gzr2gEQRrGdjGJFgensq9f
robin.wieske deleted branch fix/47-draft-tag-rename-conflict 2026-10-02 17:53:10 +02:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Wi3eDigital/forgejo-release-drafter-action!49
No description provided.