A tag deleted and pushed again at another commit moves its release: the tag run's release takes the run's commit as its target, and notes the forge wrote for the old commit, unedited, are written again for the new one, while hand-written notes and the title are kept
Browse files at this commite8693795fe38fc1903e9bdfd51e29b7d91b750c0Parent: 7d1f5e9All checks have passed
Show checks
ci/buildSucceededDetails
- fix
- a tag deleted and pushed again at another commit moves its release: the tag run's release takes the run's commit as its target, and notes the forge wrote for the old commit, unedited, are written again for the new one, while hand-written notes and the title are kept
- a run whose commit the tag no longer names leaves the target alone 📝 docs: CI releases say how a moved tag is followed ✅ test: a moved tag moves the release and its generated notes
3 changed files +128 -8
- crates/forge-web/src/ci/release.rs +78 -7
- crates/forge-web/src/release_ops.rs +46 -0
- docs/ci.md +4 -1
crates/forge-web/src/ci/release.rsmodified+78 -7
| @@ -163,7 +163,10 @@ async fn release_for( | ||
| 163 | 163 | actor: &User, |
| 164 | 164 | ) -> Result<Release, String> { |
| 165 | 165 | match state.store.release_by_tag(&repo.id, tag).await { |
| 166 | - Ok(Some(existing)) => return Ok(existing), | |
| 166 | + Ok(Some(existing)) if existing.target_oid == run.commit_oid => return Ok(existing), | |
| 167 | + Ok(Some(existing)) => { | |
| 168 | + return Ok(follow_tag(state, repo, run, tag, spec, actor, existing).await); | |
| 169 | + } | |
| 167 | 170 | Ok(None) => {} |
| 168 | 171 | Err(e) => { |
| 169 | 172 | tracing::error!(error = %e, run_id = %run.id, "release: reading the release failed"); |
| @@ -192,6 +195,40 @@ async fn release_for( | ||
| 192 | 195 | } |
| 193 | 196 | } |
| 194 | 197 | |
| 198 | +/// An existing release whose tag now names the run's commit (the tag was deleted and pushed | |
| 199 | +/// again elsewhere) follows it: its target becomes that commit, and notes the forge wrote for the | |
| 200 | +/// old target, unedited, are written again for the new one. Hand-written notes and the title are | |
| 201 | +/// kept. A failure is logged and the release is used as it is. | |
| 202 | +async fn follow_tag( | |
| 203 | + state: &AppState, | |
| 204 | + repo: &Repo, | |
| 205 | + run: &CiRun, | |
| 206 | + tag: &str, | |
| 207 | + spec: &ReleaseSpec, | |
| 208 | + actor: &User, | |
| 209 | + existing: Release, | |
| 210 | +) -> Release { | |
| 211 | + let generated = !matches!(spec.notes, ReleaseNotes::Off) | |
| 212 | + && changelog(state, repo, &existing.target_oid, tag) | |
| 213 | + .await | |
| 214 | + .as_deref() | |
| 215 | + == Ok(existing.body.as_str()); | |
| 216 | + let body = if generated { | |
| 217 | + Some(notes(state, repo, run, tag, spec).await) | |
| 218 | + } else { | |
| 219 | + None | |
| 220 | + }; | |
| 221 | + match release_ops::follow_moved_tag(state, repo, actor, &existing, &run.commit_oid, body).await | |
| 222 | + { | |
| 223 | + Ok(Some(moved)) => moved, | |
| 224 | + Ok(None) => existing, | |
| 225 | + Err(e) => { | |
| 226 | + tracing::error!(error = ?e, run_id = %run.id, tag, "release: moving the target failed"); | |
| 227 | + existing | |
| 228 | + } | |
| 229 | + } | |
| 230 | +} | |
| 231 | + | |
| 195 | 232 | /// Stores one artifact as an asset of `release`, replacing an asset with the same name. |
| 196 | 233 | async fn attach( |
| 197 | 234 | state: &AppState, |
| @@ -240,7 +277,7 @@ async fn notes( | ||
| 240 | 277 | if matches!(spec.notes, ReleaseNotes::Off) { |
| 241 | 278 | return String::new(); |
| 242 | 279 | } |
| 243 | - match changelog(state, repo, run, tag).await { | |
| 280 | + match changelog(state, repo, &run.commit_oid, tag).await { | |
| 244 | 281 | Ok(text) => text, |
| 245 | 282 | Err(e) => { |
| 246 | 283 | tracing::error!(error = %e, run_id = %run.id, tag, "release: building the notes failed"); |
| @@ -255,7 +292,7 @@ async fn notes( | ||
| 255 | 292 | async fn changelog( |
| 256 | 293 | state: &AppState, |
| 257 | 294 | repo: &Repo, |
| 258 | - run: &CiRun, | |
| 295 | + commit: &str, | |
| 259 | 296 | tag: &str, |
| 260 | 297 | ) -> Result<String, String> { |
| 261 | 298 | let dir = repo_path(&state.git.data_dir, &repo.id).map_err(|e| e.to_string())?; |
| @@ -268,7 +305,7 @@ async fn changelog( | ||
| 268 | 305 | .map_err(|e| e.to_string())?; |
| 269 | 306 | let mut previous: Option<(String, String)> = None; |
| 270 | 307 | for release in releases.iter().filter(|r| !r.draft && r.tag != tag) { |
| 271 | - if is_ancestor(env, &dir, &release.target_oid, &run.commit_oid) | |
| 308 | + if is_ancestor(env, &dir, &release.target_oid, commit) | |
| 272 | 309 | .await |
| 273 | 310 | .unwrap_or(false) |
| 274 | 311 | { |
| @@ -277,20 +314,20 @@ async fn changelog( | ||
| 277 | 314 | } |
| 278 | 315 | } |
| 279 | 316 | if previous.is_none() { |
| 280 | - previous = previous_tag(env, &dir, &run.commit_oid) | |
| 317 | + previous = previous_tag(env, &dir, commit) | |
| 281 | 318 | .await |
| 282 | 319 | .map_err(|e| e.to_string())?; |
| 283 | 320 | } |
| 284 | 321 | |
| 285 | 322 | let commits = match &previous { |
| 286 | 323 | Some((_, base)) => { |
| 287 | - let mut commits = range_log(env, &dir, base, &run.commit_oid, MAX_NOTE_COMMITS) | |
| 324 | + let mut commits = range_log(env, &dir, base, commit, MAX_NOTE_COMMITS) | |
| 288 | 325 | .await |
| 289 | 326 | .map_err(|e| e.to_string())?; |
| 290 | 327 | commits.reverse(); |
| 291 | 328 | commits |
| 292 | 329 | } |
| 293 | - None => log(env, &dir, &run.commit_oid, None, 0, MAX_NOTE_COMMITS) | |
| 330 | + None => log(env, &dir, commit, None, 0, MAX_NOTE_COMMITS) | |
| 294 | 331 | .await |
| 295 | 332 | .map_err(|e| e.to_string())?, |
| 296 | 333 | }; |
| @@ -660,6 +697,40 @@ title = "forge {tag}" | ||
| 660 | 697 | ); |
| 661 | 698 | } |
| 662 | 699 | |
| 700 | + #[tokio::test] | |
| 701 | + async fn moved_tag_moves_the_release_and_its_generated_notes() { | |
| 702 | + let fx = fixture().await; | |
| 703 | + let first = push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v1.0").await; | |
| 704 | + finish_all(&fx, &run_of(&fx, 1).await.id, None).await; | |
| 705 | + on_run_finished(&fx.state, &run_of(&fx, 1).await).await; | |
| 706 | + let before = release_named(&fx, "v1.0").await.expect("release made"); | |
| 707 | + assert_eq!(before.target_oid, first); | |
| 708 | + | |
| 709 | + let oid = commit_message(&fx.work, "fix: later"); | |
| 710 | + land(&fx, &oid, "refs/tags/v1.0"); | |
| 711 | + after_ref_update( | |
| 712 | + &fx.state, | |
| 713 | + &fx.repo, | |
| 714 | + &fx.owner.id, | |
| 715 | + "refs/tags/v1.0", | |
| 716 | + &first, | |
| 717 | + &oid, | |
| 718 | + ) | |
| 719 | + .await | |
| 720 | + .expect("post update"); | |
| 721 | + finish_all(&fx, &run_of(&fx, 2).await.id, None).await; | |
| 722 | + on_run_finished(&fx.state, &run_of(&fx, 2).await).await; | |
| 723 | + | |
| 724 | + let after = release_named(&fx, "v1.0").await.expect("release kept"); | |
| 725 | + assert_eq!(after.id, before.id); | |
| 726 | + assert_eq!(after.target_oid, oid); | |
| 727 | + assert!( | |
| 728 | + after.body.contains("later"), | |
| 729 | + "notes follow the tag: {}", | |
| 730 | + after.body | |
| 731 | + ); | |
| 732 | + } | |
| 733 | + | |
| 663 | 734 | #[tokio::test] |
| 664 | 735 | async fn existing_manual_release_keeps_its_fields_and_gets_assets() { |
| 665 | 736 | let fx = fixture().await; |
crates/forge-web/src/release_ops.rsmodified+46 -0
| @@ -407,6 +407,52 @@ pub async fn update( | ||
| 407 | 407 | Ok(after) |
| 408 | 408 | } |
| 409 | 409 | |
| 410 | +/// Moves a release to `commit` after its git tag was moved outside the forge (deleted and pushed | |
| 411 | +/// again). `body`, when given, replaces the notes. Returns None, changing nothing, when the tag | |
| 412 | +/// does not name `commit` now: a newer push moved it again. A published release emits `updated`. | |
| 413 | +pub async fn follow_moved_tag( | |
| 414 | + state: &AppState, | |
| 415 | + repo: &Repo, | |
| 416 | + actor: &User, | |
| 417 | + release: &Release, | |
| 418 | + commit: &str, | |
| 419 | + body: Option<String>, | |
| 420 | +) -> Result<Option<Release>, ReleaseError> { | |
| 421 | + ensure_same_repo(repo, release)?; | |
| 422 | + let refs = refs_of(path_of(state, repo)?).await?; | |
| 423 | + if tag_oid(&refs, &release.tag).as_deref() != Some(commit) { | |
| 424 | + return Ok(None); | |
| 425 | + } | |
| 426 | + if !state | |
| 427 | + .store | |
| 428 | + .set_release_tag(&release.id, &release.tag, commit) | |
| 429 | + .await? | |
| 430 | + { | |
| 431 | + return Err(ReleaseError::NotFound); | |
| 432 | + } | |
| 433 | + if let Some(body) = &body { | |
| 434 | + let fields = ReleaseEdit { | |
| 435 | + title: &release.title, | |
| 436 | + body, | |
| 437 | + draft: release.draft, | |
| 438 | + prerelease: release.prerelease, | |
| 439 | + }; | |
| 440 | + if !state.store.update_release(&release.id, &fields).await? { | |
| 441 | + return Err(ReleaseError::NotFound); | |
| 442 | + } | |
| 443 | + } | |
| 444 | + bump(state, repo); | |
| 445 | + let after = state | |
| 446 | + .store | |
| 447 | + .release(&release.id) | |
| 448 | + .await? | |
| 449 | + .ok_or(ReleaseError::NotFound)?; | |
| 450 | + if !after.draft { | |
| 451 | + emit_release(state, repo, actor, "updated", &after).await; | |
| 452 | + } | |
| 453 | + Ok(Some(after)) | |
| 454 | +} | |
| 455 | + | |
| 410 | 456 | /// Deletes a release and its assets, then their blobs. The git tag is kept. A published release |
| 411 | 457 | /// emits `deleted`. |
| 412 | 458 | pub async fn delete( |
docs/ci.mdmodified+4 -1
| @@ -319,7 +319,10 @@ A `[release]` table publishes a release from a tag run. It needs at least one gl | ||
| 319 | 319 | When a tag push matches `on.tags` and **every job in the run succeeds**, the forge creates the |
| 320 | 320 | release for that tag and attaches the selected artifacts. If a release for that tag already exists, |
| 321 | 321 | for example one you made by hand, the forge updates it: the existing title and notes are kept, and |
| 322 | -the selected artifacts are attached, replacing any asset with the same name. A run where any job | |
| 322 | +the selected artifacts are attached, replacing any asset with the same name. If the tag was moved | |
| 323 | +(deleted and pushed again at another commit), the release's target follows it to the run's commit, | |
| 324 | +and notes the forge wrote for the old commit, unedited, are written again for the new one; | |
| 325 | +hand-written notes are kept. A run where any job | |
| 323 | 326 | fails or is skipped creates no release. The job needs no token and no `curl`. |
| 324 | 327 | |
| 325 | 328 | This holds when jobs run in parallel. If the job that produces the artifacts (say `build`) finishes |