1use gh_workflow::{
  2    Concurrency, Event, Expression, Job, PullRequest, Push, Run, Step, Use, Workflow,
  3};
  4use indexmap::IndexMap;
  5
  6use crate::tasks::workflows::{
  7    nix_build::build_nix, runners::Arch, steps::BASH_SHELL, vars::PathCondition,
  8};
  9
 10use super::{
 11    runners::{self, Platform},
 12    steps::{self, FluentBuilder, NamedJob, named, release_job},
 13};
 14
 15pub(crate) fn run_tests() -> Workflow {
 16    // Specify anything which should potentially skip full test suite in this regex:
 17    // - docs/
 18    // - script/update_top_ranking_issues/
 19    // - .github/ISSUE_TEMPLATE/
 20    // - .github/workflows/  (except .github/workflows/ci.yml)
 21    let should_run_tests = PathCondition::inverted(
 22        "run_tests",
 23        r"^(docs/|script/update_top_ranking_issues/|\.github/(ISSUE_TEMPLATE|workflows/(?!run_tests)))",
 24    );
 25    let should_check_docs = PathCondition::new("run_docs", r"^docs/");
 26    let should_check_scripts = PathCondition::new(
 27        "run_action_checks",
 28        r"^\.github/(workflows/|actions/|actionlint.yml)|tooling/xtask",
 29    );
 30    let should_check_licences =
 31        PathCondition::new("run_licenses", r"^(Cargo.lock|script/.*licenses)");
 32    let should_build_nix = PathCondition::new(
 33        "run_nix",
 34        r"^(nix/|flake\.|Cargo\.|rust-toolchain.toml|\.cargo/config.toml)",
 35    );
 36
 37    let orchestrate = orchestrate(&[
 38        &should_check_scripts,
 39        &should_check_docs,
 40        &should_check_licences,
 41        &should_build_nix,
 42        &should_run_tests,
 43    ]);
 44
 45    let jobs = [
 46        orchestrate,
 47        check_style(),
 48        should_run_tests.guard(run_platform_tests(Platform::Windows)),
 49        should_run_tests.guard(run_platform_tests(Platform::Linux)),
 50        should_run_tests.guard(run_platform_tests(Platform::Mac)),
 51        should_run_tests.guard(doctests()),
 52        should_run_tests.guard(check_workspace_binaries()),
 53        should_run_tests.guard(check_postgres_and_protobuf_migrations()), // could be more specific here?
 54        should_run_tests.guard(check_dependencies()), // could be more specific here?
 55        should_check_docs.guard(check_docs()),
 56        should_check_licences.guard(check_licenses()),
 57        should_check_scripts.guard(check_scripts()),
 58        should_build_nix.guard(build_nix(
 59            Platform::Linux,
 60            Arch::X86_64,
 61            "debug",
 62            // *don't* cache the built output
 63            Some("-zed-editor-[0-9.]*-nightly"),
 64            &[],
 65        )),
 66        should_build_nix.guard(build_nix(
 67            Platform::Mac,
 68            Arch::ARM64,
 69            "debug",
 70            // *don't* cache the built output
 71            Some("-zed-editor-[0-9.]*-nightly"),
 72            &[],
 73        )),
 74    ];
 75    let tests_pass = tests_pass(&jobs);
 76
 77    let mut workflow = named::workflow()
 78        .add_event(Event::default()
 79            .push(
 80                Push::default()
 81                    .add_branch("main")
 82                    .add_branch("v[0-9]+.[0-9]+.x")
 83            )
 84            .pull_request(PullRequest::default().add_branch("**"))
 85        )
 86        .concurrency(Concurrency::default()
 87            .group("${{ github.workflow }}-${{ github.ref_name }}-${{ github.ref_name == 'main' && github.sha || 'anysha' }}")
 88            .cancel_in_progress(true)
 89        )
 90        .add_env(( "CARGO_TERM_COLOR", "always" ))
 91        .add_env(( "RUST_BACKTRACE", 1 ))
 92        .add_env(( "CARGO_INCREMENTAL", 0 ));
 93    for job in jobs {
 94        workflow = workflow.add_job(job.name, job.job)
 95    }
 96    workflow.add_job(tests_pass.name, tests_pass.job)
 97}
 98
 99// Generates a bash script that checks changed files against regex patterns
