Compare commits

..

13 Commits

Author SHA1 Message Date
Dandaedre 0bf23d70b5 test: stabilize coverage gate in CI
Rust CI Cargo tests, coverage, and badge generation passed.
Gitea/rust_realease/pipeline/head This commit looks good Details
2026-11-21 14:47:56 +01:00
Dandaedre 79bf210a15 test: raise coverage above CI threshold
Rust CI Cargo tests, coverage, or badge generation failed.
Gitea/rust_realease/pipeline/head There was a failure building this commit Details
2026-11-21 14:39:40 +01:00
Dandaedre ee03436977 Prepare v0.1.6-rc.5 release
Rust CI Cargo tests, coverage, or badge generation failed.
Gitea/rust_realease/pipeline/head There was a failure building this commit Details
2026-11-21 13:54:33 +01:00
Dandaedre 5806ff2fbb Authenticate GitHub asset verification
Rust CI Cargo tests, coverage, or badge generation failed.
Gitea/rust_realease/pipeline/head There was a failure building this commit Details
2026-11-21 13:53:03 +01:00
Dandaedre 1abe422646 Prepare v0.1.6-rc.4 release
Rust CI Cargo tests, coverage, or badge generation failed.
Gitea/rust_realease/pipeline/head There was a failure building this commit Details
2026-11-21 13:45:17 +01:00
Dandaedre af24eb9d52 Add GitHub and multi-endpoint releases
Rust CI Cargo tests, coverage, or badge generation failed.
Gitea/rust_realease/pipeline/head There was a failure building this commit Details
2026-11-21 13:40:38 +01:00
Dandaedre 335abf6604 Verify deployment policy for Apple named bundles
Rust CI Cargo tests, coverage, and badge generation passed.
Gitea/rust_realease/pipeline/head This commit looks good Details
2026-10-20 18:31:54 +02:00
Dandaedre 531a059b51 Support macOS executables in named bundles 2026-10-20 18:11:06 +02:00
Dandaedre 9d6e366dba feat: improve macOS release downloads
Rust CI Cargo tests, coverage, and badge generation passed.
Gitea/rust_realease/pipeline/head This commit looks good Details
2026-10-20 17:52:20 +02:00
Dandaedre b87f71c0cc fix: serialize artifact filenames as strings
Rust CI Cargo tests, coverage, and badge generation passed.
Gitea/rust_realease/pipeline/head This commit looks good Details
2026-10-20 17:48:09 +02:00
Dandaedre d89098b72d fix: stage hardlinked Cargo bundle inputs 2026-10-20 17:47:35 +02:00
Dandaedre cc4fb03c26 test: make release fixtures portable
Rust CI Cargo tests, coverage, and badge generation passed.
Gitea/rust_realease/pipeline/head This commit looks good Details
2026-10-20 16:24:21 +02:00
Dandaedre e62f38c97f docs: record macOS self release
Rust CI Cargo tests, coverage, or badge generation failed.
Gitea/rust_realease/pipeline/head There was a failure building this commit Details
2026-10-20 16:22:37 +02:00
20 changed files with 1912 additions and 119 deletions

2
Cargo.lock generated
View File

