gh_workflow_tailcall/
workflow.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//! Workflow is designed to be used for most Rust projects that are built at
//! Tailcall. Though gh-workflow makes it much easier to write workflows you
//! still need to constantly keep referring to the Github documentation to write
//! your own workflows. This module saves all that time by using feature flags
//! to enable or disable features that you want in your workflow. Based on the
//! features enabled or disabled a workflow is generated.

use ctx::Context;
use derive_setters::Setters;
use gh_workflow::{Workflow as GHWorkflow, *};
use release_plz::{Command, Release};
use toolchain::Toolchain;

#[derive(Debug, Clone, Setters)]
pub struct Workflow {
    /// When enabled, a release job is added to the workflow.
    /// *IMPORTANT:* Ensure `secrets.CARGO_REGISTRY_TOKEN` is set for your
    /// github action.
    pub auto_release: bool,

    /// Name of the workflow.
    pub name: String,
}

impl Default for Workflow {
    fn default() -> Self {
        Self { auto_release: true, name: "CI".into() }
    }
}

impl From<Workflow> for GHWorkflow {
    fn from(value: Workflow) -> Self {
        let flags = RustFlags::deny("warnings");

        let event = Event::default()
            .push(Push::default().add_branch("main"))
            .pull_request(
                PullRequest::default()
                    .add_type(PullRequestType::Opened)
                    .add_type(PullRequestType::Synchronize)
                    .add_type(PullRequestType::Reopened)
                    .add_branch("main"),
            );

        let is_main = Context::github().ref_().eq("refs/heads/main".into());
        let is_push = Context::github().event_name().eq("push".into());
        let cond = is_main.and(is_push);

        // Jobs
        let build = build_and_test();
        let mut workflow = GHWorkflow::new(value.name)
            .add_env(flags)
            .on(event)
            .add_job("build", build.clone());

        if value.auto_release {
            let permissions = Permissions::default()
                .pull_requests(Level::Write)
                .packages(Level::Write)
                .contents(Level::Write);

            let release = release_job(&cond, &build, &permissions);
            let release_pr = release_pr_job(cond, &build, permissions);
            workflow = workflow
                .add_job("release", release)
                .add_job("release-pr", release_pr);
        }

        workflow
    }
}

fn release_pr_job(cond: Context<bool>, build: &Job, permissions: Permissions) -> Job {
    Job::new("Release PR")
        .cond(cond.clone())
        .concurrency(
            Concurrency::new(Expression::new("release-${{github.ref}}")).cancel_in_progress(false),
        )
        .add_needs(build.clone())
        .add_env(Env::github())
        .add_env(Env::new(
            "CARGO_REGISTRY_TOKEN",
            "${{ secrets.CARGO_REGISTRY_TOKEN }}",
        ))
        .permissions(permissions)
        .add_step(Step::checkout())
        .add_step(Release::default().command(Command::ReleasePR))
}

fn release_job(cond: &Context<bool>, build: &Job, permissions: &Permissions) -> Job {
    Job::new("Release")
        .cond(cond.clone())
        .add_needs(build.clone())
        .add_env(Env::github())
        .add_env(Env::new(
            "CARGO_REGISTRY_TOKEN",
            "${{ secrets.CARGO_REGISTRY_TOKEN }}",
        ))
        .permissions(permissions.clone())
        .add_step(Step::checkout())
        .add_step(Release::default().command(Command::Release))
}

fn build_and_test() -> Job {
    Job::new("Build and Test")
        .permissions(Permissions::default().contents(Level::Read))
        .add_step(Step::checkout())
        .add_step(
            Toolchain::default()
                .add_stable()
                .add_nightly()
                .add_clippy()
                .add_fmt(),
        )
        .add_step(
            Cargo::new("test")
                .args("--all-features --workspace")
                .name("Cargo Test"),
        )
        .add_step(
            Cargo::new("fmt")
                .nightly()
                .args("--check")
                .name("Cargo Fmt"),
        )
        .add_step(
            Cargo::new("clippy")
                .nightly()
                .args("--all-features --workspace -- -D warnings")
                .name("Cargo Clippy"),
        )
}