use std::path::PathBuf;
pub mod vars {
pub const GITHUB_ACTIONS: &str = "GITHUB_ACTIONS";
pub const CI: &str = "CI";
pub const RUNNER_DEBUG: &str = "RUNNER_DEBUG";
pub const RUNNER_OS: &str = "RUNNER_OS";
pub const RUNNER_ARCH: &str = "RUNNER_ARCH";
pub const RUNNER_TEMP: &str = "RUNNER_TEMP";
pub const RUNNER_TOOL_CACHE: &str = "RUNNER_TOOL_CACHE";
pub const GITHUB_REPOSITORY: &str = "GITHUB_REPOSITORY";
pub const GITHUB_REPOSITORY_OWNER: &str = "GITHUB_REPOSITORY_OWNER";
pub const GITHUB_SHA: &str = "GITHUB_SHA";
pub const GITHUB_REF: &str = "GITHUB_REF";
pub const GITHUB_REF_NAME: &str = "GITHUB_REF_NAME";
pub const GITHUB_REF_TYPE: &str = "GITHUB_REF_TYPE";
pub const GITHUB_HEAD_REF: &str = "GITHUB_HEAD_REF";
pub const GITHUB_BASE_REF: &str = "GITHUB_BASE_REF";
pub const GITHUB_EVENT_NAME: &str = "GITHUB_EVENT_NAME";
pub const GITHUB_EVENT_PATH: &str = "GITHUB_EVENT_PATH";
pub const GITHUB_WORKSPACE: &str = "GITHUB_WORKSPACE";
pub const GITHUB_WORKFLOW: &str = "GITHUB_WORKFLOW";
pub const GITHUB_JOB: &str = "GITHUB_JOB";
pub const GITHUB_RUN_ID: &str = "GITHUB_RUN_ID";
pub const GITHUB_RUN_NUMBER: &str = "GITHUB_RUN_NUMBER";
pub const GITHUB_ACTOR: &str = "GITHUB_ACTOR";
pub const GITHUB_SERVER_URL: &str = "GITHUB_SERVER_URL";
pub const GITHUB_API_URL: &str = "GITHUB_API_URL";
pub const GITHUB_GRAPHQL_URL: &str = "GITHUB_GRAPHQL_URL";
}
fn var(name: &str) -> Option<String> {
std::env::var(name).ok().filter(|v| !v.is_empty())
}
#[must_use]
pub fn is_github_actions() -> bool {
std::env::var(vars::GITHUB_ACTIONS).as_deref() == Ok("true")
}
#[must_use]
pub fn is_ci() -> bool {
std::env::var(vars::CI).as_deref() == Ok("true")
}
#[must_use]
pub fn is_debug() -> bool {
std::env::var(vars::RUNNER_DEBUG).as_deref() == Ok("1")
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RunnerOs {
Linux,
Windows,
MacOs,
Other(String),
}
impl RunnerOs {
#[must_use]
pub fn from_env() -> Option<Self> {
var(vars::RUNNER_OS).map(|v| match v.as_str() {
"Linux" => RunnerOs::Linux,
"Windows" => RunnerOs::Windows,
"macOS" => RunnerOs::MacOs,
_ => RunnerOs::Other(v),
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RunnerArch {
X86,
X64,
Arm,
Arm64,
Other(String),
}
impl RunnerArch {
#[must_use]
pub fn from_env() -> Option<Self> {
var(vars::RUNNER_ARCH).map(|v| match v.as_str() {
"X86" => RunnerArch::X86,
"X64" => RunnerArch::X64,
"ARM" => RunnerArch::Arm,
"ARM64" => RunnerArch::Arm64,
_ => RunnerArch::Other(v),
})
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Context;
impl Context {
#[must_use]
pub fn new() -> Self {
Context
}
#[must_use]
pub fn repository(&self) -> Option<String> {
var(vars::GITHUB_REPOSITORY)
}
#[must_use]
pub fn repo_parts(&self) -> Option<(String, String)> {
let full = self.repository()?;
let (owner, repo) = full.split_once('/')?;
Some((owner.to_owned(), repo.to_owned()))
}
#[must_use]
pub fn repository_owner(&self) -> Option<String> {
var(vars::GITHUB_REPOSITORY_OWNER)
}
#[must_use]
pub fn sha(&self) -> Option<String> {
var(vars::GITHUB_SHA)
}
#[must_use]
pub fn git_ref(&self) -> Option<String> {
var(vars::GITHUB_REF)
}
#[must_use]
pub fn ref_name(&self) -> Option<String> {
var(vars::GITHUB_REF_NAME)
}
#[must_use]
pub fn ref_type(&self) -> Option<String> {
var(vars::GITHUB_REF_TYPE)
}
#[must_use]
pub fn head_ref(&self) -> Option<String> {
var(vars::GITHUB_HEAD_REF)
}
#[must_use]
pub fn base_ref(&self) -> Option<String> {
var(vars::GITHUB_BASE_REF)
}
#[must_use]
pub fn event_name(&self) -> Option<String> {
var(vars::GITHUB_EVENT_NAME)
}
#[must_use]
pub fn event_path(&self) -> Option<PathBuf> {
var(vars::GITHUB_EVENT_PATH).map(PathBuf::from)
}
#[must_use]
pub fn workspace(&self) -> Option<PathBuf> {
var(vars::GITHUB_WORKSPACE).map(PathBuf::from)
}
#[must_use]
pub fn workflow(&self) -> Option<String> {
var(vars::GITHUB_WORKFLOW)
}
#[must_use]
pub fn job(&self) -> Option<String> {
var(vars::GITHUB_JOB)
}
#[must_use]
pub fn run_id(&self) -> Option<u64> {
var(vars::GITHUB_RUN_ID)?.parse().ok()
}
#[must_use]
pub fn run_number(&self) -> Option<u64> {
var(vars::GITHUB_RUN_NUMBER)?.parse().ok()
}
#[must_use]
pub fn actor(&self) -> Option<String> {
var(vars::GITHUB_ACTOR)
}
#[must_use]
pub fn server_url(&self) -> Option<String> {
var(vars::GITHUB_SERVER_URL)
}
#[must_use]
pub fn api_url(&self) -> Option<String> {
var(vars::GITHUB_API_URL)
}
#[must_use]
pub fn graphql_url(&self) -> Option<String> {
var(vars::GITHUB_GRAPHQL_URL)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn os_parses_known_and_unknown() {
assert_eq!(
RunnerOs::Other("Plan9".into()),
RunnerOs::Other("Plan9".into())
);
}
#[test]
fn repo_parts_splits() {
let full = "octocat/Hello-World";
let (o, r) = full.split_once('/').unwrap();
assert_eq!((o, r), ("octocat", "Hello-World"));
}
}