@ -1027,7 +1027,7 @@ dependencies = [
[[package]]
name = "rust_realease"
version = "0.1.6-rc.3"
version = "0.1.6-rc.5"
dependencies = [
"clap",
"console",

View File

@ -1,6 +1,6 @@
[package]
name = "rust_realease"
version = "0.1.6-rc.3"
version = "0.1.6-rc.5"
edition = "2021"
publish = false
repository = "https://proxy.dandaedre.com/gitea/idle-org/rust_realease"

View File

@ -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.

View File

@ -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,9 +194,9 @@ 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. Asset replacement is not transactional: if an upload fails after a matching asset is deleted, retry the publication explicitly.
`--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.
## Artifact contract
@ -182,11 +209,46 @@ my-tool-x86_64-unknown-linux-musl-v0.1.0.tgz
└── my-tool-helper
```
Windows archive members have an `.exe` suffix. Raw assets are target-qualified to remain unambiguous, for example `my-tool-x86_64-unknown-linux-musl-v0.1.0` and `my-tool-x86_64-pc-windows-gnu-v0.1.0.exe`. `SHA256SUMS` lists every generated asset. Archive ownership and timestamps are normalized; identical binary bytes and modes produce identical archives. Separate builds can still produce different binaries.
Windows archive members have an `.exe` suffix. Raw assets are target-qualified to remain unambiguous, for example `my-tool-x86_64-unknown-linux-musl-v0.1.0` and `my-tool-x86_64-pc-windows-gnu-v0.1.0.exe`. Apple raw assets omit the dotted version suffix, for example `my-tool-aarch64-apple-darwin`; use the versioned tgz archive by default. `SHA256SUMS` lists every generated asset. Archive ownership and timestamps are normalized; identical binary bytes and modes produce identical archives. Separate builds can still produce different binaries.
## 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.

View File

@ -166,14 +166,17 @@ a successful build or create/upload a partial release.
## Artifacts and target-aware verification
Apple binary names have no `.exe` suffix. Existing archive and raw-binary
naming applies unchanged. For version `{version}` and package `tiny-wb`:
Apple binary names have no `.exe` suffix. Versioned tgz archives remain the
recommended download because their internal executable names are ordinary Cargo
binary names. Standalone Apple asset names omit the version so Finder does not
mistake the final dotted version component for a file extension. For version
`{version}` and package `tiny-wb`:
```text
tiny-wb-aarch64-apple-darwin-v{version}.tgz
tiny-wb-aarch64-apple-darwin-v{version}
tiny-wb-aarch64-apple-darwin
tiny-wb-x86_64-apple-darwin-v{version}.tgz
tiny-wb-x86_64-apple-darwin-v{version}
tiny-wb-x86_64-apple-darwin
```
Each tgz has the cargo-binstall layout:
@ -190,6 +193,17 @@ All local archives are checked before any release creation. Existing remote
collision handling, replacement policy, upload, authenticated/private download
handling, and checksum verification apply to Apple assets too.
When a release contains Apple artifacts, the publisher adds a
`## macOS Workaround` section to the release body. It is generated from the
artifacts being uploaded: named bundles are preferred, then canonical tgz
archives, with standalone binaries used only when no archive exists. The shell
snippet names the correct architecture, archive, extracted root, and executable
members. It extracts into `~/Downloads`, applies
`xattr -rd com.apple.quarantine` to the complete extracted tree, restores the
executable bits, and starts a GUI executable when one has a `-gui` name. User
notes supplied with `--message` or `--message-file` appear before this section.
An existing `## macOS Workaround` heading prevents a duplicate section.
For every Apple executable, the Linux host parses the Mach-O header and load
commands and requires:
@ -247,9 +261,10 @@ No Gitea token is required. Publication remains a separate explicit invocation.
## Verification performed for this implementation
No release, upload, tag, push, Jenkins job, or global publisher installation was
performed. A disposable clone of `tiny-wb` commit `8ed07cb` under `/tmp` was
built with this checkout's publisher and rootless Podman.
During the initial implementation verification, no release, upload, tag, push,
Jenkins job, or global publisher installation was performed. A disposable clone
of `tiny-wb` commit `8ed07cb` under `/tmp` was built with this checkout's
publisher and rootless Podman.
Real two-target command:
@ -293,4 +308,22 @@ dry-run, token-free compile-only operation, cache reuse/invalidation fields,
Mach-O rejection cases, artifact names, permissions, archive layout and eight
checksum entries. Native macOS execution, exact macOS versions/CPUs exercised by
the maintainer's earlier successful testing, hardware behavior, Gatekeeper,
signing, notarization, Jenkins and real publication remain external checks.
At that point, signing, notarization, Jenkins and real publication remained
external checks.
### Subsequent self-release
The implementation was then released as the `v0.1.6-rc.3` prerelease from
commit `609876f00c7821dd5a9d36b364c85fd883d80939`. The repository's release
configuration selected its three existing Cross targets plus
`x86_64-apple-darwin`. The token-free compile-only phase completed all four
targets in 374.9 seconds and validated the Apple Mach-O file before any remote
release mutation.
The publication phase reused that hash-validated build, uploaded eight payload
assets plus `SHA256SUMS`, downloaded every asset again, verified all checksums
and archive layouts, and completed isolated cargo-binstall installation checks
for all four targets. The Apple runtime check remained explicitly skipped
because the publisher host was Linux. The commit and annotated tag were pushed
atomically before release creation. No Jenkins job or global publisher
installation was involved.

View File

@ -155,9 +155,14 @@ and reopened to check its declared contents, modes and hashes. Partial staging i
removed on failure; successful invocation directories are retained under `dist/`.
Executable validation currently supports little-endian Linux ELF (x86-64,
aarch64, i686, riscv64gc) and Windows PE (x86-64, aarch64, i686). Unsupported
aarch64, i686, riscv64gc) Windows PE (x86-64, aarch64, i686), and thin 64-bit macOS Mach-O executables
(arm64 and x86-64). Unsupported
executable targets fail explicitly. This checks format and architecture, not ABI,
DLL completeness, license suitability or runtime behavior. Foreign executables
DLL completeness, license suitability or runtime behavior. For Apple named bundles, final verification also checks every executable against
`build.apple.deployment-target`: a dependency may have an older minimum, but not
a newer one, and only system dylib dependencies are accepted. Cargo outputs still
have their exact configured minimum checked during build/cache validation.
Foreign executables
are never executed automatically by the publisher. Trusted preparation scripts
must follow the same rule themselves.

View File

@ -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(),
}

View File

