bump_version.rs

  1use gh_workflow::{
  2    Event, Expression, Input, Job, PullRequest, PullRequestType, Push, Run, Step, UsesJob,
  3    Workflow, WorkflowDispatch,
  4};
  5use indexmap::IndexMap;
  6use indoc::indoc;
  7
  8use crate::tasks::workflows::{
  9    runners,
 10    steps::{NamedJob, named},
 11    vars::{self, JobOutput, StepOutput, one_workflow_per_non_main_branch_and_token},
 12};
 13
 14pub(crate) fn bump_version() -> Workflow {
 15    let (determine_bump_type, bump_type) = determine_bump_type();
 16    let bump_type = bump_type.as_job_output(&determine_bump_type);
 17
 18    let call_bump_version = call_bump_version(&determine_bump_type, bump_type);
 19
 20    named::workflow()
 21        .on(Event::default()
 22            .push(
 23                Push::default()
 24                    .add_branch("main")
 25                    .add_ignored_path(".github/**"),
 26            )
 27            .pull_request(PullRequest::default().add_type(PullRequestType::Labeled))
 28            .workflow_dispatch(WorkflowDispatch::default()))
 29        .concurrency(one_workflow_per_non_main_branch_and_token("labels"))
 30        .add_job(determine_bump_type.name, determine_bump_type.job)
 31        .add_job(call_bump_version.name, call_bump_version.job)
 32}
 33
 34pub(crate) fn call_bump_version(
 35    depending_job: &NamedJob,
 36    bump_type: JobOutput,
 37) -> NamedJob<UsesJob> {
 38    let job = Job::default()
 39        .cond(Expression::new(format!(
 40            "github.event.action != 'labeled' || {} != 'patch'",
 41            bump_type.expr()
 42        )))
 43        .uses(
 44            "zed-industries",
 45            "zed",
 46            ".github/workflows/extension_bump.yml",
 47            "main",
 48        )
 49        .add_need(depending_job.name.clone())
 50        .with(
 51            Input::default()
 52                .add("bump-type", bump_type.to_string())
 53                .add("force-bump", true),
 54        )
 55        .secrets(IndexMap::from([
 56            ("app-id".to_owned(), vars::ZED_ZIPPY_APP_ID.to_owned()),
 57            (
 58                "app-secret".to_owned(),
 59                vars::ZED_ZIPPY_APP_PRIVATE_KEY.to_owned(),
 60            ),
 61        ]));
 62
 63    named::job(job)
 64}
 65
 66fn determine_bump_type() -> (NamedJob, StepOutput) {
 67    let (get_bump_type, output) = get_bump_type();
 68    let job = Job::default()
 69        .runs_on(runners::LINUX_DEFAULT)
 70        .add_step(get_bump_type)
 71        .outputs([(output.name.to_owned(), output.to_string())]);
 72    (named::job(job), output)
 73}
 74
 75fn get_bump_type() -> (Step<Run>, StepOutput) {
 76    let step = named::bash(
 77        indoc! {r#"
 78            if [ "$HAS_MAJOR_LABEL" = "true" ]; then
 79                bump_type="major"
 80            elif [ "$HAS_MINOR_LABEL" = "true" ]; then
 81                bump_type="minor"
 82            else
 83                bump_type="patch"
 84            fi
 85            echo "bump_type=$bump_type" >> $GITHUB_OUTPUT
 86        "#},
 87    )
 88    .add_env(("HAS_MAJOR_LABEL",
 89        indoc!{
 90            "${{ (github.event.action == 'labeled' && github.event.label.name == 'major') ||
 91            (github.event.action == 'synchronize' && contains(github.event.pull_request.labels.*.name, 'major')) }}"
 92        }))
 93    .add_env(("HAS_MINOR_LABEL",
 94        indoc!{
 95            "${{ (github.event.action == 'labeled' && github.event.label.name == 'minor') ||
 96            (github.event.action == 'synchronize' && contains(github.event.pull_request.labels.*.name, 'minor')) }}"
 97        }))
 98    .id("get-bump-type");
 99
100    let step_output = StepOutput::new(&step, "bump_type");
101
102    (step, step_output)
103}