bump_version.rs

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