@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, BTreeSet},
fs,
io::Read,
io::{Read, Write},
path::{Path, PathBuf},
};
@ -391,6 +391,52 @@ fn add(
});
Ok(())
}
fn add_cargo(
inputs: &mut Vec<Input>,
path: &Path,
staging: &Path,
dest: &str,
target: &str,
map: &mut BTreeMap<String, ()>,
) -> Result<(), Error> {
let metadata = fs::symlink_metadata(path)?;
if !metadata.file_type().is_file() || metadata.file_type().is_symlink() {
return fail(format!(
"Not a regular Cargo executable: {}",
path.display()
));
}
insert_destination(map, dest, windows(target))?;
architecture(path, target)?;
let source_hash = package::checksum(path)?;
fs::create_dir_all(staging)?;
let mut staged = tempfile::NamedTempFile::new_in(staging)?;
let mut source = fs::File::open(path)?;
std::io::copy(&mut source, staged.as_file_mut())?;
staged.as_file_mut().flush()?;
fs::set_permissions(staged.path(), metadata.permissions())?;
let staged_hash = package::checksum(staged.path())?;
if staged_hash != source_hash || package::checksum(path)? != source_hash {
return fail(format!(
"Cargo executable changed during staging: {}",
path.display()
));
}
regular(staged.path())?;
architecture(staged.path(), target)?;
let (_, staged_path) = staged
.keep()
.map_err(|error| Error(format!("cannot retain staged Cargo executable: {error}")))?;
inputs.push(Input {
source: staged_path,
entry: Entry {
destination: dest.into(),
sha256: source_hash,
mode: 0o755,
},
});
Ok(())
}
fn add_tree(
inputs: &mut Vec<Input>,
path: &Path,
@ -450,6 +496,14 @@ fn architecture(path: &Path, target: &str) -> Result<(), Error> {
"riscv64gc" => Some(243),
_ => None,
}
} else if target.ends_with("-apple-darwin") && h[..4] == [0xcf, 0xfa, 0xed, 0xfe] {
u32::from_le_bytes(h[12..16].try_into().unwrap()) == 2
&& Some(u32::from_le_bytes(h[4..8].try_into().unwrap()))
== match arch {
"aarch64" => Some(0x0100_000c),
"x86_64" => Some(0x0100_0007),
_ => None,
}
} else {
false
};
@ -488,6 +542,7 @@ pub fn assemble(
config.package_format,
)?;
let artifact_dir = old_sums.parent().unwrap().to_path_buf();
let cargo_stage = stage.path().join("cargo-inputs");
let mut records = Vec::new();
for b in &config.bundles {
for output in outputs {
@ -509,14 +564,14 @@ pub fn assemble(
.find(|(n, _)| n == binary)
.ok_or_else(|| Error(format!("Missing built binary {binary}")))?
.1;
// Cargo output is already validated by the build layer; check its target too.
add(
// Cargo may hardlink this path to release/deps. Copy the validated bytes into
// publisher-owned staging while retaining strict link checks for other inputs.
add_cargo(
&mut inputs,
path,
&cargo_stage,
&policy::binary_filename(binary, target),
Kind::Executable,
target,
None,
&mut map,
)?;
}
@ -648,7 +703,8 @@ pub fn assemble(
"{}: portable tree/hash/architecture checks passed; native runtime {smoke}",
artifact.path.display()
);
records.push(serde_json::json!({"bundle":b.name,"target":target,"filename":artifact.path.file_name(),"sha256":artifact.sha256,"root":root,"files":entries,"checks":{"portable_tree":"passed","hashes":"passed","architecture":"passed","native_runtime":smoke}}));
let filename = artifact_filename(&artifact.path)?;
records.push(serde_json::json!({"bundle":b.name,"target":target,"filename":filename,"sha256":artifact.sha256,"root":root,"files":entries,"checks":{"portable_tree":"passed","hashes":"passed","architecture":"passed","native_runtime":smoke}}));
artifacts.push(artifact);
}
}
@ -657,7 +713,8 @@ pub fn assemble(
crate::verify::verify_archive(&a.path, a)?;
}
if !matches!(a.contents, ArtifactContents::Bundle { .. }) {
records.push(serde_json::json!({"target":a.target,"filename":a.path.file_name(),"sha256":a.sha256,"checks":{"native_runtime":"skipped","cargo_binstall":"deferred until publication"}}));
let filename = artifact_filename(&a.path)?;
records.push(serde_json::json!({"target":a.target,"filename":filename,"sha256":a.sha256,"checks":{"native_runtime":"skipped","cargo_binstall":"deferred until publication"}}));
}
}
let receipt = artifact_dir.join("artifact-manifest.json");
@ -688,7 +745,6 @@ pub fn assemble(
)?;
// Stable local discovery entry; keep public manifests free of host paths.
let mut locator = tempfile::NamedTempFile::new_in(&dist)?;
use std::io::Write as _;
locator.write_all(&serde_json::to_vec_pretty(&serde_json::json!({
"schema_version": 1,
"manifest": artifact_dir.join("artifact-manifest.json").strip_prefix(&dist).map_err(|e| Error(e.to_string()))?,
@ -700,6 +756,14 @@ pub fn assemble(
let _ = stage.keep();
Ok((artifacts, old_sums))
}
fn artifact_filename(path: &Path) -> Result<String, Error> {
let filename = path
.file_name()
.and_then(|name| name.to_str())
.ok_or_else(|| Error("Invalid artifact filename".into()))?;
component(filename, false)?;
Ok(filename.to_owned())
}
fn zip_error(e: zip::result::ZipError) -> Error {
Error(e.to_string())
}
@ -843,6 +907,103 @@ pub fn verify(path: &Path, root: &str, format: Format, entries: &[Entry]) -> Res
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bundle_macho_requires_matching_thin_64_bit_executable() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("ffprobe");
for (target, cpu) in [
("aarch64-apple-darwin", 0x0100_000cu32),
("x86_64-apple-darwin", 0x0100_0007u32),
] {
let mut bytes = [0u8; 64];
bytes[..4].copy_from_slice(&[0xcf, 0xfa, 0xed, 0xfe]);
bytes[4..8].copy_from_slice(&cpu.to_le_bytes());
bytes[12..16].copy_from_slice(&2u32.to_le_bytes());
fs::write(&path, bytes).unwrap();
architecture(&path, target).unwrap();
let wrong = if target.starts_with("aarch64") {
"x86_64-apple-darwin"
} else {
"aarch64-apple-darwin"
};
assert!(architecture(&path, wrong).is_err());
assert!(architecture(&path, "x86_64-unknown-linux-gnu").is_err());
bytes[12] = 6; // A dylib is not a runnable program.
fs::write(&path, bytes).unwrap();
assert!(architecture(&path, target).is_err());
bytes[12] = 2;
bytes[..4].copy_from_slice(&[0xca, 0xfe, 0xba, 0xbe]);
fs::write(&path, bytes).unwrap();
assert!(architecture(&path, target).is_err());
fs::write(&path, &bytes[..16]).unwrap();
assert!(architecture(&path, target).is_err());
}
}
#[test]
fn apple_bundle_verification_checks_every_executable_in_zip_and_tgz() {
use std::io::Write;
let dir = tempfile::tempdir().unwrap();
let tree = dir.path().join("tree");
fs::create_dir(&tree).unwrap();
let dependency = b"/usr/lib/libSystem.B.dylib\0";
let length = (24 + dependency.len()).div_ceil(8) * 8;
let mut bytes = vec![0u8; 56 + length];
for (offset, value) in [
(0, 0xfeedfacfu32),
(4, 0x100000c),
(12, 2),
(16, 2),
(20, 24 + length as u32),
(32, 0x32),
(36, 24),
(40, 1),
(44, 13 << 16),
(56, 0xc),
(60, length as u32),
(64, 24),
] {
bytes[offset..offset + 4].copy_from_slice(&value.to_le_bytes());
}
bytes[80..80 + dependency.len()].copy_from_slice(dependency);
for format in [Format::Zip, Format::Tgz] {
for (minimum, should_pass) in [(12u32, true), (14u32, false)] {
fs::write(tree.join("app"), &bytes).unwrap();
let mut helper = bytes.clone();
helper[44..48].copy_from_slice(&(minimum << 16).to_le_bytes());
fs::write(tree.join("ffprobe"), helper).unwrap();
fs::write(tree.join("LICENSE"), b"notice, not executable").unwrap();
let entries = [("app", 0o755), ("ffprobe", 0o755), ("LICENSE", 0o644)]
.into_iter()
.map(|(name, mode)| Entry {
destination: name.into(),
mode,
sha256: package::checksum(&tree.join(name)).unwrap(),
})
.collect::<Vec<_>>();
let path = dir.path().join("bundle");
write_archive(&path, &tree, "root", format, &entries).unwrap();
let artifact = Artifact {
target: "aarch64-apple-darwin".into(),
sha256: package::checksum(&path).unwrap(),
path: path.clone(),
contents: ArtifactContents::Bundle {
root: "root".into(),
format,
entries,
},
};
assert_eq!(
crate::verify::verify_apple_artifact(&path, &artifact, "13.0").is_ok(),
should_pass
);
let mut broken = fs::OpenOptions::new().write(true).open(&path).unwrap();
broken.write_all(b"broken").unwrap();
assert!(crate::verify::verify_apple_artifact(&path, &artifact, "13.0").is_err());
}
}
}
fn fixture() -> (
tempfile::TempDir,
SelectedPackage,
@ -922,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(),
};
@ -953,6 +1115,16 @@ required-destinations=['runtime{binary-ext}','LICENSES/runtime/LICENSE.txt','LIC
let receipt = first.iter().find(|a| a.target == "manifest").unwrap();
let text = fs::read_to_string(&receipt.path).unwrap();
assert!(!text.contains(&temp.path().display().to_string()));
let public_manifest: serde_json::Value = serde_json::from_str(&text).unwrap();
assert_eq!(public_manifest["schema_version"], 1);
let records = public_manifest["artifacts"].as_array().unwrap();
assert_eq!(records.len(), 4);
for record in records {
assert!(record["target"].is_string());
assert!(record["filename"].is_string());
assert_eq!(record["sha256"].as_str().unwrap().len(), 64);
assert!(record["checks"].is_object());
}
fs::write(temp.path().join("member/LICENSE"), "new license").unwrap();
let (third, _) = assemble(temp.path(), &p, &c, &outputs, "abc", true, &runner).unwrap();
assert_ne!(
@ -1000,6 +1172,40 @@ required-destinations=['runtime{binary-ext}','LICENSES/runtime/LICENSE.txt','LIC
assert!(regular(&link).is_err());
}
}
#[cfg(unix)]
#[test]
fn hardlinked_cargo_executable_is_copied_to_independent_staging() {
use std::os::unix::fs::{MetadataExt, PermissionsExt};
let (temp, _, _, outputs) = fixture();
let source = &outputs[0].binaries[0].1;
fs::set_permissions(source, fs::Permissions::from_mode(0o751)).unwrap();
let other_link = temp.path().join("cargo-deps-link");
fs::hard_link(source, &other_link).unwrap();
assert_eq!(fs::metadata(source).unwrap().nlink(), 2);
let mut inputs = Vec::new();
add_cargo(
&mut inputs,
source,
&temp.path().join("publisher-staging"),
"demo",
"x86_64-unknown-linux-gnu",
&mut BTreeMap::new(),
)
.unwrap();
assert_eq!(inputs.len(), 1);
let staged = &inputs[0].source;
assert_eq!(fs::read(staged).unwrap(), fs::read(source).unwrap());
assert_eq!(fs::metadata(staged).unwrap().nlink(), 1);
assert_eq!(
fs::metadata(staged).unwrap().permissions().mode() & 0o777,
0o751
);
assert_eq!(inputs[0].entry.sha256, package::checksum(source).unwrap());
assert_eq!(fs::metadata(source).unwrap().nlink(), 2);
}
#[test]
fn failed_hooks_clean_partial_artifacts_and_filter_targets() {
let (temp, p, mut c, outputs) = fixture();

View File

@ -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<String>,
#[arg(long)]
pub repository_url: Option<String>,
/// 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<PathBuf>,
#[arg(long)]
@ -109,7 +112,7 @@ pub fn run(mut args: Cli) -> Result<(), Error> {
"--prepare-repo is a separate mode; omit --stop-after.".into(),
));
}
let release_message = match args.message_file.as_deref() {
let supplied_release_message = match args.message_file.as_deref() {
Some(path) => Some(std::fs::read_to_string(path).map_err(|e| {
Error(format!(
"cannot read release message file {}: {e}",
@ -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,39 +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 = api.ensure_release(
&tag,
&commit,
args.draft,
args.prerelease,
release_message.as_deref(),
)?;
ui::success(format_args!("Release ready: {tag}"));
let release_message =
release_message_with_macos_workaround(supplied_release_message.as_deref(), &artifacts)?;
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 || asset_urls.is_some() {
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)?;
}
@ -482,40 +562,255 @@ pub fn run(mut args: Cli) -> Result<(), Error> {
Ok(())
}
fn read_token_file(path: &std::path::Path) -> Result<String, Error> {
fn release_message_with_macos_workaround(
supplied: Option<&str>,
artifacts: &[crate::package::Artifact],
) -> Result<Option<String>, Error> {
const HEADING: &str = "## macOS Workaround";
if supplied.is_some_and(|message| message.contains(HEADING)) {
return Ok(supplied.map(str::to_owned));
}
let Some(workaround) = macos_workaround(artifacts)? else {
return Ok(supplied.map(str::to_owned));
};
Ok(Some(
match supplied.map(str::trim_end).filter(|s| !s.is_empty()) {
Some(message) => format!("{message}\n\n{workaround}"),
None => workaround,
},
))
}
fn macos_workaround(artifacts: &[crate::package::Artifact]) -> Result<Option<String>, Error> {
use crate::{bundle::Format, package::ArtifactContents};
let apple = artifacts
.iter()
.filter(|artifact| build::is_apple(&artifact.target))
.collect::<Vec<_>>();
if apple.is_empty() {
return Ok(None);
}
let has_bundle = apple
.iter()
.any(|artifact| matches!(artifact.contents, ArtifactContents::Bundle { .. }));
let has_archive = apple
.iter()
.any(|artifact| matches!(artifact.contents, ArtifactContents::Archive { .. }));
let selected = apple.into_iter().filter(|artifact| {
if has_bundle {
matches!(artifact.contents, ArtifactContents::Bundle { .. })
} else if has_archive {
matches!(artifact.contents, ArtifactContents::Archive { .. })
} else {
matches!(artifact.contents, ArtifactContents::Binary)
}
});
let mut sections = Vec::new();
for artifact in selected {
let filename = artifact
.path
.file_name()
.and_then(|name| name.to_str())
.ok_or_else(|| Error("Invalid macOS artifact filename".into()))?;
let platform = if artifact.target.starts_with("aarch64-") {
"Apple Silicon"
} else {
"Intel Mac"
};
let commands = match &artifact.contents {
ArtifactContents::Bundle {
root,
format,
entries,
} => archive_workaround(
filename,
root,
*format,
entries
.iter()
.filter(|entry| entry.mode & 0o111 != 0)
.map(|entry| entry.destination.as_str()),
),
ArtifactContents::Archive { root, binaries } => archive_workaround(
filename,
root,
Format::Tgz,
binaries.iter().map(String::as_str),
),
ArtifactContents::Binary => {
let quoted = shell_quote(filename);
format!(
"downloads=\"$HOME/Downloads\"\nexecutable=\"$downloads\"/{quoted}\nxattr -d com.apple.quarantine \"$executable\" 2>/dev/null || true\nchmod +x \"$executable\"\n\"$executable\""
)
}
};
sections.push(format!(
"For **{platform}** (`{filename}`), run in Terminal:\n```sh\n{commands}\n```"
));
}
if sections.is_empty() {
return Ok(None);
}
Ok(Some(format!(
"## macOS Workaround\n\nIf macOS blocks these downloaded, unsigned executables, remove their quarantine attribute manually after verifying and trusting the release. Download the indicated archive into `~/Downloads`; the commands extract it there, remove quarantine recursively from everything in the extracted directory, restore executable permissions, and start the first executable.\n\n{}",
sections.join("\n\n")
)))
}
fn archive_workaround<'a>(
filename: &str,
root: &str,
format: crate::bundle::Format,
executables: impl Iterator<Item = &'a str>,
) -> String {
let filename = shell_quote(filename);
let root = shell_quote(root);
let executables = executables.collect::<Vec<_>>();
let extraction = match format {
crate::bundle::Format::Tgz => "tar -xzf \"$archive\" -C \"$downloads\"",
crate::bundle::Format::Zip => "ditto -x -k \"$archive\" \"$downloads\"",
};
let paths = executables
.iter()
.map(|path| format!("\"$bundle\"/{}", shell_quote(path)))
.collect::<Vec<_>>();
let chmod = if paths.is_empty() {
String::new()
} else {
format!("\nchmod +x {}", paths.join(" "))
};
let launch = paths
.iter()
.zip(executables.iter())
.find(|(_, executable)| {
executable
.rsplit('/')
.next()
.is_some_and(|name| name.ends_with("-gui"))
})
.map(|(path, _)| path)
.or_else(|| paths.first())
.map_or_else(String::new, |path| format!("\n{path}"));
format!(
"downloads=\"$HOME/Downloads\"\narchive=\"$downloads\"/{filename}\n{extraction}\nbundle=\"$downloads\"/{root}\nxattr -rd com.apple.quarantine \"$bundle\"{chmod}{launch}"
)
}
fn shell_quote(value: &str) -> String {
format!("'{}'", value.replace('\'', "'\"'\"'"))
}
#[derive(Debug)]
enum TokenFile {
Plain(String),
Named(BTreeMap<String, String>),
}
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<TokenFile, Error> {
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<String, String> = 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<String, Error> {
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)]
mod tests {
use super::*;
use crate::{
bundle::{Entry, Format},
package::{Artifact, ArtifactContents},
};
use std::{fs, path::Path};
fn write(root: &Path, relative: &str, content: &str) {
@ -611,6 +906,31 @@ mod tests {
}
}
#[test]
fn preparation_options_are_rejected_before_project_acquisition() {
let error = run(Cli::parse_from([
"rust_realease",
"missing-project",
"--commit-preparation",
]))
.unwrap_err();
assert!(error
.to_string()
.contains("Preparation options require --prepare-repo"));
let error = run(Cli::parse_from([
"rust_realease",
"missing-project",
"--prepare-repo",
"--stop-after",
"metadata",
]))
.unwrap_err();
assert!(error
.to_string()
.contains("--prepare-repo is a separate mode"));
}
#[test]
fn release_message_inputs_and_recompile_flag_parse() {
let args = Cli::try_parse_from([
@ -634,12 +954,85 @@ mod tests {
.is_err());
}
#[test]
fn macos_workaround_uses_actual_bundle_and_quarantines_its_complete_tree() {
let artifacts = vec![
Artifact {
target: "aarch64-apple-darwin".into(),
path: "dist/pptx-reporter-aarch64-apple-darwin".into(),
contents: ArtifactContents::Binary,
sha256: "0".repeat(64),
},
Artifact {
target: "aarch64-apple-darwin".into(),
path: "dist/pptx-reporter-portable-aarch64-apple-darwin-v0.1.2.tgz".into(),
contents: ArtifactContents::Bundle {
root: "pptx-reporter-portable-aarch64-apple-darwin-v0.1.2".into(),
format: Format::Tgz,
entries: vec![
Entry {
destination: "pptx-reporter".into(),
sha256: "1".repeat(64),
mode: 0o755,
},
Entry {
destination: "pptx-reporter-gui".into(),
sha256: "2".repeat(64),
mode: 0o755,
},
Entry {
destination: "resources/font.ttf".into(),
sha256: "3".repeat(64),
mode: 0o644,
},
],
},
sha256: "4".repeat(64),
},
];
let message = release_message_with_macos_workaround(Some("Release notes"), &artifacts)
.unwrap()
.unwrap();
assert!(message.starts_with("Release notes\n\n## macOS Workaround"));
assert!(message.contains("tar -xzf \"$archive\" -C \"$downloads\""));
assert!(message.contains("xattr -rd com.apple.quarantine \"$bundle\""));
assert!(message
.contains("chmod +x \"$bundle\"/'pptx-reporter' \"$bundle\"/'pptx-reporter-gui'"));
assert!(message.contains("\n\"$bundle\"/'pptx-reporter-gui'\n```"));
assert!(!message.contains("executable=\"$downloads\""));
}
#[test]
fn macos_workaround_is_not_duplicated_or_added_to_other_targets() {
let linux = Artifact {
target: "x86_64-unknown-linux-gnu".into(),
path: "dist/demo.tgz".into(),
contents: ArtifactContents::Archive {
root: "demo".into(),
binaries: vec!["demo".into()],
},
sha256: "0".repeat(64),
};
assert_eq!(
release_message_with_macos_workaround(Some("Notes"), &[linux]).unwrap(),
Some("Notes".into())
);
let existing = "Notes\n\n## macOS Workaround\n\nCustom instructions";
assert_eq!(
release_message_with_macos_workaround(Some(existing), &[]).unwrap(),
Some(existing.into())
);
}
#[test]
fn token_file_accepts_one_token_and_trailing_newlines() {
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());
@ -647,6 +1040,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 =
@ -657,4 +1068,95 @@ mod tests {
Some(Path::new("secrets/gitea-token"))
);
}
fn endpoint(name: &str, token_env: &str) -> ReleaseEndpoint {
ReleaseEndpoint {
name: name.into(),
provider: crate::config::ReleaseProvider::Gitea,
repository: "https://example.com/team/demo".into(),
token_env: token_env.into(),
}
}
#[test]
fn endpoint_tokens_are_selected_and_validated() {
let primary = endpoint("primary", "RUST_RELEASE_TEST_PRIMARY_TOKEN");
let mirror = endpoint("mirror", "RUST_RELEASE_TEST_MIRROR_TOKEN");
let plain = TokenFile::Plain("plain-secret".into());
assert_eq!(
token_for_endpoint(Some(&plain), &primary, 1).unwrap(),
"plain-secret"
);
assert!(token_for_endpoint(Some(&plain), &primary, 2).is_err());
let named = TokenFile::Named(BTreeMap::from([
("primary".into(), "one".into()),
("mirror".into(), "two".into()),
]));
assert_eq!(token_for_endpoint(Some(&named), &mirror, 2).unwrap(), "two");
assert!(token_for_endpoint(Some(&TokenFile::Named(BTreeMap::new())), &primary, 1).is_err());
assert!(validate_token_keys(Some(&named), &[primary.clone(), mirror]).is_ok());
assert!(validate_token_keys(Some(&named), &[primary]).is_err());
assert!(validate_token_keys(None, &[]).is_ok());
}
#[test]
fn token_file_rejects_non_files_and_invalid_json_values() {
let temp = tempfile::tempdir().unwrap();
assert!(read_token_file(temp.path()).is_err());
let path = temp.path().join("tokens.json");
for invalid in [
"{",
"{}",
r#"{"primary":""}"#,
"{\"primary\":\"line\\nbreak\"}",
] {
fs::write(&path, invalid).unwrap();
assert!(read_token_file(&path).is_err(), "accepted {invalid:?}");
}
}
#[test]
fn macos_workaround_covers_zip_archive_binary_and_shell_escaping() {
let bundle = Artifact {
target: "x86_64-apple-darwin".into(),
path: "dist/demo portable.zip".into(),
contents: ArtifactContents::Bundle {
root: "demo's bundle".into(),
format: Format::Zip,
entries: vec![],
},
sha256: "0".repeat(64),
};
let archive = Artifact {
target: "aarch64-apple-darwin".into(),
path: "dist/demo.tgz".into(),
contents: ArtifactContents::Archive {
root: "demo".into(),
binaries: vec!["demo".into()],
},
sha256: "1".repeat(64),
};
let binary = Artifact {
target: "aarch64-apple-darwin".into(),
path: "dist/demo-bin".into(),
contents: ArtifactContents::Binary,
sha256: "2".repeat(64),
};
let message = release_message_with_macos_workaround(None, &[bundle])
.unwrap()
.unwrap();
assert!(message.contains("ditto -x -k"));
assert!(message.contains("demo'\"'\"'s bundle"));
let message = release_message_with_macos_workaround(None, &[archive])
.unwrap()
.unwrap();
assert!(message.contains("tar -xzf"));
let message = release_message_with_macos_workaround(None, &[binary])
.unwrap()
.unwrap();
assert!(message.contains("xattr -d com.apple.quarantine \"$executable\""));
}
}

View File

@ -39,6 +39,16 @@ struct Apple {
struct Release {
#[serde(rename = "tag-prefix")]
tag_prefix: Option<String>,
endpoints: Option<Vec<FileReleaseEndpoint>>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
struct FileReleaseEndpoint {
name: String,
provider: ReleaseProvider,
repository: String,
token_env: Option<String>,
}
#[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<ReleaseEndpoint>,
pub package_format: PackageFormat,
pub repository_fallbacks: Vec<RepositoryFallback>,
}
#[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<Release>, later: Option<Release>) {
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<Config, Error> {
))
}
};
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<Config, Error> {
.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());
}
}

View File

@ -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<Self, Error> {
if !repository.starts_with("https://") {
return Err(Error(
@ -121,6 +125,7 @@ impl Gitea {
draft: bool,
prerelease: bool,
message: Option<&str>,
replace_existing_message: bool,
) -> Result<RemoteRelease, Error> {
let encoded = encode(tag);
let remote = self
@ -151,7 +156,7 @@ impl Gitea {
};
let release: RemoteRelease = serde_json::from_value(release)
.map_err(|_| Error("Gitea release response is missing id or draft state.".into()))?;
if existed {
if existed && replace_existing_message {
if let Some(message) = message {
self.request(
Method::PATCH,
@ -362,7 +367,7 @@ mod tests {
]);
let api = Gitea::with_token(&format!("{host}/gitea/team/demo"), "test-token").unwrap();
let release = api
.ensure_release("v0.1.0", "abc", false, true, Some("Release notes"))
.ensure_release("v0.1.0", "abc", false, true, Some("Release notes"), false)
.unwrap();
assert_eq!(release.id, 7);
let requests = handle.join().unwrap();
@ -391,7 +396,7 @@ mod tests {
]);
let api = Gitea::with_token(&format!("{host}/gitea/team/demo"), "test-token").unwrap();
assert_eq!(
api.ensure_release("v0.1.0", "abc", false, false, Some("Updated notes"))
api.ensure_release("v0.1.0", "abc", false, false, Some("Updated notes"), true,)
.unwrap()
.id,
9
@ -456,7 +461,7 @@ mod tests {
}]);
let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap();
let error = api
.ensure_release("v0.1.0", "abc", false, false, None)
.ensure_release("v0.1.0", "abc", false, false, None, false)
.unwrap_err()
.to_string();
assert!(error.contains("HTTP 401"));
@ -478,7 +483,7 @@ mod tests {
}]);
let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap();
let error = api
.ensure_release("v0.1.0", "abc", false, false, None)
.ensure_release("v0.1.0", "abc", false, false, None, false)
.unwrap_err()
.to_string();
assert!(error.contains(expected), "{error}");
@ -546,7 +551,7 @@ mod tests {
}]);
let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap();
assert!(api
.ensure_release("v0.1.0", "abc", false, false, None)
.ensure_release("v0.1.0", "abc", false, false, None, false)
.unwrap_err()
.to_string()
.contains("invalid JSON"));

633
src/github.rs Normal file
View File

@ -0,0 +1,633 @@
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, Error> {
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<Self, Error> {
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<Value>,
missing_ok: bool,
) -> Result<Option<Value>, 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<RemoteRelease, Error> {
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<bool, Error> {
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<Vec<Asset>, 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<BTreeMap<String, String>, 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<Reply>) -> (String, thread::JoinHandle<Vec<Vec<u8>>>) {
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::<usize>().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());
}
}
#[test]
fn updates_existing_annotated_release_and_replaces_colliding_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":"tag","sha":"tag-object"}}"#,
},
Reply {
method: "GET",
path: "/repos/team/demo/git/tags/tag-object",
status: 200,
body: r#"{"object":{"sha":"abc"}}"#,
},
Reply {
method: "GET",
path: "/repos/team/demo/releases/tags/v1.0.0",
status: 200,
body: r#"{"id":7,"draft":false}"#,
},
Reply {
method: "PATCH",
path: "/repos/team/demo/releases/7",
status: 200,
body: "",
},
Reply {
method: "GET",
path: "/repos/team/demo",
status: 200,
body: r#"{"private":true}"#,
},
Reply {
method: "GET",
path: "/repos/team/demo/releases/7/assets?per_page=100",
status: 200,
body: r#"[{"id":11,"name":"asset.bin"}]"#,
},
Reply {
method: "DELETE",
path: "/repos/team/demo/releases/assets/11",
status: 204,
body: "",
},
Reply {
method: "POST",
path: "/repos/team/demo/releases/7/assets?name=asset.bin",
status: 201,
body: "{}",
},
]);
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("new notes"), true)
.unwrap();
assert_eq!(release.id, 7);
assert!(api.repository_private().unwrap());
let _ = api.download_client();
let temp = tempfile::tempdir().unwrap();
let asset = temp.path().join("asset.bin");
std::fs::write(&asset, b"replacement").unwrap();
api.upload(release.id, &[asset.as_path()], true).unwrap();
let bodies = handle.join().unwrap();
assert!(String::from_utf8_lossy(&bodies[3]).contains("new notes"));
assert_eq!(bodies[7], b"replacement");
}
#[test]
fn reports_missing_mismatched_and_malformed_github_responses() {
let cases = [
(404, "{}", "missing"),
(
200,
r#"{"object":{"type":"commit","sha":"other"}}"#,
"differs",
),
(200, "not json", "invalid JSON"),
(500, "{}", "HTTP 500"),
];
for (status, body, expected) in cases {
let (host, handle) = server(vec![Reply {
method: "GET",
path: "/repos/team/demo/git/ref/tags/v1.0.0",
status,
body,
}]);
let api =
Github::with_origins("https://github.com/team/demo", "test-token", &host, &host)
.unwrap();
let error = api
.ensure_release("v1.0.0", "abc", false, false, None, false)
.unwrap_err();
assert!(error.to_string().contains(expected), "{error}");
handle.join().unwrap();
}
}
#[test]
fn reports_malformed_release_repository_and_asset_responses() {
let (host, handle) = server(vec![Reply {
method: "GET",
path: "/repos/team/demo/git/ref/tags/v1.0.0",
status: 200,
body: r#"{"object":{"type":"tag"}}"#,
}]);
let api = Github::with_origins("https://github.com/team/demo", "test-token", &host, &host)
.unwrap();
assert!(api
.ensure_release("v1.0.0", "abc", false, false, None, false)
.unwrap_err()
.to_string()
.contains("object SHA"));
handle.join().unwrap();
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: 200,
body: "{}",
},
]);
let api = Github::with_origins("https://github.com/team/demo", "test-token", &host, &host)
.unwrap();
assert!(api
.ensure_release("v1.0.0", "abc", false, false, None, false)
.unwrap_err()
.to_string()
.contains("missing id"));
handle.join().unwrap();
for (path, operation, expected) in [
("/repos/team/demo", "repository", "visibility"),
(
"/repos/team/demo/releases/7/assets?per_page=100",
"assets",
"invalid",
),
] {
let (host, handle) = server(vec![Reply {
method: "GET",
path,
status: 200,
body: "{}",
}]);
let api =
Github::with_origins("https://github.com/team/demo", "test-token", &host, &host)
.unwrap();
let error = if operation == "repository" {
api.repository_private().unwrap_err()
} else {
api.asset_urls(7).unwrap_err()
};
assert!(error.to_string().contains(expected), "{error}");
handle.join().unwrap();
}
}
#[test]
fn refuses_asset_collision_and_reports_upload_http_failure() {
let temp = tempfile::tempdir().unwrap();
let asset = temp.path().join("asset.bin");
std::fs::write(&asset, b"asset").unwrap();
let (host, handle) = server(vec![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();
assert!(api.upload(7, &[asset.as_path()], false).is_err());
handle.join().unwrap();
let (host, handle) = server(vec![
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: 500,
body: "{}",
},
]);
let api = Github::with_origins("https://github.com/team/demo", "test-token", &host, &host)
.unwrap();
let error = api.upload(7, &[asset.as_path()], false).unwrap_err();
assert!(error.to_string().contains("HTTP 500"), "{error}");
handle.join().unwrap();
}
}

