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::{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 .permissions(Permissions::default())
76 .runs_on(runners::LINUX_DEFAULT)
77 .add_step(get_bump_type)
78 .outputs([(output.name.to_owned(), output.to_string())]);
79 (named::job(job), output)
80}
81
82fn get_bump_type() -> (Step<Run>, StepOutput) {
83 let step = named::bash(
84 indoc! {r#"
85 if [ "$HAS_MAJOR_LABEL" = "true" ]; then
86 bump_type="major"
87 elif [ "$HAS_MINOR_LABEL" = "true" ]; then
88 bump_type="minor"
89 else
90 bump_type="patch"
91 fi
92 echo "bump_type=$bump_type" >> $GITHUB_OUTPUT
93 "#},
94 )
95 .add_env(("HAS_MAJOR_LABEL",
96 indoc!{
97 "${{ (github.event.action == 'labeled' && github.event.label.name == 'major') ||
98 (github.event.action == 'synchronize' && contains(github.event.pull_request.labels.*.name, 'major')) }}"
99 }))
100 .add_env(("HAS_MINOR_LABEL",
101 indoc!{
102 "${{ (github.event.action == 'labeled' && github.event.label.name == 'minor') ||
103 (github.event.action == 'synchronize' && contains(github.event.pull_request.labels.*.name, 'minor')) }}"
104 }))
105 .id("get-bump-type");
106
107 let step_output = StepOutput::new(&step, "bump_type");
108
109 (step, step_output)
110}