use std::collections::BTreeMap;
use std::io::Write;
use std::path::PathBuf;
use mortise_core::{
CoverageSettings, PushCi, PushContext, PushGit, ReadCache, ReadError, ReadOptions, Repository,
RulesSource, byte_limit, push_document, read_app_cached, read_repository,
};
use crate::github::{self, GitHub};
use crate::send::{self, Outcome};
use crate::{say, strings};
const DEFAULT_CLOUD_URL: &str = "https://mortisecloud.com";
#[derive(Default)]
pub(crate) struct Options {
pub(crate) path: PathBuf,
pub(crate) coverage: Option<PathBuf>,
pub(crate) environment: Option<String>,
pub(crate) dry_run: bool,
pub(crate) output: Option<PathBuf>,
pub(crate) commit: Option<String>,
pub(crate) branch: Option<String>,
pub(crate) pull_request: Option<u64>,
pub(crate) base_branch: Option<String>,
pub(crate) base_commit: Option<String>,
pub(crate) repository: Option<String>,
}
pub(crate) fn is_sha(text: &str) -> bool {
matches!(text.len(), 40 | 64) && text.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
}
pub(crate) fn is_environment(text: &str) -> bool {
let mut bytes = text.bytes();
bytes
.next()
.is_some_and(|b| b.is_ascii_lowercase() || b.is_ascii_digit())
&& text.len() <= 32
&& bytes.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
}
pub(crate) fn is_repository(text: &str) -> bool {
let part = |part: &str| {
!part.is_empty()
&& part
.bytes()
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'.' | b'-'))
};
text.len() <= 200
&& text
.split_once('/')
.is_some_and(|(owner, name)| part(owner) && part(name))
}
pub(crate) fn run(options: &Options) -> u8 {
let github = github::read();
let cloud = if options.dry_run {
None
} else {
let fork = github
.as_ref()
.and_then(|github| github.pull_request.as_ref())
.is_some_and(|pull| pull.fork);
match (token(), cloud_url()) {
(Ok(None), _) if fork => {
say(strings::FORK_WITHOUT_TOKEN);
return crate::PASSED;
}
(Ok(None), _) => {
say(strings::NO_TOKEN);
return crate::USAGE;
}
(Err(message), _) | (_, Err(message)) => {
say(&message);
return crate::USAGE;
}
(Ok(Some(token)), Ok(url)) => Some((url, token)),
}
};
let report = options
.coverage
.as_ref()
.map(|path| std::path::absolute(path).unwrap_or_else(|_| path.clone()));
let read_options = ReadOptions {
coverage_given: CoverageSettings {
limit: None,
report,
},
..ReadOptions::default()
};
let app = match read_app_cached(
&options.path,
&BTreeMap::new(),
&read_options,
&mut ReadCache::default(),
) {
Ok(app) => app,
Err(error @ ReadError::CoverageReport { .. }) => {
say(&strings::unreadable_report(&error.to_string()));
return crate::USAGE;
}
Err(error) => {
say(&strings::not_an_app("push", &error.to_string()));
return crate::NOT_AN_APP;
}
};
if let RulesSource::Invalid(message) = &app.rules_source {
say(&strings::invalid_rules_file(message));
}
if let Some(message) = &app.coverage_ignored {
say(&strings::coverage_ignored(message));
}
let repository = read_repository(&app.root);
let context = match context(options, github.as_ref(), repository.as_ref()) {
Ok(context) => context,
Err(message) => {
say(&message);
return crate::USAGE;
}
};
let document = push_document(app, context);
let mut pretty = serde_json::to_string_pretty(&document).expect("the document is plain data");
pretty.push('\n');
let written = match &options.output {
Some(path) => std::fs::write(path, &pretty),
None if options.dry_run => {
let mut out = std::io::stdout().lock();
match out.write_all(pretty.as_bytes()).and_then(|()| out.flush()) {
Err(error) if error.kind() == std::io::ErrorKind::BrokenPipe => Ok(()),
other => other,
}
}
None => Ok(()),
};
if let Err(error) = written {
say(&strings::document_not_written(&error.to_string()));
return crate::NOT_WRITTEN;
}
let compact = serde_json::to_vec(&document).expect("the document is plain data");
let body = gzip(&compact);
if let Some(limit) = document
.over_limit()
.or_else(|| byte_limit(compact.len(), body.len()))
{
say(&strings::over_limit(&limit));
return crate::REFUSED;
}
let Some((url, token)) = cloud else {
return crate::PASSED;
};
let hide = |text: &str| hide_token(text, &token);
match send::send(&url, &token, &body, &mut std::thread::sleep) {
Outcome::Accepted { url, warnings } => {
for warning in warnings {
say(&strings::cloud_warning(&hide(&warning)));
}
match url {
Some(url) => println!("{}", crate::printable(&strings::sent(&hide(&url)))),
None => println!("{}", strings::SENT_WITHOUT_URL),
}
crate::PASSED
}
Outcome::NotTaken { status, problem } => {
say(&strings::not_taken(status, &hide(&problem.summary())));
crate::PASSED
}
Outcome::Unreachable { reason } => {
say(&strings::unreachable(&hide(&reason)));
crate::PASSED
}
Outcome::Refused { status, problem } => {
say(&strings::refused(status, &hide(&problem.summary())));
for (pointer, message) in &problem.errors {
say(&strings::refused_member(&hide(pointer), &hide(message)));
}
crate::REFUSED
}
}
}
fn hide_token(text: &str, token: &str) -> String {
let text = text.replace(token, strings::HIDDEN_TOKEN);
match token.rsplit_once('_') {
Some((_, secret)) if secret.len() >= 16 => text.replace(secret, strings::HIDDEN_TOKEN),
_ => text,
}
}
fn token() -> Result<Option<String>, String> {
let Some(token) = github::var("MORTISE_TOKEN") else {
return Ok(None);
};
let token = token.trim();
if !token.bytes().all(|b| b.is_ascii_graphic()) {
return Err(strings::WRONG_TOKEN.to_owned());
}
Ok(Some(token.to_owned()))
}
fn cloud_url() -> Result<String, String> {
let base = github::var("MORTISE_CLOUD_URL").unwrap_or_else(|| DEFAULT_CLOUD_URL.to_owned());
endpoint(&base)
}
fn endpoint(base: &str) -> Result<String, String> {
let base = base.trim().trim_end_matches('/');
let wrong = || strings::wrong_cloud_url(base);
let uri: ureq::http::Uri = base.parse().map_err(|_| wrong())?;
let authority = uri.authority().ok_or_else(wrong)?;
if authority.as_str().contains('@') {
return Err(wrong());
}
let host = authority.host();
let local = ["localhost", "127.0.0.1", "[::1]"]
.iter()
.any(|local| host.eq_ignore_ascii_case(local));
match uri.scheme_str() {
Some(scheme) if scheme.eq_ignore_ascii_case("https") => {}
Some(scheme) if scheme.eq_ignore_ascii_case("http") && local => {}
_ => return Err(wrong()),
}
Ok(format!("{base}/api/v1/snapshots"))
}
fn context(
options: &Options,
github: Option<&GitHub>,
repository: Option<&Repository>,
) -> Result<PushContext, String> {
let pull = github.and_then(|github| github.pull_request.as_ref());
let head = repository.and_then(|repository| repository.commit.clone());
let github_sha = github.and_then(|github| github.commit.clone());
if options.commit.is_none()
&& let (Some(head), Some(sha)) = (&head, &github_sha)
&& head != sha
{
say(&strings::head_not_github_sha(head, sha));
}
let commit = options
.commit
.clone()
.or(head)
.or(github_sha)
.ok_or_else(|| strings::COMMIT_UNKNOWN.to_owned())?;
let pull_request = options.pull_request.or(pull.map(|pull| pull.number));
let base_branch = options
.base_branch
.clone()
.or_else(|| pull.and_then(|pull| pull.base_branch.clone()));
let base_commit = options
.base_commit
.clone()
.or_else(|| pull.and_then(|pull| pull.base_commit.clone()));
let complete = pull_request.is_some() && base_branch.is_some() && base_commit.is_some();
if let Some(error) = github.and_then(|github| github.event_error.as_ref())
&& !complete
{
return Err(error.clone());
}
if pull_request.is_some() && !complete {
return Err(strings::PULL_REQUEST_NEEDS_BASE.to_owned());
}
let head_commit = pull_request.map(|_| {
pull.and_then(|pull| pull.head_commit.clone())
.unwrap_or_else(|| commit.clone())
});
let branch = options
.branch
.clone()
.or_else(|| pull.and_then(|pull| pull.head_branch.clone()))
.or_else(|| github.and_then(|github| github.branch.clone()))
.or_else(|| repository.and_then(|repository| repository.branch.clone()));
let ci = match github {
Some(github) => PushCi {
provider: "github-actions".to_owned(),
run_id: github.run_id.clone(),
run_attempt: github.run_attempt,
run_url: github.run_url.clone(),
},
None => PushCi {
provider: "other".to_owned(),
run_id: None,
run_attempt: None,
run_url: None,
},
};
Ok(PushContext {
generated_at: jiff::Timestamp::now()
.strftime("%Y-%m-%dT%H:%M:%SZ")
.to_string(),
tool_version: env!("CARGO_PKG_VERSION").to_owned(),
app_path: repository
.map_or_else(|| ".".to_owned(), |repository| repository.app_path.clone()),
git: PushGit {
repository: options
.repository
.clone()
.or_else(|| github.and_then(|github| github.repository.clone())),
commit,
head_commit,
branch,
base_branch,
base_commit,
pull_request,
shallow: repository.is_some_and(|repository| repository.shallow),
},
ci,
environment: options.environment.clone(),
})
}
fn gzip(json: &[u8]) -> Vec<u8> {
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
encoder
.write_all(json)
.and_then(|()| encoder.finish())
.expect("writing to memory does not fail")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_token_goes_in_clear_only_to_this_computer() {
let path = "/api/v1/snapshots";
assert_eq!(
endpoint("https://mortisecloud.com/").unwrap(),
format!("https://mortisecloud.com{path}")
);
for local in [
"http://localhost:8000",
"http://127.0.0.1",
"http://[::1]:80/",
] {
assert!(endpoint(local).is_ok(), "{local}");
}
let at = '@';
for wrong in [
"http://mortisecloud.com".to_string(),
format!("http://localhost:x{at}evil.example/"),
format!("http://localhost{at}evil.example"),
format!("https://user:pass{at}mortisecloud.com"),
"http://localhost.evil.example".to_string(),
"ftp://localhost".to_string(),
"mortisecloud.com".to_string(),
] {
assert!(endpoint(&wrong).is_err(), "{wrong}");
}
}
}