100// and sets GitHub output variables accordingly
101fn orchestrate(rules: &[&PathCondition]) -> NamedJob {
102    let name = "orchestrate".to_owned();
103    let step_name = "filter".to_owned();
104    let mut script = String::new();
105
106    script.push_str(indoc::indoc! {r#"
107        if [ -z "$GITHUB_BASE_REF" ]; then
108          echo "Not in a PR context (i.e., push to main/stable/preview)"
109          COMPARE_REV="$(git rev-parse HEAD~1)"
110        else
111          echo "In a PR context comparing to pull_request.base.ref"
112          git fetch origin "$GITHUB_BASE_REF" --depth=350
113          COMPARE_REV="$(git merge-base "origin/${GITHUB_BASE_REF}" HEAD)"
114        fi
115        CHANGED_FILES="$(git diff --name-only "$COMPARE_REV" ${{ github.sha }})"
116
117        check_pattern() {
118          local output_name="$1"
119          local pattern="$2"
120          local grep_arg="$3"
121
122          echo "$CHANGED_FILES" | grep "$grep_arg" "$pattern" && \
123            echo "${output_name}=true" >> "$GITHUB_OUTPUT" || \
124            echo "${output_name}=false" >> "$GITHUB_OUTPUT"
125        }
126
127    "#});
128
129    let mut outputs = IndexMap::new();
130
131    for rule in rules {
132        assert!(
133            rule.set_by_step
134                .borrow_mut()
135                .replace(name.clone())
136                .is_none()
137        );
138        assert!(
139            outputs
140                .insert(
141                    rule.name.to_owned(),
142                    format!("${{{{ steps.{}.outputs.{} }}}}", step_name, rule.name)
143                )
144                .is_none()
145        );
146
147        let grep_arg = if rule.invert { "-qvP" } else { "-qP" };
148        script.push_str(&format!(
149            "check_pattern \"{}\" '{}' {}\n",
150            rule.name, rule.pattern, grep_arg
151        ));
152    }
153
154    let job = Job::default()
155        .runs_on(runners::LINUX_SMALL)
156        .cond(Expression::new(
157            "github.repository_owner == 'zed-industries'",
158        ))
159        .outputs(outputs)
160        .add_step(steps::checkout_repo().add_with((
161            "fetch-depth",
162            "${{ github.ref == 'refs/heads/main' && 2 || 350 }}",
163        )))
164        .add_step(
165            Step::new(step_name.clone())
166                .run(script)
167                .id(step_name)
168                .shell(BASH_SHELL),
169        );
170
171    NamedJob { name, job }
172}
173
174pub(crate) fn tests_pass(jobs: &[NamedJob]) -> NamedJob {
175    let mut script = String::from(indoc::indoc! {r#"
176        set +x
177        EXIT_CODE=0
178
179        check_result() {
180          echo "* $1: $2"
181          if [[ "$2" != "skipped" && "$2" != "success" ]]; then EXIT_CODE=1; fi
182        }
183
184    "#});
185
186    script.push_str(
187        &jobs
188            .iter()
189            .map(|job| {
190                format!(
191                    "check_result \"{}\" \"${{{{ needs.{}.result }}}}\"",
192                    job.name, job.name
193                )
194            })
195            .collect::<Vec<_>>()
196            .join("\n"),
197    );
198
199    script.push_str("\n\nexit $EXIT_CODE\n");
200
201    let job = Job::default()
202        .runs_on(runners::LINUX_SMALL)
203        .needs(
204            jobs.iter()
205                .map(|j| j.name.to_string())
206                .collect::<Vec<String>>(),
207        )
208        .cond(Expression::new(
209            "github.repository_owner == 'zed-industries' && always()",
210        ))
211        .add_step(named::bash(&script));
212
213    named::job(job)
214}
215
216fn check_style() -> NamedJob {
217    fn check_for_typos() -> Step<Use> {
218        named::uses(
219            "crate-ci",
220            "typos",
221            "80c8a4945eec0f6d464eaf9e65ed98ef085283d1",
222        ) // v1.38.1
223        .with(("config", "./typos.toml"))
224    }
225    named::job(
226        release_job(&[])
227            .runs_on(runners::LINUX_MEDIUM)
228            .add_step(steps::checkout_repo())
229            .add_step(steps::setup_pnpm())
230            .add_step(steps::script("./script/prettier"))
231            .add_step(steps::script("./script/check-todos"))
232            .add_step(steps::script("./script/check-keymaps"))
233            .add_step(check_for_typos())
234            .add_step(steps::cargo_fmt()),
235    )
236}
237
238fn check_dependencies() -> NamedJob {
239    fn install_cargo_machete() -> Step<Use> {
240        named::uses(
241            "clechasseur",
242            "rs-cargo",
243            "8435b10f6e71c2e3d4d3b7573003a8ce4bfc6386", // v2
244        )
245        .add_with(("command", "install"))
246        .add_with(("args", "cargo-machete@0.7.0"))
247    }
248
249    fn run_cargo_machete() -> Step<Use> {
250        named::uses(
251            "clechasseur",
252            "rs-cargo",
253            "8435b10f6e71c2e3d4d3b7573003a8ce4bfc6386", // v2
254        )
255        .add_with(("command", "machete"))
256    }
257
258    fn check_cargo_lock() -> Step<Run> {
259        named::bash("cargo update --locked --workspace")
260    }
261
262    fn check_vulnerable_dependencies() -> Step<Use> {
263        named::uses(
264            "actions",
265            "dependency-review-action",
266            "67d4f4bd7a9b17a0db54d2a7519187c65e339de8", // v4
267        )
268        .if_condition(Expression::new("github.event_name == 'pull_request'"))
269        .with(("license-check", false))
270    }
271
272    named::job(
273        release_job(&[])
274            .runs_on(runners::LINUX_SMALL)
275            .add_step(steps::checkout_repo())
276            .add_step(install_cargo_machete())
277            .add_step(run_cargo_machete())
278            .add_step(check_cargo_lock())
279            .add_step(check_vulnerable_dependencies()),
280    )
281}
282
283fn check_workspace_binaries() -> NamedJob {
284    named::job(
285        release_job(&[])
286            .runs_on(runners::LINUX_LARGE)
287            .add_step(steps::checkout_repo())
288            .add_step(steps::setup_cargo_config(Platform::Linux))
289            .map(steps::install_linux_dependencies)
290            .add_step(steps::script("cargo build -p collab"))
291            .add_step(steps::script("cargo build --workspace --bins --examples"))
292            .add_step(steps::cleanup_cargo_config(Platform::Linux)),
293    )
294}
295
296pub(crate) fn run_platform_tests(platform: Platform) -> NamedJob {
297    let runner = match platform {
298        Platform::Windows => runners::WINDOWS_DEFAULT,
299        Platform::Linux => runners::LINUX_DEFAULT,
300        Platform::Mac => runners::MAC_DEFAULT,
301    };
302    NamedJob {
303        name: format!("run_tests_{platform}"),
304        job: release_job(&[])
305            .cond(Expression::new("false"))
306            .runs_on(runner)
307            .add_step(steps::checkout_repo())
308            .add_step(steps::setup_cargo_config(platform))
309            .when(
310                platform == Platform::Linux,
311                steps::install_linux_dependencies,
312            )
313            .add_step(steps::setup_node())
314            .add_step(steps::clippy(platform))
315            .add_step(steps::cargo_install_nextest(platform))
316            .add_step(steps::clear_target_dir_if_large(platform))
317            .add_step(steps::cargo_nextest(platform))
318            .add_step(steps::cleanup_cargo_config(platform)),
319    }
320}
321
322pub(crate) fn check_postgres_and_protobuf_migrations() -> NamedJob {
323    fn remove_untracked_files() -> Step<Run> {
324        named::bash("git clean -df")
325    }
326
327    fn ensure_fresh_merge() -> Step<Run> {
328        named::bash(indoc::indoc! {r#"
329            if [ -z "$GITHUB_BASE_REF" ];
330            then
331              echo "BUF_BASE_BRANCH=$(git merge-base origin/main HEAD)" >> "$GITHUB_ENV"
332            else
333              git checkout -B temp
334              git merge -q "origin/$GITHUB_BASE_REF" -m "merge main into temp"
335              echo "BUF_BASE_BRANCH=$GITHUB_BASE_REF" >> "$GITHUB_ENV"
336            fi
337        "#})
338    }
339
340    fn bufbuild_setup_action() -> Step<Use> {
341        named::uses("bufbuild", "buf-setup-action", "v1").add_with(("version", "v1.29.0"))
342    }
343
344    fn bufbuild_breaking_action() -> Step<Use> {
345        named::uses("bufbuild", "buf-breaking-action", "v1").add_with(("input", "crates/proto/proto/"))
346            .add_with(("against", "https://github.com/${GITHUB_REPOSITORY}.git#branch=${BUF_BASE_BRANCH},subdir=crates/proto/proto/"))
347    }
348
349    named::job(
350        release_job(&[])
351            .runs_on(runners::MAC_DEFAULT)
352            .add_step(steps::checkout_repo().with(("fetch-depth", 0))) // fetch full history
353            .add_step(remove_untracked_files())
354            .add_step(ensure_fresh_merge())
355            .add_step(bufbuild_setup_action())
356            .add_step(bufbuild_breaking_action()),
357    )
358}
359
360fn doctests() -> NamedJob {
361    fn run_doctests() -> Step<Run> {
362        named::bash(indoc::indoc! {r#"
363            cargo test --workspace --doc --no-fail-fast
364        "#})
365        .id("run_doctests")
366    }
367
368    named::job(
369        release_job(&[])
370            .runs_on(runners::LINUX_DEFAULT)
371            .add_step(steps::checkout_repo())
372            .add_step(steps::cache_rust_dependencies())
373            .map(steps::install_linux_dependencies)
374            .add_step(steps::setup_cargo_config(Platform::Linux))
375            .add_step(run_doctests())
376            .add_step(steps::cleanup_cargo_config(Platform::Linux)),
377    )
378}
379
380fn check_licenses() -> NamedJob {
381    named::job(
382        Job::default()
383            .runs_on(runners::LINUX_SMALL)
384            .add_step(steps::checkout_repo())
385            .add_step(steps::script("./script/check-licenses"))
386            .add_step(steps::script("./script/generate-licenses")),
387    )
388}
389
390fn check_docs() -> NamedJob {
391    fn lychee_link_check(dir: &str) -> Step<Use> {
392        named::uses(
393            "lycheeverse",
394            "lychee-action",
395            "82202e5e9c2f4ef1a55a3d02563e1cb6041e5332",
396        ) // v2.4.1
397        .add_with(("args", format!("--no-progress --exclude '^http' '{dir}'")))
398        .add_with(("fail", true))
399        .add_with(("jobSummary", false))
400    }
401
402    fn install_mdbook() -> Step<Use> {
403        named::uses(
404            "peaceiris",
405            "actions-mdbook",
406            "ee69d230fe19748b7abf22df32acaa93833fad08", // v2
407        )
408        .with(("mdbook-version", "0.4.37"))
409    }
410
411    fn build_docs() -> Step<Run> {
412        named::bash(indoc::indoc! {r#"
413            mkdir -p target/deploy
414            mdbook build ./docs --dest-dir=../target/deploy/docs/
415        "#})
416    }
417
418    named::job(
419        release_job(&[])
420            .runs_on(runners::LINUX_LARGE)
421            .add_step(steps::checkout_repo())
422            .add_step(steps::setup_cargo_config(Platform::Linux))
423            // todo(ci): un-inline build_docs/action.yml here
424            .add_step(steps::cache_rust_dependencies())
425            .add_step(
426                lychee_link_check("./docs/src/**/*"), // check markdown links
427            )
428            .map(steps::install_linux_dependencies)
429            .add_step(install_mdbook())
430            .add_step(build_docs())
431            .add_step(
432                lychee_link_check("target/deploy/docs"), // check links in generated html
433            ),
434    )
435}
436
437fn check_scripts() -> NamedJob {
438    fn download_actionlint() -> Step<Run> {
439        named::bash(
440            "bash <(curl https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash)",
441        )
442    }
443
444    fn run_actionlint() -> Step<Run> {
445        named::bash(indoc::indoc! {r#"
446            ${{ steps.get_actionlint.outputs.executable }} -color
447        "#})
448    }
449
450    fn run_shellcheck() -> Step<Run> {
451        named::bash("./script/shellcheck-scripts error")
452    }
453
454    fn check_xtask_workflows() -> Step<Run> {
455        named::bash(indoc::indoc! {r#"
456            cargo xtask workflows
457            if ! git diff --exit-code .github; then
458              echo "Error: .github directory has uncommitted changes after running 'cargo xtask workflows'"
459              echo "Please run 'cargo xtask workflows' locally and commit the changes"
460              exit 1
461            fi
462        "#})
463    }
464
465    named::job(
466        release_job(&[])
467            .runs_on(runners::LINUX_SMALL)
468            .add_step(steps::checkout_repo())
469            .add_step(run_shellcheck())
470            .add_step(download_actionlint().id("get_actionlint"))
471            .add_step(run_actionlint())
472            .add_step(check_xtask_workflows()),
473    )
474}