Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/fleet-report-back-reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ jobs:
MEMBERS: ${{ inputs.members }}
ADAPTER_TIMEOUT: ${{ inputs.adapter-timeout-seconds }}
FLEET_REF: ${{ job.workflow_sha }}
# Pull-request readiness (finishingbot, advisory): the PR number from
# the caller's event, and this job's read-only token. The driver
# takes FLEET_PR_TOKEN out of its environment before any adapter runs
# and hands it to finishing-bot alone. This job grants only
# `contents: read`; GitHub documents the pull-request, checks and
# rules reads as open to any caller for public data, and whatever the
# token cannot read is named in the row ("could not read: ..."), so a
# private repository's row says so rather than reading clean.
PR_NUMBER: ${{ github.event.pull_request.number }}
FLEET_PR_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
# The job limit (timeout-minutes) assumes at most 300 s per adapter; a
Expand All @@ -255,6 +265,7 @@ jobs:
args=(--repo . --out "$RUNNER_TEMP/report-backs" --bin-dir "$RUNNER_TEMP/fleet-bin"
--enrolment "$ENROLMENT" --fleet-ref "$FLEET_REF"
--timeout "$ADAPTER_TIMEOUT" --summary "$GITHUB_STEP_SUMMARY")
[ -z "${PR_NUMBER:-}" ] || args+=(--pr "$PR_NUMBER" --pr-repo "$GITHUB_REPOSITORY")
IFS=',' read -r -a _m <<< "${MEMBERS:-}"
for m in "${_m[@]}"; do m="${m// /}"; [ -n "$m" ] && args+=(--member "$m"); done
bash "$FLEET_DIR/scripts/fleet-report-back.sh" "${args[@]}"
Expand Down
5 changes: 5 additions & 0 deletions bots/finishingbot/src/analyzers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ pub struct AuditResult {
pub tooling: AnalysisResult,
/// V1 release readiness
pub v1_readiness: AnalysisResult,
/// Pull-request readiness, when a pull request was named (`audit --pr`).
/// ADVISORY: deliberately left out of the totals and of
/// `should_block_release`, so it never changes the exit status.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub pr_readiness: Option<crate::pr_readiness::PrReadiness>,
}

