use std::fmt::Write;
use std::path::{Path, PathBuf};
use clap::{Args, Subcommand};
use oakum::config::PrStatus;
use oakum::state::{ReleaseState, RenderTarget};
use serde_json::Value;
use super::changelog::{self, ReleaseNote};
use super::config::{load_config, LoadedConfig};
use super::git::{Git, Op};
use super::github::{self, FileAddition, FileChanges, FileDeletion, Look};
use super::release_state;
use super::render;
use super::repository;
use super::stale;
use super::template::load_template_body;
use super::version::{self, VersionArgs, VersionWritePlan};
use super::CliError;
use super::{deliver_out, deliver_report, say_err, say_out};
pub(super) const VERSION_BRANCH: &str = "oakum/version-packages";
pub(super) const VERSION_PR_AUTHOR: &str = "github-actions[bot]";
const VERSION_HEADLINE: &str = "chore(release): version packages";
const DEFAULT_TITLE: &str = VERSION_HEADLINE;
pub(super) const DEFAULT_COMMIT: &str = VERSION_HEADLINE;
#[derive(Debug, Args)]
pub(super) struct CiArgs {
#[command(subcommand)]
command: CiCommand,
}
#[derive(Debug, Subcommand)]
enum CiCommand {
#[command(
after_help = "A run that versions anything writes one commit and force-updates `oakum/version-packages` to it, so a commit pushed to that branch by hand is replaced by the next run."
)]
VersionPr(VersionArgs),
PrStatus(PrStatusArgs),
PostComment(PostCommentArgs),
}
#[derive(Debug, Args)]
struct PrStatusArgs {
#[arg(long, value_name = "REF", help = super::FROM_PLAN)]
from: Option<String>,
#[arg(long, value_name = "DIR")]
emit_comment: Option<PathBuf>,
}
#[derive(Debug, Args)]
struct PostCommentArgs {
#[arg(value_name = "DIR")]
dir: PathBuf,
}
pub(super) fn run(args: &CiArgs) -> Result<(), CliError> {
match &args.command {
CiCommand::VersionPr(args) => run_version_pr(args),
CiCommand::PrStatus(args) => run_pr_status(args),
CiCommand::PostComment(args) => run_post_comment(args),
}
}
fn run_pr_status(args: &PrStatusArgs) -> Result<(), CliError> {
if let Some(dir) = args.emit_comment.as_deref() {
reset_emitted(dir)?;
}
let repo = repository::discover()?;
let config = load_config(&repo)?;
if config.is_default() {
say_err(super::config::DEFAULTS_NOTE);
}
let channels = config.pr_status();
let emit = args.emit_comment.as_deref();
if channels == PrStatus::None {
say_err(
"pr-status is set to `none`, so no coverage comment and no job summary were written.",
);
if let Some(dir) = emit {
return emit_no_comment(dir);
}
clear_stale_comment(&repo);
return Ok(());
}
if on_version_packages_branch() {
say_err(&format!(
"this is the version pull request, which carries the plan in its body, so {} written.",
nothing_was(channels)
));
if let Some(dir) = emit {
emit_no_comment(dir)?;
} else if matches!(channels, PrStatus::Comment | PrStatus::Both) {
clear_stale_comment(&repo);
}
return Ok(());
}
let want_comment = matches!(channels, PrStatus::Comment | PrStatus::Both);
let (state, next) = match pr_status_state(&repo, &config, args.from.as_deref()) {
Ok(found) => found,
Err(err) => {
if want_comment {
mark_stale(&repo, emit, &err.detail());
} else if let Some(dir) = emit {
if let Err(also) = emit_no_comment(dir) {
say_err(&format!("could not emit the no-comment marker: {also}"));
}
}
return Err(err);
}
};
let want_summary = matches!(channels, PrStatus::Summary | PrStatus::Both);
let Some(comment) = render::render_comment(&state, Some(&next)) else {
say_err(&format!(
"nothing to post: no package these commits touch plans a release or lacks covering intent, so {} written.",
nothing_was(channels)
));
if let Some(dir) = emit {
emit_no_comment(dir)?;
} else if want_comment {
clear_stale_comment(&repo);
}
return Ok(());
};
let comment = with_provenance(&comment, &Provenance::of_this_run(&repo));
let summary = render::render_summary(&state, Some(&next));
if want_summary {
write_step_summary(&summary)?;
}
if let Some(dir) = emit {
if !want_comment {
say_err(
"pr-status is set to `summary`, so the plan went to the job summary and --emit-comment wrote no comment.",
);
return emit_no_comment(dir);
}
return emit_comment_file(dir, &comment);
}
if !want_comment {
return Ok(());
}
post_or_degrade(&repo, &comment, want_summary, &summary)
}
fn post_or_degrade(
repo: &repository::Repository,
comment: &str,
want_summary: bool,
summary: &str,
) -> Result<(), CliError> {
match post_pr_comment(repo, comment) {
Ok(None) => Ok(()),
Ok(Some(left)) => {
say_err(&left);
Ok(())
}
Err(CommentBlocked::Forbidden) => {
degrade_to_summary(
"comment requested but this run has no write permission (fork pull request); wrote the plan to the job summary instead.",
want_summary,
summary,
)
}
Err(CommentBlocked::NoToken) => {
degrade_to_summary(
"comment requested but GITHUB_TOKEN is unset; wrote the plan to the job summary instead.",
want_summary,
summary,
)
}
Err(CommentBlocked::NoPullNumber) => {
degrade_to_summary(
"comment requested but no pull request number could be read from GITHUB_EVENT_PATH or GITHUB_REF; wrote the plan to the job summary instead.",
want_summary,
summary,
)
}
Err(CommentBlocked::Failed(err)) => degrade_to_summary(
&format!(
"comment requested but GitHub did not accept the comment ({err}); wrote the plan to the job summary instead."
),
want_summary,
summary,
),
}
}
struct Provenance {
commit: Option<String>,
run: Option<(String, String)>,
}
impl Provenance {
fn of_this_run(repo: &repository::Repository) -> Self {
let commit = event_head_sha().or_else(|| {
Git::at_repository(repo)
.ok()
.and_then(|git| git.head().ok())
.map(|head| head.as_str().to_owned())
});
Self {
commit,
run: actions_run(),
}
}
fn of_poster() -> Self {
Self {
commit: event_head_sha(),
run: workflow_run_run().or_else(actions_run),
}
}
fn checked(&self) -> String {
let Some(commit) = self.commit.as_deref() else {
return String::from("this pull request");
};
let short = commit.get(..7).unwrap_or(commit);
match &self.run {
Some((number, url)) => format!("`{short}` ([run #{number}]({url}))"),
None => format!("`{short}`"),
}
}
fn line(&self) -> Option<String> {
let commit = self.commit.as_deref()?;
let short = commit.get(..7).unwrap_or(commit);
Some(match &self.run {
Some((number, url)) => format!("Release plan for `{short}` · [run #{number}]({url})"),
None => format!("Release plan for `{short}`"),
})
}
}
fn event_head_sha() -> Option<String> {
let bytes = std::fs::read(std::env::var("GITHUB_EVENT_PATH").ok()?).ok()?;
let value = serde_json::from_slice::<Value>(&bytes).ok()?;
["/pull_request/head/sha", "/workflow_run/head_sha"]
.into_iter()
.find_map(|pointer| value.pointer(pointer).and_then(Value::as_str))
.filter(|sha| sha.len() >= 7 && sha.bytes().all(|b| b.is_ascii_hexdigit()))
.map(str::to_owned)
}
fn actions_run() -> Option<(String, String)> {
let var = |name: &str| {
std::env::var(name)
.ok()
.map(|value| value.trim().to_owned())
.filter(|value| !value.is_empty())
};
let digits = |name: &str| var(name).filter(|value| value.bytes().all(|b| b.is_ascii_digit()));
let plain = |name: &str| {
var(name).filter(|value| !value.contains(|c: char| c.is_whitespace() || "()[]".contains(c)))
};
let url = format!(
"{}/{}/actions/runs/{}",
plain("GITHUB_SERVER_URL")?,
plain("GITHUB_REPOSITORY")?,
digits("GITHUB_RUN_ID")?
);
Some((digits("GITHUB_RUN_NUMBER")?, url))
}
fn workflow_run_run() -> Option<(String, String)> {
let bytes = std::fs::read(std::env::var("GITHUB_EVENT_PATH").ok()?).ok()?;
let value = serde_json::from_slice::<Value>(&bytes).ok()?;
let number = value.pointer("/workflow_run/run_number")?.as_u64()?;
let url = value
.pointer("/workflow_run/html_url")?
.as_str()
.filter(|url| {
!url.is_empty() && !url.contains(|c: char| c.is_whitespace() || "()[]".contains(c))
})?;
Some((number.to_string(), url.to_owned()))
}
fn mark_stale(repo: &repository::Repository, emit: Option<&Path>, reason: &str) {
if let Some(dir) = emit {
if let Err(err) = emit_failed(dir, reason) {
say_err(&format!("could not emit the stale-plan banner: {err}"));
}
return;
}
match post_stale(repo, reason) {
Ok(marked) => {
say_err(&marked.said);
if let Some(left) = marked.left {
say_err(&left);
}
}
Err(CommentBlocked::NoToken) => say_err(
"GITHUB_TOKEN is unset, so oakum's plan comment was not marked stale; an earlier plan may still read as current",
),
Err(CommentBlocked::NoPullNumber) => say_err(
"no pull request number could be read from GITHUB_EVENT_PATH or GITHUB_REF, so oakum's plan comment was not marked stale",
),
Err(CommentBlocked::Forbidden) => say_err(
"this run has no write permission (fork pull request), so oakum's plan comment was not marked stale; an earlier plan may still read as current",
),
Err(CommentBlocked::Failed(err)) => say_err(&format!(
"could not mark oakum's plan comment stale ({err}); an earlier plan may still read as current"
)),
}
}
struct Marked {
said: String,
left: Option<String>,
}
fn post_stale(repo: &repository::Repository, reason: &str) -> Result<Marked, CommentBlocked> {
let token = actions_token().ok_or(CommentBlocked::NoToken)?;
let number = pull_number().ok_or(CommentBlocked::NoPullNumber)?;
let git = Git::at_repository(repo).map_err(CliError::from)?;
let (owner, name) = repository_slug(&git)?;
let client = github::Client::new(token)?;
let checked = Provenance::of_this_run(repo).checked();
Ok(post_stale_with(
&client, &owner, &name, number, reason, &checked,
)?)
}
fn post_stale_with(
client: &github::Client,
owner: &str,
name: &str,
number: u64,
reason: &str,
checked: &str,
) -> Result<Marked, github::Error> {
let previous = client.plan_comment(owner, name, number, render::PR_PLAN_MARKER)?;
let stale = stale::stale_comment(previous.as_deref(), reason, checked, MAX_BODY_CHARS);
let upserted =
client.upsert_plan_comment(owner, name, number, render::PR_PLAN_MARKER, &stale.body)?;
let first = reason.lines().next().unwrap_or_default();
let mut said = format!("marked oakum's plan comment on #{number} stale: {first}");
if stale.dropped_plan {
said.push_str(" (the last plan did not fit beside the banner, so it was left out)");
}
Ok(Marked {
said,
left: upserted.cleanup.err().map(|err| {
format!(
"removing older copies of oakum's plan comment on #{number} failed ({err}), so an older plan may still read as current"
)
}),
})
}
fn with_provenance(comment: &str, provenance: &Provenance) -> String {
let Some(line) = provenance.line() else {
return comment.to_owned();
};
let head = format!("{}\n", render::PR_PLAN_MARKER);
comment.strip_prefix(&head).map_or_else(
|| comment.to_owned(),
|rest| format!("{head}{line}\n{rest}"),
)
}
const fn nothing_was(channels: PrStatus) -> &'static str {
match channels {
PrStatus::Comment => "no coverage comment was",
PrStatus::Summary => "no job summary was",
PrStatus::Both => "no coverage comment and no job summary were",
PrStatus::None => "nothing was",
}
}
fn degrade_to_summary(
message: &str,
summary_already_written: bool,
summary: &str,
) -> Result<(), CliError> {
say_err(message);
if !summary_already_written {
write_step_summary(summary)?;
}
Ok(())
}
const EMITTED_COMMENT_FILE: &str = "oakum-pr-comment.md";
const NO_COMMENT_FILE: &str = "oakum-pr-comment.none";
const NO_COMMENT_TEXT: &str = "oakum ci pr-status had no comment for this pull request\n";
const EMITTED_END: &str = "<!-- /oakum:pr-plan -->\n";
const FORMAT_FILE: &str = "oakum-pr-comment.format";
const EMIT_FORMAT: u32 = 2;
const FAILED_FILE: &str = "oakum-pr-comment.failed";
fn emit_comment_file(dir: &Path, comment: &str) -> Result<(), CliError> {
let mut body = comment.to_owned();
if !body.ends_with('\n') {
body.push('\n');
}
body.push_str(EMITTED_END);
emit_format(dir)?;
write_emitted(dir, EMITTED_COMMENT_FILE, &body)
}
fn emit_no_comment(dir: &Path) -> Result<(), CliError> {
emit_format(dir)?;
write_emitted(dir, NO_COMMENT_FILE, NO_COMMENT_TEXT)
}
fn emit_failed(dir: &Path, reason: &str) -> Result<(), CliError> {
emit_format(dir)?;
write_emitted(
dir,
FAILED_FILE,
&format!("{}\n{EMITTED_END}", reason.trim_end()),
)
}
fn emit_format(dir: &Path) -> Result<(), CliError> {
write_emitted(
dir,
FORMAT_FILE,
&format!(
"format {EMIT_FORMAT}\noakum {}\n",
env!("CARGO_PKG_VERSION")
),
)
}
fn emitted_format(dir: &Path) -> Result<u32, CliError> {
let path = dir.join(FORMAT_FILE);
let Some(text) = read_emitted(&path)? else {
return Ok(1);
};
let mut lines = text.lines();
let format = lines
.next()
.and_then(|line| line.strip_prefix("format "))
.and_then(|number| number.parse::<u32>().ok());
let emitter = lines
.next()
.and_then(|line| line.strip_prefix("oakum "))
.filter(|version| !version.trim().is_empty());
let (Some(format), Some(emitter)) = (format, emitter) else {
return Err(CliError::unverified(format!(
"unverified: {} is not the format record `ci pr-status` writes; nothing was posted or removed",
path.display()
)));
};
if format > EMIT_FORMAT {
return Err(CliError::unverified(format!(
"unverified: the comment was emitted by oakum {emitter} (artifact format {format}) and this is oakum {} (format {EMIT_FORMAT}), which cannot read it; nothing was posted or removed. This resolves once the job that posts runs the newer oakum, as after the upgrade merges",
env!("CARGO_PKG_VERSION")
)));
}
Ok(format)
}
fn write_emitted(dir: &Path, name: &str, text: &str) -> Result<(), CliError> {
std::fs::create_dir_all(dir).map_err(|err| {
CliError::new(format!(
"failed to create --emit-comment directory {}: {err}",
dir.display()
))
})?;
let path = dir.join(name);
let staged = dir.join(format!(".{name}.oakum-write"));
let created = || {
match std::fs::remove_file(&staged) {
Err(err) if err.kind() != std::io::ErrorKind::NotFound => return Err(err),
_ => {}
}
let mut file = std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&staged)?;
std::io::Write::write_all(&mut file, text.as_bytes())
};
created()
.and_then(|()| std::fs::rename(&staged, &path))
.map_err(|err| {
let _ = std::fs::remove_file(&staged);
CliError::new(format!(
"failed to write --emit-comment file {}: {err}",
path.display()
))
})
}
fn reset_emitted(dir: &Path) -> Result<(), CliError> {
for name in [
EMITTED_COMMENT_FILE,
NO_COMMENT_FILE,
FAILED_FILE,
FORMAT_FILE,
] {
let path = dir.join(name);
match std::fs::remove_file(&path) {
Ok(()) => {}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
Err(err) => {
return Err(CliError::new(format!(
"failed to remove stale --emit-comment file {}: {err}",
path.display()
)));
}
}
}
Ok(())
}
const MAX_BODY_CHARS: usize = 65_536;
fn run_post_comment(args: &PostCommentArgs) -> Result<(), CliError> {
let dir = &args.dir;
let format = emitted_format(dir)?;
let body_path = dir.join(EMITTED_COMMENT_FILE);
let none_path = dir.join(NO_COMMENT_FILE);
let body = read_emitted(&body_path)?;
let none = match read_emitted(&none_path)? {
Some(text) if text == NO_COMMENT_TEXT => true,
Some(_) => {
return Err(CliError::unverified(format!(
"unverified: {} is not the marker `ci pr-status` writes; nothing was posted or removed",
none_path.display()
)));
}
None => false,
};
let failed_path = dir.join(FAILED_FILE);
let failed = read_emitted(&failed_path)?;
if failed.is_some() && format < 2 {
return Err(CliError::unverified(format!(
"unverified: {} holds {FAILED_FILE} in an artifact of format {format}, which has none; nothing was posted or removed",
dir.display()
)));
}
let present = [body.is_some(), none, failed.is_some()]
.into_iter()
.filter(|&found| found)
.count();
if present > 1 {
return Err(CliError::unverified(format!(
"unverified: {} holds more than one of {EMITTED_COMMENT_FILE}, {NO_COMMENT_FILE} and {FAILED_FILE}, so which run it came from cannot be told; nothing was posted",
dir.display()
)));
}
if present == 0 {
return Err(CliError::unverified(format!(
"unverified: {} holds none of {EMITTED_COMMENT_FILE}, {NO_COMMENT_FILE} and {FAILED_FILE}, so the emitted comment may have been lost on the way; nothing was posted or removed",
dir.display()
)));
}
let body = body
.map(|text| emitted_body(&body_path, &text))
.transpose()?;
let failed = failed
.map(|text| emitted_reason(&failed_path, &text))
.transpose()?;
let token = actions_token().ok_or_else(|| {
CliError::new("`oakum ci post-comment` needs GITHUB_TOKEN with pull-requests: write")
})?;
let number = post_pull_number().ok_or_else(|| {
CliError::new(
"`oakum ci post-comment` takes the pull request from its own event (a `pull_request`, or a `workflow_run` from a same-repository pull request) and found none; a fork's `workflow_run` names no pull request",
)
})?;
let (owner, name) = env_repository_slug()?;
let client = github::Client::new(token).map_err(CliError::from)?;
let refused = |err: github::Error| {
match err {
github::Error::Forbidden { .. } => CliError::unverified(format!(
"unverified: this job's token cannot write to #{number} (a fork pull request, or a token without pull-requests: write); the comment was not posted or removed"
)),
err => CliError::from(err),
}
};
if let Some(reason) = failed {
let checked = Provenance::of_poster().checked();
let marked = post_stale_with(&client, &owner, &name, number, &reason, &checked).map_err(
|err| match err {
github::Error::Forbidden { .. } => CliError::unverified(format!(
"unverified: this job's token cannot write to #{number} (a fork pull request, or a token without pull-requests: write); the comment was not marked stale"
)),
err => CliError::from(err),
},
)?;
say_err(&marked.said);
if let Some(left) = marked.left {
return Err(CliError::unverified(format!("unverified: {left}")));
}
} else if let Some(body) = body {
let upserted = client
.upsert_plan_comment(&owner, &name, number, render::PR_PLAN_MARKER, &body)
.map_err(refused)?;
upserted.cleanup.map_err(|err| {
CliError::unverified(format!("unverified: {}", older_copies_left(number, &err)))
})?;
say_err(&format!("posted the plan comment on #{number}."));
} else {
client
.delete_plan_comments(&owner, &name, number, render::PR_PLAN_MARKER)
.map_err(refused)?;
say_err(&format!(
"the emitting run had no comment, so oakum's plan comment was removed from #{number} if it had one."
));
}
Ok(())
}
fn emitted_body(path: &Path, text: &str) -> Result<String, CliError> {
if !text.is_empty() && !text.starts_with(render::PR_PLAN_MARKER) {
return Err(CliError::new(format!(
"{} does not start with oakum's `{}` marker, so it is not a comment oakum wrote; nothing was posted",
path.display(),
render::PR_PLAN_MARKER
)));
}
let Some(body) = text.strip_suffix(EMITTED_END) else {
return Err(CliError::unverified(format!(
"unverified: {} does not end with `{}`, so it may have been cut short; nothing was posted",
path.display(),
EMITTED_END.trim_end()
)));
};
let chars = body.chars().count();
if chars > MAX_BODY_CHARS {
return Err(CliError::new(format!(
"{} is {chars} characters, over GitHub's {MAX_BODY_CHARS}-character comment limit; nothing was posted",
path.display()
)));
}
Ok(body.to_owned())
}
fn emitted_reason(path: &Path, text: &str) -> Result<String, CliError> {
let Some(reason) = text.strip_suffix(EMITTED_END) else {
return Err(CliError::unverified(format!(
"unverified: {} does not end with `{}`, so it may have been cut short; nothing was posted",
path.display(),
EMITTED_END.trim_end()
)));
};
if reason.trim().is_empty() {
return Err(CliError::unverified(format!(
"unverified: {} names no reason; nothing was posted",
path.display()
)));
}
Ok(reason.to_owned())
}
fn read_emitted(path: &Path) -> Result<Option<String>, CliError> {
let unread =
|why: String| CliError::unverified(format!("unverified: {}: {why}", path.display()));
match std::fs::symlink_metadata(path) {
Ok(meta) if !meta.is_file() => {
return Err(unread(String::from(
"not a regular file, so it was not read; nothing was posted or removed",
)));
}
Ok(_) => {}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(err) => return Err(unread(format!("could not be inspected: {err}"))),
}
std::fs::read_to_string(path)
.map(Some)
.map_err(|err| unread(format!("could not be read: {err}")))
}
fn post_pull_number() -> Option<u64> {
pull_number().or_else(|| {
let bytes = std::fs::read(std::env::var("GITHUB_EVENT_PATH").ok()?).ok()?;
let value = serde_json::from_slice::<Value>(&bytes).ok()?;
workflow_run_pull_number(&value)
})
}
fn workflow_run_pull_number(value: &Value) -> Option<u64> {
match value
.pointer("/workflow_run/pull_requests")
.and_then(Value::as_array)
.map(Vec::as_slice)
{
Some([only]) => only.pointer("/number").and_then(Value::as_u64),
_ => None,
}
}
fn env_repository_slug() -> Result<(String, String), CliError> {
let value = std::env::var("GITHUB_REPOSITORY").unwrap_or_default();
value
.trim()
.split_once('/')
.filter(|(owner, name)| !owner.is_empty() && !name.is_empty() && !name.contains('/'))
.map(|(owner, name)| (owner.to_owned(), name.to_owned()))
.ok_or_else(|| {
CliError::new(format!(
"GITHUB_REPOSITORY is `{value}`, not `owner/name`, so the pull request cannot be found"
))
})
}
fn pr_status_state(
repo: &repository::Repository,
config: &LoadedConfig,
from: Option<&str>,
) -> Result<(ReleaseState, render::NextStep), CliError> {
release_state::release_state(
repo,
config,
from,
RenderTarget::Comment,
release_state::CoverageMode::Required,
)
}
fn write_step_summary(text: &str) -> Result<(), CliError> {
if let Ok(path) = std::env::var("GITHUB_STEP_SUMMARY") {
let path = path.trim();
if !path.is_empty() {
use std::io::Write as _;
let mut file = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(path)
.map_err(|err| {
CliError::new(format!("failed to write GITHUB_STEP_SUMMARY: {err}"))
})?;
file.write_all(text.as_bytes()).map_err(|err| {
CliError::new(format!("failed to write GITHUB_STEP_SUMMARY: {err}"))
})?;
if !text.ends_with('\n') {
file.write_all(b"\n").map_err(|err| {
CliError::new(format!("failed to write GITHUB_STEP_SUMMARY: {err}"))
})?;
}
return Ok(());
}
}
deliver_report(text).map_err(|err| CliError::undelivered("the plan", &err))?;
Ok(())
}
fn post_pr_comment(
repo: &repository::Repository,
body: &str,
) -> Result<Option<String>, CommentBlocked> {
let token = actions_token().ok_or(CommentBlocked::NoToken)?;
let number = pull_number().ok_or(CommentBlocked::NoPullNumber)?;
let git = Git::at_repository(repo).map_err(CliError::from)?;
let (owner, name) = repository_slug(&git)?;
let client = github::Client::new(token)?;
let upserted =
client.upsert_plan_comment(&owner, &name, number, render::PR_PLAN_MARKER, body)?;
Ok(upserted
.cleanup
.err()
.map(|err| older_copies_left(number, &err)))
}
fn older_copies_left(number: u64, err: &github::Error) -> String {
format!("posted the plan comment on #{number}, but removing its older copies failed: {err}")
}
fn actions_token() -> Option<String> {
match std::env::var("GITHUB_TOKEN") {
Ok(token) if !token.is_empty() => Some(token),
_ => None,
}
}
fn pull_number() -> Option<u64> {
if let Ok(path) = std::env::var("GITHUB_EVENT_PATH") {
if let Ok(bytes) = std::fs::read(path) {
if let Ok(value) = serde_json::from_slice::<Value>(&bytes) {
if let Some(number) = pull_number_from_event(&value) {
return Some(number);
}
}
}
}
pull_number_from_ref(std::env::var("GITHUB_REF").ok().as_deref())
}
fn on_version_packages_branch() -> bool {
let head = std::env::var("GITHUB_HEAD_REF").ok();
if head.as_deref().is_some_and(|head| head != VERSION_BRANCH) {
return false;
}
let Ok(path) = std::env::var("GITHUB_EVENT_PATH") else {
if head.is_some() {
say_err("GITHUB_EVENT_PATH is not set, so the version pull request cannot be recognised; treating this run as a contributor pull request");
}
return false;
};
let value = match std::fs::read(&path)
.map_err(|err| err.to_string())
.and_then(|bytes| serde_json::from_slice::<Value>(&bytes).map_err(|err| err.to_string()))
{
Ok(value) => value,
Err(err) => {
say_err(&format!(
"event payload at {path} could not be read ({err}); treating this run as a contributor pull request"
));
return false;
}
};
let text = |pointer: &str| value.pointer(pointer).and_then(Value::as_str);
if text("/pull_request/head/ref") != Some(VERSION_BRANCH) {
return false;
}
let Ok(this_repo) = std::env::var("GITHUB_REPOSITORY") else {
say_err("GITHUB_REPOSITORY is not set, so the version pull request cannot be recognised; treating this run as a contributor pull request");
return false;
};
text("/pull_request/head/repo/full_name") == Some(this_repo.as_str())
&& text("/pull_request/user/type") == Some("Bot")
&& text("/sender/type") == Some("Bot")
}
fn pull_number_from_event(value: &Value) -> Option<u64> {
if let Some(number) = value
.pointer("/pull_request/number")
.and_then(Value::as_u64)
{
return Some(number);
}
if value
.pointer("/issue/pull_request")
.is_some_and(|value| !value.is_null())
{
return value.pointer("/issue/number").and_then(Value::as_u64);
}
None
}
fn pull_number_from_ref(value: Option<&str>) -> Option<u64> {
let value = value?.trim();
let mut parts = value.split('/');
if parts.next()? != "refs" || parts.next()? != "pull" {
return None;
}
parts.next()?.parse().ok()
}
fn clear_stale_comment(repo: &repository::Repository) {
match delete_pr_comment(repo) {
Ok(()) => {}
Err(CommentBlocked::Forbidden | CommentBlocked::NoToken | CommentBlocked::NoPullNumber) => {
say_err(
"a leftover plan comment could not be removed with this run's token, so an earlier plan may still be visible on the pull request",
);
}
Err(CommentBlocked::Failed(err)) => {
say_err(&format!("could not remove a leftover plan comment ({err})"));
}
}
}
fn delete_pr_comment(repo: &repository::Repository) -> Result<(), CommentBlocked> {
let token = actions_token().ok_or(CommentBlocked::NoToken)?;
let number = pull_number().ok_or(CommentBlocked::NoPullNumber)?;
let git = Git::at_repository(repo).map_err(CliError::from)?;
let (owner, name) = repository_slug(&git)?;
let client = github::Client::new(token)?;
client.delete_plan_comments(&owner, &name, number, render::PR_PLAN_MARKER)?;
Ok(())
}
enum CommentBlocked {
NoToken,
NoPullNumber,
Forbidden,
Failed(CliError),
}
impl From<CliError> for CommentBlocked {
fn from(err: CliError) -> Self {
Self::Failed(err)
}
}
impl From<github::Error> for CommentBlocked {
fn from(err: github::Error) -> Self {
match err {
github::Error::Forbidden { .. } => Self::Forbidden,
err => Self::Failed(CliError::from(err)),
}
}
}
fn run_version_pr(args: &VersionArgs) -> Result<(), CliError> {
let prepared = version::plan_writes(args)?;
prepared.say_dropped_intent();
let releases = prepared.needs_github();
if !releases && prepared.consume.files().is_empty() {
say_out("nothing to version");
return Ok(());
}
let client =
github::Client::new(github::token().ok_or_else(|| {
CliError::new("`oakum ci version-pr` needs GITHUB_TOKEN or GH_TOKEN")
})?)?;
let git = Git::at_repository(&prepared.repo)?;
let (owner, name) = repository_slug(&git)?;
let open_for_deletes = if releases {
None
} else {
let Some(open) = open_version_pull(&client, &owner, &name)? else {
say_out(&format!(
"nothing to version: {} pending bump file(s) release nothing, so no pull request was opened; the next release consumes them",
prepared.consume.files().len()
));
return Ok(());
};
Some(open)
};
let default_branch = client.default_branch(&owner, &name)?;
let base_oid = match client.branch_head(&owner, &name, &default_branch)? {
Look::Found(oid) => oid,
Look::Empty => {
return Err(CliError::new(format!(
"default branch `{default_branch}` has no head"
)));
}
};
let head = local_head(&git)?;
if head != base_oid {
let why = match client.compare(&owner, &name, &head, &base_oid)? {
Look::Found(github::Comparison::Ahead) => {
say_out(&format!(
"superseded: `{default_branch}` moved on from checkout HEAD `{head}` \
to `{base_oid}`; this run writes nothing, and the version pull request \
waits for a run on `{base_oid}`"
));
return Ok(());
}
Look::Found(github::Comparison::Behind) => "behind it",
Look::Found(github::Comparison::Diverged) => "diverged from it",
Look::Found(github::Comparison::Identical) => "identical by GitHub's compare",
Look::Empty => "GitHub's compare returned 404",
};
return Err(CliError::new(format!(
"checkout HEAD `{head}` is not `{default_branch}` at `{base_oid}` ({why})"
)));
}
let additions = github_additions(&prepared)?;
let deletions = github_deletions(&prepared)?;
let headline = commit_headline(&prepared)?;
let title = pr_title(&prepared)?;
let body = pr_body(&prepared)?;
let existing = match open_for_deletes {
Some(open) => Some(open),
None => version_pull(&client, &owner, &name)?,
};
client.replace_branch_commit(
&owner,
&name,
VERSION_BRANCH,
&base_oid,
&headline,
FileChanges {
additions: &additions,
deletions: &deletions,
},
)?;
prepared.say_landed_notes();
let opened = if let Some(pull) = existing {
client.update_pull(&owner, &name, pull.number, &title, &body)?
} else {
client.create_pull(
&owner,
&name,
VERSION_BRANCH,
&default_branch,
&title,
&body,
)?
};
deliver_out(&opened.html_url).map_err(|err| {
CliError::undelivered(
format!(
"version pull request {} is open, but its URL",
opened.html_url
),
&err,
)
})?;
say_err(&author_note(opened.author.as_ref()));
Ok(())
}
fn author_note(author: Option<&github::PullAuthor>) -> String {
let Some(author) = author else {
return String::from(
"version pull request author: not reported by GitHub; \
whether the scaffolded `oakum check --strict` skip applies is unverified",
);
};
let consequence = match (author.login.as_deref(), author.kind.as_deref()) {
(None, _) => String::from(
"GitHub reported no author login, so whether the scaffolded \
`oakum check --strict` skip applies is unverified",
),
(Some(VERSION_PR_AUTHOR), _) => String::from(
"the scaffolded `oakum check --strict` skip trusts this login and also tests the \
event sender, which this run cannot observe",
),
(Some(_), Some(kind)) if kind != "Bot" => String::from(
"the scaffolded `oakum check --strict` skip does not apply, so it runs on this pull request, \
unless the workflow is one `oakum init --token` or `--app` printed, whose skip matches the branch \
and repository instead",
),
(Some(login), _) => format!(
"the scaffolded `oakum check --strict` skip trusts `{VERSION_PR_AUTHOR}`; \
where the workflow still names that login, the check runs on this pull request, \
so name `{login}` there; a skip `oakum init --token` or `--app` printed names no login"
),
};
format!(
"version pull request author: {} ({}); {consequence}",
author.login.as_deref().unwrap_or("login not reported"),
author.kind.as_deref().unwrap_or("type not reported")
)
}
fn local_head(git: &Git) -> Result<String, CliError> {
let sha = git.text(Op::Head).map_err(|err| {
err.recast(format!(
"`oakum ci version-pr` needs a git HEAD to compare with the default branch ({})",
err.detail()
))
})?;
if sha.is_empty() {
return Err(CliError::new("git HEAD is empty"));
}
Ok(sha)
}
fn open_version_pull(
client: &github::Client,
owner: &str,
name: &str,
) -> Result<Option<github::PullRequest>, CliError> {
match client.open_pulls_for_head(owner, name, VERSION_BRANCH)? {
Look::Found(pulls) if pulls.len() > 1 => Err(CliError::new(format!(
"multiple open version pull requests on `{VERSION_BRANCH}` ({})",
pulls.len()
))),
Look::Found(pulls) => Ok(pulls.into_iter().next()),
Look::Empty => Ok(None),
}
}
fn version_pull(
client: &github::Client,
owner: &str,
name: &str,
) -> Result<Option<github::PullRequest>, CliError> {
match open_version_pull(client, owner, name)? {
Some(open) => Ok(Some(open)),
None => match client.pulls_for_head(owner, name, VERSION_BRANCH, "closed")? {
Look::Found(pulls) => {
let unmerged: Vec<_> = pulls.into_iter().filter(|pull| !pull.merged).collect();
match unmerged.len() {
0 => Ok(None),
1 => Ok(Some(unmerged.into_iter().next().expect("one pull"))),
count => Err(CliError::new(format!(
"multiple closed unmerged version pull requests on `{VERSION_BRANCH}` ({count})"
))),
}
}
Look::Empty => Ok(None),
},
}
}
pub(super) fn repository_slug(git: &Git) -> Result<(String, String), CliError> {
repository_slug_from(git, "origin")
}
pub(super) fn repository_slug_from(git: &Git, remote: &str) -> Result<(String, String), CliError> {
if let Ok(value) = std::env::var("GITHUB_REPOSITORY") {
let value = value.trim();
if !value.is_empty() {
return parse_slug(value).ok_or_else(|| {
CliError::new(format!("GITHUB_REPOSITORY `{value}` is not owner/repo"))
});
}
}
let url = git.text(Op::RemoteUrl { remote }).map_err(|err| {
err.recast(format!(
"needs GITHUB_REPOSITORY or a git `{remote}` remote ({})",
err.detail()
))
})?;
parse_github_origin(&url).ok_or_else(|| {
CliError::new(format!(
"git `{remote}` `{url}` is not a github.com owner/repo URL"
))
})
}
fn parse_slug(value: &str) -> Option<(String, String)> {
let (owner, name) = value.split_once('/')?;
if owner.is_empty() || name.is_empty() || name.contains('/') {
return None;
}
Some((owner.to_owned(), name.to_owned()))
}
fn parse_github_origin(url: &str) -> Option<(String, String)> {
let rest = url
.strip_prefix("git@github.com:")
.or_else(|| url.strip_prefix("https://github.com/"))
.or_else(|| url.strip_prefix("http://github.com/"))
.or_else(|| url.strip_prefix("ssh://git@github.com/"))
.or_else(|| url.strip_prefix("git://github.com/"))?;
let rest = rest.trim_end_matches('/').trim_end_matches(".git");
parse_slug(rest)
}
fn github_path(path: &Path) -> Result<String, CliError> {
let raw = path
.to_str()
.ok_or_else(|| CliError::new("a version write path is not valid UTF-8"))?;
github::git_path(raw, "write").map_err(CliError::from)
}
fn github_additions(prepared: &VersionWritePlan) -> Result<Vec<FileAddition>, CliError> {
let mut additions = Vec::new();
for write in &prepared.writes {
if write.original() == write.next() {
continue;
}
additions.push(FileAddition::from_text(
github_path(write.path())?,
write.next(),
));
}
Ok(additions)
}
fn github_deletions(prepared: &VersionWritePlan) -> Result<Vec<FileDeletion>, CliError> {
prepared
.consume
.files()
.iter()
.map(|path| FileDeletion::new(github_path(path)?).map_err(CliError::from))
.collect()
}
fn commit_headline(prepared: &VersionWritePlan) -> Result<String, CliError> {
render_pref(
&prepared.repo,
"commit-message",
prepared.commit_message.as_ref(),
DEFAULT_COMMIT,
prepared,
)
}
fn pr_title(prepared: &VersionWritePlan) -> Result<String, CliError> {
render_pref(
&prepared.repo,
"title",
prepared.title.as_ref(),
DEFAULT_TITLE,
prepared,
)
}
fn render_pref(
repo: &repository::Repository,
name: &str,
source: Option<&oakum::template::TemplateSource>,
default: &str,
prepared: &VersionWritePlan,
) -> Result<String, CliError> {
let Some(source) = source else {
return Ok(default.to_owned());
};
let body = load_template_body(repo.dir(), repo.path(), source)?;
let state = prepared.release_state();
let rendered = oakum::template::render(name, &body, &state)
.map_err(|err| CliError::new(err.to_string()))?;
let rendered = rendered.trim();
if rendered.is_empty() {
return Err(CliError::new(format!(
"{name} template rendered an empty string"
)));
}
Ok(rendered.to_owned())
}
fn pr_body(prepared: &VersionWritePlan) -> Result<String, CliError> {
let state = prepared.release_state();
let mut head = render::render_summary(&state, None);
if !head.ends_with('\n') {
head.push('\n');
}
let tail = pr_body_tail(prepared);
let length = github_len(&head) + github_len(&tail);
if length > MAX_BODY_CHARS {
return Err(CliError::new(format!(
"the version pull request body is {length} characters before its release notes, over GitHub's {MAX_BODY_CHARS}-character limit; nothing was pushed"
)));
}
let Some(notes) = release_notes_section(&prepared.release_notes, MAX_BODY_CHARS - length)
else {
return Err(CliError::new(format!(
"the version pull request body is {length} characters before its release notes, leaving no room under GitHub's {MAX_BODY_CHARS}-character limit to name them; nothing was pushed"
)));
};
Ok(head + ¬es + &tail)
}
fn github_len(text: &str) -> usize {
text.encode_utf16().count()
}
fn release_notes_section(notes: &[ReleaseNote], budget: usize) -> Option<String> {
const HEADING: &str = "\n### Release notes\n\n";
const COMPACT: &str = "\n### Release notes\n\nLeft out for length; each package's entry is in its changelog on this branch.\n";
if notes.is_empty() {
return Some(String::new());
}
let open = if notes.len() == 1 { " open" } else { "" };
let pointer = |note: &ReleaseNote, why: &str| {
format!(
"- {} {} ({}): {why}; see `{}` on this branch\n\n",
note.package,
note.version,
changelog::ecosystem_label(note.ecosystem),
note.path
)
};
let rendered: Vec<(Option<String>, String)> = notes
.iter()
.map(|note| {
if note.entry.trim().is_empty() {
(None, pointer(note, "the entry is empty"))
} else if holds_raw_html(¬e.entry) {
(None, pointer(note, "left out because it holds raw HTML"))
} else {
let block = format!(
"<details{open}>\n<summary>{} {} ({})</summary>\n\n{}\n\n</details>\n\n",
note.package,
note.version,
changelog::ecosystem_label(note.ecosystem),
note.entry
);
(Some(block), pointer(note, "left out for length"))
}
})
.collect();
let fits = |pieces: usize| pieces - 1 <= budget;
let mut reserve: usize = rendered.iter().map(|(_, line)| github_len(line)).sum();
if !fits(github_len(HEADING) + reserve) {
return (github_len(COMPACT) <= budget).then(|| String::from(COMPACT));
}
let mut section = String::from(HEADING);
for (block, line) in rendered {
reserve -= github_len(&line);
match block {
Some(block) if fits(github_len(§ion) + github_len(&block) + reserve) => {
section.push_str(&block);
}
_ => section.push_str(&line),
}
}
section.truncate(section.trim_end().len());
section.push('\n');
Some(section)
}
fn holds_raw_html(entry: &str) -> bool {
let mut read_whole = false;
entry.lines().any(|line| {
if line.trim().is_empty() {
read_whole = false;
}
let line = if read_whole {
line.to_owned()
} else {
let (text, continues) = outside_code_spans(line);
read_whole = continues;
text
};
let opens_block = after_container_markers(&line)
.strip_prefix('<')
.is_some_and(|tag| tag.starts_with(|c: char| c.is_ascii_alphabetic() || c == '/'));
let lower = line.to_ascii_lowercase();
opens_block
|| ["<!", "<?", "<details", "</details"]
.iter()
.any(|marker| lower.contains(marker))
})
}
fn outside_code_spans(line: &str) -> (String, bool) {
let mut out = String::new();
let mut rest = line;
let mut left_open = false;
while let Some(start) = rest.find('`') {
if rest[..start].contains('<') || rest[..start].contains("](") {
break;
}
let backslashes = rest[..start].len() - rest[..start].trim_end_matches('\\').len();
if backslashes % 2 == 1 {
out.push_str(&rest[..=start]);
rest = &rest[start + 1..];
continue;
}
out.push_str(&rest[..start]);
let after_start = &rest[start..];
let run = after_start.len() - after_start.trim_start_matches('`').len();
let body = &after_start[run..];
let mut search = 0;
let close = loop {
let Some(found) = body[search..].find('`') else {
break None;
};
let at = search + found;
let len = body[at..].len() - body[at..].trim_start_matches('`').len();
if len == run {
break Some(at);
}
search = at + len;
};
if let Some(at) = close {
rest = &body[at + run..];
} else {
out.push_str(&after_start[..run]);
rest = body;
left_open = true;
}
}
out.push_str(rest);
let continues = left_open || out.contains('<') || out.contains("](");
(out, continues)
}
fn after_container_markers(line: &str) -> &str {
let mut rest = line;
loop {
let trimmed = rest.trim_start();
if let Some(after) = trimmed.strip_prefix('>') {
rest = after;
continue;
}
let digits = trimmed.len()
- trimmed
.trim_start_matches(|c: char| c.is_ascii_digit())
.len();
let marker = if digits > 0 {
trimmed[digits..].strip_prefix(['.', ')'])
} else {
trimmed.strip_prefix(['-', '*', '+'])
};
match marker {
Some(after) if after.starts_with(char::is_whitespace) => rest = after,
_ => return trimmed,
}
}
}
fn pr_body_tail(prepared: &VersionWritePlan) -> String {
let mut body = String::new();
if !prepared.lockfile_left.is_empty() {
body.push_str(
"\n### Lockfile entries left for `pnpm install`\n\n\
A frozen install fails on this branch until `pnpm install` updates these \
entries. Each `oakum ci version-pr` run replaces this branch, so fix an \
entry that had already drifted on the default branch, and push any other \
fix here after the last run.\n\n",
);
for line in &prepared.lockfile_left {
let _ = writeln!(body, "- {line}");
}
}
if !prepared.anchor_notes.is_empty() {
body.push_str("\n### Catalog entries sharing a YAML anchor\n\n");
for note in &prepared.anchor_notes {
let _ = writeln!(body, "- {note}");
}
}
let _ = write!(body, "\nGenerated by oakum {}.\n", prepared.tool_version);
body
}
#[cfg(test)]
mod tests {
use super::{
commit_headline, github_len, github_path, parse_github_origin, parse_slug, pr_body,
pr_title, pull_number_from_event, pull_number_from_ref, release_notes_section,
workflow_run_pull_number, ReleaseNote, VersionWritePlan, MAX_BODY_CHARS, VERSION_HEADLINE,
};
use crate::cli::intent::DroppedIntent;
use crate::cli::repository;
use crate::cli::write_set::{Consume, PlannedWrite};
use oakum::plan::Plan;
use oakum::state::{CoverageOutcome, ReleaseState, RenderTarget, UnresolvedScope};
use serde_json::json;
use std::path::PathBuf;
fn stub_plan(writes: Vec<PlannedWrite>, consume: Consume) -> VersionWritePlan {
VersionWritePlan {
repo: repository::discover().expect("test checkout is a git repository"),
writes,
consume,
plan: Plan::default(),
tool_version: String::from("0.0.0"),
title: None,
commit_message: None,
lockfile_left: Vec::new(),
anchor_notes: Vec::new(),
release_notes: Vec::new(),
dropped: DroppedIntent::default(),
}
}
fn section(notes: &[ReleaseNote], budget: usize) -> String {
release_notes_section(notes, budget).unwrap_or_default()
}
fn note(package: &str, entry: String) -> ReleaseNote {
ReleaseNote {
ecosystem: oakum::plan::Ecosystem::Cargo,
package: package.to_owned(),
version: semver::Version::new(1, 2, 3),
path: format!("crates/{package}/CHANGELOG.md"),
entry,
}
}
#[test]
fn the_version_pr_body_quotes_each_entry_in_a_collapsed_block() {
let plan = VersionWritePlan {
release_notes: vec![
note("alpha", String::from("### Fixed\n\n- alpha fix")),
note("beta", String::from("### Added\n\n- beta feature")),
],
..stub_plan(Vec::new(), Consume::default())
};
let body = pr_body(&plan).expect("body");
assert!(
body.contains(
"### Release notes\n\n<details>\n<summary>alpha 1.2.3 (cargo)</summary>\n\n### Fixed\n\n- alpha fix\n\n</details>\n\n<details>\n<summary>beta 1.2.3 (cargo)</summary>"
),
"{body}"
);
assert!(body.ends_with("Generated by oakum 0.0.0.\n"), "{body}");
}
#[test]
fn a_single_entry_renders_open() {
let plan = VersionWritePlan {
release_notes: vec![note("alpha", String::from("- fix"))],
..stub_plan(Vec::new(), Consume::default())
};
let body = pr_body(&plan).expect("body");
assert!(
body.contains("<details open>\n<summary>alpha 1.2.3 (cargo)</summary>"),
"{body}"
);
}
#[test]
fn an_entry_past_the_body_limit_names_its_file() {
let plan = VersionWritePlan {
release_notes: vec![
note("alpha", "a".repeat(MAX_BODY_CHARS / 2)),
note("beta", "b".repeat(MAX_BODY_CHARS / 2)),
note("gamma", String::from("- small")),
],
..stub_plan(Vec::new(), Consume::default())
};
let body = pr_body(&plan).expect("body");
assert!(github_len(&body) <= MAX_BODY_CHARS, "{}", body.len());
assert!(body.contains("<summary>alpha 1.2.3 (cargo)</summary>"));
assert!(!body.contains(&"b".repeat(100)));
assert!(
body.contains(
"- beta 1.2.3 (cargo): left out for length; see `crates/beta/CHANGELOG.md` on this branch"
),
"{}",
&body[body.len().saturating_sub(800)..]
);
assert!(body.contains("<summary>gamma 1.2.3 (cargo)</summary>"));
assert!(body.ends_with("Generated by oakum 0.0.0.\n"));
}
#[test]
fn the_release_notes_section_stays_within_any_budget() {
let notes = [
note("alpha", "a".repeat(100)),
note("beta", "b".repeat(100)),
];
let full = github_len(§ion(¬es, usize::MAX));
for budget in 0..full + 50 {
let section = section(¬es, budget);
assert!(github_len(§ion) <= budget, "{budget}: {section}");
}
assert!(section(¬es, 120).contains("Left out for length"));
}
#[test]
fn an_entry_that_just_fits_is_quoted() {
let notes = [note("alpha", "a".repeat(100))];
let full = section(¬es, usize::MAX);
assert_eq!(section(¬es, github_len(&full)), full);
assert!(section(¬es, github_len(&full) - 1).contains("left out for length"));
}
#[test]
fn the_budget_counts_utf16_units() {
let notes = [note("alpha", "😀".repeat(50))];
let full = section(¬es, usize::MAX);
assert!(section(¬es, full.chars().count()).contains("left out for length"));
}
#[test]
fn an_entry_holding_raw_html_names_its_file() {
for entry in [
"<pre>example",
"- fix\n\n</details>",
" <!-- open",
"- a <details> tag",
"- <!-- open",
"> <pre>",
"1. <script>",
"- fix <?processing",
"- one\n ```\n code\noutdent\n<!-- open\n```",
"```html\n<pre>\n```",
"-\t<pre>",
"- a `` <details> `` block with an unmatched `<details> tag",
"- an ``unclosed span <details>",
"use \\`<details>` now",
"[x](url \"`\") <details> `",
"see <a title=\"`\"> <details> `",
"[x](/u\n\"`\") <details> `",
"a <a title=\"\n`\"> <details> `",
"`start\n` <details> ` end`",
"- a `b\n c` <details> `d",
] {
let notes = [
note("alpha", entry.to_owned()),
note("beta", String::from("- ok")),
];
let section = section(¬es, usize::MAX);
assert!(
section.contains("- alpha 1.2.3 (cargo): left out because it holds raw HTML"),
"{entry}: {section}"
);
assert!(
section.contains("<summary>beta 1.2.3 (cargo)</summary>"),
"{section}"
);
}
for entry in [
"```rust\nlet a = b;\n```",
"- use `Vec<T>` now",
"a < b holds",
"A heading inside an HTML comment or a `<details>` block no longer counts.",
"- `<!-- x -->` and ``a `<?pi` b`` are text",
"- ``a`b <details> c`` closes only on a run of its own length",
"a stray ` in one paragraph\n\n- a `<details>` block in the next",
] {
let quoted = section(&[note("alpha", entry.to_owned())], usize::MAX);
assert!(quoted.contains("<summary>"), "{entry}: {quoted}");
}
}
#[test]
fn an_empty_entry_says_so() {
let section = section(&[note("alpha", String::from("\n"))], usize::MAX);
assert!(
section.contains("- alpha 1.2.3 (cargo): the entry is empty"),
"{section}"
);
assert!(!section.contains("<details"), "{section}");
}
#[test]
fn a_body_over_the_limit_is_refused() {
let plan = VersionWritePlan {
anchor_notes: vec!["x".repeat(MAX_BODY_CHARS)],
release_notes: vec![note("alpha", String::from("- fix"))],
..stub_plan(Vec::new(), Consume::default())
};
let err = pr_body(&plan).expect_err("over the limit");
assert!(err.to_string().contains("over GitHub's"), "{err}");
assert!(err.to_string().contains("nothing was pushed"), "{err}");
let plan = VersionWritePlan {
anchor_notes: Vec::new(),
..plan
};
assert!(pr_body(&plan).is_ok());
}
#[test]
fn notes_with_no_room_for_a_line_are_refused() {
let stub = stub_plan(Vec::new(), Consume::default());
let bare = github_len(&pr_body(&stub).expect("body"));
let plan = VersionWritePlan {
anchor_notes: vec!["x".repeat(MAX_BODY_CHARS - bare - 80)],
release_notes: vec![note("alpha", String::from("- fix"))],
..stub
};
let err = pr_body(&plan).expect_err("no room for the notes");
assert!(err.to_string().contains("leaving no room"), "{err}");
}
#[test]
fn the_version_pr_body_names_intent_commits_dropped() {
let plan = VersionWritePlan {
dropped: DroppedIntent {
named_unmanaged: vec![oakum::plan::PackageId::new(
oakum::plan::Ecosystem::Cargo,
"beta",
)],
unresolved: vec![UnresolvedScope {
commit: String::from("0123456789abcdef"),
scope: String::from("gamma"),
}],
awaiting_tag: Vec::new(),
updates_unread: Vec::new(),
only_updates: std::collections::BTreeSet::new(),
},
..stub_plan(Vec::new(), Consume::default())
};
let body = pr_body(&plan).expect("body");
assert!(
body.contains("Named by a commit but not version-managed: beta (`cargo`)"),
"{body}"
);
assert!(
body.contains("commit 0123456 asks for a release under scope `gamma`"),
"{body}"
);
let templated = VersionWritePlan {
title: Some(oakum::template::TemplateSource::Inline(String::from(
"release{% for p in named_unmanaged %} without {{ p.name }}{% endfor %}{% for c in unresolved_scopes %} or {{ c.scope }}{% endfor %}",
))),
..plan
};
assert_eq!(
pr_title(&templated).expect("title"),
"release without beta or gamma"
);
}
#[test]
fn an_unconfigured_title_is_the_commit_headline_and_is_conventional() {
let plan = stub_plan(Vec::new(), Consume::default());
let title = pr_title(&plan).expect("title");
assert_eq!(title, commit_headline(&plan).expect("headline"));
let parsed = git_conventional::Commit::parse(&title)
.expect("the title a squash merge lands parses as a conventional commit");
assert_eq!(parsed.type_(), git_conventional::Type::CHORE);
}
#[test]
fn a_configured_message_and_title_stay_on_their_own_surfaces() {
let mut message_only = stub_plan(Vec::new(), Consume::default());
message_only.commit_message = Some(oakum::template::TemplateSource::Inline(String::from(
"chore(release): v9.9.9",
)));
assert_eq!(
commit_headline(&message_only).expect("headline"),
"chore(release): v9.9.9"
);
assert_eq!(pr_title(&message_only).expect("title"), VERSION_HEADLINE);
let mut title_only = stub_plan(Vec::new(), Consume::default());
title_only.title = Some(oakum::template::TemplateSource::Inline(String::from(
"Version Packages",
)));
assert_eq!(pr_title(&title_only).expect("title"), "Version Packages");
assert_eq!(
commit_headline(&title_only).expect("headline"),
VERSION_HEADLINE
);
}
#[test]
fn slug_rejects_extra_segments() {
assert!(parse_slug("oakoss/oakum/extra").is_none());
assert_eq!(
parse_slug("oakoss/oakum"),
Some((String::from("oakoss"), String::from("oakum")))
);
}
#[test]
fn origin_urls_resolve_owner_and_repo() {
for url in [
"git@github.com:oakoss/oakum.git",
"https://github.com/oakoss/oakum.git",
"https://github.com/oakoss/oakum",
"ssh://git@github.com/oakoss/oakum.git",
"git://github.com/oakoss/oakum.git",
] {
assert_eq!(
parse_github_origin(url),
Some((String::from("oakoss"), String::from("oakum"))),
"{url}"
);
}
assert!(parse_github_origin("git@gitlab.com:oakoss/oakum.git").is_none());
}
#[test]
fn author_note_settles_only_what_the_author_decides() {
fn note(login: Option<&str>, kind: Option<&str>) -> String {
super::author_note(Some(&super::github::PullAuthor {
login: login.map(str::to_owned),
kind: kind.map(str::to_owned),
}))
}
assert!(super::author_note(None).contains("not reported by GitHub"));
assert!(super::author_note(None).contains("unverified"));
for kind in ["User", "Organization"] {
let person = note(Some("jbabin91"), Some(kind));
assert!(person.contains(&format!("jbabin91 ({kind})")), "{person}");
assert!(person.contains("does not apply"), "{person}");
}
for kind in [Some("Bot"), None] {
let bot_note = note(Some("github-actions[bot]"), kind);
assert!(
bot_note.contains("also tests the event sender"),
"{bot_note}"
);
assert!(!bot_note.contains("does not apply"), "{bot_note}");
}
for (kind, shown) in [(Some("Bot"), "(Bot)"), (None, "(type not reported)")] {
let app = note(Some("oakoss[bot]"), kind);
assert!(app.contains(&format!("oakoss[bot] {shown}")), "{app}");
assert!(app.contains("trusts `github-actions[bot]`"), "{app}");
assert!(app.contains("the check runs on this pull request"), "{app}");
assert!(app.contains("name `oakoss[bot]` there"), "{app}");
assert!(!app.contains("does not apply"), "{app}");
}
let no_login = note(None, Some("Bot"));
assert!(no_login.contains("login not reported (Bot)"), "{no_login}");
assert!(no_login.contains("unverified"), "{no_login}");
}
#[test]
fn pr_body_stamps_the_tool_version() {
let prepared = stub_plan(Vec::new(), Consume::default());
let body = pr_body(&prepared).expect("body");
assert!(body.contains("No packages planned."), "{body}");
assert!(body.contains("Generated by oakum 0.0.0.\n"), "{body}");
assert!(!body.contains("pnpm install"), "{body}");
}
#[test]
fn pr_body_names_lockfile_entries_left() {
let mut prepared = stub_plan(Vec::new(), Consume::default());
prepared.lockfile_left = vec![String::from(
"pnpm-lock.yaml: left `importers/packages/b/dependencies/a` specifier is `x`, not the `y` the manifest had",
)];
let body = pr_body(&prepared).expect("body");
assert!(
body.contains("### Lockfile entries left for `pnpm install`"),
"{body}"
);
assert!(
body.contains("\n- pnpm-lock.yaml: left `importers/packages/b/dependencies/a`"),
"{body}"
);
assert!(body.ends_with("Generated by oakum 0.0.0.\n"), "{body}");
}
#[test]
fn pr_body_names_catalog_entries_sharing_an_anchor() {
let mut prepared = stub_plan(Vec::new(), Consume::default());
assert!(!pr_body(&prepared).expect("body").contains("YAML anchor"));
prepared.anchor_notes = vec![String::from(
"pnpm-workspace.yaml: `catalog/a` was the alias `*caret`; it is written out, so `&caret` does not move with it",
)];
let body = pr_body(&prepared).expect("body");
assert!(
body.contains(
"### Catalog entries sharing a YAML anchor\n\n- pnpm-workspace.yaml: `catalog/a` was the alias"
),
"{body}"
);
assert!(body.ends_with("Generated by oakum 0.0.0.\n"), "{body}");
}
#[test]
fn needs_github_is_true_for_a_write_without_deletes() {
let prepared = stub_plan(
vec![PlannedWrite::new(
PathBuf::from("Cargo.toml"),
"0.1.0",
"0.1.1",
)],
Consume::default(),
);
assert!(prepared.needs_github());
}
#[test]
fn needs_github_is_false_for_a_delete_without_writes() {
let prepared = stub_plan(Vec::new(), Consume::new(".changeset", ["one.md"]));
assert!(!prepared.needs_github());
}
#[test]
fn needs_github_is_true_for_a_delete_beside_a_write() {
let prepared = stub_plan(
vec![PlannedWrite::new(
PathBuf::from("Cargo.toml"),
"0.1.0",
"0.1.1",
)],
Consume::new(".changeset", ["one.md"]),
);
assert!(prepared.needs_github());
}
#[test]
fn github_path_normalizes_backslashes() {
assert_eq!(
github_path(std::path::Path::new("foo\\bar.md")).expect("path"),
"foo/bar.md"
);
}
#[test]
fn pull_number_parses_actions_ref() {
assert_eq!(pull_number_from_ref(Some("refs/pull/12/merge")), Some(12));
assert_eq!(pull_number_from_ref(Some("refs/heads/main")), None);
assert_eq!(pull_number_from_ref(None), None);
}
#[test]
fn pull_number_from_event_accepts_pr_shapes_only() {
assert_eq!(
pull_number_from_event(&json!({"pull_request":{"number":4}})),
Some(4)
);
assert_eq!(
pull_number_from_event(&json!({"issue":{"number":4,"pull_request":{}}})),
Some(4)
);
assert_eq!(pull_number_from_event(&json!({"issue":{"number":4}})), None);
assert_eq!(
pull_number_from_event(&json!({"issue":{"number":4,"pull_request":null}})),
None
);
let run = json!({"workflow_run":{"pull_requests":[{"number":7}]}});
assert_eq!(pull_number_from_event(&run), None);
assert_eq!(workflow_run_pull_number(&run), Some(7));
for none in [json!([]), json!([{"number":7},{"number":8}])] {
assert_eq!(
workflow_run_pull_number(&json!({"workflow_run":{"pull_requests":none}})),
None
);
}
}
#[test]
fn the_schema_names_every_variable_the_state_surfaces_render_with() {
for surface in ["commit-message", "title"] {
crate::cli::schema_names_every_variable(
surface,
ReleaseState::from_plan(
&oakum::plan::Plan::default(),
CoverageOutcome::NotAsked,
RenderTarget::Status,
),
);
}
}
}