action-core 0.0.13

GitHub actions toolkit
Documentation
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
pub mod env;
pub mod input;
pub mod summary;
pub mod utils;

use std::collections::HashMap;
use std::path::Path;

#[cfg(feature = "derive")]
pub use action_derive::Action;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Hash)]
pub enum LogLevel {
    Debug,
    Error,
    Warning,
    Notice,
}

impl std::fmt::Display for LogLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            LogLevel::Debug => write!(f, "debug"),
            LogLevel::Error => write!(f, "error"),
            LogLevel::Warning => write!(f, "warning"),
            LogLevel::Notice => write!(f, "notice"),
        }
    }
}

/// Prepare key value message.
///
/// # Errors
/// If the value contains the randomly generated delimiter.
pub fn prepare_kv_message(key: &str, value: &str) -> Result<String, ValueError> {
    use uuid::Uuid;
    let delimiter = format!("ghadelimiter_{}", Uuid::new_v4());

    // These should realistically never happen,
    // but just in case someone finds a way to exploit
    // uuid generation let's not allow keys or values that
    // contain the delimiter.
    if key.contains(&delimiter) {
        return Err(ValueError::ContainsDelimiter { delimiter });
    }

    if value.contains(&delimiter) {
        return Err(ValueError::ContainsDelimiter { delimiter });
    }
    Ok(format!("{key}<<{delimiter}\n{value}\n{delimiter}"))
}

/// Sets env variable for this action and future actions in the job.
///
/// # Errors
/// If the file command fails.
pub fn export_var(
    env: &(impl env::Read + env::Write),
    name: impl AsRef<str>,
    value: impl Into<String>,
) -> Result<(), CommandError> {
    let value = value.into();
    env.set(name.as_ref(), &value);

    if env.get("GITHUB_ENV").is_some() {
        let message = prepare_kv_message(name.as_ref(), &value)?;
        issue_file_command("ENV", message)?;
        return Ok(());
    }

    issue(
        &CommandBuilder::new("set-env", value)
            .property("name", name.as_ref())
            .build(),
    );
    Ok(())
}

/// Registers a secret which will get masked from logs.
pub fn set_secret(secret: impl Into<String>) {
    issue(&CommandBuilder::new("add-mask", secret).build());
}

/// Prepends a path to the `PATH` environment variable.
///
/// # Errors
/// If the paths can not be joined.
fn prepend_to_path(
    env: &impl env::Write,
    path: impl AsRef<Path>,
) -> Result<(), std::env::JoinPathsError> {
    if let Some(old_path) = std::env::var_os("PATH") {
        let paths = [path.as_ref().to_path_buf()]
            .into_iter()
            .chain(std::env::split_paths(&old_path));
        let new_path = std::env::join_paths(paths)?;
        env.set("PATH", new_path);
    }
    Ok(())
}

pub trait Parse {
    type Input;

    #[must_use]
    fn parse() -> HashMap<Self::Input, Option<String>> {
        Self::parse_from(&env::OsEnv)
    }

    #[must_use]
    fn parse_from<E: env::Read>(env: &E) -> HashMap<Self::Input, Option<String>>;
}

/// Enables or disables the echoing of commands into stdout for the rest of the step.
///
/// Echoing is disabled by default if `ACTIONS_STEP_DEBUG` is not set.
pub fn set_command_echo(enabled: bool) {
    issue(&CommandBuilder::new("echo", if enabled { "on" } else { "off" }).build());
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Hash)]
pub enum ExitCode {
    /// A code indicating that the action was successful
    Success = 0,
    /// A code indicating that the action was a failure
    Failure = 1,
}

/// Sets the action status to failed.
///
/// When the action exits it will be with an exit code of 1.
pub fn fail(message: impl std::fmt::Display) {
    error!("{}", message);
    std::process::exit(ExitCode::Failure as i32);
}

/// Gets whether Actions Step Debug is on or not.
#[must_use]
pub fn is_debug() -> bool {
    std::env::var("RUNNER_DEBUG")
        .map(|v| v.trim() == "1")
        .unwrap_or(false)
}

#[derive(Debug)]
pub struct CommandBuilder {
    command: String,
    message: String,
    props: HashMap<String, String>,
}

