Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| GitHub Actions on g1t, part one: reading workflows | 1 | //! Reading a workflow file: its triggers, jobs and steps, and notes on |
| 2 | //! anything in it that runs differently on g1t, so moving a repository | |
| 3 | //! from GitHub says plainly what to expect. | |
| 4 | ||
| 5 | use serde::{Deserialize, Serialize}; | |
| 6 | use serde_json::{Map, Value}; | |
| 7 | ||
| 8 | use crate::filter::{Filter, Patterns}; | |
| 9 | ||
| GitHub Actions on g1t, part three: .g1t/workflows, the pages, the docs | 10 | /// Where workflows live: GitHub's `.github/workflows`, under g1t's own |
| 11 | /// folder, so moving a repository to g1t is renaming `.github` to `.g1t`. | |
| 12 | /// g1t never reads `.github`, which stays GitHub's. | |
| 13 | pub const FOLDER: &str = ".g1t/workflows"; | |
| GitHub Actions on g1t, part one: reading workflows | 14 | |
| 15 | /// The events a workflow can name that g1t starts runs for. | |
| 16 | pub const SUPPORTED_EVENTS: &[&str] = &[ | |
| 17 | "push", | |
| 18 | "pull_request", | |
| 19 | "pull_request_target", | |
| 20 | "pull_request_review", | |
| 21 | "issues", | |
| 22 | "issue_comment", | |
| 23 | "schedule", | |
| 24 | "workflow_dispatch", | |
| 25 | "repository_dispatch", | |
| 26 | "workflow_call", | |
| Actions: workflow_run, workflow.completed, artifacts on the run page, Node 24 | 27 | "workflow_run", |
| GitHub Actions on g1t, part one: reading workflows | 28 | "merge_group", |
| 29 | "create", | |
| 30 | "delete", | |
| 31 | ]; | |
| 32 | ||
| 33 | /// The `types` each event has when a workflow gives none, as on GitHub. | |
| 34 | pub fn default_types(event: &str) -> &'static [&'static str] { | |
| 35 | match event { | |
| 36 | "pull_request" | "pull_request_target" => &["opened", "synchronize", "reopened"], | |
| 37 | "merge_group" => &["checks_requested"], | |
| 38 | _ => &[], | |
| 39 | } | |
| 40 | } | |
| 41 | ||
| 42 | /// How much a note matters. | |
| 43 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| 44 | #[serde(rename_all = "snake_case")] | |
| 45 | pub enum Severity { | |
| 46 | /// Runs, slightly differently. | |
| 47 | Info, | |
| 48 | /// Runs, but something in it does nothing or may not work. | |
| 49 | Warning, | |
| 50 | /// Does not run on g1t. | |
| 51 | Unsupported, | |
| 52 | } | |
| 53 | ||
| 54 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| 55 | pub struct Note { | |
| 56 | pub severity: Severity, | |
| 57 | /// The job, if the note is about one. | |
| 58 | #[serde(skip_serializing_if = "Option::is_none")] | |
| 59 | pub job: Option<String>, | |
| 60 | pub message: String, | |
| 61 | } | |
| 62 | ||
| 63 | /// One event a workflow is started by, with its filters. | |
| 64 | #[derive(Clone, Debug, Default, PartialEq, Eq)] | |
| 65 | pub struct Trigger { | |
| 66 | pub event: String, | |
| 67 | /// Activity types; empty means the event's defaults (or all). | |
| 68 | pub types: Vec<String>, | |
| 69 | pub branches: Filter, | |
| 70 | pub tags: Filter, | |
| 71 | pub paths: Filter, | |
| 72 | /// For `schedule`. | |
| 73 | pub crons: Vec<String>, | |
| 74 | /// For `workflow_dispatch` and `workflow_call`: the inputs, as written. | |
| 75 | pub inputs: Map<String, Value>, | |
| Actions: workflow_run, workflow.completed, artifacts on the run page, Node 24 | 76 | /// For `workflow_run`: the names of the workflows it follows. |
| 77 | pub workflows: Vec<String>, | |
| GitHub Actions on g1t, part one: reading workflows | 78 | } |
| 79 | ||
| 80 | impl Trigger { | |
| 81 | /// Whether an activity type starts it. | |
| 82 | pub fn wants_type(&self, action: Option<&str>) -> bool { | |
| 83 | let Some(action) = action else { return true }; | |
| 84 | if self.types.is_empty() { | |
| A repository has its own sidebar, as settings do | 85 | // A g1t agent's pull request has no code until it is marked |
| 86 | // ready, so that is when its default runs start, as `opened` | |
| 87 | // would on GitHub. | |
| 88 | if action == "ready_for_review" && self.event.starts_with("pull_request") && self.event != "pull_request_review" { | |
| 89 | return true; | |
| 90 | } | |
| GitHub Actions on g1t, part one: reading workflows | 91 | let defaults = default_types(&self.event); |
| 92 | return defaults.is_empty() || defaults.contains(&action); | |
| 93 | } | |
| 94 | self.types.iter().any(|t| t == action) | |
| 95 | } | |
| 96 | } | |
| 97 | ||
| 98 | #[derive(Clone, Debug, PartialEq)] | |
| 99 | pub struct Step { | |
| 100 | pub id: Option<String>, | |
| 101 | pub name: Option<String>, | |
| 102 | pub condition: Option<String>, | |
| 103 | pub uses: Option<String>, | |
| 104 | pub run: Option<String>, | |
| 105 | /// The whole step as written, for the sandbox. | |
| 106 | pub raw: Value, | |
| 107 | } | |
| 108 | ||
| 109 | impl Step { | |
| 110 | /// How the step is shown when it has no name. | |
| 111 | pub fn title(&self) -> String { | |
| 112 | if let Some(name) = &self.name { | |
| 113 | return name.clone(); | |
| 114 | } | |
| 115 | if let Some(uses) = &self.uses { | |
| 116 | return format!("Run {uses}"); | |
| 117 | } | |
| 118 | let first = self.run.as_deref().unwrap_or_default().lines().find(|line| !line.trim().is_empty()).unwrap_or_default(); | |
| 119 | format!("Run {}", first.trim()) | |
| 120 | } | |
| 121 | } | |
| 122 | ||
| 123 | #[derive(Clone, Debug, PartialEq)] | |
| 124 | pub struct Job { | |
| 125 | /// Its key under `jobs:`. | |
| 126 | pub id: String, | |
| 127 | pub name: Option<String>, | |
| 128 | pub needs: Vec<String>, | |
| 129 | pub condition: Option<String>, | |
| 130 | pub runs_on: Value, | |
| 131 | /// `strategy.matrix`, as written (it may be an expression). | |
| 132 | pub matrix: Option<Value>, | |
| 133 | pub fail_fast: bool, | |
| 134 | pub max_parallel: Option<u32>, | |
| 135 | /// A reusable workflow it calls (`uses:` on a job). | |
| 136 | pub uses: Option<String>, | |
| 137 | pub steps: Vec<Step>, | |
| 138 | /// The whole job as written, for the sandbox. | |
| 139 | pub raw: Value, | |
| 140 | } | |
| 141 | ||
| 142 | #[derive(Clone, Debug, PartialEq)] | |
| 143 | pub struct Workflow { | |
| 144 | pub name: Option<String>, | |
| 145 | pub run_name: Option<String>, | |
| 146 | pub triggers: Vec<Trigger>, | |
| 147 | pub env: Map<String, Value>, | |
| 148 | pub concurrency: Option<Concurrency>, | |
| 149 | pub jobs: Vec<Job>, | |
| 150 | pub notes: Vec<Note>, | |
| 151 | /// The whole workflow as written. | |
| 152 | pub raw: Value, | |
| 153 | } | |
| 154 | ||
| 155 | #[derive(Clone, Debug, PartialEq, Eq)] | |
| 156 | pub struct Concurrency { | |
| 157 | /// May hold an expression. | |
| 158 | pub group: String, | |
| 159 | pub cancel_in_progress: Value, | |
| 160 | } | |
| 161 | ||
| 162 | impl Workflow { | |
| 163 | pub fn trigger(&self, event: &str) -> Option<&Trigger> { | |
| 164 | self.triggers.iter().find(|trigger| trigger.event == event) | |
| 165 | } | |
| 166 | ||
| 167 | /// The name shown for it: its `name`, or its file's path. | |
| 168 | pub fn display_name(&self, path: &str) -> String { | |
| 169 | self.name.clone().unwrap_or_else(|| path.to_owned()) | |
| 170 | } | |
| 171 | ||
| 172 | /// The job ids in an order where each comes after the jobs it needs. | |
| 173 | pub fn job_order(&self) -> Vec<&str> { | |
| 174 | let mut ordered: Vec<&str> = Vec::new(); | |
| 175 | while ordered.len() < self.jobs.len() { | |
| 176 | let before = ordered.len(); | |
| 177 | for job in &self.jobs { | |
| 178 | if !ordered.contains(&job.id.as_str()) && job.needs.iter().all(|need| ordered.contains(&need.as_str())) { | |
| 179 | ordered.push(&job.id); | |
| 180 | } | |
| 181 | } | |
| 182 | if ordered.len() == before { | |
| 183 | break; | |
| 184 | } | |
| 185 | } | |
| 186 | ordered | |
| 187 | } | |
| 188 | } | |
| 189 | ||
| 190 | /// YAML to JSON, keeping the order of keys. Keys that are not strings | |
| 191 | /// (`on: true` in YAML 1.1, numbers) become their text. | |
| 192 | pub fn yaml_to_json(value: &serde_yaml::Value) -> Value { | |
| 193 | match value { | |
| 194 | serde_yaml::Value::Null => Value::Null, | |
| 195 | serde_yaml::Value::Bool(flag) => Value::Bool(*flag), | |
| 196 | serde_yaml::Value::Number(number) => { | |
| 197 | if let Some(n) = number.as_i64() { | |
| 198 | Value::from(n) | |
| 199 | } else if let Some(n) = number.as_u64() { | |
| 200 | Value::from(n) | |
| 201 | } else { | |
| 202 | number.as_f64().and_then(serde_json::Number::from_f64).map_or(Value::Null, Value::Number) | |
| 203 | } | |
| 204 | } | |
| 205 | serde_yaml::Value::String(text) => Value::String(text.clone()), | |
| 206 | serde_yaml::Value::Sequence(items) => Value::Array(items.iter().map(yaml_to_json).collect()), | |
| 207 | serde_yaml::Value::Mapping(map) => { | |
| 208 | let mut out = Map::new(); | |
| 209 | for (key, value) in map { | |
| 210 | let key = match key { | |
| 211 | serde_yaml::Value::String(text) => text.clone(), | |
| 212 | serde_yaml::Value::Bool(flag) => flag.to_string(), | |
| 213 | serde_yaml::Value::Number(number) => number.to_string(), | |
| 214 | _ => continue, | |
| 215 | }; | |
| 216 | out.insert(key, yaml_to_json(value)); | |
| 217 | } | |
| 218 | Value::Object(out) | |
| 219 | } | |
| 220 | serde_yaml::Value::Tagged(tagged) => yaml_to_json(&tagged.value), | |
| 221 | } | |
| 222 | } | |
| 223 | ||
| 224 | fn texts(value: Option<&Value>) -> Vec<String> { | |
| 225 | match value { | |
| 226 | Some(Value::String(text)) => vec![text.clone()], | |
| 227 | Some(Value::Array(items)) => items | |
| 228 | .iter() | |
| 229 | .filter_map(|item| match item { | |
| 230 | Value::String(text) => Some(text.clone()), | |
| 231 | Value::Number(n) => Some(n.to_string()), | |
| 232 | _ => None, | |
| 233 | }) | |
| 234 | .collect(), | |
| 235 | _ => Vec::new(), | |
| 236 | } | |
| 237 | } | |
| 238 | ||
| 239 | fn text(value: Option<&Value>) -> Option<String> { | |
| 240 | match value? { | |
| 241 | Value::String(text) => Some(text.clone()), | |
| 242 | Value::Number(n) => Some(n.to_string()), | |
| 243 | Value::Bool(flag) => Some(flag.to_string()), | |
| 244 | _ => None, | |
| 245 | } | |
| 246 | } | |
| 247 | ||
| 248 | fn filter(spec: &Map<String, Value>, only: &str, ignore: &str) -> Filter { | |
| 249 | let list = |key: &str| spec.get(key).map(|value| Patterns::new(&texts(Some(value)))); | |
| 250 | Filter { only: list(only), ignore: list(ignore) } | |
| 251 | } | |
| 252 | ||
| 253 | fn trigger(event: &str, spec: &Value) -> Trigger { | |
| 254 | let mut trigger = Trigger { event: event.to_owned(), ..Trigger::default() }; | |
| 255 | match spec { | |
| 256 | Value::Object(spec) => { | |
| 257 | trigger.types = texts(spec.get("types")); | |
| 258 | trigger.branches = filter(spec, "branches", "branches-ignore"); | |
| 259 | trigger.tags = filter(spec, "tags", "tags-ignore"); | |
| 260 | trigger.paths = filter(spec, "paths", "paths-ignore"); | |
| 261 | if let Some(Value::Object(inputs)) = spec.get("inputs") { | |
| 262 | trigger.inputs = inputs.clone(); | |
| 263 | } | |
| Actions: workflow_run, workflow.completed, artifacts on the run page, Node 24 | 264 | trigger.workflows = texts(spec.get("workflows")); |
| GitHub Actions on g1t, part one: reading workflows | 265 | } |
| 266 | Value::Array(entries) if event == "schedule" => { | |
| 267 | trigger.crons = entries.iter().filter_map(|entry| text(entry.get("cron"))).collect(); | |
| 268 | } | |
| 269 | _ => {} | |
| 270 | } | |
| 271 | trigger | |
| 272 | } | |
| 273 | ||
| 274 | /// Reads a workflow. `Err` is what is wrong with the file, for the person | |
| 275 | /// who wrote it; what reads but runs differently is in `notes`. | |
| 276 | pub fn parse(source: &str) -> Result<Workflow, String> { | |
| 277 | let yaml: serde_yaml::Value = serde_yaml::from_str(source).map_err(|error| format!("It is not valid YAML: {error}"))?; | |
| 278 | let raw = yaml_to_json(&yaml); | |
| 279 | let Value::Object(root) = &raw else { | |
| 280 | return Err("A workflow is a mapping with `on` and `jobs`.".to_owned()); | |
| 281 | }; | |
| 282 | let mut notes = Vec::new(); | |
| 283 | let mut note = |severity, job: Option<&str>, message: String| notes.push(Note { severity, job: job.map(str::to_owned), message }); | |
| 284 | ||
| 285 | // `on`, in any of its three shapes. YAML 1.1 readers turn `on` into | |
| 286 | // `true`; this reader keeps it, and accepts both. | |
| 287 | let on = root.get("on").or_else(|| root.get("true")).ok_or("`on` is missing: say which events start the workflow.")?; | |
| 288 | let mut triggers = Vec::new(); | |
| 289 | match on { | |
| 290 | Value::String(event) => triggers.push(trigger(event, &Value::Null)), | |
| 291 | Value::Array(events) => { | |
| 292 | for event in events { | |
| 293 | let Value::String(event) = event else { return Err("`on` lists event names.".to_owned()) }; | |
| 294 | triggers.push(trigger(event, &Value::Null)); | |
| 295 | } | |
| 296 | } | |
| 297 | Value::Object(events) => { | |
| 298 | for (event, spec) in events { | |
| 299 | triggers.push(trigger(event, spec)); | |
| 300 | } | |
| 301 | } | |
| 302 | _ => return Err("`on` is an event, a list of events, or a mapping of events to their filters.".to_owned()), | |
| 303 | } | |
| 304 | for trigger in &triggers { | |
| 305 | if !SUPPORTED_EVENTS.contains(&trigger.event.as_str()) { | |
| 306 | note( | |
| 307 | Severity::Unsupported, | |
| 308 | None, | |
| 309 | format!("g1t has no `{}` event, so that trigger never starts it.", trigger.event), | |
| 310 | ); | |
| 311 | } | |
| 312 | if trigger.event == "pull_request_target" { | |
| 313 | note( | |
| 314 | Severity::Info, | |
| 315 | None, | |
| 316 | "`pull_request_target` runs like `pull_request`, on the pull request's head, with the repository's secrets.".to_owned(), | |
| 317 | ); | |
| 318 | } | |
| 319 | if trigger.event == "workflow_call" && triggers.len() == 1 { | |
| 320 | note(Severity::Info, None, "It is a reusable workflow: it runs when another workflow calls it.".to_owned()); | |
| 321 | } | |
| 322 | } | |
| 323 | ||
| 324 | let env = match root.get("env") { | |
| 325 | Some(Value::Object(env)) => env.clone(), | |
| 326 | _ => Map::new(), | |
| 327 | }; | |
| 328 | let concurrency = match root.get("concurrency") { | |
| 329 | Some(Value::String(group)) => Some(Concurrency { group: group.clone(), cancel_in_progress: Value::Bool(false) }), | |
| 330 | Some(Value::Object(spec)) => text(spec.get("group")).map(|group| Concurrency { | |
| 331 | group, | |
| 332 | cancel_in_progress: spec.get("cancel-in-progress").cloned().unwrap_or(Value::Bool(false)), | |
| 333 | }), | |
| 334 | _ => None, | |
| 335 | }; | |
| 336 | ||
| 337 | let Some(Value::Object(job_specs)) = root.get("jobs") else { | |
| 338 | return Err("`jobs` is missing: a workflow needs at least one job.".to_owned()); | |
| 339 | }; | |
| 340 | if job_specs.is_empty() { | |
| 341 | return Err("`jobs` is empty: a workflow needs at least one job.".to_owned()); | |
| 342 | } | |
| 343 | let mut jobs = Vec::new(); | |
| 344 | for (id, spec) in job_specs { | |
| 345 | let Value::Object(spec) = spec else { | |
| 346 | return Err(format!("Job `{id}` is a mapping.")); | |
| 347 | }; | |
| 348 | let uses = text(spec.get("uses")); | |
| 349 | let steps_raw = match spec.get("steps") { | |
| 350 | Some(Value::Array(steps)) => steps.clone(), | |
| 351 | None if uses.is_some() => Vec::new(), | |
| 352 | None => return Err(format!("Job `{id}` has no `steps`.")), | |
| 353 | Some(_) => return Err(format!("Job `{id}`: `steps` is a list.")), | |
| 354 | }; | |
| 355 | let mut steps = Vec::new(); | |
| 356 | for (index, step) in steps_raw.iter().enumerate() { | |
| 357 | let Value::Object(fields) = step else { | |
| 358 | return Err(format!("Job `{id}`, step {}: a step is a mapping.", index + 1)); | |
| 359 | }; | |
| 360 | let step = Step { | |
| 361 | id: text(fields.get("id")), | |
| 362 | name: text(fields.get("name")), | |
| 363 | condition: text(fields.get("if")), | |
| 364 | uses: text(fields.get("uses")), | |
| 365 | run: text(fields.get("run")), | |
| 366 | raw: step.clone(), | |
| 367 | }; | |
| 368 | match (&step.uses, &step.run) { | |
| 369 | (Some(_), Some(_)) => return Err(format!("Job `{id}`, step {}: a step has `uses` or `run`, not both.", index + 1)), | |
| 370 | (None, None) => return Err(format!("Job `{id}`, step {}: a step needs `uses` or `run`.", index + 1)), | |
| 371 | _ => {} | |
| 372 | } | |
| 373 | if let Some(uses) = &step.uses | |
| 374 | && let Some((severity, message)) = action_note(uses) | |
| 375 | { | |
| 376 | note(severity, Some(id), message); | |
| 377 | } | |
| 378 | if let Some(shell) = text(fields.get("shell")) | |
| 379 | && matches!(shell.as_str(), "pwsh" | "powershell" | "cmd") | |
| 380 | { | |
| 381 | note(Severity::Unsupported, Some(id), format!("Steps with `shell: {shell}` need Windows or PowerShell, which g1t's Linux runners do not have.")); | |
| 382 | } | |
| 383 | steps.push(step); | |
| 384 | } | |
| 385 | let runs_on = spec.get("runs-on").cloned().unwrap_or(Value::Null); | |
| 386 | for label in texts(Some(&runs_on)).iter().chain(runs_on.get("labels").map(|l| texts(Some(l))).unwrap_or_default().iter()) { | |
| 387 | let lower = label.to_ascii_lowercase(); | |
| 388 | if lower.contains("windows") || lower.contains("macos") { | |
| 389 | note( | |
| 390 | Severity::Unsupported, | |
| 391 | Some(id), | |
| 392 | format!("`runs-on: {label}`: g1t runs jobs on Linux only, so this job fails."), | |
| 393 | ); | |
| 394 | } else if lower == "self-hosted" { | |
| 395 | note(Severity::Info, Some(id), "`self-hosted`: g1t runs it on its own Linux runner.".to_owned()); | |
| 396 | } | |
| 397 | } | |
| 398 | if spec.contains_key("services") { | |
| 399 | note(Severity::Unsupported, Some(id), "`services` containers (such as a database) are not started on g1t yet.".to_owned()); | |
| 400 | } | |
| 401 | if spec.contains_key("container") { | |
| 402 | note(Severity::Warning, Some(id), "`container`: steps run on g1t's runner image instead of that container.".to_owned()); | |
| 403 | } | |
| 404 | if spec.contains_key("environment") { | |
| 405 | note(Severity::Info, Some(id), "`environment`: protection rules are not enforced on g1t yet; the job runs with the repository's secrets.".to_owned()); | |
| 406 | } | |
| 407 | let (matrix, fail_fast, max_parallel) = match spec.get("strategy") { | |
| 408 | Some(Value::Object(strategy)) => ( | |
| 409 | strategy.get("matrix").cloned(), | |
| 410 | strategy.get("fail-fast").and_then(Value::as_bool).unwrap_or(true), | |
| 411 | strategy.get("max-parallel").and_then(Value::as_u64).map(|n| n as u32), | |
| 412 | ), | |
| 413 | _ => (None, true, None), | |
| 414 | }; | |
| GitHub Actions on g1t, part two: running workflows | 415 | if uses.is_some() { |
| 416 | note(Severity::Unsupported, Some(id), "Reusable workflows (`uses:` on a job) are not called on g1t yet, so this job fails.".to_owned()); | |
| GitHub Actions on g1t, part one: reading workflows | 417 | } |
| 418 | jobs.push(Job { | |
| 419 | id: id.clone(), | |
| 420 | name: text(spec.get("name")), | |
| 421 | needs: texts(spec.get("needs")), | |
| 422 | condition: text(spec.get("if")), | |
| 423 | runs_on, | |
| 424 | matrix, | |
| 425 | fail_fast, | |
| 426 | max_parallel, | |
| 427 | uses, | |
| 428 | steps, | |
| 429 | raw: Value::Object(spec.clone()), | |
| 430 | }); | |
| 431 | } | |
| 432 | for job in &jobs { | |
| 433 | for need in &job.needs { | |
| 434 | if !jobs.iter().any(|other| &other.id == need) { | |
| 435 | return Err(format!("Job `{}` needs `{need}`, and there is no job called that.", job.id)); | |
| 436 | } | |
| 437 | } | |
| 438 | } | |
| 439 | let workflow = Workflow { | |
| 440 | name: text(root.get("name")), | |
| 441 | run_name: text(root.get("run-name")), | |
| 442 | triggers, | |
| 443 | env, | |
| 444 | concurrency, | |
| 445 | jobs, | |
| 446 | notes, | |
| 447 | raw, | |
| 448 | }; | |
| 449 | if workflow.job_order().len() < workflow.jobs.len() { | |
| 450 | return Err("The jobs' `needs` go round in a circle.".to_owned()); | |
| 451 | } | |
| 452 | Ok(workflow) | |
| 453 | } | |
| 454 | ||
| 455 | /// What to say about an action g1t runs differently, if anything. | |
| 456 | fn action_note(uses: &str) -> Option<(Severity, String)> { | |
| 457 | if uses.starts_with("docker://") { | |
| 458 | return Some((Severity::Unsupported, format!("`{uses}`: Docker actions do not run on g1t yet."))); | |
| 459 | } | |
| 460 | let name = uses.split('@').next().unwrap_or(uses).to_ascii_lowercase(); | |
| 461 | match name.as_str() { | |
| 462 | "actions/checkout" => Some((Severity::Info, "`actions/checkout` checks out from g1t.".to_owned())), | |
| 463 | "actions/cache" | "actions/cache/restore" | "actions/cache/save" => Some(( | |
| 464 | Severity::Warning, | |
| 465 | format!("`{name}`: g1t has no cache yet, so it always misses and the job does the work again."), | |
| 466 | )), | |
| 467 | "actions/upload-artifact" | "actions/download-artifact" => Some(( | |
| 468 | Severity::Warning, | |
| 469 | format!("`{name}`: artifacts are kept for the run on g1t, and passed between its jobs."), | |
| 470 | )), | |
| 471 | _ => None, | |
| 472 | } | |
| 473 | } | |
| 474 | ||
| 475 | #[cfg(test)] | |
| 476 | mod tests { | |
| 477 | use super::*; | |
| 478 | ||
| 479 | const CI: &str = r#" | |
| 480 | name: CI | |
| 481 | on: | |
| 482 | push: | |
| 483 | branches: [main] | |
| 484 | paths-ignore: ["docs/**"] | |
| 485 | pull_request: | |
| 486 | workflow_dispatch: | |
| 487 | inputs: | |
| 488 | debug: | |
| 489 | type: boolean | |
| 490 | default: false | |
| 491 | schedule: | |
| 492 | - cron: "0 3 * * *" | |
| 493 | concurrency: | |
| 494 | group: ci-${{ github.ref }} | |
| 495 | cancel-in-progress: true | |
| 496 | env: | |
| 497 | CARGO_TERM_COLOR: always | |
| 498 | jobs: | |
| 499 | test: | |
| 500 | runs-on: ${{ matrix.os }} | |
| 501 | strategy: | |
| 502 | matrix: | |
| 503 | os: [ubuntu-latest, windows-latest] | |
| Actions: workflow_run, workflow.completed, artifacts on the run page, Node 24 | 504 | node: [22, 24] |
| GitHub Actions on g1t, part one: reading workflows | 505 | steps: |
| Actions: workflow_run, workflow.completed, artifacts on the run page, Node 24 | 506 | - uses: actions/checkout@v7 |
| 507 | - uses: actions/setup-node@v7 | |
| GitHub Actions on g1t, part one: reading workflows | 508 | with: |
| 509 | node-version: ${{ matrix.node }} | |
| 510 | - run: npm ci | |
| 511 | - name: Test | |
| 512 | run: npm test | |
| 513 | deploy: | |
| 514 | needs: test | |
| 515 | if: github.ref == 'refs/heads/main' | |
| 516 | runs-on: ubuntu-latest | |
| 517 | steps: | |
| 518 | - run: echo deploy | |
| 519 | "#; | |
| 520 | ||
| 521 | #[test] | |
| 522 | fn a_whole_workflow_reads() { | |
| 523 | let workflow = parse(CI).unwrap(); | |
| 524 | assert_eq!(workflow.name.as_deref(), Some("CI")); | |
| 525 | assert_eq!(workflow.triggers.iter().map(|t| t.event.as_str()).collect::<Vec<_>>(), ["push", "pull_request", "workflow_dispatch", "schedule"]); | |
| 526 | let push = workflow.trigger("push").unwrap(); | |
| 527 | assert!(push.branches.allows("main")); | |
| 528 | assert!(!push.branches.allows("dev")); | |
| 529 | assert!(!push.paths.allows_paths(&["docs/a.md".into()])); | |
| 530 | assert_eq!(workflow.trigger("schedule").unwrap().crons, ["0 3 * * *"]); | |
| 531 | assert!(workflow.trigger("workflow_dispatch").unwrap().inputs.contains_key("debug")); | |
| 532 | assert_eq!(workflow.concurrency.as_ref().unwrap().group, "ci-${{ github.ref }}"); | |
| 533 | assert_eq!(workflow.jobs.len(), 2); | |
| 534 | assert_eq!(workflow.jobs[1].needs, ["test"]); | |
| Actions: workflow_run, workflow.completed, artifacts on the run page, Node 24 | 535 | assert_eq!(workflow.jobs[0].steps[0].title(), "Run actions/checkout@v7"); |
| GitHub Actions on g1t, part one: reading workflows | 536 | assert_eq!(workflow.jobs[0].steps[2].title(), "Run npm ci"); |
| 537 | assert_eq!(workflow.jobs[0].steps[3].title(), "Test"); | |
| 538 | assert_eq!(workflow.job_order(), ["test", "deploy"]); | |
| 539 | assert_eq!(workflow.env["CARGO_TERM_COLOR"], "always"); | |
| 540 | } | |
| 541 | ||
| 542 | #[test] | |
| 543 | fn short_forms_of_on() { | |
| 544 | let one = parse("on: push\njobs:\n a:\n runs-on: ubuntu-latest\n steps: [{ run: 'true' }]").unwrap(); | |
| 545 | assert_eq!(one.triggers[0].event, "push"); | |
| 546 | let list = parse("on: [push, pull_request]\njobs:\n a:\n runs-on: ubuntu-latest\n steps: [{ run: 'true' }]").unwrap(); | |
| 547 | assert_eq!(list.triggers.len(), 2); | |
| 548 | let pr = list.trigger("pull_request").unwrap(); | |
| 549 | assert!(pr.wants_type(Some("opened"))); | |
| 550 | assert!(pr.wants_type(Some("synchronize"))); | |
| 551 | assert!(!pr.wants_type(Some("closed"))); | |
| A repository has its own sidebar, as settings do | 552 | assert!(pr.wants_type(Some("ready_for_review"))); |
| GitHub Actions on g1t, part one: reading workflows | 553 | let typed = parse("on:\n pull_request:\n types: [closed]\njobs:\n a:\n runs-on: ubuntu-latest\n steps: [{ run: 'true' }]").unwrap(); |
| 554 | assert!(typed.trigger("pull_request").unwrap().wants_type(Some("closed"))); | |
| 555 | assert!(!typed.trigger("pull_request").unwrap().wants_type(Some("opened"))); | |
| 556 | } | |
| 557 | ||
| 558 | #[test] | |
| 559 | fn notes_say_what_runs_differently() { | |
| 560 | let workflow = parse( | |
| Actions: workflow_run, workflow.completed, artifacts on the run page, Node 24 | 561 | "on: [push, release]\njobs:\n win:\n runs-on: windows-latest\n services:\n db: { image: postgres }\n steps:\n - uses: actions/cache@v6\n - uses: docker://alpine\n - run: dir\n shell: pwsh", |
| GitHub Actions on g1t, part one: reading workflows | 562 | ) |
| 563 | .unwrap(); | |
| 564 | let unsupported: Vec<&str> = | |
| 565 | workflow.notes.iter().filter(|n| n.severity == Severity::Unsupported).map(|n| n.message.as_str()).collect(); | |
| 566 | assert!(unsupported.iter().any(|m| m.contains("`release`"))); | |
| 567 | assert!(unsupported.iter().any(|m| m.contains("windows-latest"))); | |
| 568 | assert!(unsupported.iter().any(|m| m.contains("services"))); | |
| 569 | assert!(unsupported.iter().any(|m| m.contains("docker://alpine"))); | |
| 570 | assert!(unsupported.iter().any(|m| m.contains("pwsh"))); | |
| 571 | assert!(workflow.notes.iter().any(|n| n.severity == Severity::Warning && n.message.contains("actions/cache"))); | |
| 572 | } | |
| 573 | ||
| 574 | #[test] | |
| 575 | fn mistakes_are_explained() { | |
| 576 | let problem = |yaml: &str| parse(yaml).unwrap_err(); | |
| 577 | assert!(problem("jobs: {}").contains("`on` is missing")); | |
| 578 | assert!(problem("on: push").contains("`jobs` is missing")); | |
| 579 | assert!(problem("on: push\njobs:\n a:\n runs-on: x").contains("no `steps`")); | |
| 580 | assert!(problem("on: push\njobs:\n a:\n runs-on: x\n steps: [{ name: nothing }]").contains("`uses` or `run`")); | |
| 581 | assert!(problem("on: push\njobs:\n a:\n needs: b\n runs-on: x\n steps: [{ run: x }]").contains("no job called that")); | |
| 582 | assert!( | |
| 583 | problem("on: push\njobs:\n a:\n needs: b\n runs-on: x\n steps: [{ run: x }]\n b:\n needs: a\n runs-on: x\n steps: [{ run: x }]") | |
| 584 | .contains("circle") | |
| 585 | ); | |
| 586 | assert!(problem("on: push\njobs: [1]").contains("`jobs`")); | |
| 587 | assert!(problem(": : :").contains("not valid YAML")); | |
| 588 | } | |
| 589 | } |