wrkflw-executor 0.8.0

Workflow execution engine for wrkflw
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! Parser for GitHub Actions workflow commands embedded in step output.
//!
//! GitHub Actions recognises special `::command::` lines in stdout to set
//! outputs, mask values, group log lines, and emit annotations. This module
//! extracts those commands from raw step output so the engine can apply their
//! effects.

/// A parsed workflow command from step output.
#[derive(Debug, Clone, PartialEq)]
pub enum WorkflowCommand {
    Error {
        message: String,
        file: Option<String>,
        line: Option<u32>,
        end_line: Option<u32>,
        col: Option<u32>,
        end_column: Option<u32>,
        title: Option<String>,
    },
    Warning {
        message: String,
        file: Option<String>,
        line: Option<u32>,
        end_line: Option<u32>,
        col: Option<u32>,
        end_column: Option<u32>,
        title: Option<String>,
    },
    Notice {
        message: String,
        file: Option<String>,
        line: Option<u32>,
        end_line: Option<u32>,
        col: Option<u32>,
        end_column: Option<u32>,
        title: Option<String>,
    },
    Debug {
        message: String,
    },
    Group {
        name: String,
    },
    EndGroup,
    AddMask {
        value: String,
    },
    /// Deprecated `::set-output` command (replaced by GITHUB_OUTPUT file).
    SetOutput {
        name: String,
        value: String,
    },
    /// `::save-state` command.
    SaveState {
        name: String,
        value: String,
    },
    StopCommands {
        token: String,
    },
}

/// Decode GitHub Actions percent-encoded values.
///
/// GitHub Actions encodes: `%25` → `%`, `%0A` → `\n`, `%0D` → `\r`,
/// `%3A` → `:`, `%2C` → `,`, `%3B` → `;`.
fn decode_value(s: &str) -> String {
    // Decode %25 (percent) LAST to avoid double-decode: if input contains
    // `%250A`, decoding %25 first would turn it into `%0A`, which the next
    // step would incorrectly decode to `\n`. GitHub Actions decodes %25 last.
    s.replace("%0A", "\n")
        .replace("%0a", "\n")
        .replace("%0D", "\r")
        .replace("%0d", "\r")
        .replace("%3A", ":")
        .replace("%3a", ":")
        .replace("%2C", ",")
        .replace("%2c", ",")
        .replace("%3B", ";")
        .replace("%3b", ";")
        .replace("%25", "%")
}

/// Parse all workflow commands from step output text.
///
/// Returns the commands in the order they appear. Lines that are not
/// workflow commands are silently skipped.
pub fn parse_workflow_commands(output: &str) -> Vec<WorkflowCommand> {
    let mut commands = Vec::new();
    // Pre-computed resume sentinel to avoid allocation per line
    let mut resume_sentinel: Option<String> = None;

    for line in output.lines() {
        // GitHub Actions only recognizes commands starting at column 0 —
        // indented lines must not be treated as commands.
        let trimmed = line.trim_end();

        // If commands are stopped, look for the resume token
        if let Some(ref sentinel) = resume_sentinel {
            if trimmed == sentinel.as_str() {
                resume_sentinel = None;
            }
            continue;
        }

        if !trimmed.starts_with("::") {
            continue;
        }

        if let Some(cmd) = parse_command_line(trimmed) {
            if let WorkflowCommand::StopCommands { ref token } = cmd {
                if token.is_empty() {
                    // Empty token creates an unmatchable sentinel — skip
                    continue;
                }
                resume_sentinel = Some(format!("::{}::", token));
            } else {
                commands.push(cmd);
            }
        }
    }

    commands
}