impl AuditResult {
Expand Down
1 change: 1 addition & 0 deletions bots/finishingbot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod analyzers;
pub mod baseline;
pub mod config;
pub mod error;
pub mod pr_readiness;

pub use analyzers::{
claims::ClaimsAnalyzer, license::LicenseAnalyzer, placeholder::PlaceholderAnalyzer,
Expand Down
162 changes: 158 additions & 4 deletions bots/finishingbot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod config;
mod error;
#[cfg(feature = "fleet")]
mod fleet;
mod pr_readiness;

use clap::{Parser, Subcommand};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -59,6 +60,40 @@ enum Command {
/// Fail on any warnings (strict mode)
#[arg(long)]
strict: bool,

/// Also report whether this pull request is ready to review and
/// merge (advisory; never changes the exit status). Needs --pr-repo.
#[arg(long, value_name = "NUMBER", requires = "pr_repo")]
pr: Option<u64>,

/// Repository of --pr, as owner/name
#[arg(long, value_name = "OWNER/NAME", requires = "pr")]
pr_repo: Option<String>,

/// GitHub Actions run doing this audit: its own checks are not
/// counted as pending
#[arg(long, value_name = "RUN_ID")]
exclude_run_id: Option<u64>,
},

/// Report whether a pull request is ready to review and merge (advisory)
PrReadiness {
/// Repository, as owner/name
#[arg(long, value_name = "OWNER/NAME", required_unless_present = "snapshot")]
repo: Option<String>,

/// Pull request number
#[arg(long, value_name = "NUMBER", required_unless_present = "snapshot")]
pr: Option<u64>,

/// Evaluate a recorded snapshot (JSON) instead of reading GitHub
#[arg(long, value_name = "FILE", conflicts_with_all = ["repo", "pr"])]
snapshot: Option<PathBuf>,

/// GitHub Actions run doing this check: its own checks are not
/// counted as pending
#[arg(long, value_name = "RUN_ID")]
exclude_run_id: Option<u64>,
},

/// Apply automatic fixes
Expand Down Expand Up @@ -148,7 +183,24 @@ fn main() -> ExitCode {
match cli.command {
Some(Command::Init { format }) => handle_init(&config_path, &format),
Some(Command::Show) => handle_show(&config),
Some(Command::Audit { strict }) => handle_audit(&cli.path, &config, strict, &cli.format),
Some(Command::Audit {
strict,
pr,
pr_repo,
exclude_run_id,
}) => {
let readiness = match (pr, pr_repo) {
(Some(n), Some(repo)) => Some(read_pr_readiness(&repo, n, exclude_run_id)),
_ => None,
};
handle_audit(&cli.path, &config, strict, &cli.format, readiness)
}
Some(Command::PrReadiness {
repo,
pr,
snapshot,
exclude_run_id,
}) => handle_pr_readiness(repo, pr, snapshot, exclude_run_id, &cli.format),
Some(Command::Fix { only }) => handle_fix(&cli.path, &config, only.as_deref()),
Some(Command::License { list_allowed }) => handle_license(&cli.path, &config, list_allowed),
Some(Command::Placeholders { action }) => handle_placeholders(&cli.path, &config, &action),
Expand All @@ -163,11 +215,101 @@ fn main() -> ExitCode {
}) => handle_fleet_mode(repo, context, session, output, config),
None => {
// Default to audit
handle_audit(&cli.path, &config, false, &cli.format)
handle_audit(&cli.path, &config, false, &cli.format, None)
}
}
}

/// Read and evaluate one pull request's readiness. Never fails: a pull
/// request that cannot be read is a `skipped` report saying why.
fn read_pr_readiness(
repo: &str,
pr: u64,
exclude_run_id: Option<u64>,
) -> pr_readiness::PrReadiness {
use pr_readiness::{evaluate, github, EvalOptions, PrReadiness};
let started = std::time::Instant::now();
if !github::valid_repo_slug(repo) {
return PrReadiness::skipped(Some(pr), &format!("not an owner/name repository: {repo:?}"));
}
let client = match github::Client::from_env() {
Ok(c) => c,
Err(e) => return PrReadiness::skipped(Some(pr), &e),
};
let rt = match tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
{
Ok(rt) => rt,
Err(e) => return PrReadiness::skipped(Some(pr), &format!("async runtime: {e}")),
};
match rt.block_on(client.snapshot(repo, pr)) {
Ok(snapshot) => {
let mut r = evaluate(&snapshot, &EvalOptions { exclude_run_id });
r.duration_ms = started.elapsed().as_millis() as u64;
r
}
Err(e) => PrReadiness::skipped(Some(pr), &e),
}
}

fn handle_pr_readiness(
repo: Option<String>,
pr: Option<u64>,
snapshot: Option<PathBuf>,
exclude_run_id: Option<u64>,
format: &str,
) -> ExitCode {
use pr_readiness::{evaluate, EvalOptions, Snapshot};
let report = if let Some(path) = snapshot {
let parsed = std::fs::read_to_string(&path)
.map_err(|e| e.to_string())
.and_then(|t| serde_json::from_str::<Snapshot>(&t).map_err(|e| e.to_string()));
match parsed {
Ok(s) => evaluate(&s, &EvalOptions { exclude_run_id }),
Err(e) => {
eprintln!("Error reading snapshot {}: {}", path.display(), e);
return ExitCode::FAILURE;
}
}
} else {
// clap guarantees both when no snapshot is given.
read_pr_readiness(
repo.as_deref().unwrap_or_default(),
pr.unwrap_or_default(),
exclude_run_id,
)
};
if format == "json" {
match serde_json::to_string_pretty(&report) {
Ok(json) => println!("{}", json),
Err(e) => eprintln!("JSON serialization error: {}", e),
}
} else {
print_pr_readiness(&report);
}
// Advisory: the verdict never sets the exit status; only a failure to
// evaluate at all does, so a caller can tell "not ready" from "broken".
if report.skipped.is_some() {
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
}
}

fn print_pr_readiness(r: &pr_readiness::PrReadiness) {
println!();
println!("PULL REQUEST READINESS (advisory)");
println!("{}", r.summary);
if let Some(u) = &r.url {
println!("{}", u);
}
for f in &r.findings {
println!(" [{}] {:?}: {}", f.id, f.severity, f.message);
}
println!();
}

#[cfg(feature = "fleet")]
fn handle_fleet_mode(
repo: PathBuf,
Expand Down Expand Up @@ -319,7 +461,13 @@ fn handle_show(config: &config::Config) -> ExitCode {
ExitCode::SUCCESS
}

fn handle_audit(path: &Path, config: &config::Config, strict: bool, format: &str) -> ExitCode {
fn handle_audit(
path: &Path,
config: &config::Config,
strict: bool,
format: &str,
pr_readiness: Option<pr_readiness::PrReadiness>,
) -> ExitCode {
use crate::analyzers::{
claims::ClaimsAnalyzer, license::LicenseAnalyzer, placeholder::PlaceholderAnalyzer,
release::ReleaseAnalyzer, scm_files::ScmFilesAnalyzer, testing::TestingAnalyzer,
Expand Down Expand Up @@ -402,6 +550,9 @@ fn handle_audit(path: &Path, config: &config::Config, strict: bool, format: &str
}
};

// Advisory: carried in the report, never in the exit status below.
result.pr_readiness = pr_readiness;

// Output results
if format == "json" {
match serde_json::to_string_pretty(&result) {
Expand All @@ -410,9 +561,12 @@ fn handle_audit(path: &Path, config: &config::Config, strict: bool, format: &str
}
} else {
print_summary(&result);
if let Some(r) = &result.pr_readiness {
print_pr_readiness(r);
}
}

// Determine exit code
// Determine exit code (pull-request readiness takes no part)
if result.should_block_release() {
ExitCode::FAILURE
} else if strict && result.total_warnings() > 0 {
Expand Down
Loading
Loading