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