/// Parse a single `::command param=val,param=val::message` line.
fn parse_command_line(line: &str) -> Option<WorkflowCommand> {
    // Format: ::command param1=val1,param2=val2::message
    // The line starts with "::" — strip it.
    let rest = line.strip_prefix("::").unwrap_or(line);

    // Find the second "::" that separates command+params from the message
    let (cmd_part, raw_message) = if let Some(idx) = rest.find("::") {
        (&rest[..idx], rest[idx + 2..].to_string())
    } else {
        return None;
    };

    // Decode percent-encoded values in the message
    let message = decode_value(&raw_message);

    // Split command name from params (space-separated)
    let (cmd_name, params_str) = if let Some(idx) = cmd_part.find(' ') {
        (&cmd_part[..idx], &cmd_part[idx + 1..])
    } else {
        (cmd_part, "")
    };

    let params = parse_params(params_str);

    // Helper to extract common annotation parameters
    let build_annotation = |kind: &str,
                            message: String,
                            params: &std::collections::HashMap<String, String>|
     -> Option<WorkflowCommand> {
        let file = params.get("file").map(|v| decode_value(v));
        let line = params.get("line").and_then(|v| v.parse().ok());
        let end_line = params.get("endLine").and_then(|v| v.parse().ok());
        let col = params.get("col").and_then(|v| v.parse().ok());
        let end_column = params.get("endColumn").and_then(|v| v.parse().ok());
        let title = params.get("title").map(|v| decode_value(v));
        Some(match kind {
            "error" => WorkflowCommand::Error {
                message,
                file,
                line,
                end_line,
                col,
                end_column,
                title,
            },
            "warning" => WorkflowCommand::Warning {
                message,
                file,
                line,
                end_line,
                col,
                end_column,
                title,
            },
            _ => WorkflowCommand::Notice {
                message,
                file,
                line,
                end_line,
                col,
                end_column,
                title,
            },
        })
    };

    match cmd_name {
        "error" => build_annotation("error", message, &params),
        "warning" => build_annotation("warning", message, &params),
        "notice" => build_annotation("notice", message, &params),
        "debug" => Some(WorkflowCommand::Debug { message }),
        "group" => Some(WorkflowCommand::Group { name: message }),
        "endgroup" => Some(WorkflowCommand::EndGroup),
        "add-mask" => Some(WorkflowCommand::AddMask { value: message }),
        "set-output" => {
            let name = params
                .get("name")
                .map(|v| decode_value(v))
                .unwrap_or_default();
            if name.is_empty() {
                return None;
            }
            Some(WorkflowCommand::SetOutput {
                name,
                value: message,
            })
        }
        "save-state" => {
            let name = params
                .get("name")
                .map(|v| decode_value(v))
                .unwrap_or_default();
            if name.is_empty() {
                return None;
            }
            Some(WorkflowCommand::SaveState {
                name,
                value: message,
            })
        }
        "stop-commands" => Some(WorkflowCommand::StopCommands { token: message }),
        _ => None,
    }
}

