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