From af24eb9d52749bca5de150e34c3950159250a7f5 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 21 Nov 2026 13:40:38 +0100 Subject: [PATCH] Add GitHub and multi-endpoint releases --- README.md | 21 +- docs/advanced-usage.md | 66 ++++++- src/build.rs | 1 + src/bundle.rs | 1 + src/cli.rs | 288 +++++++++++++++++++++------ src/config.rs | 142 ++++++++++++++ src/gitea.rs | 6 +- src/github.rs | 427 +++++++++++++++++++++++++++++++++++++++++ src/main.rs | 2 + src/prepare.rs | 2 + src/release.rs | 83 ++++++++ src/runner.rs | 33 +++- src/verify.rs | 29 ++- 13 files changed, 1019 insertions(+), 82 deletions(-) create mode 100644 src/github.rs create mode 100644 src/release.rs diff --git a/README.md b/README.md index ec1adb9..26100e9 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,8 @@ ![line coverage](https://assets.dandaedre.com/coverage/idle-org/rust_realease/main/coverage.svg) `rust_realease` builds Rust binary packages for multiple targets and publishes -them as Gitea release assets. It is intended for maintainers who want +them as Gitea or GitHub release assets, including mirrored releases sent to +multiple repositories. It is intended for maintainers who want repeatable Cross builds, checksums, cargo-binstall archives, and post-upload verification without maintaining a separate release pipeline. @@ -33,7 +34,7 @@ cargo install --path . --locked ## Quick start Preview a local project without changing files, building binaries, or -contacting Gitea: +contacting a release service: ```bash rust_realease ./my-tool --dry-run @@ -54,29 +55,35 @@ Build local artifacts through the package stage with: rust_realease ./my-tool --compile-only ``` -This mode does not require a Gitea token or contact Gitea. Artifacts and +This mode does not require a release token or contact Gitea or GitHub. Artifacts and `SHA256SUMS` are written under `dist/release-*/`. A later publication with the same clean commit and build settings reuses the completed target builds; pass `--recompile` to rebuild them instead. -## Publish a Gitea release +## Publish a release The package version must match a local tag, and the same tag must already exist on the remote. Publication also requires a clean checkout and an HTTPS -Gitea release destination. +Gitea or GitHub release destination. -Store a Gitea access token in a single-line UTF-8 file and pass it directly: +For one destination, store an access token in a single-line UTF-8 file: ```bash rust_realease ./my-tool --token-file ../gitea_token.txt ``` The token file takes precedence over the configured token environment -variable. Without `--token-file`, the default is `GITEA_TOKEN`. Grant the token +variable. Without `--token-file`, the defaults are `GITEA_TOKEN` and +`GITHUB_TOKEN`. Grant the token repository read and release write access; preparation pushes also require code write access. Tokens are redacted from diagnostics and are not passed to Cargo or Cross child processes. +Configure named `[[release.endpoints]]` entries to publish the same release to +several repositories. Each can use its own `token-env`, or `--token-file` can +point to a JSON object whose keys match the endpoint names. See the advanced +guide for the complete configuration and token-file format. + Use `--prepare-repo --dry-run` to preview supported metadata and repository preparation before publishing. See the advanced guide for explicit commit, tag, push, release-note, private-repository, and asset-replacement workflows. diff --git a/docs/advanced-usage.md b/docs/advanced-usage.md index efb05f2..3360f8f 100644 --- a/docs/advanced-usage.md +++ b/docs/advanced-usage.md @@ -42,6 +42,18 @@ Missing automatically discovered files are ignored. A path explicitly passed wit [release] tag-prefix = "v" +[[release.endpoints]] +name = "github" +provider = "github" +repository = "https://github.com/example/my-tool" +token-env = "MY_TOOL_GITHUB_TOKEN" + +[[release.endpoints]] +name = "gitea-mirror" +provider = "gitea" +repository = "https://git.example.com/example/my-tool" +token-env = "MY_TOOL_GITEA_TOKEN" + [build] targets = [ "x86_64-unknown-linux-musl", @@ -61,6 +73,21 @@ fallbacks = [] token-env = "GITEA_TOKEN" ``` +`release.endpoints` is optional. When omitted, the release repository is taken +from `--repository-url`, Cargo metadata, or Git origin as before. A +`github.com` repository automatically selects GitHub and uses `GITHUB_TOKEN`; +other hosts select Gitea and use `[gitea].token-env`. When endpoints are +configured, the first is the primary repository used for Cargo release +metadata and cargo-binstall verification. A later configuration layer replaces +the complete endpoint list, while repository fallback lists continue to merge. +Endpoint names must be unique and are also the stable keys used by JSON token +files. GitHub endpoints currently target github.com; self-hosted GitHub +Enterprise API layouts are not inferred. + +`--repository-url` remains the single-destination override and cannot be mixed +with configured endpoints. Put that repository in `release.endpoints` when it +must participate in a multi-destination release. + Use `--tag-prefix ""` for an unprefixed tag such as `0.3.1`. `--tag TAG` must match the selected package version and configured prefix. `--unlocked` permits builds without the usual locked dependency resolution; the default uses `--locked`. `package.format` accepts `"tgz"`, `"binary"`, or `"both"`. The default `"tgz"` produces cargo-binstall archives. `"binary"` produces only directly downloadable executables and does not require or verify cargo-binstall metadata. `"both"` produces both forms. The `--artifact-format` option overrides this setting for an individual run. @@ -167,7 +194,7 @@ For repositories whose build scripts need a selected Rust toolchain inside Cross `--compile-only` runs through the package stage and then stops: it performs host checks, cross-compiles, and writes local artifacts without requiring a Gitea token or creating or uploading a release. It is the explicit artifact-build mode and conflicts with `--dry-run`, `--prepare-repo`, and `--stop-after`. A later publication from the same clean commit reuses the completed build when its fingerprint still matches. -`--stop-after` can stop at `inspect`, `metadata`, `verify`, `build`, `package`, `release`, `upload`, or `binstall`. `verify` runs host `cargo check` and `cargo test`; `--skip-tests` skips only tests. A successful Cross build from a clean checkout records its Git commit, package, targets, profile, lock mode, container engine, active Rust toolchain, and binary list. A later run with the same inputs reuses those regular binary files from Cargo's target directory; dirty checkouts, changed inputs, missing files, and invalid cache records build in a fresh target directory instead. `--recompile` explicitly bypasses a matching completed build. `release` creates or reuses the Gitea release; `upload` uploads archives and checksums. The final stage downloads assets anonymously for public repositories or with Gitea authentication for private repositories, checks their checksums and archive layout, and runs isolated `cargo-binstall` installs for public targets. `--skip-binstall-test` skips that install check, while retaining download verification. +`--stop-after` can stop at `inspect`, `metadata`, `verify`, `build`, `package`, `release`, `upload`, or `binstall`. `verify` runs host `cargo check` and `cargo test`; `--skip-tests` skips only tests. A successful Cross build from a clean checkout records its Git commit, package, targets, profile, lock mode, container engine, active Rust toolchain, and binary list. A later run with the same inputs reuses those regular binary files from Cargo's target directory; dirty checkouts, changed inputs, missing files, and invalid cache records build in a fresh target directory instead. `--recompile` explicitly bypasses a matching completed build. `release` creates or reuses every configured release; `upload` uploads archives and checksums to each one. The final stage downloads assets anonymously for public repositories or with authenticated Gitea/GitHub API access for private repositories, checks their checksums and archive layout, and runs isolated `cargo-binstall` installs for a public primary endpoint. `--skip-binstall-test` skips that install check, while retaining download verification. `--draft --stop-after upload` is useful when a release must stay private for review. Private repositories require `--skip-binstall-test`; cargo-binstall cannot fetch their attachment URLs, even when Gitea authentication is configured. `--prerelease` and `--draft` set properties when a release is created; reusing a release preserves its existing settings. `-m/--message TEXT` sets release notes, while `--message-file PATH` reads them from a UTF-8 file; they are mutually exclusive. Notes are included when creating a release and replace the body of an existing tagged release when explicitly supplied. Releases with macOS artifacts automatically append an artifact-specific quarantine-removal section unless the notes already contain a `## macOS Workaround` heading. Asset replacement is not transactional: if an upload fails after a matching asset is deleted, retry the publication explicitly. @@ -186,7 +213,42 @@ Windows archive members have an `.exe` suffix. Raw assets are target-qualified t ## Authentication and security -Authenticated Gitea API publication requires HTTPS. Pass `--token-file PATH` to read one token from a UTF-8 file; trailing line endings are ignored, while empty and multiline files are rejected. The file takes precedence over the configured environment variable. Without that option, the token comes from `gitea.token-env`, which defaults to `GITEA_TOKEN`. Tokens are never accepted as a positional argument or URL. Child Cargo and Cross processes do not receive them. An HTTPS preparation push to the matching release repository can use the token through a temporary askpass helper; SSH pushes use normal Git credentials. Grant the token account code read and release write access, plus code write when using `--push-preparation`. +Authenticated Gitea and GitHub API publication requires HTTPS. Pass +`--token-file PATH` to read one token from a UTF-8 file; trailing line endings +are ignored, while empty and multiline plaintext files are rejected. A +plaintext file is valid only when publishing to one endpoint. For several +endpoints, use a JSON object keyed by the configured endpoint names: + +```json +{ + "github": "github-token", + "gitea-mirror": "gitea-token" +} +``` + +Every configured endpoint must have a token and unknown JSON keys are rejected, +which catches misspelled endpoint names. Alternatively, omit `--token-file` and +set each endpoint's `token-env`; this is the supported way to use several +environment tokens. The legacy Gitea default is `GITEA_TOKEN`, while GitHub +defaults to `GITHUB_TOKEN`. Tokens are never accepted as a positional argument +or URL. Child Cargo and Cross processes do not receive them. An HTTPS +preparation push uses the primary endpoint token supplied by the token file +through a temporary askpass helper; SSH pushes use normal Git credentials. +Grant each token repository read and release write access, plus code write when +using `--push-preparation`. + +Before building, every endpoint is authenticated and its repository visibility +is checked. Release creation, asset upload, and downloaded checksum/archive +verification then run for every endpoint in configuration order. A failure can +therefore leave earlier endpoints updated; rerunning is safe when assets do not +collide, or use `--replace-assets` when replacement is intended. Drafts must be +published on every endpoint before final public verification. + +Private Gitea repositories retain authenticated download verification. GitHub +assets are verified through their asset-ID API URLs with redirect handling, as +required for authenticated private downloads. Private primary repositories +still require `--skip-binstall-test`, because cargo-binstall does not receive +these release credentials. Normal publication does not create a commit, tag, or push. Existing asset names cause an error unless `--replace-assets` is set; unrelated assets are not deleted. Private assets are verified with authenticated downloads and do not establish public installability. diff --git a/src/build.rs b/src/build.rs index dfaf452..80ee879 100644 --- a/src/build.rs +++ b/src/build.rs @@ -684,6 +684,7 @@ mod tests { locked: true, tag_prefix: "v".into(), token_env: "GITEA_TOKEN".into(), + release_endpoints: Vec::new(), package_format: crate::config::PackageFormat::Tgz, repository_fallbacks: Vec::new(), } diff --git a/src/bundle.rs b/src/bundle.rs index 960efb2..bce962d 100644 --- a/src/bundle.rs +++ b/src/bundle.rs @@ -1083,6 +1083,7 @@ required-destinations=['runtime{binary-ext}','LICENSES/runtime/LICENSE.txt','LIC locked: true, tag_prefix: "v".into(), token_env: "RELEASE_TEST_TOKEN".into(), + release_endpoints: Vec::new(), package_format: crate::config::PackageFormat::Tgz, repository_fallbacks: Vec::new(), }; diff --git a/src/cli.rs b/src/cli.rs index 97142f8..46f174c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,19 +1,22 @@ use crate::{ build, - config::{self, Overrides, PackageFormat}, + config::{self, Overrides, PackageFormat, ReleaseEndpoint, ReleaseProvider}, error::Error, - gitea::Gitea, manifest, model::Stage, policy, prepare, project, + release::ReleaseClient, runner::Runner, ui, verify, }; use clap::Parser; -use std::path::PathBuf; +use std::{collections::BTreeMap, path::PathBuf}; #[derive(Debug, Parser)] -#[command(about = "Build and publish Rust binary releases to Gitea", version)] +#[command( + about = "Build and publish Rust binary releases to Gitea and GitHub", + version +)] pub struct Cli { /// Local directory or Git URL. pub source: String, @@ -82,7 +85,7 @@ pub struct Cli { pub cross_image_tag: Option, #[arg(long)] pub repository_url: Option, - /// Read the Gitea access token from a UTF-8 file instead of the configured environment variable. + /// Read one plaintext token or a JSON object of endpoint tokens from a UTF-8 file. #[arg(long, value_name = "PATH")] pub token_file: Option, #[arg(long)] @@ -118,13 +121,13 @@ pub fn run(mut args: Cli) -> Result<(), Error> { })?), None => args.message.clone(), }; - let file_token = args + let file_tokens = args .token_file .as_deref() .map(read_token_file) .transpose()?; let acquisition_runner = Runner::new("GITEA_TOKEN") - .with_secret(file_token.as_deref()) + .with_secret(file_tokens.as_ref().and_then(TokenFile::plain)) .with_verbose(args.verbose); let checkout = project::acquire_project( &args.source, @@ -149,7 +152,13 @@ pub fn run(mut args: Cli) -> Result<(), Error> { }, )?; let runner = Runner::new(&config.token_env) - .with_secret(file_token.as_deref()) + .with_token_envs( + config + .release_endpoints + .iter() + .map(|endpoint| endpoint.token_env.clone()), + ) + .with_secret(file_tokens.as_ref().and_then(TokenFile::plain)) .with_verbose(args.verbose); let root = checkout .root @@ -206,9 +215,44 @@ pub fn run(mut args: Cli) -> Result<(), Error> { ) })?; let normalized_candidate = project::normalize_repository(&candidate)?; - let repository = config.resolve_repository(&candidate)?; + let mut endpoints = config.release_endpoints.clone(); + if args.repository_url.is_some() && !endpoints.is_empty() { + return Err(Error( + "--repository-url cannot be combined with configured release.endpoints.".into(), + )); + } + let repository = if let Some(primary) = endpoints.first() { + primary.repository.clone() + } else { + config.resolve_repository(&candidate)? + }; + if endpoints.is_empty() { + let provider = if url::Url::parse(&repository) + .ok() + .and_then(|url| url.host_str().map(str::to_owned)) + .as_deref() + == Some("github.com") + { + ReleaseProvider::Github + } else { + ReleaseProvider::Gitea + }; + endpoints.push(ReleaseEndpoint { + name: "default".into(), + provider, + repository: repository.clone(), + token_env: match provider { + ReleaseProvider::Gitea => config.token_env.clone(), + ReleaseProvider::Github => "GITHUB_TOKEN".into(), + }, + }); + } if repository != normalized_candidate { - println!("Repository fallback: {normalized_candidate} -> {repository}"); + if config.release_endpoints.is_empty() { + println!("Repository fallback: {normalized_candidate} -> {repository}"); + } else { + println!("Primary release repository: {repository}"); + } } if publishing { if let Some(origin) = project::origin_url(&checkout.root, &runner) { @@ -242,6 +286,11 @@ pub fn run(mut args: Cli) -> Result<(), Error> { println!("{}: {actual} -> {:?}", issue.key, issue.expected); } if args.prepare_repo { + let preparation_token = match file_tokens.as_ref() { + Some(TokenFile::Plain(token)) => Some(token.as_str()), + Some(TokenFile::Named(tokens)) => tokens.get(&endpoints[0].name).map(String::as_str), + None => None, + }; return prepare::run( &checkout.root, &selected, @@ -250,7 +299,7 @@ pub fn run(mut args: Cli) -> Result<(), Error> { prepare::Options { tag: &tag, image_tag: args.cross_image_tag.as_deref(), - token: file_token.as_deref(), + token: preparation_token, dry_run: args.dry_run, commit: args.commit_preparation, create_tag: args.create_tag, @@ -306,10 +355,15 @@ pub fn run(mut args: Cli) -> Result<(), Error> { println!("Binstall {key}: {value}"); } } - ui::detail( - "Release destination", - format_args!("{repository}/releases/tag/{tag}"), - ); + for endpoint in &endpoints { + ui::detail( + "Release destination", + format_args!( + "{} ({}) {}/releases/tag/{tag}", + endpoint.name, endpoint.provider, endpoint.repository + ), + ); + } let needs_cross = args.stop_after != Some(Stage::Verify); let needs_binstall = config.package_format.archives() && !args.skip_binstall_test @@ -318,15 +372,18 @@ pub fn run(mut args: Cli) -> Result<(), Error> { .is_none_or(|stage| stage.order() >= Stage::Binstall.order()); let publication = if publishing { project::validate_checkout_tag(&checkout.root, &tag, &runner)?; - let api = match file_token.as_deref() { - Some(token) => Gitea::with_token(&repository, token)?, - None => Gitea::new(&repository, &config.token_env)?, - }; - let private = api.repository_private()?; - if private && needs_binstall { - return Err(Error("This repository is private; cargo-binstall cannot verify its private release downloads. Rerun with --skip-binstall-test to verify uploaded assets with Gitea authentication.".into())); + validate_token_keys(file_tokens.as_ref(), &endpoints)?; + let mut clients = Vec::new(); + for endpoint in &endpoints { + let token = token_for_endpoint(file_tokens.as_ref(), endpoint, endpoints.len())?; + let api = ReleaseClient::new(endpoint, &token)?; + let private = api.repository_private()?; + clients.push((endpoint.clone(), api, private)); } - Some((api, private)) + if clients.first().is_some_and(|(_, _, private)| *private) && needs_binstall { + return Err(Error("The primary release repository is private; cargo-binstall cannot verify its private release downloads. Rerun with --skip-binstall-test to verify uploaded assets with authentication.".into())); + } + Some(clients) } else { None }; @@ -442,42 +499,62 @@ pub fn run(mut args: Cli) -> Result<(), Error> { if args.stop_after == Some(Stage::Package) { return Ok(()); } - let (api, private) = + let clients = publication.ok_or_else(|| Error("Publication client was not initialized.".into()))?; let release_message = release_message_with_macos_workaround(supplied_release_message.as_deref(), &artifacts)?; - let release = api.ensure_release( - &tag, - &commit, - args.draft, - args.prerelease, - release_message.as_deref(), - supplied_release_message.is_some(), - )?; - ui::success(format_args!("Release ready: {tag}")); + let mut releases = Vec::new(); + for (endpoint, api, private) in clients { + let release = api.ensure_release( + &tag, + &commit, + args.draft, + args.prerelease, + release_message.as_deref(), + supplied_release_message.is_some(), + )?; + ui::success(format_args!("Release ready on {}: {tag}", endpoint.name)); + releases.push((endpoint, api, private, release)); + } if args.stop_after == Some(Stage::Release) { return Ok(()); } let mut paths: Vec<&std::path::Path> = artifacts.iter().map(|a| a.path.as_path()).collect(); paths.push(sums.as_path()); - api.upload(release.id, &paths, args.replace_assets)?; + for (endpoint, api, _, release) in &releases { + api.upload(release.id, &paths, args.replace_assets)?; + ui::success(format_args!("Assets uploaded to {}", endpoint.name)); + } if args.stop_after == Some(Stage::Upload) { return Ok(()); } - if release.draft { - return Err(Error("Draft uploaded. Public verification requires publishing the draft; use --stop-after upload for draft workflows.".into())); + if releases.iter().any(|(_, _, _, release)| release.draft) { + return Err(Error("A draft was uploaded. Public verification requires publishing every draft; use --stop-after upload for draft workflows.".into())); } - verify::verify_published( - &repository, - &tag, - &artifacts, - &sums, - if private { + for (endpoint, api, private, release) in &releases { + let asset_urls = api.asset_urls(release.id)?; + let authenticated = if *private { Some(api.download_client()) } else { None - }, - )?; + }; + if let Some(asset_urls) = asset_urls.as_ref() { + verify::verify_published_with_asset_urls( + &endpoint.repository, + &tag, + &artifacts, + &sums, + authenticated, + Some(asset_urls), + )?; + } else { + verify::verify_published(&endpoint.repository, &tag, &artifacts, &sums, authenticated)?; + } + ui::success(format_args!( + "Published assets verified on {}", + endpoint.name + )); + } if config.package_format.archives() && !args.skip_binstall_test { verify::verify_binstall(&checkout.root, &selected, &config.targets, &runner)?; } @@ -624,35 +701,107 @@ fn shell_quote(value: &str) -> String { format!("'{}'", value.replace('\'', "'\"'\"'")) } -fn read_token_file(path: &std::path::Path) -> Result { +#[derive(Debug)] +enum TokenFile { + Plain(String), + Named(BTreeMap), +} + +impl TokenFile { + fn plain(&self) -> Option<&str> { + match self { + Self::Plain(token) => Some(token), + Self::Named(_) => None, + } + } +} + +fn read_token_file(path: &std::path::Path) -> Result { if !path.is_file() { return Err(Error(format!( - "Gitea token file is not a regular file: {}", + "token file is not a regular file: {}", path.display() ))); } - let mut token = std::fs::read_to_string(path).map_err(|e| { - Error(format!( - "cannot read Gitea token file {}: {e}", - path.display() - )) - })?; + let source = std::fs::read_to_string(path) + .map_err(|e| Error(format!("cannot read token file {}: {e}", path.display())))?; + let trimmed = source.trim_end_matches(['\r', '\n']); + if trimmed.trim_start().starts_with('{') { + let tokens: BTreeMap = serde_json::from_str(trimmed) + .map_err(|e| Error(format!("invalid JSON token file {}: {e}", path.display())))?; + if tokens.is_empty() + || tokens + .values() + .any(|token| token.is_empty() || token.contains(['\r', '\n'])) + { + return Err(Error(format!( + "JSON token file must map endpoint names to nonempty single-line tokens: {}", + path.display() + ))); + } + return Ok(TokenFile::Named(tokens)); + } + let mut token = source; while token.ends_with(['\r', '\n']) { token.pop(); } if token.is_empty() { - return Err(Error(format!( - "Gitea token file is empty: {}", - path.display() - ))); + return Err(Error(format!("token file is empty: {}", path.display()))); } if token.contains(['\r', '\n']) { return Err(Error(format!( - "Gitea token file must contain exactly one token: {}", + "plaintext token file must contain exactly one token: {}", path.display() ))); } - Ok(token) + Ok(TokenFile::Plain(token)) +} + +fn token_for_endpoint( + file: Option<&TokenFile>, + endpoint: &ReleaseEndpoint, + endpoint_count: usize, +) -> Result { + match file { + Some(TokenFile::Plain(token)) if endpoint_count == 1 => Ok(token.clone()), + Some(TokenFile::Plain(_)) => Err(Error( + "A plaintext token file can only be used with one release endpoint; use a JSON object keyed by endpoint name." + .into(), + )), + Some(TokenFile::Named(tokens)) => tokens.get(&endpoint.name).cloned().ok_or_else(|| { + Error(format!( + "Token file has no token for release endpoint {:?}.", + endpoint.name + )) + }), + None => std::env::var(&endpoint.token_env) + .ok() + .filter(|token| !token.is_empty()) + .ok_or_else(|| { + Error(format!( + "Set {} before publishing endpoint {:?}.", + endpoint.token_env, endpoint.name + )) + }), + } +} + +fn validate_token_keys( + file: Option<&TokenFile>, + endpoints: &[ReleaseEndpoint], +) -> Result<(), Error> { + let Some(TokenFile::Named(tokens)) = file else { + return Ok(()); + }; + if let Some(unknown) = tokens + .keys() + .find(|name| !endpoints.iter().any(|endpoint| &endpoint.name == *name)) + { + return Err(Error(format!( + "Token file contains unknown release endpoint {unknown:?}." + ))); + } + Ok(()) } #[cfg(test)] @@ -855,7 +1004,10 @@ mod tests { let temp = tempfile::tempdir().unwrap(); let path = temp.path().join("token"); fs::write(&path, "secret-token\r\n").unwrap(); - assert_eq!(read_token_file(&path).unwrap(), "secret-token"); + assert_eq!( + read_token_file(&path).unwrap().plain(), + Some("secret-token") + ); fs::write(&path, "first\nsecond\n").unwrap(); assert!(read_token_file(&path).is_err()); @@ -863,6 +1015,24 @@ mod tests { assert!(read_token_file(&path).is_err()); } + #[test] + fn token_file_accepts_json_tokens_keyed_by_endpoint() { + let temp = tempfile::tempdir().unwrap(); + let path = temp.path().join("tokens.json"); + fs::write(&path, r#"{"github":"github-token","mirror":"gitea-token"}"#).unwrap(); + let TokenFile::Named(tokens) = read_token_file(&path).unwrap() else { + panic!("expected named tokens"); + }; + assert_eq!( + tokens.get("github").map(String::as_str), + Some("github-token") + ); + assert_eq!( + tokens.get("mirror").map(String::as_str), + Some("gitea-token") + ); + } + #[test] fn token_file_option_parses_as_a_path() { let args = diff --git a/src/config.rs b/src/config.rs index 9390bce..790ae08 100644 --- a/src/config.rs +++ b/src/config.rs @@ -39,6 +39,16 @@ struct Apple { struct Release { #[serde(rename = "tag-prefix")] tag_prefix: Option, + endpoints: Option>, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields, rename_all = "kebab-case")] +struct FileReleaseEndpoint { + name: String, + provider: ReleaseProvider, + repository: String, + token_env: Option, } #[derive(Debug, Clone, Deserialize, Default)] @@ -86,10 +96,35 @@ pub struct Config { pub apple: AppleConfig, pub tag_prefix: String, pub token_env: String, + pub release_endpoints: Vec, pub package_format: PackageFormat, pub repository_fallbacks: Vec, } +#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "lowercase")] +pub enum ReleaseProvider { + Gitea, + Github, +} + +impl std::fmt::Display for ReleaseProvider { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + Self::Gitea => "gitea", + Self::Github => "github", + }) + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ReleaseEndpoint { + pub name: String, + pub provider: ReleaseProvider, + pub repository: String, + pub token_env: String, +} + pub const DEFAULT_APPLE_IMAGE: &str = "ghcr.io/rust-cross/cargo-zigbuild@sha256:9b4f6b3eb9e8f9fefb3960fe288ddba576627d66d9eff851ce7f62d2455564a5"; #[derive(Debug, Clone, PartialEq, Eq)] @@ -238,6 +273,9 @@ fn merge_release(current: &mut Option, later: Option) { if later.tag_prefix.is_some() { current.tag_prefix = later.tag_prefix; } + if later.endpoints.is_some() { + current.endpoints = later.endpoints; + } } } @@ -288,6 +326,67 @@ fn finish(file: FileConfig, overrides: Overrides) -> Result { )) } }; + let mut release_endpoints = Vec::new(); + for endpoint in release.endpoints.clone().unwrap_or_default() { + if endpoint.name.is_empty() + || !endpoint + .name + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"_-".contains(&b)) + { + return Err(Error( + "release endpoint names must contain only letters, digits, underscores or hyphens." + .into(), + )); + } + if release_endpoints + .iter() + .any(|existing: &ReleaseEndpoint| existing.name == endpoint.name) + { + return Err(Error(format!( + "duplicate release endpoint name {:?}.", + endpoint.name + ))); + } + let repository = project::normalize_repository(&endpoint.repository) + .map_err(|e| Error(format!("invalid release endpoint repository: {e}")))?; + if !repository.starts_with("https://") { + return Err(Error(format!( + "release endpoint {:?} repository must use HTTPS.", + endpoint.name + ))); + } + if endpoint.provider == ReleaseProvider::Github + && url::Url::parse(&repository) + .ok() + .and_then(|url| url.host_str().map(str::to_owned)) + .as_deref() + != Some("github.com") + { + return Err(Error(format!( + "GitHub endpoint {:?} must use a github.com repository URL.", + endpoint.name + ))); + } + let token_env = endpoint + .token_env + .unwrap_or_else(|| match endpoint.provider { + ReleaseProvider::Gitea => "GITEA_TOKEN".into(), + ReleaseProvider::Github => "GITHUB_TOKEN".into(), + }); + if !valid_env(&token_env) { + return Err(Error(format!( + "release endpoint {:?} token-env must be an environment variable name.", + endpoint.name + ))); + } + release_endpoints.push(ReleaseEndpoint { + name: endpoint.name, + provider: endpoint.provider, + repository, + token_env, + }); + } let mut repository_fallbacks = Vec::new(); for fallback in repository.fallbacks { let origin = project::normalize_repository(&fallback.origin) @@ -344,6 +443,7 @@ fn finish(file: FileConfig, overrides: Overrides) -> Result { .or(release.tag_prefix) .unwrap_or_else(|| "v".into()), token_env: gitea.token_env.unwrap_or_else(|| "GITEA_TOKEN".into()), + release_endpoints, package_format: overrides.package_format.unwrap_or(package_format), repository_fallbacks, }; @@ -562,4 +662,46 @@ mod tests { assert!(parse(source, Overrides::default()).is_err(), "{source}"); } } + + #[test] + fn release_endpoints_are_typed_and_later_layers_replace_the_list() { + let config = parse_sources( + vec![ + ( + "user".into(), + "[[release.endpoints]]\nname='old'\nprovider='gitea'\nrepository='https://git.example/team/demo'\n" + .into(), + ), + ( + "project".into(), + "[[release.endpoints]]\nname='github'\nprovider='github'\nrepository='https://github.com/team/demo.git'\ntoken-env='DEMO_GITHUB_TOKEN'\n[[release.endpoints]]\nname='mirror'\nprovider='gitea'\nrepository='https://git.example/team/demo'\ntoken-env='DEMO_GITEA_TOKEN'\n" + .into(), + ), + ], + Overrides::default(), + ) + .unwrap(); + assert_eq!(config.release_endpoints.len(), 2); + assert_eq!(config.release_endpoints[0].name, "github"); + assert_eq!( + config.release_endpoints[0].provider, + ReleaseProvider::Github + ); + assert_eq!( + config.release_endpoints[0].repository, + "https://github.com/team/demo" + ); + assert_eq!(config.release_endpoints[1].provider, ReleaseProvider::Gitea); + + assert!(parse( + "[[release.endpoints]]\nname='bad name'\nprovider='gitea'\nrepository='https://git.example/team/demo'", + Overrides::default() + ) + .is_err()); + assert!(parse( + "[[release.endpoints]]\nname='enterprise'\nprovider='github'\nrepository='https://github.example/team/demo'", + Overrides::default() + ) + .is_err()); + } } diff --git a/src/gitea.rs b/src/gitea.rs index 71e9bdd..9996011 100644 --- a/src/gitea.rs +++ b/src/gitea.rs @@ -9,7 +9,10 @@ use reqwest::{ }; use serde::Deserialize; use serde_json::{json, Value}; -use std::{env, fs::File, path::Path, time::Duration}; +use std::{fs::File, path::Path, time::Duration}; + +#[cfg(test)] +use std::env; #[derive(Debug, Deserialize)] pub struct RemoteRelease { @@ -30,6 +33,7 @@ pub struct Gitea { } impl Gitea { + #[cfg(test)] pub fn new(repository: &str, token_env: &str) -> Result { if !repository.starts_with("https://") { return Err(Error( diff --git a/src/github.rs b/src/github.rs new file mode 100644 index 0000000..94622ac --- /dev/null +++ b/src/github.rs @@ -0,0 +1,427 @@ +use crate::{error::Error, gitea::RemoteRelease}; +use reqwest::{ + blocking::{Body, Client}, + header::{HeaderMap, HeaderValue, ACCEPT, AUTHORIZATION, CONTENT_LENGTH, CONTENT_TYPE}, + Method, StatusCode, +}; +use serde::Deserialize; +use serde_json::{json, Value}; +use std::{collections::BTreeMap, fs::File, path::Path, time::Duration}; + +#[derive(Debug, Deserialize)] +struct Asset { + id: u64, + name: String, +} + +pub struct Github { + client: Client, + base: String, + uploads: String, +} + +impl Github { + pub fn with_token(repository: &str, token: &str) -> Result { + Self::with_origins( + repository, + token, + "https://api.github.com", + "https://uploads.github.com", + ) + } + + pub(crate) fn with_origins( + repository: &str, + token: &str, + api_origin: &str, + upload_origin: &str, + ) -> Result { + let parsed = url::Url::parse(repository) + .map_err(|_| Error("Invalid GitHub repository URL.".into()))?; + if parsed.scheme() != "https" || parsed.host_str() != Some("github.com") { + return Err(Error( + "GitHub publishing requires an HTTPS github.com repository URL.".into(), + )); + } + let parts: Vec<_> = parsed + .path_segments() + .into_iter() + .flatten() + .filter(|part| !part.is_empty()) + .collect(); + if parts.len() != 2 { + return Err(Error( + "GitHub repository URL must end in owner/repository.".into(), + )); + } + let repository_path = format!("{}/{}", parts[0], parts[1]); + let mut headers = HeaderMap::new(); + headers.insert( + AUTHORIZATION, + HeaderValue::from_str(&format!("Bearer {token}")) + .map_err(|_| Error("Invalid GitHub token value.".into()))?, + ); + headers.insert( + ACCEPT, + HeaderValue::from_static("application/vnd.github+json"), + ); + headers.insert( + "x-github-api-version", + HeaderValue::from_static("2022-11-28"), + ); + let client = Client::builder() + .default_headers(headers) + .user_agent("rust_realease/0.1") + .timeout(Duration::from_secs(120)) + .redirect(reqwest::redirect::Policy::limited(10)) + .build() + .map_err(|_| Error("Cannot initialize GitHub HTTP client.".into()))?; + Ok(Self { + client, + base: format!("{api_origin}/repos/{repository_path}"), + uploads: format!("{upload_origin}/repos/{repository_path}"), + }) + } + + fn request( + &self, + method: Method, + path: &str, + body: Option, + missing_ok: bool, + ) -> Result, Error> { + let mut request = self + .client + .request(method.clone(), format!("{}{path}", self.base)); + if let Some(body) = body { + request = request.json(&body); + } + let response = request + .send() + .map_err(|_| Error(format!("GitHub {method} {path}: connection failed.")))?; + if missing_ok && response.status() == StatusCode::NOT_FOUND { + return Ok(None); + } + if !response.status().is_success() { + return Err(Error(format!( + "GitHub {method} {path}: HTTP {}.", + response.status().as_u16() + ))); + } + let bytes = response + .bytes() + .map_err(|_| Error(format!("GitHub {method} {path}: response failed.")))?; + if bytes.is_empty() { + return Ok(Some(Value::Null)); + } + serde_json::from_slice(&bytes) + .map(Some) + .map_err(|_| Error(format!("GitHub {method} {path}: invalid JSON response."))) + } + + pub fn ensure_release( + &self, + tag: &str, + commit: &str, + draft: bool, + prerelease: bool, + message: Option<&str>, + replace_existing_message: bool, + ) -> Result { + let encoded = encode(tag); + let reference = self + .request(Method::GET, &format!("/git/ref/tags/{encoded}"), None, true)? + .ok_or_else(|| { + Error(format!( + "Remote tag {tag:?} is missing. Push the tag explicitly before publishing." + )) + })?; + let mut remote_commit = reference + .pointer("/object/sha") + .and_then(Value::as_str) + .map(str::to_owned); + if reference.pointer("/object/type").and_then(Value::as_str) == Some("tag") { + let sha = remote_commit + .as_deref() + .ok_or_else(|| Error("GitHub tag response is missing its object SHA.".into()))?; + let tag_object = self + .request(Method::GET, &format!("/git/tags/{sha}"), None, false)? + .ok_or_else(|| Error("GitHub annotated tag response is empty.".into()))?; + remote_commit = tag_object + .pointer("/object/sha") + .and_then(Value::as_str) + .map(str::to_owned); + } + if remote_commit.as_deref() != Some(commit) { + return Err(Error( + "Remote tag commit differs from the checkout; refusing publication.".into(), + )); + } + let (release, existed) = if let Some(existing) = self.request( + Method::GET, + &format!("/releases/tags/{encoded}"), + None, + true, + )? { + (existing, true) + } else { + ( + self.request(Method::POST, "/releases", Some(json!({"tag_name": tag, "name": tag, "target_commitish": commit, "draft": draft, "prerelease": prerelease, "body": message.unwrap_or_default()})), false)? + .ok_or_else(|| Error("GitHub release creation returned no response.".into()))?, + false, + ) + }; + let release: RemoteRelease = serde_json::from_value(release) + .map_err(|_| Error("GitHub release response is missing id or draft state.".into()))?; + if existed && replace_existing_message { + if let Some(message) = message { + self.request( + Method::PATCH, + &format!("/releases/{}", release.id), + Some(json!({"body": message})), + false, + )?; + } + } + Ok(release) + } + + pub fn repository_private(&self) -> Result { + self.request(Method::GET, "", None, false)? + .and_then(|repository| repository.get("private").and_then(Value::as_bool)) + .ok_or_else(|| Error("GitHub repository response is missing visibility.".into())) + } + + pub fn download_client(&self) -> &Client { + &self.client + } + + fn assets(&self, release_id: u64) -> Result, Error> { + let response = self + .request( + Method::GET, + &format!("/releases/{release_id}/assets?per_page=100"), + None, + false, + )? + .ok_or_else(|| Error("GitHub assets response is empty.".into()))?; + serde_json::from_value(response) + .map_err(|_| Error("GitHub assets response is invalid.".into())) + } + + pub fn asset_urls(&self, release_id: u64) -> Result, Error> { + Ok(self + .assets(release_id)? + .into_iter() + .map(|asset| { + ( + asset.name, + format!("{}/releases/assets/{}", self.base, asset.id), + ) + }) + .collect()) + } + + pub fn upload(&self, release_id: u64, paths: &[&Path], replace: bool) -> Result<(), Error> { + let existing = self.assets(release_id)?; + for path in paths { + let name = path + .file_name() + .and_then(|name| name.to_str()) + .ok_or_else(|| Error("Invalid upload filename.".into()))?; + let collisions: Vec<_> = existing.iter().filter(|asset| asset.name == name).collect(); + if !collisions.is_empty() && !replace { + return Err(Error(format!( + "Release asset already exists: {name}. Use --replace-assets to replace it explicitly." + ))); + } + for asset in collisions { + self.request( + Method::DELETE, + &format!("/releases/assets/{}", asset.id), + None, + false, + )?; + } + let input = File::open(path) + .map_err(|e| Error(format!("cannot open asset {}: {e}", path.display())))?; + let size = input + .metadata() + .map_err(|e| Error(format!("cannot inspect asset {}: {e}", path.display())))? + .len(); + let mut url = + url::Url::parse(&format!("{}/releases/{release_id}/assets", self.uploads)) + .map_err(|_| Error("Invalid GitHub upload URL.".into()))?; + url.query_pairs_mut().append_pair("name", name); + let response = self + .client + .post(url) + .header(CONTENT_TYPE, "application/octet-stream") + .header(CONTENT_LENGTH, size) + .body(Body::new(input)) + .send() + .map_err(|_| { + Error(format!( + "GitHub POST /releases/{release_id}/assets: connection failed." + )) + })?; + if !response.status().is_success() { + return Err(Error(format!( + "GitHub POST /releases/{release_id}/assets: HTTP {}.", + response.status().as_u16() + ))); + } + println!("Uploaded {name}"); + } + Ok(()) + } +} + +fn encode(segment: &str) -> String { + url::form_urlencoded::byte_serialize(segment.as_bytes()).collect() +} + +#[cfg(test)] +mod tests { + use super::*; + use std::{ + io::{Read, Write}, + net::TcpListener, + thread, + }; + + struct Reply { + method: &'static str, + path: &'static str, + status: u16, + body: &'static str, + } + + fn server(replies: Vec) -> (String, thread::JoinHandle>>) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = format!("http://{}", listener.local_addr().unwrap()); + let handle = thread::spawn(move || { + let mut bodies = Vec::new(); + for reply in replies { + let (mut stream, _) = listener.accept().unwrap(); + let mut bytes = Vec::new(); + let mut buffer = [0_u8; 8192]; + let header_end = loop { + let count = stream.read(&mut buffer).unwrap(); + bytes.extend_from_slice(&buffer[..count]); + if let Some(index) = bytes.windows(4).position(|part| part == b"\r\n\r\n") { + break index + 4; + } + }; + let headers = String::from_utf8_lossy(&bytes[..header_end]); + let request_line = headers.lines().next().unwrap(); + assert_eq!( + request_line, + format!("{} {} HTTP/1.1", reply.method, reply.path) + ); + assert!(headers + .lines() + .any(|line| line.eq_ignore_ascii_case("authorization: Bearer test-token"))); + assert!(headers.lines().any(|line| { + line.eq_ignore_ascii_case("accept: application/vnd.github+json") + })); + let length = headers + .lines() + .find_map(|line| { + line.to_ascii_lowercase() + .strip_prefix("content-length: ") + .map(str::to_owned) + }) + .and_then(|value| value.parse::().ok()) + .unwrap_or(0); + while bytes.len() < header_end + length { + let count = stream.read(&mut buffer).unwrap(); + bytes.extend_from_slice(&buffer[..count]); + } + bodies.push(bytes[header_end..header_end + length].to_vec()); + write!( + stream, + "HTTP/1.1 {} OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}", + reply.status, + reply.body.len(), + reply.body + ) + .unwrap(); + } + bodies + }); + (address, handle) + } + + #[test] + fn creates_release_and_uploads_raw_github_asset() { + let (host, handle) = server(vec![ + Reply { + method: "GET", + path: "/repos/team/demo/git/ref/tags/v1.0.0", + status: 200, + body: r#"{"object":{"type":"commit","sha":"abc"}}"#, + }, + Reply { + method: "GET", + path: "/repos/team/demo/releases/tags/v1.0.0", + status: 404, + body: "{}", + }, + Reply { + method: "POST", + path: "/repos/team/demo/releases", + status: 201, + body: r#"{"id":7,"draft":false}"#, + }, + Reply { + method: "GET", + path: "/repos/team/demo/releases/7/assets?per_page=100", + status: 200, + body: "[]", + }, + Reply { + method: "POST", + path: "/repos/team/demo/releases/7/assets?name=asset.bin", + status: 201, + body: "{}", + }, + Reply { + method: "GET", + path: "/repos/team/demo/releases/7/assets?per_page=100", + status: 200, + body: r#"[{"id":11,"name":"asset.bin"}]"#, + }, + ]); + let api = Github::with_origins("https://github.com/team/demo", "test-token", &host, &host) + .unwrap(); + let release = api + .ensure_release("v1.0.0", "abc", false, false, Some("notes"), false) + .unwrap(); + let temp = tempfile::tempdir().unwrap(); + let asset = temp.path().join("asset.bin"); + std::fs::write(&asset, b"asset bytes").unwrap(); + api.upload(release.id, &[asset.as_path()], false).unwrap(); + assert_eq!( + api.asset_urls(release.id) + .unwrap() + .get("asset.bin") + .map(String::as_str), + Some(format!("{host}/repos/team/demo/releases/assets/11").as_str()) + ); + let bodies = handle.join().unwrap(); + assert!(String::from_utf8_lossy(&bodies[2]).contains("\"tag_name\":\"v1.0.0\"")); + assert_eq!(bodies[4], b"asset bytes"); + } + + #[test] + fn rejects_non_github_and_malformed_repository_urls() { + for repository in [ + "http://github.com/team/demo", + "https://example.com/team/demo", + "https://github.com/team/demo/extra", + ] { + assert!(Github::with_token(repository, "token").is_err()); + } + } +} diff --git a/src/main.rs b/src/main.rs index c1897f3..3fa5f89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod cli; mod config; mod error; mod gitea; +mod github; mod macho; mod manifest; mod model; @@ -13,6 +14,7 @@ mod package; mod policy; mod prepare; mod project; +mod release; mod runner; mod ui; mod verify; diff --git a/src/prepare.rs b/src/prepare.rs index c76a073..bb66860 100644 --- a/src/prepare.rs +++ b/src/prepare.rs @@ -481,6 +481,7 @@ mod tests { locked: true, tag_prefix: "v".into(), token_env: "GITEA_TOKEN".into(), + release_endpoints: Vec::new(), package_format: crate::config::PackageFormat::Tgz, repository_fallbacks: Vec::new(), }; @@ -545,6 +546,7 @@ mod tests { locked: true, tag_prefix: "v".into(), token_env: "GITEA_TOKEN".into(), + release_endpoints: Vec::new(), package_format: crate::config::PackageFormat::Tgz, repository_fallbacks: Vec::new(), }; diff --git a/src/release.rs b/src/release.rs new file mode 100644 index 0000000..67745f8 --- /dev/null +++ b/src/release.rs @@ -0,0 +1,83 @@ +use crate::{ + config::{ReleaseEndpoint, ReleaseProvider}, + error::Error, + gitea::{Gitea, RemoteRelease}, + github::Github, +}; +use reqwest::blocking::Client; +use std::{collections::BTreeMap, path::Path}; + +pub enum ReleaseClient { + Gitea(Gitea), + Github(Github), +} + +impl ReleaseClient { + pub fn new(endpoint: &ReleaseEndpoint, token: &str) -> Result { + match endpoint.provider { + ReleaseProvider::Gitea => { + Gitea::with_token(&endpoint.repository, token).map(Self::Gitea) + } + ReleaseProvider::Github => { + Github::with_token(&endpoint.repository, token).map(Self::Github) + } + } + } + + pub fn ensure_release( + &self, + tag: &str, + commit: &str, + draft: bool, + prerelease: bool, + message: Option<&str>, + replace_existing_message: bool, + ) -> Result { + match self { + Self::Gitea(api) => api.ensure_release( + tag, + commit, + draft, + prerelease, + message, + replace_existing_message, + ), + Self::Github(api) => api.ensure_release( + tag, + commit, + draft, + prerelease, + message, + replace_existing_message, + ), + } + } + + pub fn repository_private(&self) -> Result { + match self { + Self::Gitea(api) => api.repository_private(), + Self::Github(api) => api.repository_private(), + } + } + + pub fn upload(&self, release_id: u64, paths: &[&Path], replace: bool) -> Result<(), Error> { + match self { + Self::Gitea(api) => api.upload(release_id, paths, replace), + Self::Github(api) => api.upload(release_id, paths, replace), + } + } + + pub fn download_client(&self) -> &Client { + match self { + Self::Gitea(api) => api.download_client(), + Self::Github(api) => api.download_client(), + } + } + + pub fn asset_urls(&self, release_id: u64) -> Result>, Error> { + match self { + Self::Gitea(_) => Ok(None), + Self::Github(api) => api.asset_urls(release_id).map(Some), + } + } +} diff --git a/src/runner.rs b/src/runner.rs index 43cf4c0..b72876b 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -3,20 +3,33 @@ use regex::Regex; use std::{path::Path, process::Command, sync::OnceLock}; pub struct Runner { - token_env: String, + token_envs: Vec, secrets: Vec, verbose: bool, } impl Runner { pub fn new(token_env: &str) -> Self { + let mut token_envs = vec!["GITEA_TOKEN".to_owned(), "GITHUB_TOKEN".to_owned()]; + if !token_envs.iter().any(|name| name == token_env) { + token_envs.push(token_env.to_owned()); + } Self { - token_env: token_env.to_owned(), + token_envs, secrets: Vec::new(), verbose: false, } } + pub fn with_token_envs(mut self, names: impl IntoIterator) -> Self { + for name in names { + if !self.token_envs.contains(&name) { + self.token_envs.push(name); + } + } + self + } + pub fn with_secret(mut self, secret: Option<&str>) -> Self { if let Some(secret) = secret.filter(|secret| !secret.is_empty()) { self.secrets.push(secret.to_owned()); @@ -50,11 +63,10 @@ impl Runner { eprintln!("$ {}", self.redact(&command)); } let mut process = Command::new(program); - process - .args(args) - .current_dir(cwd) - .env_remove("GITEA_TOKEN") - .env_remove(&self.token_env); + process.args(args).current_dir(cwd); + for name in &self.token_envs { + process.env_remove(name); + } for name in removals { process.env_remove(name); } @@ -116,10 +128,11 @@ impl Runner { command .args(&args[1..]) .current_dir(cwd) - .env_remove("GITEA_TOKEN") - .env_remove(&self.token_env) .stdout(Stdio::piped()) .stderr(Stdio::piped()); + for name in &self.token_envs { + command.env_remove(name); + } for (name, _) in std::env::vars_os() { let upper = name.to_string_lossy().to_uppercase(); if ["TOKEN", "PASSWORD", "SECRET", "CREDENTIAL"] @@ -218,7 +231,7 @@ impl Runner { for secret in &self.secrets { without_token = without_token.replace(secret, "[REDACTED]"); } - for name in ["GITEA_TOKEN", self.token_env.as_str()] { + for name in &self.token_envs { if let Ok(secret) = std::env::var(name) { if !secret.is_empty() { without_token = without_token.replace(&secret, "[REDACTED]"); diff --git a/src/verify.rs b/src/verify.rs index e6e46fc..4de730c 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -6,8 +6,9 @@ use crate::{ runner::Runner, }; use flate2::read::GzDecoder; -use reqwest::blocking::Client; +use reqwest::{blocking::Client, header::ACCEPT}; use std::{ + collections::BTreeMap, fs::{self, File}, io::{Read, Write}, path::Path, @@ -236,6 +237,17 @@ pub fn verify_published( artifacts: &[Artifact], sums: &Path, authenticated_client: Option<&Client>, +) -> Result<(), Error> { + verify_published_with_asset_urls(repository, tag, artifacts, sums, authenticated_client, None) +} + +pub fn verify_published_with_asset_urls( + repository: &str, + tag: &str, + artifacts: &[Artifact], + sums: &Path, + authenticated_client: Option<&Client>, + asset_urls: Option<&BTreeMap>, ) -> Result<(), Error> { let downloads = tempfile::Builder::new() .prefix("rust-realease-download-") @@ -258,8 +270,19 @@ pub fn verify_published( .file_name() .and_then(|s| s.to_str()) .ok_or_else(|| Error("Invalid asset filename.".into()))?; - let mut response = client - .get(asset_url(repository, tag, filename)) + let url = match asset_urls { + Some(urls) => urls.get(filename).cloned().ok_or_else(|| { + Error(format!( + "{filename}: release API did not return the uploaded asset." + )) + })?, + None => asset_url(repository, tag, filename), + }; + let mut request = client.get(url); + if asset_urls.is_some() { + request = request.header(ACCEPT, "application/octet-stream"); + } + let mut response = request .send() .map_err(|_| Error(format!("{filename}: release download failed.")))?; if response.status() != reqwest::StatusCode::OK {