impl CommandBuilder {
    #[must_use]
    pub fn new(command: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            command: command.into(),
            message: message.into(),
            props: HashMap::new(),
        }
    }

    #[must_use]
    pub fn property(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.props.insert(key.into(), value.into());
        self
    }

    #[must_use]
    pub fn properties(mut self, props: HashMap<String, String>) -> Self {
        self.props.extend(props);
        self
    }

    #[must_use]
    pub fn build(self) -> Command {
        let Self {
            command,
            message,
            props,
        } = self;
        Command {
            command,
            message,
            props,
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Command {
    command: String,
    message: String,
    props: HashMap<String, String>,
}

impl Command {
    #[must_use]
    pub fn new(command: String, message: String, props: HashMap<String, String>) -> Self {
        Self {
            command,
            message,
            props,
        }
    }
}

impl std::fmt::Display for Command {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        const CMD_STRING: &str = "::";
        write!(f, "{}{}", CMD_STRING, self.command)?;
        if !self.props.is_empty() {
            write!(f, " ")?;
        }
        for (i, (k, v)) in self.props.iter().enumerate() {
            if i > 0 {
                write!(f, ",")?;
            }
            if v.is_empty() {
                continue;
            }
            write!(f, "{k}={}", utils::escape_property(v))?;
        }
        write!(f, "{}{}", CMD_STRING, self.message)
    }
}

pub fn issue(cmd: &Command) {
    println!("{cmd}");
}

#[derive(thiserror::Error, Debug)]
pub enum ValueError {
    #[error("should not contain delimiter `{delimiter}`")]
    ContainsDelimiter { delimiter: String },
}

#[derive(thiserror::Error, Debug)]
pub enum FileCommandError {
    #[error("missing env variable for file command {cmd}")]
    Missing {
        source: std::env::VarError,
        cmd: String,
    },
    #[error(transparent)]
    Io(#[from] std::io::Error),

    #[error(transparent)]
    Value(#[from] ValueError),
}

#[derive(thiserror::Error, Debug)]
pub enum CommandError {
    #[error(transparent)]
    File(#[from] FileCommandError),

    #[error(transparent)]
    Value(#[from] ValueError),
}

/// Issue a file command.
///
/// # Errors
/// When no env variable for the file command exists or writing fails.
pub fn issue_file_command(
    command: impl AsRef<str>,
    message: impl AsRef<str>,
) -> Result<(), FileCommandError> {
    use std::io::Write;
    let key = format!("GITHUB_{}", command.as_ref());
    let file_path = std::env::var(key).map_err(|source| FileCommandError::Missing {
        source,
        cmd: command.as_ref().to_string(),
    })?;
    let file = std::fs::OpenOptions::new().append(true).open(file_path)?;
    let mut file = std::io::BufWriter::new(file);
    writeln!(file, "{}", message.as_ref())?;
    Ok(())
}

#[derive(thiserror::Error, Debug)]
pub enum AddPathError {
    #[error(transparent)]
    File(#[from] FileCommandError),

    #[error(transparent)]
    Join(#[from] std::env::JoinPathsError),
}

/// Prepends a path to the `PATH` environment variable.
///
/// Persisted for this action and future actions.
///
/// # Errors
/// If the file command
pub fn add_path(
    env: &(impl env::Read + env::Write),
    path: impl AsRef<Path>,
) -> Result<(), AddPathError> {
    let path_string = path.as_ref().to_string_lossy();
    prepend_to_path(env, path.as_ref())?;

    if env.get("GITHUB_PATH").is_some() {
        issue_file_command("PATH", &path_string)?;
    } else {
        issue(&CommandBuilder::new("add-path", path_string).build());
    }
    Ok(())
}

// pub fn issue_command(
//     command: impl AsRef<str>,
//     message: impl std::fmt::Display,
//     props: HashMap<String, String>,
// ) {
//     let cmd= Command::new(command.as_ref(), message.to_string(), props);
//     issue();
// }

#[derive(Default, Debug, Hash, PartialEq, Eq)]
pub struct AnnotationProperties {
    pub title: Option<String>,
    pub file: Option<String>,
    pub start_line: Option<usize>,
    pub end_line: Option<usize>,
    pub start_column: Option<usize>,
    pub end_column: Option<usize>,
}

impl<H> From<AnnotationProperties> for HashMap<String, String, H>
where
    H: std::hash::BuildHasher + Default,
{
    fn from(props: AnnotationProperties) -> Self {
        [
            ("title".to_string(), props.title),
            ("file".to_string(), props.file),
            (
                "line".to_string(),
                props.start_line.map(|line| line.to_string()),
            ),
            (
                "endLine".to_string(),
                props.end_line.map(|line| line.to_string()),
            ),
            (
                "col".to_string(),
                props.start_column.map(|col| col.to_string()),
            ),
            (
                "endColumn".to_string(),
                props.end_column.map(|col| col.to_string()),
            ),
        ]
        .into_iter()
        .filter_map(|(k, v)| v.map(|v| (k, v)))
        .collect()
    }
}

/// Adds an error issue.
pub fn issue_level(
    level: LogLevel,
    message: impl Into<String>,
    props: Option<AnnotationProperties>,
) {
    let props = props.unwrap_or_default();
    issue(
        &CommandBuilder::new(level.to_string(), message)
            .properties(props.into())
            .build(),
    );
}

// /// Writes debug message to user log.
// pub fn debug(message: impl std::fmt::Display) {
//     issue_command("debug", message, HashMap::new())
// }

// /// Adds an error issue.
// pub fn error(message: impl ToString, props: AnnotationProperties) {
//     issue_level(LogLevel::Error, message, props);
// }

#[macro_export]
macro_rules! debug {
        ($($arg:tt)*) => {{
            $crate::issue_level(
                $crate::LogLevel::Debug,
                format!($($arg)*),
                None,
            );
        }};
    }

#[macro_export]
macro_rules! warning {
    ($($arg:tt)*) => {{
        $crate::issue_level(
            $crate::LogLevel::Warning,
            format!($($arg)*),
            None,
        );
    }};
}

#[macro_export]
macro_rules! error {
    ($($arg:tt)*) => {{
        $crate::issue_level(
            $crate::LogLevel::Error,
            format!($($arg)*),
            None,
        );
    }};
}

#[macro_export]
macro_rules! notice {
    ($($arg:tt)*) => {{
        $crate::issue_level(
            $crate::LogLevel::Notice,
            format!($($arg)*),
            None,
        );
    }};
}

#[macro_export]
macro_rules! info {
    ($($arg:tt)*) => { println!($($arg)*); };
}

// /// Adds a warning issue.
// pub fn issue_warning(message: impl ToString, props: AnnotationProperties) {
//     issue_level(LogLevel::Warning, message, props);
// }
//
// /// Adds a notice issue
// pub fn notice(message: impl std::fmt::Display, props: AnnotationProperties) {
//     issue_level(LogLevel::Notice, message, props);
// }

/// Begin an output group.
///
/// Output until the next `group_end` will be foldable in this group.
pub fn start_group(name: impl Into<String>) {
    issue(&CommandBuilder::new("group", name).build());
}

/// End an output group.
pub fn end_group() {
    issue(&CommandBuilder::new("endgroup", "").build());
}

/// Saves state for current action, the state can only be retrieved by this action's post job execution.
///
/// # Errors
/// If the file command fails.
pub fn save_state(
    env: &impl env::Read,
    name: impl AsRef<str>,
    value: impl Into<String>,
) -> Result<(), CommandError> {
    if env.get("GITHUB_STATE").is_some() {
        let message = prepare_kv_message(name.as_ref(), &value.into())?;
        issue_file_command("STATE", message)?;
        return Ok(());
    }

    issue(
        &CommandBuilder::new("save-state", value)
            .property("name", name.as_ref())
            .build(),
    );
    Ok(())
}

/// Gets the value of an state set by this action's main execution.
#[must_use]
pub fn get_state(name: impl AsRef<str>) -> Option<String> {
    std::env::var(format!("STATE_{}", name.as_ref())).ok()
}

/// Wrap an asynchronous function call in a group.
///
/// Returns the same type as the function itself.
pub async fn group<T>(name: impl Into<String>, fut: impl std::future::Future<Output = T>) -> T {
    start_group(name);
    let res: T = fut.await;

    end_group();
    res
}