Skip to content
Merged
Prev Previous commit
fix: add ?prefix= server-side cert lookup for CLI short-ID resolution
  • Loading branch information
Gravirei committed Jul 10, 2026
commit e657346a0c14c3f5eff361219f96cfc2509a0546
13 changes: 12 additions & 1 deletion crates/gitlawb-node/src/api/certs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,18 @@ pub async fn list_certs(
let (record, _rules) =
crate::api::authorize_repo_read(&state, &owner, &name, caller, "/").await?;

let certs = state.db.list_ref_certificates(&record.id, limit).await?;
// When a prefix is given (short-ID resolution from the CLI) use a
// generous limit and delegate to the prefix-matched query so the
// caller can resolve IDs regardless of how many certs exist.
let prefix = params.get("prefix").filter(|p| !p.is_empty());
let certs = if let Some(prefix) = prefix {
state
.db
.list_ref_certificates_by_prefix(&record.id, prefix, 200)
.await?
} else {
state.db.list_ref_certificates(&record.id, limit).await?
};
let certs_json: Vec<serde_json::Value> = certs
.iter()
.map(|c| {
Expand Down
24 changes: 24 additions & 0 deletions crates/gitlawb-node/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,30 @@ impl Db {
Ok(rows.into_iter().map(row_to_cert).collect())
}

/// Look up ref certificates whose id starts with the given prefix.
/// Used by the CLI for short-ID resolution where the caller does not know
/// the full UUID. Bounded by `limit` for safety — the caller should pass a
/// generous cap (e.g. 200) since prefix-matching narrows the result set.
pub async fn list_ref_certificates_by_prefix(
&self,
repo_id: &str,
prefix: &str,
limit: i64,
) -> Result<Vec<RefCertificate>> {
let limit = limit.max(1);
let pattern = format!("{}%", prefix);
let rows = sqlx::query(
"SELECT id, repo_id, ref_name, old_sha, new_sha, pusher_did, node_did, signature, issued_at
FROM ref_certificates WHERE repo_id = $1 AND id LIKE $2 ORDER BY issued_at DESC LIMIT $3",
)
.bind(repo_id)
.bind(&pattern)
.bind(limit)
.fetch_all(&self.pool)
.await?;
Ok(rows.into_iter().map(row_to_cert).collect())
}

pub async fn get_ref_certificate(&self, id: &str) -> Result<Option<RefCertificate>> {
let row = sqlx::query(
"SELECT id, repo_id, ref_name, old_sha, new_sha, pusher_did, node_did, signature, issued_at
Expand Down
77 changes: 77 additions & 0 deletions crates/gitlawb-node/src/test_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3816,4 +3816,81 @@ mod tests {
"certificates array length matches count"
);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

#[sqlx::test]
async fn list_certs_prefix_resolves_deep_cert(pool: PgPool) {
let owner = "did:key:zPREFIXDEEPTESTAAAAAAAAAAAAAAAAAAAAAAA";
let state = test_state(pool).await;
state
.db
.create_repo(&seed_repo(owner, "deep-repo"))
.await
.expect("seed repo");
let repo = state
.db
.get_repo(owner, "deep-repo")
.await
.unwrap()
.expect("repo must exist");

// Insert 55 certs with distinct refs — only the newest 50 fit in a
// default list_certs response, so a short-ID for cert #0 requires the
// prefix query to reach it.
for i in 0..55u64 {
state
.db
.insert_ref_certificate(&seed_cert(
&format!("deep-{i:04}"),
&repo.id,
&format!("refs/heads/feature-{i}"),
&format!("2026-07-03T20:{i:02}:00Z"),
))
.await
.unwrap();
}

let router = || {
Router::new()
.route(
"/api/v1/repos/{owner}/{repo}/certs",
axum::routing::get(crate::api::certs::list_certs),
)
.with_state(state.clone())
};

// Default list (no prefix) returns only the 50 newest — cert-0000 is absent.
let body = json_body(
router()
.oneshot(anon_get(&format!("/api/v1/repos/{owner}/deep-repo/certs")))
.await
.unwrap(),
)
.await;
assert_eq!(body["count"].as_u64().unwrap(), 50, "default limit 50");

// Prefix lookup finds the deep cert by short prefix.
let body = json_body(
router()
.oneshot(anon_get(&format!(
"/api/v1/repos/{owner}/deep-repo/certs?prefix=deep-0"
)))
.await
.unwrap(),
)
.await;
assert!(
body["count"].as_u64().unwrap_or(0) >= 1,
"prefix query returns at least one result"
);
let ids: Vec<&str> = body["certificates"]
.as_array()
.unwrap()
.iter()
.filter_map(|c| c["id"].as_str())
.collect();
assert!(
ids.iter().any(|id| id.starts_with("deep-0")),
"result includes the deep cert matching the prefix"
);
}
}
3 changes: 1 addition & 2 deletions crates/gl/src/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ async fn resolve_cert_id(client: &NodeClient, owner: &str, name: &str, id: &str)
return Ok(id.to_string());
}

let path = format!("/api/v1/repos/{owner}/{name}/certs");
let path = format!("/api/v1/repos/{owner}/{name}/certs?prefix={id}");
let resp: Value = client
.get_authed(&path)
.await?
Expand All @@ -200,7 +200,6 @@ async fn resolve_cert_id(client: &NodeClient, owner: &str, name: &str, id: &str)
let matches: Vec<String> = certs
.iter()
.filter_map(|cert| cert["id"].as_str())
.filter(|cert_id| cert_id.starts_with(id))
.map(ToString::to_string)
.collect();

Expand Down
Loading