Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
crates_io_client: Add GitHub Trusted Publishing configuration endpoints (#66473)
Finn Evers8a41a5cc04b3
diff --git a/Cargo.lock b/Cargo.lockindex 6695899a1f7..0404e33240b 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -4105,6 +4105,7 @@ dependencies = ["anyhow","async-trait","chrono",+ "derive_more","futures 0.3.34","http_client","itertools 0.14.0",diff --git a/tooling/crates_io_client/Cargo.toml b/tooling/crates_io_client/Cargo.tomlindex ea72a581839..4c3eefd60d6 100644--- a/tooling/crates_io_client/Cargo.toml+++ b/tooling/crates_io_client/Cargo.toml@@ -20,6 +20,7 @@ doctest = falseanyhow.workspace = trueasync-trait.workspace = truechrono.workspace = true+derive_more = { workspace = true, features = ["display"] }futures.workspace = truehttp_client.workspace = trueitertools.workspace = truediff --git a/tooling/crates_io_client/src/crates_io_client.rs b/tooling/crates_io_client/src/crates_io_client.rsindex 1b6e627dc44..27a2d759cde 100644--- a/tooling/crates_io_client/src/crates_io_client.rs+++ b/tooling/crates_io_client/src/crates_io_client.rs@@ -29,6 +29,19 @@ pub trait CratesIoClient: Send + Sync {/// Requires a token with the `trusted-publishing` endpoint scope that/// belongs to an owner of the crate.async fn update_crate(&self, name: &CrateName, update: &CrateUpdate) -> Result<Crate>;++ /// Requires a token with the `trusted-publishing` endpoint scope that+ /// belongs to an owner of the crate.+ async fn list_github_configs(&self, crate_name: &CrateName) -> Result<Vec<GitHubConfig>>;++ /// Requires a token with the `trusted-publishing` endpoint scope that+ /// belongs to an owner of the crate with a verified email address.+ /// crates.io allows at most five configurations per crate.+ async fn create_github_config(&self, config: &NewGitHubConfig) -> Result<GitHubConfig>;++ /// Requires a token with the `trusted-publishing` endpoint scope that+ /// belongs to an owner of the configuration's crate.+ async fn delete_github_config(&self, id: GitHubConfigId) -> Result<()>;}#[derive(Debug, thiserror::Error)]diff --git a/tooling/crates_io_client/src/real_crates_io_client.rs b/tooling/crates_io_client/src/real_crates_io_client.rsindex f282a3f9c93..3449c9320d8 100644--- a/tooling/crates_io_client/src/real_crates_io_client.rs+++ b/tooling/crates_io_client/src/real_crates_io_client.rs@@ -17,7 +17,8 @@ const BASE_URL: &str = "https://crates.io/api/v1";// User agent and request rate requirements: https://crates.io/data-accessconst USER_AGENT: &str = "Zed GPUI releases (https://github.com/zed-industries/zed)";pub(crate) const MIN_REQUEST_INTERVAL: Duration = Duration::from_secs(1);-const MAX_PER_PAGE: u32 = 100;+const MAX_PER_PAGE: usize = 100;+const GITHUB_CONFIGS_PATH: &str = "/trusted_publishing/github_configs";/// Reserving the send time before waiting makes concurrent requests queue up/// behind each other instead of all waking at the same instant.@@ -171,11 +172,11 @@ impl CratesIoClient for RealCratesIoClient {let mut crates = Vec::new();loop {let mut page: CrateListResponse = self.request_json(Method::GET, url, None).await?;- let is_last_page = page.crates.is_empty();+ let is_full_page = page.crates.len() == MAX_PER_PAGE;crates.append(&mut page.crates);match page.meta.next_page {- Some(next_page) if !is_last_page => {+ Some(next_page) if is_full_page => {url = Self::url(&format!("/crates{next_page}"))?;}_ => return Ok(crates),@@ -221,4 +222,51 @@ impl CratesIoClient for RealCratesIoClient {.await?;Ok(response.krate)}++ async fn list_github_configs(&self, crate_name: &CrateName) -> Result<Vec<GitHubConfig>> {+ #[derive(Deserialize)]+ struct GitHubConfigListResponse {+ github_configs: Vec<GitHubConfig>,+ }++ let mut url = Self::url(GITHUB_CONFIGS_PATH)?;+ url.query_pairs_mut()+ .append_pair("per_page", &MAX_PER_PAGE.to_string())+ .append_pair("crate", crate_name.as_str());+ let response: GitHubConfigListResponse = self.request_json(Method::GET, url, None).await?;+ if response.github_configs.len() >= MAX_PER_PAGE {+ return Err(anyhow::anyhow!(+ "crates.io returned more GitHub Trusted Publishing configurations for `{crate_name}` than expected"+ )+ .into());+ }+ Ok(response.github_configs)+ }++ async fn create_github_config(&self, config: &NewGitHubConfig) -> Result<GitHubConfig> {+ #[derive(Serialize)]+ struct CreateRequest<'a> {+ github_config: &'a NewGitHubConfig,+ }++ #[derive(Deserialize)]+ struct CreateResponse {+ github_config: GitHubConfig,+ }++ let body = serde_json::to_vec(&CreateRequest {+ github_config: config,+ })+ .context("Failed to serialize GitHub Trusted Publishing configuration")?;+ let response: CreateResponse = self+ .request_json(Method::POST, Self::url(GITHUB_CONFIGS_PATH)?, Some(body))+ .await?;+ Ok(response.github_config)+ }++ async fn delete_github_config(&self, id: GitHubConfigId) -> Result<()> {+ let url = Self::url(&format!("{GITHUB_CONFIGS_PATH}/{id}"))?;+ self.request_response(Method::DELETE, url, None).await?;+ Ok(())+ }}diff --git a/tooling/crates_io_client/src/simulated_crates_io_client.rs b/tooling/crates_io_client/src/simulated_crates_io_client.rsindex 0f0afd3ab6e..ed13bc566dc 100644--- a/tooling/crates_io_client/src/simulated_crates_io_client.rs+++ b/tooling/crates_io_client/src/simulated_crates_io_client.rs@@ -16,9 +16,12 @@ struct CratesIoState {site_metadata: SiteMetadata,token_user: UserId,crates: BTreeMap<CrateName, SimulatedCrate>,+ github_configs: BTreeMap<GitHubConfigId, GitHubConfig>,latest_id: i32,}+const MAX_GITHUB_CONFIGS_PER_CRATE: usize = 5;+struct SimulatedCrate {krate: Crate,owners: Vec<UserId>,@@ -38,6 +41,20 @@ impl CratesIoState {message: format!("crate `{name}` does not exist"),})}++ /// crates.io answers non-owners of a crate with a bad request rather than+ /// a permission error on the Trusted Publishing endpoints.+ fn require_token_user_owns(&mut self, name: &CrateName) -> Result<()> {+ let token_user = self.token_user;+ if self.simulated_crate(name)?.owners.contains(&token_user) {+ Ok(())+ } else {+ Err(CratesIoError::Upstream {+ status: StatusCode::BAD_REQUEST,+ message: "You are not an owner of this crate".to_owned(),+ })+ }+ }}/// Keyword and category IDs are only part of single-crate responses.@@ -67,6 +84,7 @@ impl SimulatedCratesIoClient {},token_user: UserId(1),crates: BTreeMap::new(),+ github_configs: BTreeMap::new(),latest_id: 1,}),})@@ -231,4 +249,62 @@ impl CratesIoClient for SimulatedCratesIoClient {}Ok(listed(&simulated_crate.krate))}++ async fn list_github_configs(&self, crate_name: &CrateName) -> Result<Vec<GitHubConfig>> {+ let mut state = self.state.lock();+ state.require_token_user_owns(crate_name)?;+ Ok(state+ .github_configs+ .values()+ .filter(|config| &config.crate_name == crate_name)+ .cloned()+ .collect())+ }++ async fn create_github_config(&self, config: &NewGitHubConfig) -> Result<GitHubConfig> {+ let mut state = self.state.lock();+ state.require_token_user_owns(&config.crate_name)?;+ let existing_configs = state+ .github_configs+ .values()+ .filter(|existing| existing.crate_name == config.crate_name)+ .count();+ if existing_configs >= MAX_GITHUB_CONFIGS_PER_CRATE {+ return Err(CratesIoError::Upstream {+ status: StatusCode::CONFLICT,+ message: format!(+ "This crate already has the maximum number of GitHub Trusted Publishing configurations ({MAX_GITHUB_CONFIGS_PER_CRATE})"+ ),+ });+ }++ let id = GitHubConfigId(state.next_id());+ let repository_owner_id = GitHubAccountId(state.next_id());+ let github_config = GitHubConfig {+ id,+ crate_name: config.crate_name.clone(),+ repository_owner: config.repository_owner.clone(),+ repository_owner_id,+ repository_name: config.repository_name.clone(),+ workflow_filename: config.workflow_filename.clone(),+ environment: config.environment.clone(),+ created_at: test_timestamp(),+ };+ state.github_configs.insert(id, github_config.clone());+ Ok(github_config)+ }++ async fn delete_github_config(&self, id: GitHubConfigId) -> Result<()> {+ let mut state = self.state.lock();+ let crate_name = state+ .github_configs+ .get(&id)+ .map(|config| config.crate_name.clone())+ .ok_or_else(|| CratesIoError::NotFound {+ message: "Not Found".to_owned(),+ })?;+ state.require_token_user_owns(&crate_name)?;+ state.github_configs.remove(&id);+ Ok(())+ }}diff --git a/tooling/crates_io_client/src/tests.rs b/tooling/crates_io_client/src/tests.rsindex 2ce93d0dcc0..7eecc2ebb24 100644--- a/tooling/crates_io_client/src/tests.rs+++ b/tooling/crates_io_client/src/tests.rs@@ -131,23 +131,24 @@ fn test_error_responses_distinguish_permission_and_upstream_failures() {}#[test]-fn test_list_crates_follows_next_page_until_exhausted() {+fn test_list_crates_requests_next_page_only_after_full_page() {let (fake_http_client, requests) = recording_fake_http_client(|uri| {- let page = if uri.contains("seek=") {- json!({- "crates": [crate_json("gpui_util")],- "meta": { "total": 2, "next_page": null, "prev_page": null },- })+ let crates = if uri.contains("seek=") {+ vec![crate_json("last")]} else {- json!({- "crates": [crate_json("gpui")],- "meta": {- "total": 2,- "next_page": "?user_id=7&per_page=100&seek=abc",- "prev_page": null,- },- })+ (0..100)+ .map(|index| crate_json(&format!("crate_{index}")))+ .collect()};+ // crates.io can link a next page even when no more crates follow.+ let page = json!({+ "crates": crates,+ "meta": {+ "total": 101,+ "next_page": "?user_id=7&per_page=100&seek=abc",+ "prev_page": null,+ },+ });(StatusCode::OK, page)});let client = RealCratesIoClient::new(fake_http_client, ApiToken::new("secret"));@@ -159,12 +160,10 @@ fn test_list_crates_follows_next_page_until_exhausted() {})).unwrap();- let names = crates- .iter()- .map(|krate| krate.name.as_str())- .collect::<Vec<_>>();- assert_eq!(names, ["gpui", "gpui_util"]);+ assert_eq!(crates.len(), 101);+ assert_eq!(crates[100].name.as_str(), "last");let requests = requests.lock();+ assert_eq!(requests.len(), 2);assert_eq!(requests[0].uri,"https://crates.io/api/v1/crates?per_page=100&user_id=7&include_yanked=no"@@ -198,6 +197,89 @@ fn test_update_crate_sends_settings_wrapped_in_crate_object() {);}+fn github_config_json(id: i32) -> Value {+ json!({+ "id": id,+ "crate": "gpui",+ "repository_owner": "zed-industries",+ "repository_owner_id": 79345384,+ "repository_name": "zed",+ "workflow_filename": "release.yml",+ "environment": null,+ "created_at": "2026-01-01T00:00:00Z",+ })+}++#[test]+fn test_github_config_requests_match_crates_io_api() {+ let (fake_http_client, requests) = recording_fake_http_client(|uri| {+ let body = if uri.contains("crate=") {+ json!({+ "github_configs": [github_config_json(3)],+ "meta": { "total": 1, "next_page": null },+ })+ } else {+ json!({ "github_config": github_config_json(3) })+ };+ (StatusCode::OK, body)+ });+ // Separate clients keep the test from waiting on the rate limit.+ let client = || RealCratesIoClient::new(fake_http_client.clone(), ApiToken::new("secret"));+ let gpui = CrateName::new("gpui");++ let configs = smol::block_on(client().list_github_configs(&gpui)).unwrap();+ smol::block_on(client().create_github_config(&NewGitHubConfig {+ crate_name: gpui,+ repository_owner: "zed-industries".to_owned(),+ repository_name: "zed".to_owned(),+ workflow_filename: "release.yml".to_owned(),+ environment: None,+ }))+ .unwrap();+ smol::block_on(client().delete_github_config(configs[0].id)).unwrap();++ let requests = requests.lock();+ assert_eq!(+ requests[0].uri,+ "https://crates.io/api/v1/trusted_publishing/github_configs?per_page=100&crate=gpui"+ );+ assert_eq!(requests[1].method, Method::POST);+ assert_eq!(+ serde_json::from_str::<Value>(&requests[1].body).unwrap(),+ json!({+ "github_config": {+ "crate": "gpui",+ "repository_owner": "zed-industries",+ "repository_name": "zed",+ "workflow_filename": "release.yml",+ "environment": null,+ }+ })+ );+ assert_eq!(requests[2].method, Method::DELETE);+ assert_eq!(+ requests[2].uri,+ "https://crates.io/api/v1/trusted_publishing/github_configs/3"+ );+}++#[test]+fn test_list_github_configs_rejects_full_page() {+ let (fake_http_client, _requests) = recording_fake_http_client(|_| {+ let configs = (0..100).map(github_config_json).collect::<Vec<_>>();+ let page = json!({+ "github_configs": configs,+ "meta": { "total": 101, "next_page": "?crate=gpui&per_page=100&seek=abc" },+ });+ (StatusCode::OK, page)+ });+ let client = RealCratesIoClient::new(fake_http_client, ApiToken::new("secret"));++ let error = smol::block_on(client.list_github_configs(&CrateName::new("gpui"))).unwrap_err();++ assert!(matches!(error, CratesIoError::Request(_)));+}+#[test]fn test_throttle_spaces_requests_by_minimum_interval() {let throttle = RequestThrottle::new();diff --git a/tooling/crates_io_client/src/types.rs b/tooling/crates_io_client/src/types.rsindex 5272c94660f..a03f7048acc 100644--- a/tooling/crates_io_client/src/types.rs+++ b/tooling/crates_io_client/src/types.rs@@ -1,6 +1,7 @@use std::fmt;use chrono::{DateTime, Utc};+use derive_more::Display;use serde::{Deserialize, Serialize};use url::Url;@@ -8,6 +9,12 @@ use url::Url;pub struct UserId(pub i32);#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]pub struct VersionId(pub i32);+#[derive(+ Debug, Display, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,+)]+pub struct GitHubConfigId(pub i32);+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]+pub struct GitHubAccountId(pub i32);#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]pub struct CrateName(pub String);@@ -119,3 +126,27 @@ pub struct CrateUpdate {#[serde(skip_serializing_if = "Option::is_none")]pub trustpub_only: Option<bool>,}++#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct GitHubConfig {+ pub id: GitHubConfigId,+ #[serde(rename = "crate")]+ pub crate_name: CrateName,+ pub repository_owner: String,+ /// Lets crates.io recognize the repository after its owner is renamed.+ pub repository_owner_id: GitHubAccountId,+ pub repository_name: String,+ pub workflow_filename: String,+ pub environment: Option<String>,+ pub created_at: DateTime<Utc>,+}++#[derive(Debug, Clone, PartialEq, Eq, Serialize)]+pub struct NewGitHubConfig {+ #[serde(rename = "crate")]+ pub crate_name: CrateName,+ pub repository_owner: String,+ pub repository_name: String,+ pub workflow_filename: String,+ pub environment: Option<String>,+}