Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/github-release-package-list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
cargo/sampo-github-action: minor
---

Added a package list to the GitHub Action's `create-github-release` input, to create GitHub Releases for those packages only.
6 changes: 3 additions & 3 deletions crates/sampo-github-action/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:

### Creating GitHub Releases and Discussions

Set the `create-github-release` input to `true` to create a GitHub Release for each new tag, with notes generated from the changesets it includes. If a release fails, rerunning the job completes the missing tags, releases, and assets.
Set the `create-github-release` input to `true` to create a GitHub Release for each new tag, with notes generated from the changesets it includes. If a release fails, rerunning the job completes the missing tags, releases, and assets. To release only some packages, such as one app in a monorepo, set it to a comma-separated list of package names instead (e.g., `my-app`).

To also open a GitHub Discussion for each release, set `open-discussion` to `true` (all packages) or a comma-separated list of package names (e.g., `sampo,sampo-github-action`). Use `discussion-category` to specify the target category.

Expand Down Expand Up @@ -130,8 +130,8 @@ The action supports the following inputs:
- `pr-title`: title of the release PR that `auto` prepares (defaults to `Release (<current-branch>)`).
- `stabilize-pr-branch`: working branch used for the stabilize PR that `auto` prepares (defaults to `stabilize/<current-branch>` with `/` replaced by `-`).
- `stabilize-pr-title`: title of the stabilize PR that `auto` prepares (defaults to `Release stable (<current-branch>)`).
- `create-github-release`: if `true`, create GitHub Releases for new tags.
- `open-discussion`: create a GitHub Discussion for released packages. Accepts `true` (all packages), `false` (none, default), or a comma-separated list of package names to filter (e.g., `sampo,sampo-github-action`). Requires `create-github-release: true`.
- `create-github-release`: create GitHub Releases for new tags. Accepts `true` (all packages), `false` (none, default), or a comma-separated list of package names to filter (e.g., `my-app`).
- `open-discussion`: create a GitHub Discussion for released packages. Accepts `true` (all packages), `false` (none, default), or a comma-separated list of package names to filter (e.g., `sampo,sampo-github-action`). Only applies to the releases `create-github-release` creates.
- `discussion-category`: preferred Discussions category slug when creating releases.
- `release-assets`: comma or newline separated list of paths or glob patterns for pre-built artifacts to upload when creating GitHub releases. Use `=>` to rename matches (e.g. `dist/*.zip => my-tool.zip`). Placeholders `{{tag}}`, `{{crate}}`, and `{{version}}` are available.
- `github-token`: GitHub token to create/update PRs (defaults to `GITHUB_TOKEN` env).
Expand Down
2 changes: 1 addition & 1 deletion crates/sampo-github-action/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ inputs:
description: "Title for the Stabilize PR when auto prepares it"
required: false
create-github-release:
description: "If true, create GitHub releases for new tags when publishing"
description: "Create GitHub releases for new tags when publishing. Either 'true' (all packages), 'false' (none), or a comma-separated list of package names (e.g. 'my-app')"
required: false
default: "false"
open-discussion:
Expand Down
205 changes: 98 additions & 107 deletions crates/sampo-github-action/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ use std::process::ExitCode;

