bump_version.rs

  1use gh_workflow::{
  2    Event, Expression, Input, Job, Level, Permissions, PullRequest, PullRequestType, Push, Run,
  3    Step, UsesJob, Workflow, WorkflowDispatch,
  4};
  5use indexmap::IndexMap;
  6use indoc::indoc;
  7
  8use crate::tasks::workflows::{
  9    runners,
 10    steps::{CommonJobConditions, 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        .permissions(
 44            Permissions::default()
 45                .contents(Level::Write)
 46                .issues(Level::Write)
 47                .pull_requests(Level::Write),
 48        )
 49        .uses(
 50            "zed-industries",
 51            "zed",
 52            ".github/workflows/extension_bump.yml",
 53            "main",
 54        )
 55        .add_need(depending_job.name.clone())
 56        .with(
 57            Input::default()
 58                .add("bump-type", bump_type.to_string())
 59                .add("force-bump", true),
 60        )
 61        .secrets(IndexMap::from([
 62            ("app-id".to_owned(), vars::ZED_ZIPPY_APP_ID.to_owned()),
 63            (
 64                "app-secret".to_owned(),
 65                vars::ZED_ZIPPY_APP_PRIVATE_KEY.to_owned(),
 66            ),
 67        ]));
 68
 69    named::job(job)
 70}
 71
 72fn determine_bump_type() -> (NamedJob, StepOutput) {
 73    let (get_bump_type, output) = get_bump_type();
 74    let job = Job::default()
 75        .with_repository_owner_guard()
 76        .permissions(Permissions::default())
 77        .runs_on(runners::LINUX_SMALL)
 78        .add_step(get_bump_type)
 79        .outputs([(output.name.to_owned(), output.to_string())]);
 80    (named::job(job), output)
 81}
 82
 83fn get_bump_type() -> (Step<Run>, StepOutput) {
 84    let step = named::bash(
 85        indoc! {r#"
 86            if [ "$HAS_MAJOR_LABEL" = "true" ]; then
 87                bump_type="major"
 88            elif [ "$HAS_MINOR_LABEL" = "true" ]; then
 89                bump_type="minor"
 90            else
 91                bump_type="patch"
 92            fi
 93            echo "bump_type=$bump_type" >> $GITHUB_OUTPUT
 94        "#},
 95    )
 96    .add_env(("HAS_MAJOR_LABEL",
 97        indoc!{
 98            "${{ (github.event.action == 'labeled' && github.event.label.name == 'major') ||
 99            (github.event.action == 'synchronize' && contains(github.event.pull_request.labels.*.name, 'major')) }}"
100        }))
101    .add_env(("HAS_MINOR_LABEL",
102        indoc!{
103            "${{ (github.event.action == 'labeled' && github.event.label.name == 'minor') ||
104            (github.event.action == 'synchronize' && contains(github.event.pull_request.labels.*.name, 'minor')) }}"
105        }))
106    .id("get-bump-type");
107
108    let step_output = StepOutput::new(&step, "bump_type");
109
110    (step, step_output)
111}