Skip to content

Commit

Permalink
get basic CI display working
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasLaPiana committed Jan 1, 2024
1 parent 78c6b24 commit f39d725
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 55 deletions.
114 changes: 114 additions & 0 deletions src/ci.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use crate::models::CiInfo;
use chrono;
use chrono::{DateTime, Utc};
use cli_table::{format::Justify, print_stdout, Cell, Style, Table};
use colored::Colorize;
use git2::Repository;
use octocrab::models::workflows::Conclusion;
use octocrab::params::workflows::Filter;

pub struct RunResult {
name: String,
job: String,
status: Conclusion,
started_at: Option<DateTime<Utc>>,
ended_at: Option<DateTime<Utc>>,
}
impl RunResult {
pub fn get_elapsed_time(&self) -> String {
let elapsed_time = match self.ended_at {
Some(ended_at) => {
let started_at = self.started_at.as_ref().unwrap();
let elapsed_time = ended_at.signed_duration_since(started_at).num_seconds();
elapsed_time.to_string()
}
None => "N/A".to_string(),
};
elapsed_time
}
}

/// Print the execution results in a pretty table format
pub fn display_ci_results(results: &[RunResult]) {
let mut table = Vec::new();

results.iter().for_each(|result| {
let status = match result.status {
Conclusion::Success => "Success"
.to_string()
.green()
.cell()
.justify(Justify::Center),
Conclusion::Failure => "Failure".to_string().red().cell().justify(Justify::Center),
_ => "Other".to_string().red().cell().justify(Justify::Center),
};

table.push(vec![
result.name.to_owned().cell(),
result.job.clone().cell().justify(Justify::Center),
status,
result.get_elapsed_time().cell().justify(Justify::Center),
])
});

assert!(print_stdout(
table
.table()
.title(vec![
"Name".yellow().cell().bold(true),
"Job".yellow().cell().bold(true),
"Status".yellow().cell().bold(true),
"Elapsed Time".yellow().cell().bold(true),
])
.bold(true),
)
.is_ok());
}

/// Show the most recent CI workflow
pub async fn display_ci_status(ci_info: CiInfo) {
let repo = Repository::open_from_env().unwrap();
let head = repo.head().unwrap();
assert!(head.is_branch());
let branch = head.name().unwrap().split('/').last().unwrap();
println!("> Getting CI status for branch: {}", branch);

let instance = octocrab::instance();
let octo_instance = instance.workflows(ci_info.repo_owner, ci_info.repo_name);

let workflow = octo_instance
.list_all_runs()
.page(1u32)
.per_page(1)
.branch(branch)
.send()
.await
.unwrap()
.into_iter()
.next()
.unwrap();

let mut results = Vec::new();

let jobs = octo_instance
.list_jobs(workflow.id)
.per_page(100)
.page(1u8)
.filter(Filter::All)
.send()
.await
.unwrap();

jobs.into_iter().for_each(|job| {
let steps = job.steps.into_iter().map(|step| RunResult {
name: step.name,
job: job.name.clone(),
status: step.conclusion.unwrap(),
started_at: step.started_at,
ended_at: step.completed_at,
});
results.extend(steps);
});

display_ci_results(&results);
}
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod ci;
mod cli;
mod docs;
mod execution;
Expand Down Expand Up @@ -94,7 +95,7 @@ pub async fn rox() -> RoxResult<()> {
}
"ci" => {
assert!(ci.is_some());
output::display_ci_status(ci.unwrap()).await;
ci::display_ci_status(ci.unwrap()).await;
std::process::exit(0);
}
"pl" => {
Expand Down
55 changes: 1 addition & 54 deletions src/output.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::models::{AllResults, CiInfo, PassFail};
use crate::models::{AllResults, PassFail};
use cli_table::{format::Justify, print_stdout, Cell, Style, Table};
use colored::Colorize;
use git2::Repository;
use octocrab::params::workflows::Filter;

const LOG_DIR: &str = ".rox";

Expand Down Expand Up @@ -83,54 +81,3 @@ pub fn display_execution_results(results: &AllResults) {
)
.is_ok());
}

/// Show the most recent CI workflow
pub async fn display_ci_status(ci_info: CiInfo) {
let repo = Repository::open_from_env().unwrap();
let head = repo.head().unwrap();
assert!(head.is_branch());
let branch = head.name().unwrap().split('/').last().unwrap();
println!("> Getting CI status for branch: {}", branch);

let instance = octocrab::instance();
let octo_instance = instance.workflows(ci_info.repo_owner, ci_info.repo_name);

let workflow = octo_instance
.list_all_runs()
.page(1u32)
.per_page(1)
.branch(branch)
.send()
.await
.unwrap()
.into_iter()
.next()
.unwrap();

let jobs = octo_instance
.list_jobs(workflow.id)
// Optional Parameters
.per_page(100)
.page(1u8)
.filter(Filter::All)
// Send the request
.send()
.await
.unwrap();

jobs.into_iter().for_each(|job| {
println!(
"{} | {} | {:?} | {}",
job.id, job.name, job.status, job.started_at
);

job.steps.into_iter().for_each(|step| {
println!(
"{} | {:?} | {}",
step.name,
step.status,
step.started_at.unwrap()
)
})
});
}

0 comments on commit f39d725

Please sign in to comment.