/// Parse `key=value,key=value` parameter string into a map.
fn parse_params(s: &str) -> std::collections::HashMap<String, String> {
    let mut map = std::collections::HashMap::new();
    if s.is_empty() {
        return map;
    }
    for pair in s.split(',') {
        if let Some(eq) = pair.find('=') {
            let key = pair[..eq].trim().to_string();
            let value = pair[eq + 1..].trim().to_string();
            map.insert(key, value);
        }
    }
    map
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_error_command() {
        let output = "::error file=app.js,line=10,col=5::Something went wrong";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
        match &cmds[0] {
            WorkflowCommand::Error {
                message,
                file,
                line,
                col,
                ..
            } => {
                assert_eq!(message, "Something went wrong");
                assert_eq!(file.as_deref(), Some("app.js"));
                assert_eq!(*line, Some(10));
                assert_eq!(*col, Some(5));
            }
            _ => panic!("expected Error command"),
        }
    }

    #[test]
    fn parse_error_with_end_line_and_title() {
        let output =
            "::error file=app.js,line=5,endLine=10,col=1,endColumn=20,title=Syntax Error::bad code";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
        match &cmds[0] {
            WorkflowCommand::Error {
                message,
                file,
                line,
                end_line,
                col,
                end_column,
                title,
            } => {
                assert_eq!(message, "bad code");
                assert_eq!(file.as_deref(), Some("app.js"));
                assert_eq!(*line, Some(5));
                assert_eq!(*end_line, Some(10));
                assert_eq!(*col, Some(1));
                assert_eq!(*end_column, Some(20));
                assert_eq!(title.as_deref(), Some("Syntax Error"));
            }
            _ => panic!("expected Error command"),
        }
    }

    #[test]
    fn parse_warning_no_params() {
        let output = "::warning::This is a warning";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
        match &cmds[0] {
            WorkflowCommand::Warning { message, file, .. } => {
                assert_eq!(message, "This is a warning");
                assert!(file.is_none());
            }
            _ => panic!("expected Warning"),
        }
    }

    #[test]
    fn parse_set_output() {
        let output = "::set-output name=version::1.2.3";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
        match &cmds[0] {
            WorkflowCommand::SetOutput { name, value } => {
                assert_eq!(name, "version");
                assert_eq!(value, "1.2.3");
            }
            _ => panic!("expected SetOutput"),
        }
    }

    #[test]
    fn parse_set_output_rejects_empty_name() {
        let output = "::set-output::some value";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 0);
    }

    #[test]
    fn parse_save_state_rejects_empty_name() {
        let output = "::save-state::some value";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 0);
    }

    #[test]
    fn parse_group_endgroup() {
        let output = "::group::My Group\nsome output\n::endgroup::";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 2);
        assert!(matches!(&cmds[0], WorkflowCommand::Group { name } if name == "My Group"));
        assert!(matches!(&cmds[1], WorkflowCommand::EndGroup));
    }

    #[test]
    fn parse_add_mask() {
        let output = "::add-mask::my-secret-value";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
        assert!(
            matches!(&cmds[0], WorkflowCommand::AddMask { value } if value == "my-secret-value")
        );
    }

    #[test]
    fn parse_debug() {
        let output = "::debug::Debug message here";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
        assert!(
            matches!(&cmds[0], WorkflowCommand::Debug { message } if message == "Debug message here")
        );
    }

    #[test]
    fn parse_notice() {
        let output = "::notice file=README.md::Check this out";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
        match &cmds[0] {
            WorkflowCommand::Notice { message, file, .. } => {
                assert_eq!(message, "Check this out");
                assert_eq!(file.as_deref(), Some("README.md"));
            }
            _ => panic!("expected Notice"),
        }
    }

    #[test]
    fn non_command_lines_skipped() {
        let output = "regular output\n::warning::warn\nmore output\n";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
    }

    #[test]
    fn stop_commands() {
        let output =
            "::stop-commands::pause\n::warning::should be ignored\n::pause::\n::warning::visible";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
        assert!(
            matches!(&cmds[0], WorkflowCommand::Warning { message, .. } if message == "visible")
        );
    }

    #[test]
    fn stop_commands_empty_token_ignored() {
        // An empty stop-commands token should be ignored (not permanently stop parsing)
        let output = "::stop-commands::\n::warning::still visible";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
        assert!(
            matches!(&cmds[0], WorkflowCommand::Warning { message, .. } if message == "still visible")
        );
    }

    #[test]
    fn save_state() {
        let output = "::save-state name=isPost::true";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
        match &cmds[0] {
            WorkflowCommand::SaveState { name, value } => {
                assert_eq!(name, "isPost");
                assert_eq!(value, "true");
            }
            _ => panic!("expected SaveState"),
        }
    }

    #[test]
    fn multiple_commands() {
        let output = "::group::Build\nbuilding...\n::endgroup::\n::set-output name=result::ok\n::warning::slow build";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 4);
    }

    #[test]
    fn url_decoding_in_message() {
        let output = "::error::Line1%0ALine2%0ALine3";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
        match &cmds[0] {
            WorkflowCommand::Error { message, .. } => {
                assert_eq!(message, "Line1\nLine2\nLine3");
            }
            _ => panic!("expected Error"),
        }
    }

    #[test]
    fn url_decoding_percent_and_colon() {
        let output = "::add-mask::secret%3Avalue%25encoded";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
        match &cmds[0] {
            WorkflowCommand::AddMask { value } => {
                assert_eq!(value, "secret:value%encoded");
            }
            _ => panic!("expected AddMask"),
        }
    }

    #[test]
    fn url_decoding_in_file_param() {
        let output = "::error file=src%3Amain.rs,line=10::oops";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
        match &cmds[0] {
            WorkflowCommand::Error { file, .. } => {
                assert_eq!(file.as_deref(), Some("src:main.rs"));
            }
            _ => panic!("expected Error"),
        }
    }

    #[test]
    fn url_decoding_no_double_decode() {
        // %250A is a literal percent-encoded "%0A" — it should decode to "%0A",
        // NOT to a newline. This verifies that %25 is decoded last.
        let output = "::error::before%250Aafter";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
        match &cmds[0] {
            WorkflowCommand::Error { message, .. } => {
                assert_eq!(message, "before%0Aafter");
            }
            _ => panic!("expected Error"),
        }
    }

    #[test]
    fn url_decoding_comma_and_semicolon() {
        let output = "::error::item1%2Citem2%3Bitem3";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
        match &cmds[0] {
            WorkflowCommand::Error { message, .. } => {
                assert_eq!(message, "item1,item2;item3");
            }
            _ => panic!("expected Error"),
        }
    }

    #[test]
    fn url_decoding_comma_and_semicolon_lowercase() {
        let output = "::warning::a%2cb%3bc";
        let cmds = parse_workflow_commands(output);
        assert_eq!(cmds.len(), 1);
        match &cmds[0] {
            WorkflowCommand::Warning { message, .. } => {
                assert_eq!(message, "a,b;c");
            }
            _ => panic!("expected Warning"),
        }
    }
}