use std::error::Error;
use std::ffi::{OsStr, OsString};
use std::path::PathBuf;
use std::sync::OnceLock;
pub mod gh_cli;
pub mod gh_cli_fake;
pub mod util;
pub fn init_github_cli(repo: String, fake: bool) -> Box<dyn GitHub> {
if fake {
Box::new(gh_cli_fake::GitHubCliFake::new(repo))
} else {
Box::new(gh_cli::GitHubCli::new(repo))
}
}
pub trait GitHub {
fn run_summary(&self, repo: Option<&str>, run_id: &str) -> Result<String, Box<dyn Error>>;
fn failed_job_log(&self, repo: Option<&str>, job_id: &str) -> Result<String, Box<dyn Error>>;
fn create_issue(
&self,
repo: Option<&str>,
title: &str,
body: &str,
labels: &[String],
) -> Result<(), Box<dyn Error>>;
fn issue_bodies_open_with_label(
&self,
repo: Option<&str>,
label: &str,
) -> Result<Vec<String>, Box<dyn Error>>;
fn all_labels(&self, repo: Option<&str>) -> Result<Vec<String>, Box<dyn Error>>;
fn create_label(
&self,
repo: Option<&str>,
name: &str,
color: &str,
description: &str,
force: bool,
) -> Result<(), Box<dyn Error>>;
fn default_repo(&self) -> &str;
}
include!(concat!(env!("OUT_DIR"), "/include_gh_cli.rs"));
pub static GITHUB_CLI: OnceLock<OsString> = OnceLock::new();
pub fn gh_cli() -> &'static OsStr {
GITHUB_CLI.get_or_init(|| {
let gh_cli_path = gh_cli_first_time_setup().unwrap();
OsString::from(gh_cli_path)
})
}
pub fn gh_cli_first_time_setup() -> Result<PathBuf, Box<dyn Error>> {
let mut path = std::env::current_exe()?;
path.pop();
path.push("gh-workflow-parser-deps");
if !path.exists() {
std::fs::create_dir(&path)?;
}
let gh_cli_path = path.join("gh_cli");
if !gh_cli_path.exists() {
log::debug!("the gh_cli file at {gh_cli_path:?} doesn't exist. Creating it...");
let gh_cli_bytes = GH_CLI_BYTES;
log::trace!("gh_cli_bytes size: {}", gh_cli_bytes.len());
let decompressed_gh_cli = crate::util::bzip2_decompress(gh_cli_bytes)?;
log::trace!("decompressed_gh_cli size: {}", decompressed_gh_cli.len());
std::fs::write(&gh_cli_path, decompressed_gh_cli)?;
#[cfg(target_os = "linux")]
crate::util::set_linux_file_permissions(&gh_cli_path, 0o755)?;
log::debug!("gh_cli file written to {gh_cli_path:?}");
} else {
log::debug!(
"the gh_cli file at {gh_cli_path:?} already exists. Skipping first time setup..."
);
}
Ok(gh_cli_path)
}