diff --git a/crates/forge-git/src/browse.rs b/crates/forge-git/src/browse.rs index fad5f7a..529361b 100644 --- a/crates/forge-git/src/browse.rs +++ b/crates/forge-git/src/browse.rs @@ -191,6 +191,19 @@ pub fn default_branch_commit(repo: &Path, branch: &str) -> Result Ok(is_commit(&repo, id)?.then(|| id.to_hex().to_string())) } +/// The commit a full object id names, peeling annotated tags (a pushed `git tag -a` points its +/// ref at a tag object). None when the object is missing or does not peel to a commit. +pub fn peel_to_commit(repo: &Path, oid: &str) -> Result> { + let repo = open(repo)?; + let Some(object) = repo.try_find_object(parse_oid(oid)?).map_err(gerr)? else { + return Ok(None); + }; + Ok(object + .peel_to_commit() + .ok() + .map(|c| c.id.to_hex().to_string())) +} + /// Resolve user input to a full commit oid. Accepts a branch or tag name, or a 4-64 char hex prefix /// that matches exactly one commit. Anything else resolves to None. pub fn resolve_commit(repo: &Path, rev: &str) -> Result> { diff --git a/crates/forge-web/src/ci/schedule.rs b/crates/forge-web/src/ci/schedule.rs index 6a79a1d..c8929de 100644 --- a/crates/forge-web/src/ci/schedule.rs +++ b/crates/forge-web/src/ci/schedule.rs @@ -9,7 +9,7 @@ use forge_core::authz::{Action, authorize}; use forge_core::ci::{CiRun, NewJob}; use forge_core::store::commit_status::StatusState; use forge_core::{PullRequest, Repo}; -use forge_git::browse::{read_blob, resolve_commit}; +use forge_git::browse::{peel_to_commit, read_blob, resolve_commit}; use tokio::io::AsyncWriteExt; use super::api::fit_to_cap; @@ -84,6 +84,12 @@ pub async fn on_ref_update( return; }; let result = async { + // An annotated tag's ref names a tag object: the run, its statuses and any release are + // for the commit it points at. + let Some(commit) = peeled_commit(state, &repo.id, new_oid).await else { + return forge_core::Result::Ok(()); + }; + let new_oid = commit.as_str(); let loaded = load(state, &repo.id, new_oid).await; if let Plan::Run { jobs, @@ -515,6 +521,26 @@ pub(super) async fn config_at(state: &AppState, repo_id: &str, commit: &str) -> } } +/// The commit `oid` peels to, logging why when it names none. +async fn peeled_commit(state: &AppState, repo_id: &str, oid: &str) -> Option { + let dir = match forge_git::repo::repo_path(&state.git.data_dir, repo_id) { + Ok(path) => path, + Err(e) => { + tracing::error!(error = %e, repo_id, "ci: bad repo path"); + return None; + } + }; + let rev = oid.to_owned(); + match blocking(move || peel_to_commit(&dir, &rev)).await { + Ok(Some(commit)) => Some(commit), + Ok(None) => None, // a tag on a tree or blob: nothing to build + Err(e) => { + tracing::error!(error = %e, repo_id, oid, "ci: peeling the pushed ref failed"); + None + } + } +} + async fn load(state: &AppState, repo_id: &str, commit: &str) -> Loaded { let repo_dir = match forge_git::repo::repo_path(&state.git.data_dir, repo_id) { Ok(path) => path, @@ -947,6 +973,32 @@ run = "make" .expect("run"); assert_eq!(run.trigger, "tag"); assert_eq!(run.git_ref, "refs/tags/v1.0"); + + // An annotated tag's ref names a tag object; the run is for the commit under it. + let head = commit(&fx.work, &[(CONFIG_PATH, CONFIG_TAGS)]); + git(&fx.work, &["tag", "-a", "v2.0", "-m", "v2.0"]); + let tag_obj = git(&fx.work, &["rev-parse", "v2.0"]); + assert_ne!(tag_obj, head); + land(&fx, &tag_obj, "refs/tags/v2.0"); + after_ref_update( + &fx.state, + &fx.repo, + &fx.owner.id, + "refs/tags/v2.0", + ZERO, + &tag_obj, + ) + .await + .expect("post update"); + assert_eq!(runs_total(&fx).await, 3); + let run = fx + .state + .store + .run_by_number(&fx.repo.id, 3) + .await + .expect("query") + .expect("run"); + assert_eq!(run.commit_oid, head); } /// Opens a pull request `author` -> `branch` whose head repo is `head_repo_id`, and runs the