#[derive(Debug, Clone)]
struct GitHubReleaseOptions {
/// Create GitHub releases for newly created tags during publish
create_github_release: bool,
/// Packages whose new tags get a GitHub release during publish
create_github_release: PackageFilter,
/// Filter for which packages should have GitHub Discussions opened
open_discussion: DiscussionFilter,
open_discussion: PackageFilter,
/// Preferred Discussions category slug (e.g., "announcements")
discussion_category: Option<String>,
/// Release asset patterns provided by the workflow (already-built artifacts)
Expand All @@ -35,7 +35,7 @@ struct GitHubReleaseOptions {
impl GitHubReleaseOptions {
fn from_config(config: &Config) -> Self {
Self {
create_github_release: config.create_github_release,
create_github_release: config.create_github_release.clone(),
open_discussion: config.open_discussion.clone(),
discussion_category: config.discussion_category.clone(),
asset_specs: parse_asset_specs(config.release_assets.as_deref()),
Expand Down Expand Up @@ -73,55 +73,56 @@ impl Mode {
}
}

/// Filter for which packages should have GitHub Discussions opened.
///
/// Supports:
/// - `All`: Open discussions for all packages (when input is "true")
/// - `None`: Never open discussions (when input is "false" or empty)
/// - `Packages`: Only open discussions for specific packages (comma-separated list)
/// Packages selected by an input that takes `true`, `false`, or a package list, such as
/// `create-github-release` and `open-discussion`.
#[derive(Debug, Clone)]
enum DiscussionFilter {
/// Open discussions for all released packages
enum PackageFilter {
All,
/// Never open discussions
None,
/// Open discussions only for these specific package names
Packages(Vec<String>),
}

impl DiscussionFilter {
/// Parse the INPUT_OPEN_DISCUSSION environment variable value.
///
impl PackageFilter {
/// Accepts:
/// - "true" -> All
/// - "false" or empty -> None
/// - "true" or "1" -> All
/// - "false", "0" or empty -> None
/// - "pkg1,pkg2,pkg3" -> Packages(["pkg1", "pkg2", "pkg3"])
fn parse(input: &str) -> Self {
let trimmed = input.trim();
if trimmed.is_empty() || trimmed.eq_ignore_ascii_case("false") {
DiscussionFilter::None
} else if trimmed.eq_ignore_ascii_case("true") {
DiscussionFilter::All
if trimmed.is_empty() || trimmed == "0" || trimmed.eq_ignore_ascii_case("false") {
PackageFilter::None
} else if trimmed == "1" || trimmed.eq_ignore_ascii_case("true") {
PackageFilter::All
} else {
let packages: Vec<String> = trimmed
.split(',')
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect();
if packages.is_empty() {
DiscussionFilter::None
PackageFilter::None
} else {
DiscussionFilter::Packages(packages)
PackageFilter::Packages(packages)
}
}
}

/// Check if a discussion should be opened for a given package name.
fn should_open_for(&self, package_name: &str) -> bool {
fn is_enabled(&self) -> bool {
!matches!(self, PackageFilter::None)
}

fn matches(&self, package_name: &str) -> bool {
match self {
PackageFilter::All => true,
PackageFilter::None => false,
PackageFilter::Packages(packages) => packages.iter().any(|p| p == package_name),
}
}

fn matches_tag(&self, tag: &str, config: Option<&SampoConfig>) -> bool {
match self {
DiscussionFilter::All => true,
DiscussionFilter::None => false,
DiscussionFilter::Packages(packages) => packages.iter().any(|p| p == package_name),
PackageFilter::All => true,
_ => parse_tag_with_config(tag, config).is_some_and(|(name, _)| self.matches(&name)),
}
}
}
Expand Down Expand Up @@ -184,11 +185,11 @@ struct Config {
/// Title to use for the Stabilize PR (default: 'Release stable (<branch>)')
stabilize_pr_title: Option<String>,

/// Create GitHub releases for newly created tags during publish
create_github_release: bool,
/// Packages whose new tags get a GitHub release during publish
create_github_release: PackageFilter,

/// Filter for which packages should have GitHub Discussions opened
open_discussion: DiscussionFilter,
open_discussion: PackageFilter,

/// Preferred Discussions category slug (e.g., "announcements")
discussion_category: Option<String>,
Expand Down Expand Up @@ -270,12 +271,12 @@ impl Config {
.filter(|v| !v.is_empty());

let create_github_release = std::env::var("INPUT_CREATE_GITHUB_RELEASE")
.map(|v| v.eq_ignore_ascii_case("true") || v.trim() == "1")
.unwrap_or(false);
.map(|v| PackageFilter::parse(&v))
.unwrap_or(PackageFilter::None);

let open_discussion = std::env::var("INPUT_OPEN_DISCUSSION")
.map(|v| DiscussionFilter::parse(&v))
.unwrap_or(DiscussionFilter::None);
.map(|v| PackageFilter::parse(&v))
.unwrap_or(PackageFilter::None);

let discussion_category = std::env::var("INPUT_DISCUSSION_CATEGORY")
.ok()
Expand Down Expand Up @@ -448,7 +449,7 @@ fn execute_operations(
branch
);
let github_options = GitHubReleaseOptions::from_config(config);
let github_client = if github_options.create_github_release {
let github_client = if github_options.create_github_release.is_enabled() {
Some(create_github_client()?)
} else {
None
Expand All @@ -471,7 +472,7 @@ fn execute_operations(
}
Mode::Publish => {
let github_options = GitHubReleaseOptions::from_config(config);
let github_client = if github_options.create_github_release {
let github_client = if github_options.create_github_release.is_enabled() {
Some(create_github_client()?)
} else {
None
Expand Down Expand Up @@ -855,6 +856,12 @@ fn push_tags_and_create_releases(
published: &mut bool,
) -> Vec<String> {
let mut failures = Vec::new();
let config = SampoConfig::load(workspace).ok();
let gets_release = |tag: &String| {
github_options
.create_github_release
.matches_tag(tag, config.as_ref())
};

if dry_run {
if !new_tags.is_empty() {
Expand All @@ -865,12 +872,14 @@ fn push_tags_and_create_releases(
for tag in new_tags {
println!(" - {}", tag);
}
if github_options.create_github_release {
let release_tags: Vec<&String> =
new_tags.iter().filter(|tag| gets_release(tag)).collect();
if !release_tags.is_empty() {
println!(
"Would create {} GitHub releases (skipped in dry-run mode)",
new_tags.len()
release_tags.len()
);
for tag in new_tags {
for tag in release_tags {
println!(" - {}", tag);
}
}
Expand All @@ -890,11 +899,11 @@ fn push_tags_and_create_releases(
}
*published |= !release_tags.is_empty();

let Some(client) = github_client.filter(|_| github_options.create_github_release) else {
let Some(client) = github_client.filter(|_| github_options.create_github_release.is_enabled())
else {
return failures;
};

let config = SampoConfig::load(workspace).ok();
match git::tags_at_head(workspace) {
Ok(tags) => {
let earlier_tags = tags.into_iter().filter(|tag| {
Expand All @@ -904,6 +913,7 @@ fn push_tags_and_create_releases(
}
Err(e) => failures.push(format!("tags at HEAD: {}", e)),
}
release_tags.retain(gets_release);

for tag in &release_tags {
println!("Creating GitHub release for {}", tag);
Expand Down Expand Up @@ -1014,9 +1024,7 @@ fn create_github_release_for_tag(
// Optionally open a Discussion for this release (based on filter)
if published_now
&& let Some((package_name, _version)) = parse_tag_with_config(tag, config.as_ref())
&& github_options
.open_discussion
.should_open_for(&package_name)
&& github_options.open_discussion.matches(&package_name)
&& let Err(e) = github_client.create_discussion(
tag,
&body,
Expand Down Expand Up @@ -1411,8 +1419,8 @@ mod tests {
pr_title: None,
stabilize_pr_branch: None,
stabilize_pr_title: None,
create_github_release: false,
open_discussion: DiscussionFilter::None,
create_github_release: PackageFilter::None,
open_discussion: PackageFilter::None,
discussion_category: None,
release_assets: None,
};
Expand Down Expand Up @@ -1824,84 +1832,67 @@ mod tests {
}

#[test]
fn test_discussion_filter_parsing() {
// "false" or empty -> None
assert!(matches!(
DiscussionFilter::parse("false"),
DiscussionFilter::None
));
assert!(matches!(
DiscussionFilter::parse("FALSE"),
DiscussionFilter::None
));
assert!(matches!(
DiscussionFilter::parse(""),
DiscussionFilter::None
));
assert!(matches!(
DiscussionFilter::parse(" "),
DiscussionFilter::None
));

// "true" -> All
assert!(matches!(
DiscussionFilter::parse("true"),
DiscussionFilter::All
));
assert!(matches!(
DiscussionFilter::parse("TRUE"),
DiscussionFilter::All
));
assert!(matches!(
DiscussionFilter::parse("True"),
DiscussionFilter::All
));
fn test_package_filter_parsing() {
// "false", "0" or empty -> None
assert!(matches!(PackageFilter::parse("0"), PackageFilter::None));
assert!(matches!(PackageFilter::parse(" , "), PackageFilter::None));
assert!(matches!(PackageFilter::parse("false"), PackageFilter::None));
assert!(matches!(PackageFilter::parse("FALSE"), PackageFilter::None));
assert!(matches!(PackageFilter::parse(""), PackageFilter::None));
assert!(matches!(PackageFilter::parse(" "), PackageFilter::None));

// "true" or "1" -> All
assert!(matches!(PackageFilter::parse(" 1 "), PackageFilter::All));
assert!(matches!(PackageFilter::parse("true"), PackageFilter::All));
assert!(matches!(PackageFilter::parse("TRUE"), PackageFilter::All));
assert!(matches!(PackageFilter::parse("True"), PackageFilter::All));

// Package list
match DiscussionFilter::parse("sampo,sampo-github-action") {
DiscussionFilter::Packages(pkgs) => {
match PackageFilter::parse("sampo,sampo-github-action") {
PackageFilter::Packages(pkgs) => {
assert_eq!(pkgs, vec!["sampo", "sampo-github-action"]);
}
_ => panic!("Expected Packages variant"),
}

// Package list with whitespace
match DiscussionFilter::parse(" pkg-a , pkg-b , pkg-c ") {
DiscussionFilter::Packages(pkgs) => {
match PackageFilter::parse(" pkg-a , pkg-b , pkg-c ") {
PackageFilter::Packages(pkgs) => {
assert_eq!(pkgs, vec!["pkg-a", "pkg-b", "pkg-c"]);
}
_ => panic!("Expected Packages variant"),
}

// Single package
match DiscussionFilter::parse("my-crate") {
DiscussionFilter::Packages(pkgs) => {
match PackageFilter::parse("my-crate") {
PackageFilter::Packages(pkgs) => {
assert_eq!(pkgs, vec!["my-crate"]);
}
_ => panic!("Expected Packages variant"),
}
}

#[test]
fn test_discussion_filter_should_open_for() {
// All opens for any package
let all = DiscussionFilter::All;
assert!(all.should_open_for("sampo"));
assert!(all.should_open_for("any-package"));

// None never opens
let none = DiscussionFilter::None;
assert!(!none.should_open_for("sampo"));
assert!(!none.should_open_for("any-package"));

// Packages only opens for listed packages
let packages = DiscussionFilter::Packages(vec![
"sampo".to_string(),
"sampo-github-action".to_string(),
]);
assert!(packages.should_open_for("sampo"));
assert!(packages.should_open_for("sampo-github-action"));
assert!(!packages.should_open_for("sampo-core"));
assert!(!packages.should_open_for("sampo-github-bot"));
fn test_package_filter_matches() {
let all = PackageFilter::All;
assert!(all.matches("sampo"));
assert!(all.matches("any-package"));

let none = PackageFilter::None;
assert!(!none.matches("sampo"));
assert!(!none.matches("any-package"));

let packages =
PackageFilter::Packages(vec!["sampo".to_string(), "sampo-github-action".to_string()]);
assert!(packages.matches("sampo"));
assert!(packages.matches("sampo-github-action"));
assert!(!packages.matches("sampo-core"));
assert!(!packages.matches("sampo-github-bot"));

assert!(packages.matches_tag("sampo-v0.4.0", None));
assert!(!packages.matches_tag("sampo-core-v0.4.0", None));
assert!(!packages.matches_tag("not a tag", None));
assert!(all.matches_tag("not a tag", None));
assert!(!none.matches_tag("sampo-v0.4.0", None));
}
}
Loading