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            "github.event.action != 'labeled' || {} != 'patch'",
35            bump_type.expr()
36        )))
37        .uses(
38            "zed-industries",
39            "zed",
40            ".github/workflows/extension_bump.yml",
41            "main",
42        )
43        .add_need(depending_job.name.clone())
44        .with(
45            Input::default()
46                .add("bump-type", bump_type.to_string())
47                .add("force-bump", true),
48        )
49        .secrets(IndexMap::from([
50            ("app-id".to_owned(), vars::ZED_ZIPPY_APP_ID.to_owned()),
51            (
52                "app-secret".to_owned(),
53                vars::ZED_ZIPPY_APP_PRIVATE_KEY.to_owned(),
54            ),
55        ]));
56
57    named::job(job)
58}
59
60fn determine_bump_type() -> (NamedJob, StepOutput) {
61    let (get_bump_type, output) = get_bump_type();
62    let job = Job::default()
63        .runs_on(runners::LINUX_DEFAULT)
64        .add_step(get_bump_type)
65        .outputs([(output.name.to_owned(), output.to_string())]);
66    (named::job(job), output)
67}
68
69fn get_bump_type() -> (Step<Run>, StepOutput) {
70    let step = named::bash(
71        indoc! {r#"
72            if [ "$HAS_MAJOR_LABEL" = "true" ]; then
73                bump_type="major"
74            elif [ "$HAS_MINOR_LABEL" = "true" ]; then
75                bump_type="minor"
76            else
77                bump_type="patch"
78            fi
79            echo "bump_type=$bump_type" >> $GITHUB_OUTPUT
80        "#},
81    )
82    .add_env(("HAS_MAJOR_LABEL",
83        indoc!{
84            "${{ (github.event.action == 'labeled' && github.event.label.name == 'major') ||
85            (github.event.action == 'synchronize' && contains(github.event.pull_request.labels.*.name, 'major')) }}"
86        }))
87    .add_env(("HAS_MINOR_LABEL",
88        indoc!{
89            "${{ (github.event.action == 'labeled' && github.event.label.name == 'minor') ||
90            (github.event.action == 'synchronize' && contains(github.event.pull_request.labels.*.name, 'minor')) }}"
91        }))
92    .id("get-bump-type");
93
94    let step_output = StepOutput::new(&step, "bump_type");
95
96    (step, step_output)
97}