View File

@ -15,6 +15,15 @@ pub fn verify_path(path: &Path, target: &str, minimum: &str) -> Result<(), Error
}
pub fn verify(bytes: &[u8], target: &str, minimum: &str) -> Result<(), Error> {
verify_policy(bytes, target, minimum, true)
}
/// Bundled runtime tools may support an older macOS version than the application.
pub fn verify_compatible(bytes: &[u8], target: &str, minimum: &str) -> Result<(), Error> {
verify_policy(bytes, target, minimum, false)
}
fn verify_policy(bytes: &[u8], target: &str, minimum: &str, exact: bool) -> Result<(), Error> {
if bytes.len() < 32 || bytes[..4] != MH_MAGIC_64_LE {
return Err(Error(
"expected a little-endian 64-bit Mach-O executable".into(),
@ -86,9 +95,9 @@ pub fn verify(bytes: &[u8], target: &str, minimum: &str) -> Result<(), Error> {
return Err(Error("Mach-O load command count/size mismatch".into()));
}
let found = found_minimum.ok_or_else(|| Error("Mach-O has no LC_BUILD_VERSION".into()))?;
if found != expected_minimum {
if (exact && found != expected_minimum) || (!exact && found > expected_minimum) {
return Err(Error(format!(
"Mach-O deployment minimum {} does not match configured {minimum}",
"Mach-O deployment minimum {} is incompatible with configured {minimum}",
display_version(found)
)));
}
@ -165,6 +174,8 @@ mod tests {
fn validates_architecture_deployment_minimum_and_system_dependencies() {
let arm = fixture(0x0100_000c, 13 << 16, "/usr/lib/libSystem.B.dylib");
verify(&arm, "aarch64-apple-darwin", "13.0").unwrap();
verify_compatible(&arm, "aarch64-apple-darwin", "14.0").unwrap();
assert!(verify_compatible(&arm, "aarch64-apple-darwin", "12.0").is_err());
assert!(verify(&arm, "x86_64-apple-darwin", "13.0").is_err());
assert!(verify(&arm, "aarch64-apple-darwin", "14.0").is_err());

View File

@ -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;

View File

@ -19,7 +19,11 @@ pub fn binary_filename(binary: &str, target: &str) -> String {
}
pub fn binary_asset_name(binary: &str, target: &str, version: &str) -> String {
binary_filename(&format!("{binary}-{target}-v{version}"), target)
if target.ends_with("-apple-darwin") {
binary_filename(&format!("{binary}-{target}"), target)
} else {
binary_filename(&format!("{binary}-{target}-v{version}"), target)
}
}
pub fn binstall_metadata(prefix: &str) -> [(&'static str, String); 3] {
@ -52,6 +56,10 @@ mod tests {
binary_asset_name("demo", "x86_64-pc-windows-gnu", "0.3.1"),
"demo-x86_64-pc-windows-gnu-v0.3.1.exe"
);
assert_eq!(
binary_asset_name("demo", "aarch64-apple-darwin", "0.3.1"),
"demo-aarch64-apple-darwin"
);
let metadata = binstall_metadata("release-");
assert_eq!(
metadata[0].1,

View File

@ -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(),
};

83
src/release.rs Normal file
View File

@ -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<Self, Error> {
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<RemoteRelease, Error> {
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<bool, Error> {
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<Option<BTreeMap<String, String>>, Error> {
match self {
Self::Gitea(_) => Ok(None),
Self::Github(api) => api.asset_urls(release_id).map(Some),
}
}
}

View File

@ -3,20 +3,33 @@ use regex::Regex;
use std::{path::Path, process::Command, sync::OnceLock};
pub struct Runner {
token_env: String,
token_envs: Vec<String>,
secrets: Vec<String>,
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<Item = String>) -> 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]");

View File

@ -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,
@ -152,9 +153,53 @@ pub fn verify_apple_artifact(
}
Ok(())
}
ArtifactContents::Bundle { .. } => Err(Error(
"Apple named bundles require an explicit Mach-O deployment policy".into(),
)),
ArtifactContents::Bundle {
root,
format,
entries,
} => {
// Recheck the declared tree, hashes and modes before interpreting executable bytes.
crate::bundle::verify(path, root, *format, entries)?;
let mut expected = entries
.iter()
.filter(|entry| entry.mode & 0o111 != 0)
.map(|entry| format!("{root}/{}", entry.destination))
.collect::<std::collections::BTreeSet<_>>();
let inspect = |reader: &mut dyn Read| -> Result<(), Error> {
let mut bytes = Vec::new();
reader
.read_to_end(&mut bytes)
.map_err(|e| Error(format!("cannot read Apple bundle executable: {e}")))?;
crate::macho::verify_compatible(&bytes, &artifact.target, deployment_target)
};
match format {
crate::bundle::Format::Zip => {
let mut archive = zip::ZipArchive::new(File::open(path)?)
.map_err(|e| Error(format!("invalid Apple ZIP: {e}")))?;
for name in &expected {
let mut entry = archive
.by_name(name)
.map_err(|e| Error(format!("missing Apple executable {name}: {e}")))?;
inspect(&mut entry)?;
}
expected.clear();
}
crate::bundle::Format::Tgz => {
let mut archive = tar::Archive::new(GzDecoder::new(File::open(path)?));
for entry in archive.entries()? {
let mut entry = entry?;
let name = entry.path()?.to_string_lossy().into_owned();
if expected.remove(&name) {
inspect(&mut entry)?;
}
}
}
}
if !expected.is_empty() {
return Err(Error("Missing Apple bundle executables".into()));
}
Ok(())
}
}
}
@ -192,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<String, String>>,
) -> Result<(), Error> {
let downloads = tempfile::Builder::new()
.prefix("rust-realease-download-")
@ -214,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 {

View File

@ -264,13 +264,23 @@ pathlib.Path(c['manifest_path']).write_text(json.dumps(dict(schema_version=1,tar
executable(
&bin.join("cross"),
r#"#!/usr/bin/env python3
import sys,pathlib,shutil,json,os
import sys,pathlib,json,os
a=sys.argv
assert '--message-format=json-render-diagnostics' in a
out=pathlib.Path(a[a.index('--target-dir')+1])/'actual output é'
out.mkdir(parents=True)
binary=out/'demo'
shutil.copyfile('/bin/true',binary)
deps=out/'deps'
deps.mkdir()
backing=deps/'demo-build-id'
elf=bytearray(64)
elf[:4]=b'\x7fELF'
elf[4]=2
elf[5]=1
elf[18:20]=(62).to_bytes(2,'little')
backing.write_bytes(elf)
backing.chmod(0o751)
os.link(backing,binary)
print(json.dumps(dict(reason='compiler-artifact',target=dict(name='demo',kind=['bin']),executable="/target/actual output é/demo")))
with open(os.environ['CROSS_LOG'],'a') as f: f.write('build\n')
"#,
@ -343,6 +353,7 @@ with open(os.environ['CROSS_LOG'],'a') as f: f.write('build\n')
}
#[test]
#[cfg(target_os = "linux")]
fn mixed_cross_and_apple_compile_only_routes_and_packages_eight_assets() {
let temporary = TempDir::new().unwrap();
let project = temporary.path().join("mixed checkout with spaces");
@ -466,7 +477,7 @@ with open(os.environ['CROSS_LOG'],'a') as stream: stream.write('zig:'+target+'\n
assert!(artifact_dir
.join(format!("demo-{target}-v2.3.4.tgz"))
.is_file());
let binary = artifact_dir.join(format!("demo-{target}-v2.3.4"));
let binary = artifact_dir.join(format!("demo-{target}"));
assert!(binary.is_file());
assert_ne!(
fs::metadata(binary).unwrap().permissions().mode() & 0o111,