compliance_check.rs

 1use gh_workflow::{Event, Expression, Job, Run, Schedule, Step, Workflow};
 2
 3use crate::tasks::workflows::{
 4    runners,
 5    steps::{self, CommonJobConditions, named},
 6    vars::{self, StepOutput},
 7};
 8
 9pub fn compliance_check() -> Workflow {
10    let check = scheduled_compliance_check();
11
12    named::workflow()
13        .on(Event::default().schedule([Schedule::new("30 17 * * 2")]))
14        .add_env(("CARGO_TERM_COLOR", "always"))
15        .add_job(check.name, check.job)
16}
17
18fn scheduled_compliance_check() -> steps::NamedJob {
19    let determine_version_step = named::bash(indoc::indoc! {r#"
20        VERSION=$(sed -n 's/^version = "\(.*\)"/\1/p' crates/zed/Cargo.toml | tr -d '[:space:]')
21        if [ -z "$VERSION" ]; then
22            echo "Could not determine version from crates/zed/Cargo.toml"
23            exit 1
24        fi
25        TAG="v${VERSION}-pre"
26        echo "Checking compliance for $TAG"
27        echo "tag=$TAG" >> "$GITHUB_OUTPUT"
28    "#})
29    .id("determine-version");
30
31    let tag_output = StepOutput::new(&determine_version_step, "tag");
32
33    fn run_compliance_check(tag: &StepOutput) -> Step<Run> {
34        named::bash(
35            r#"cargo xtask compliance "$LATEST_TAG" --branch main --report-path target/compliance-report"#,
36        )
37        .id("run-compliance-check")
38        .add_env(("LATEST_TAG", tag.to_string()))
39        .add_env(("GITHUB_APP_ID", vars::ZED_ZIPPY_APP_ID))
40        .add_env(("GITHUB_APP_KEY", vars::ZED_ZIPPY_APP_PRIVATE_KEY))
41    }
42
43    fn send_failure_slack_notification(tag: &StepOutput) -> Step<Run> {
44        named::bash(indoc::indoc! {r#"
45            MESSAGE="⚠️ Scheduled compliance check failed for upcoming preview release $LATEST_TAG: There are PRs with missing reviews."
46
47            curl -X POST -H 'Content-type: application/json' \
48                --data "$(jq -n --arg text "$MESSAGE" '{"text": $text}')" \
49                "$SLACK_WEBHOOK"
50        "#})
51        .if_condition(Expression::new("failure()"))
52        .add_env(("SLACK_WEBHOOK", vars::SLACK_WEBHOOK_WORKFLOW_FAILURES))
53        .add_env(("LATEST_TAG", tag.to_string()))
54    }
55
56    named::job(
57        Job::default()
58            .with_repository_owner_guard()
59            .runs_on(runners::LINUX_SMALL)
60            .add_step(steps::checkout_repo().with_full_history())
61            .add_step(steps::cache_rust_dependencies_namespace())
62            .add_step(determine_version_step)
63            .add_step(run_compliance_check(&tag_output))
64            .add_step(send_failure_slack_notification(&tag_output)),
65    )
66}