gh_workflow/
ctx.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//! A type-safe implementation of workflow context: <https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs>

use std::fmt;
use std::marker::PhantomData;
use std::rc::Rc;

use gh_workflow_macros::Context;

use crate::Expression;

#[derive(Clone)]
pub struct Context<A> {
    marker: PhantomData<A>,
    step: Step,
}

#[derive(Default, Clone)]
enum Step {
    #[default]
    Root,
    Select {
        name: Rc<String>,
        object: Box<Step>,
    },
    Eq {
        left: Box<Step>,
        right: Box<Step>,
    },
    And {
        left: Box<Step>,
        right: Box<Step>,
    },
    Or {
        left: Box<Step>,
        right: Box<Step>,
    },
    Literal(String),
    Concat {
        left: Box<Step>,
        right: Box<Step>,
    },
}

impl<A> Context<A> {
    fn new() -> Self {
        Context { marker: PhantomData, step: Step::Root }
    }

    fn select<B>(&self, path: impl Into<String>) -> Context<B> {
        Context {
            marker: PhantomData,
            step: Step::Select {
                name: Rc::new(path.into()),
                object: Box::new(self.step.clone()),
            },
        }
    }

    pub fn eq(&self, other: Context<A>) -> Context<bool> {
        Context {
            marker: Default::default(),
            step: Step::Eq {
                left: Box::new(self.step.clone()),
                right: Box::new(other.step.clone()),
            },
        }
    }

    pub fn and(&self, other: Context<A>) -> Context<bool> {
        Context {
            marker: Default::default(),
            step: Step::And {
                left: Box::new(self.step.clone()),
                right: Box::new(other.step.clone()),
            },
        }
    }

    pub fn or(&self, other: Context<A>) -> Context<bool> {
        Context {
            marker: Default::default(),
            step: Step::Or {
                left: Box::new(self.step.clone()),
                right: Box::new(other.step.clone()),
            },
        }
    }
}

impl Context<String> {
    pub fn concat(&self, other: Context<String>) -> Context<String> {
        Context {
            marker: Default::default(),
            step: Step::Concat {
                left: Box::new(self.step.clone()),
                right: Box::new(other.step),
            },
        }
    }
}

#[allow(unused)]
#[derive(Context)]
pub struct Github {
    /// The name of the action currently running, or the id of a step.
    action: String,
    /// The path where an action is located. This property is only supported in
    /// composite actions.
    action_path: String,
    /// For a step executing an action, this is the ref of the action being
    /// executed.
    action_ref: String,
    /// For a step executing an action, this is the owner and repository name of
    /// the action.
    action_repository: String,
    /// For a composite action, the current result of the composite action.
    action_status: String,
    /// The username of the user that triggered the initial workflow run.
    actor: String,
    /// The account ID of the person or app that triggered the initial workflow
    /// run.
    actor_id: String,
    /// The URL of the GitHub REST API.
    api_url: String,
    /// The base_ref or target branch of the pull request in a workflow run.
    base_ref: String,
    /// Path on the runner to the file that sets environment variables from
    /// workflow commands.
    env: String,
    /// The full event webhook payload.
    event: serde_json::Value,
    /// The name of the event that triggered the workflow run.
    event_name: String,
    /// The path to the file on the runner that contains the full event webhook
    /// payload.
    event_path: String,
    /// The URL of the GitHub GraphQL API.
    graphql_url: String,
    /// The head_ref or source branch of the pull request in a workflow run.
    head_ref: String,
    /// The job id of the current job.
    job: String,
    /// The path of the repository.
    path: String,
    /// The short ref name of the branch or tag that triggered the workflow run.
    ref_name: String,
    /// true if branch protections are configured for the ref that triggered the
    /// workflow run.
    ref_protected: bool,
    /// The type of ref that triggered the workflow run. Valid values are branch
    /// or tag.
    ref_type: String,
    /// The owner and repository name.
    repository: String,
    /// The ID of the repository.
    repository_id: String,
    /// The repository owner's username.
    repository_owner: String,
    /// The repository owner's account ID.
    repository_owner_id: String,
    /// The Git URL to the repository.
    repository_url: String,
    /// The number of days that workflow run logs and artifacts are kept.
    retention_days: String,
    /// A unique number for each workflow run within a repository.
    run_id: String,
    /// A unique number for each run of a particular workflow in a repository.
    run_number: String,
    /// A unique number for each attempt of a particular workflow run in a
    /// repository.
    run_attempt: String,
    /// The source of a secret used in a workflow.
    secret_source: String,
    /// The URL of the GitHub server.
    server_url: String,
    /// The commit SHA that triggered the workflow.
    sha: String,
    /// A token to authenticate on behalf of the GitHub App installed on your
    /// repository.
    token: String,
    /// The username of the user that initiated the workflow run.
    triggering_actor: String,
    /// The name of the workflow.
    workflow: String,
    /// The ref path to the workflow.
    workflow_ref: String,
    /// The commit SHA for the workflow file.
    workflow_sha: String,
    /// The default working directory on the runner for steps.
    workspace: String,
}

