use serde_json::Value;
use crate::strings;
pub(crate) struct GitHub {
pub(crate) commit: Option<String>,
pub(crate) repository: Option<String>,
pub(crate) branch: Option<String>,
pub(crate) run_id: Option<String>,
pub(crate) run_attempt: Option<u32>,
pub(crate) run_url: Option<String>,
pub(crate) pull_request: Option<PullRequest>,
pub(crate) event_error: Option<String>,
}
pub(crate) struct PullRequest {
pub(crate) number: u64,
pub(crate) head_commit: Option<String>,
pub(crate) head_branch: Option<String>,
pub(crate) base_commit: Option<String>,
pub(crate) base_branch: Option<String>,
pub(crate) fork: bool,
}
pub(crate) fn var(name: &str) -> Option<String> {
std::env::var(name)
.ok()
.filter(|value| !value.trim().is_empty())
}
pub(crate) fn read() -> Option<GitHub> {
if var("GITHUB_ACTIONS").as_deref() != Some("true") {
return None;
}
let repository = var("GITHUB_REPOSITORY");
let run_id = var("GITHUB_RUN_ID");
let run_url = match (var("GITHUB_SERVER_URL"), &repository, &run_id) {
(Some(server), Some(repository), Some(id)) => Some(format!(
"{}/{repository}/actions/runs/{id}",
server.trim_end_matches('/')
)),
_ => None,
};
let (pull_request, event_error) = match var("GITHUB_EVENT_NAME").as_deref() {
Some(event @ ("pull_request" | "pull_request_target")) => {
match pull_request(event == "pull_request") {
Ok(pull) => (Some(pull), None),
Err(error) => (None, Some(error)),
}
}
_ => (None, None),
};
Some(GitHub {
commit: var("GITHUB_SHA"),
repository,
branch: var("GITHUB_REF").and_then(|r| r.strip_prefix("refs/heads/").map(str::to_owned)),
run_id,
run_attempt: var("GITHUB_RUN_ATTEMPT").and_then(|n| n.parse().ok()),
run_url,
pull_request,
event_error,
})
}
fn pull_request(can_be_fork: bool) -> Result<PullRequest, String> {
let path = var("GITHUB_EVENT_PATH").ok_or_else(|| strings::NO_EVENT_FILE.to_owned())?;
let event: Value = std::fs::read_to_string(&path)
.map_err(|error| error.to_string())
.and_then(|text| serde_json::from_str(&text).map_err(|error| error.to_string()))
.map_err(|error| strings::unreadable_event(&path, &error))?;
let pull = &event["pull_request"];
let number = event["number"]
.as_u64()
.or_else(|| pull["number"].as_u64())
.ok_or_else(|| strings::unreadable_event(&path, strings::NO_NUMBER))?;
let text = |value: &Value| value.as_str().map(str::to_owned);
let head_repository = &pull["head"]["repo"]["full_name"];
Ok(PullRequest {
number,
head_commit: text(&pull["head"]["sha"]),
head_branch: text(&pull["head"]["ref"]),
base_commit: text(&pull["base"]["sha"]),
base_branch: text(&pull["base"]["ref"]),
fork: can_be_fork
&& (head_repository.is_null() || *head_repository != pull["base"]["repo"]["full_name"]),
})
}