From e8693795fe38fc1903e9bdfd51e29b7d91b750c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 4 Nov 2026 13:10:32 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20a=20tag=20deleted=20and?= =?UTF-8?q?=20pushed=20again=20at=20another=20commit=20moves=20its=20relea?= =?UTF-8?q?se:=20the=20tag=20run's=20release=20takes=20the=20run's=20commi?= =?UTF-8?q?t=20as=20its=20target,=20and=20notes=20the=20forge=20wrote=20fo?= =?UTF-8?q?r=20the=20old=20commit,=20unedited,=20are=20written=20again=20f?= =?UTF-8?q?or=20the=20new=20one,=20while=20hand-written=20notes=20and=20th?= =?UTF-8?q?e=20title=20are=20kept;=20a=20run=20whose=20commit=20the=20tag?= =?UTF-8?q?=20no=20longer=20names=20leaves=20the=20target=20alone=20?= =?UTF-8?q?=F0=9F=93=9D=20docs:=20CI=20releases=20say=20how=20a=20moved=20?= =?UTF-8?q?tag=20is=20followed=20=E2=9C=85=20test:=20a=20moved=20tag=20mov?= =?UTF-8?q?es=20the=20release=20and=20its=20generated=20notes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/forge-web/src/ci/release.rs | 85 ++++++++++++++++++++++++++--- crates/forge-web/src/release_ops.rs | 46 ++++++++++++++++ docs/ci.md | 5 +- 3 files changed, 128 insertions(+), 8 deletions(-) diff --git a/crates/forge-web/src/ci/release.rs b/crates/forge-web/src/ci/release.rs index 4f8dfe4..d04775e 100644 --- a/crates/forge-web/src/ci/release.rs +++ b/crates/forge-web/src/ci/release.rs @@ -163,7 +163,10 @@ async fn release_for( actor: &User, ) -> Result { match state.store.release_by_tag(&repo.id, tag).await { - Ok(Some(existing)) => return Ok(existing), + Ok(Some(existing)) if existing.target_oid == run.commit_oid => return Ok(existing), + Ok(Some(existing)) => { + return Ok(follow_tag(state, repo, run, tag, spec, actor, existing).await); + } Ok(None) => {} Err(e) => { tracing::error!(error = %e, run_id = %run.id, "release: reading the release failed"); @@ -192,6 +195,40 @@ async fn release_for( } } +/// An existing release whose tag now names the run's commit (the tag was deleted and pushed +/// again elsewhere) follows it: its target becomes that commit, and notes the forge wrote for the +/// old target, unedited, are written again for the new one. Hand-written notes and the title are +/// kept. A failure is logged and the release is used as it is. +async fn follow_tag( + state: &AppState, + repo: &Repo, + run: &CiRun, + tag: &str, + spec: &ReleaseSpec, + actor: &User, + existing: Release, +) -> Release { + let generated = !matches!(spec.notes, ReleaseNotes::Off) + && changelog(state, repo, &existing.target_oid, tag) + .await + .as_deref() + == Ok(existing.body.as_str()); + let body = if generated { + Some(notes(state, repo, run, tag, spec).await) + } else { + None + }; + match release_ops::follow_moved_tag(state, repo, actor, &existing, &run.commit_oid, body).await + { + Ok(Some(moved)) => moved, + Ok(None) => existing, + Err(e) => { + tracing::error!(error = ?e, run_id = %run.id, tag, "release: moving the target failed"); + existing + } + } +} + /// Stores one artifact as an asset of `release`, replacing an asset with the same name. async fn attach( state: &AppState, @@ -240,7 +277,7 @@ async fn notes( if matches!(spec.notes, ReleaseNotes::Off) { return String::new(); } - match changelog(state, repo, run, tag).await { + match changelog(state, repo, &run.commit_oid, tag).await { Ok(text) => text, Err(e) => { tracing::error!(error = %e, run_id = %run.id, tag, "release: building the notes failed"); @@ -255,7 +292,7 @@ async fn notes( async fn changelog( state: &AppState, repo: &Repo, - run: &CiRun, + commit: &str, tag: &str, ) -> Result { let dir = repo_path(&state.git.data_dir, &repo.id).map_err(|e| e.to_string())?; @@ -268,7 +305,7 @@ async fn changelog( .map_err(|e| e.to_string())?; let mut previous: Option<(String, String)> = None; for release in releases.iter().filter(|r| !r.draft && r.tag != tag) { - if is_ancestor(env, &dir, &release.target_oid, &run.commit_oid) + if is_ancestor(env, &dir, &release.target_oid, commit) .await .unwrap_or(false) { @@ -277,20 +314,20 @@ async fn changelog( } } if previous.is_none() { - previous = previous_tag(env, &dir, &run.commit_oid) + previous = previous_tag(env, &dir, commit) .await .map_err(|e| e.to_string())?; } let commits = match &previous { Some((_, base)) => { - let mut commits = range_log(env, &dir, base, &run.commit_oid, MAX_NOTE_COMMITS) + let mut commits = range_log(env, &dir, base, commit, MAX_NOTE_COMMITS) .await .map_err(|e| e.to_string())?; commits.reverse(); commits } - None => log(env, &dir, &run.commit_oid, None, 0, MAX_NOTE_COMMITS) + None => log(env, &dir, commit, None, 0, MAX_NOTE_COMMITS) .await .map_err(|e| e.to_string())?, }; @@ -660,6 +697,40 @@ title = "forge {tag}" ); } + #[tokio::test] + async fn moved_tag_moves_the_release_and_its_generated_notes() { + let fx = fixture().await; + let first = push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v1.0").await; + finish_all(&fx, &run_of(&fx, 1).await.id, None).await; + on_run_finished(&fx.state, &run_of(&fx, 1).await).await; + let before = release_named(&fx, "v1.0").await.expect("release made"); + assert_eq!(before.target_oid, first); + + let oid = commit_message(&fx.work, "fix: later"); + land(&fx, &oid, "refs/tags/v1.0"); + after_ref_update( + &fx.state, + &fx.repo, + &fx.owner.id, + "refs/tags/v1.0", + &first, + &oid, + ) + .await + .expect("post update"); + finish_all(&fx, &run_of(&fx, 2).await.id, None).await; + on_run_finished(&fx.state, &run_of(&fx, 2).await).await; + + let after = release_named(&fx, "v1.0").await.expect("release kept"); + assert_eq!(after.id, before.id); + assert_eq!(after.target_oid, oid); + assert!( + after.body.contains("later"), + "notes follow the tag: {}", + after.body + ); + } + #[tokio::test] async fn existing_manual_release_keeps_its_fields_and_gets_assets() { let fx = fixture().await; diff --git a/crates/forge-web/src/release_ops.rs b/crates/forge-web/src/release_ops.rs index 856727d..83fcdf7 100644 --- a/crates/forge-web/src/release_ops.rs +++ b/crates/forge-web/src/release_ops.rs @@ -407,6 +407,52 @@ pub async fn update( Ok(after) } +/// Moves a release to `commit` after its git tag was moved outside the forge (deleted and pushed +/// again). `body`, when given, replaces the notes. Returns None, changing nothing, when the tag +/// does not name `commit` now: a newer push moved it again. A published release emits `updated`. +pub async fn follow_moved_tag( + state: &AppState, + repo: &Repo, + actor: &User, + release: &Release, + commit: &str, + body: Option, +) -> Result, ReleaseError> { + ensure_same_repo(repo, release)?; + let refs = refs_of(path_of(state, repo)?).await?; + if tag_oid(&refs, &release.tag).as_deref() != Some(commit) { + return Ok(None); + } + if !state + .store + .set_release_tag(&release.id, &release.tag, commit) + .await? + { + return Err(ReleaseError::NotFound); + } + if let Some(body) = &body { + let fields = ReleaseEdit { + title: &release.title, + body, + draft: release.draft, + prerelease: release.prerelease, + }; + if !state.store.update_release(&release.id, &fields).await? { + return Err(ReleaseError::NotFound); + } + } + bump(state, repo); + let after = state + .store + .release(&release.id) + .await? + .ok_or(ReleaseError::NotFound)?; + if !after.draft { + emit_release(state, repo, actor, "updated", &after).await; + } + Ok(Some(after)) +} + /// Deletes a release and its assets, then their blobs. The git tag is kept. A published release /// emits `deleted`. pub async fn delete( diff --git a/docs/ci.md b/docs/ci.md index af61cbe..de5b16c 100644 --- a/docs/ci.md +++ b/docs/ci.md @@ -319,7 +319,10 @@ A `[release]` table publishes a release from a tag run. It needs at least one gl When a tag push matches `on.tags` and **every job in the run succeeds**, the forge creates the release for that tag and attaches the selected artifacts. If a release for that tag already exists, for example one you made by hand, the forge updates it: the existing title and notes are kept, and -the selected artifacts are attached, replacing any asset with the same name. A run where any job +the selected artifacts are attached, replacing any asset with the same name. If the tag was moved +(deleted and pushed again at another commit), the release's target follows it to the run's commit, +and notes the forge wrote for the old commit, unedited, are written again for the new one; +hand-written notes are kept. A run where any job fails or is skipped creates no release. The job needs no token and no `curl`. This holds when jobs run in parallel. If the job that produces the artifacts (say `build`) finishes