impl Context<Github> {
    pub fn ref_(&self) -> Context<String> {
        self.select("ref")
    }
}

impl fmt::Display for Step {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Step::Root => write!(f, ""),
            Step::Select { name, object } => {
                if matches!(**object, Step::Root) {
                    write!(f, "{}", name)
                } else {
                    write!(f, "{}.{}", object, name)
                }
            }
            Step::Eq { left, right } => {
                write!(f, "{} == {}", left, right)
            }
            Step::And { left, right } => {
                write!(f, "{} && {}", left, right)
            }
            Step::Or { left, right } => {
                write!(f, "{} || {}", left, right)
            }
            Step::Literal(value) => {
                write!(f, "'{}'", value)
            }
            Step::Concat { left, right } => {
                write!(f, "{}{}", left, right)
            }
        }
    }
}

impl<A> fmt::Display for Context<A> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "${{{{ {} }}}}", self.step.to_string().replace('"', ""))
    }
}

impl<A> From<Context<A>> for Expression {
    fn from(value: Context<A>) -> Self {
        Expression::new(value.to_string())
    }
}

impl<T: Into<String>> From<T> for Context<String> {
    fn from(value: T) -> Self {
        Context {
            marker: Default::default(),
            step: Step::Literal(value.into()),
        }
    }
}

#[allow(unused)]
#[derive(Context)]
/// The job context contains information about the currently running job.
pub struct Job {
    /// A unique number for each container in a job. This property is only
    /// available if the job uses a container.
    container: Container,

    /// The services configured for a job. This property is only available if
    /// the job uses service containers.
    services: Services,

    /// The status of the current job.
    status: JobStatus,
}

/// The status of a job execution
#[derive(Clone)]
pub enum JobStatus {
    /// The job completed successfully
    Success,
    /// The job failed
    Failure,
    /// The job was cancelled
    Cancelled,
}

#[derive(Context)]
#[allow(unused)]
/// Container information for a job. This is only available if the job runs in a
/// container.
pub struct Container {
    /// The ID of the container
    id: String,
    /// The container network
    network: String,
}

#[derive(Context)]

/// Services configured for a job. This is only available if the job uses
/// service containers.
pub struct Services {}

#[cfg(test)]
mod test {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn test_expr() {
        let github = Context::github(); // Expr<Github>

        assert_eq!(github.to_string(), "${{ github }}");

        let action = github.action(); // Expr<String>
        assert_eq!(action.to_string(), "${{ github.action }}");

        let action_path = github.action_path(); // Expr<String>
        assert_eq!(action_path.to_string(), "${{ github.action_path }}");
    }

    #[test]
    fn test_expr_eq() {
        let github = Context::github();
        let action = github.action();
        let action_path = github.action_path();

        let expr = action.eq(action_path);

        assert_eq!(
            expr.to_string(),
            "${{ github.action == github.action_path }}"
        );
    }

    #[test]
    fn test_expr_and() {
        let push = Context::github().event_name().eq("push".into());
        let main = Context::github().ref_().eq("ref/heads/main".into());
        let expr = push.and(main);

        assert_eq!(
            expr.to_string(),
            "${{ github.event_name == 'push' && github.ref == 'ref/heads/main' }}"
        )
    }

    #[test]
    fn test_expr_or() {
        let github = Context::github();
        let action = github.action();
        let action_path = github.action_path();
        let action_ref = github.action_ref();

        let expr = action.eq(action_path).or(action.eq(action_ref));

        assert_eq!(
            expr.to_string(),
            "${{ github.action == github.action_path || github.action == github.action_ref }}"
        );
    }
}