Skip to content

g1t/services/actions/src/trigger.rs

897 lines40,561 bytesCodeBlame
1//! What starts a run: an event on the bus, a schedule, or someone running a
2//! workflow by hand. Each finds the workflows that want it, at the commit
3//! the event is about, and checks their filters.
4
5use g1t_actions::events::{RunInfo, github_events};
6use g1t_actions::workflow::{self, Trigger, Workflow};
7use g1t_contracts::access::{self, Capability};
8use g1t_contracts::actions::{DispatchArgs, WorkflowRun};
9use g1t_contracts::events::Event;
10use g1t_contracts::identity::{AGENT_ID, AGENT_NAME, UsernamesArgs};
11use g1t_contracts::repos::{Commit, CompareArgs, Comparison, LogArgs, Repo, RepoPath};
12use g1t_contracts::work::{IssueDetail, PullDetail, ViewArgs};
13use g1t_contracts::{FailureCode, Outcome, User, new_id};
14use g1t_kit::now_ms;
15use serde_json::{Map, Value, json};
16use worker::Result;
17
18use crate::plan::NewRun;
19use crate::sync::{Read, WorkflowRow};
20use crate::{API, Actions, SITE, check, fail, payload};
21
22/// What an event is about, worked out once for every workflow it starts.
23struct Subject {
24 /// Where the workflow files are read, and at which commit.
25 source: RepoPath,
26 source_ref: Option<String>,
27 git_ref: String,
28 sha: String,
29 head_ref: Option<String>,
30 base_ref: Option<String>,
31 pull: Option<u32>,
32 /// The branch or tag for `branches`/`tags` filters; for pull requests,
33 /// the branch they merge into.
34 filter_ref: String,
35 /// The files it changes, for `paths` filters; `None` until needed.
36 paths: Option<Vec<String>>,
37 /// For a push, what to compare to find the files.
38 compare: Option<(Option<String>, String)>,
39 payload: Value,
40 title: String,
41 trusted: bool,
42}
43
44/// Whether whoever a pull request is for is trusted without asking
45/// identity: g1t's agent in work nobody asked it for, or someone whose
46/// role here is known to allow pushing.
47fn trusted_outright(owner: &User, repo: &Repo) -> bool {
48 owner.id == AGENT_ID || access::can(Some(owner), repo, Capability::Push)
49}
50
51impl Actions {
52 async fn username(&self, id: Option<&str>) -> Result<Option<String>> {
53 let Some(id) = id else { return Ok(None) };
54 if id == AGENT_ID {
55 return Ok(Some(AGENT_NAME.to_owned()));
56 }
57 let names: std::collections::HashMap<String, String> =
58 g1t_kit::call(&self.identity, "usernames", &UsernamesArgs { ids: vec![id.to_owned()] }).await?;
59 Ok(names.get(id).cloned())
60 }
61
62 /// Whether whoever a pull request is for (Pull::owner: whoever asked
63 /// g1t for it, or its author) could push to the repository, so its
64 /// runs get the secrets and a token. Anyone else's, a reader's included
65 /// (who may open one on a private repository too), runs without them.
66 /// A change g1t made for someone is trusted as they are.
67 async fn insider(&self, owner: &User, repo: &Repo, ws: &User) -> Result<bool> {
68 if trusted_outright(owner, repo) {
69 return Ok(true);
70 }
71 // Stored authors carry no memberships or grants: ask identity, as
72 // the workspace (which may see anyone's permission).
73 let permission: Outcome<access::PermissionInfo> = g1t_kit::call(
74 &self.identity,
75 "collaborator_permission",
76 &access::CollaboratorPermissionArgs {
77 viewer: Some(ws.clone()),
78 path: RepoPath { namespace: repo.namespace.clone(), name: repo.name.clone() },
79 username: owner.username.clone(),
80 },
81 )
82 .await?;
83 Ok(permission
84 .into_result()
85 .ok()
86 .and_then(|info| info.role)
87 .is_some_and(|role| access::allows(role, Capability::Push)))
88 }
89
90 async fn commits(&self, repo: &Repo, actor: &User, after: &str, before: Option<&str>) -> Result<Vec<Commit>> {
91 let log: Outcome<Vec<Commit>> = g1t_kit::call(
92 &self.repos,
93 "log",
94 &LogArgs {
95 path: RepoPath {
96 namespace: repo.namespace.clone(),
97 name: repo.name.clone(),
98 },
99 viewer: Some(actor.clone()),
100 git_ref: Some(after.to_owned()),
101 limit: 20,
102 },
103 )
104 .await?;
105 let mut commits: Vec<Commit> = log.into_result().unwrap_or_default();
106 if let Some(before) = before
107 && let Some(at) = commits.iter().position(|commit| commit.hash == before)
108 {
109 commits.truncate(at);
110 }
111 // GitHub lists them oldest first, with the head commit last.
112 commits.reverse();
113 Ok(commits)
114 }
115
116 async fn changed_paths(&self, repo: &Repo, actor: &User, base: Option<String>, head: String) -> Result<Vec<String>> {
117 let compared: Outcome<Comparison> = g1t_kit::call(
118 &self.repos,
119 "compare",
120 &CompareArgs {
121 repo_id: repo.id.clone(),
122 viewer: Some(actor.clone()),
123 base,
124 head: Some(head),
125 base_branch: None,
126 },
127 )
128 .await?;
129 Ok(compared.into_result().map(|c| c.files.into_iter().map(|f| f.path).collect()).unwrap_or_default())
130 }
131
132 async fn default_head(&self, repo: &Repo) -> Result<Option<String>> {
133 g1t_kit::call(
134 &self.repos,
135 "head",
136 &g1t_contracts::repos::HeadArgs {
137 repo_id: repo.id.clone(),
138 branch: repo.default_branch.clone(),
139 },
140 )
141 .await
142 }
143
144 fn repo_path(repo: &Repo) -> RepoPath {
145 RepoPath {
146 namespace: repo.namespace.clone(),
147 name: repo.name.clone(),
148 }
149 }
150
151 /// The subject of an event of `kind`, as GitHub's `event_name`.
152 async fn subject(&self, event: &Event, event_name: &str, action: Option<&str>, repo: &Repo, ws: &User, sender: &str) -> Result<Option<Subject>> {
153 let path = Self::repo_path(repo);
154 let data = &event.data;
155 let on_default = |sha: String, payload: Value, title: String, pull: Option<u32>| Subject {
156 source: path.clone(),
157 source_ref: None,
158 git_ref: format!("refs/heads/{}", repo.default_branch),
159 sha,
160 head_ref: None,
161 base_ref: None,
162 pull,
163 filter_ref: format!("refs/heads/{}", repo.default_branch),
164 paths: None,
165 compare: None,
166 payload,
167 title,
168 trusted: true,
169 };
170 let view = |number: u32| ViewArgs {
171 repo: path.clone(),
172 number,
173 viewer: Some(ws.clone()),
174 after_seq: 0,
175 };
176 Ok(match event_name {
177 "push" => {
178 let (Some(git_ref), Some(after)) = (data["ref"].as_str(), data["after"].as_str()) else {
179 return Ok(None);
180 };
181 // The merge queue's states run merge_group workflows, not push ones.
182 if git_ref.starts_with("refs/heads/g1t-queue/") {
183 return Ok(None);
184 }
185 let before = data["before"].as_str();
186 let commits = self.commits(repo, ws, after, before).await?;
187 let title = commits.last().map(|c| c.message.lines().next().unwrap_or_default().to_owned()).unwrap_or_default();
188 let mut payload = payload::push(repo, git_ref, before, after, &commits, sender);
189 if let Some(head) = commits.last() {
190 payload["head_commit"] = payload::commit(repo, head);
191 }
192 Some(Subject {
193 source: path.clone(),
194 source_ref: Some(after.to_owned()),
195 git_ref: git_ref.to_owned(),
196 sha: after.to_owned(),
197 head_ref: None,
198 base_ref: None,
199 pull: None,
200 filter_ref: git_ref.to_owned(),
201 paths: None,
202 compare: Some((before.map(str::to_owned), after.to_owned())),
203 payload,
204 title,
205 trusted: true,
206 })
207 }
208 "pull_request" | "pull_request_target" | "pull_request_review" => {
209 let Some(number) = data["number"].as_u64().map(|n| n as u32) else { return Ok(None) };
210 let detail: Outcome<PullDetail> = g1t_kit::call(&self.work, "get_pull", &view(number)).await?;
211 let Outcome::Ok(detail) = detail else { return Ok(None) };
212 let pull = &detail.pull;
213 let base_ref = pull.base_branch(&repo.default_branch).to_owned();
214 let mut payload = json!({
215 "action": action,
216 "number": pull.number,
217 "pull_request": payload::pull(repo, pull),
218 "repository": payload::repository(repo),
219 "sender": payload::user(sender),
220 });
221 payload::changed(&mut payload, data);
222 if event_name == "pull_request_review" {
223 let review = detail.comments.iter().rev().find(|c| c.verdict.is_some());
224 payload["review"] = json!({
225 "state": review.and_then(|r| r.verdict).map(|v| format!("{v:?}").to_lowercase()),
226 "body": review.map(|r| r.body.clone()),
227 "user": review.map(|r| payload::user(&r.author.username)),
228 });
229 }
230 let trusted = self.insider(pull.owner(), repo, ws).await?;
231 let head_ref = payload::head_ref(pull);
232 if event_name == "pull_request_target" {
233 // In the base's context: its workflows, its head.
234 let Some(sha) = self.default_head(repo).await? else { return Ok(None) };
235 let mut subject = on_default(sha, payload, pull.title.clone(), Some(pull.number));
236 subject.head_ref = Some(head_ref);
237 subject.base_ref = Some(base_ref.clone());
238 subject.filter_ref = format!("refs/heads/{base_ref}");
239 subject.paths = Some(pull.files.iter().map(|f| f.path.clone()).collect());
240 return Ok(Some(subject));
241 }
242 // A merged pull request's run is on the commit it landed as,
243 // in the repository; otherwise on its head, where that is.
244 let landed = match (action, data["commit"].as_str()) {
245 (Some("closed"), Some(commit)) => Some(commit.to_owned()),
246 _ => None,
247 };
248 let sha = match (&landed, data["commit"].as_str(), &pull.head_commit) {
249 (Some(commit), _, _) => commit.clone(),
250 (None, Some(commit), _) => commit.to_owned(),
251 (None, None, Some(head)) => head.clone(),
252 (None, None, None) => return Ok(None),
253 };
254 let source = match landed {
255 Some(_) => path.clone(),
256 None => pull.fork.clone().unwrap_or_else(|| path.clone()),
257 };
258 Some(Subject {
259 source,
260 source_ref: Some(sha.clone()),
261 git_ref: format!("refs/pull/{}/merge", pull.number),
262 sha,
263 head_ref: Some(head_ref),
264 base_ref: Some(base_ref.clone()),
265 pull: Some(pull.number),
266 // `branches` filters on pull requests name the base.
267 filter_ref: format!("refs/heads/{base_ref}"),
268 paths: Some(pull.files.iter().map(|f| f.path.clone()).collect()),
269 compare: None,
270 payload,
271 title: pull.title.clone(),
272 trusted,
273 })
274 }
275 "workflow_run" => {
276 // A run of a workflow_run workflow does not start another,
277 // so two such workflows cannot set each other off.
278 if data["event"].as_str() == Some("workflow_run") {
279 return Ok(None);
280 }
281 let Some(sha) = self.default_head(repo).await? else { return Ok(None) };
282 let head_branch = data["ref"].as_str().unwrap_or_default().trim_start_matches("refs/heads/").to_owned();
283 let name = data["workflow"].as_str().unwrap_or_default();
284 let payload = json!({
285 "action": "completed",
286 "workflow_run": {
287 "id": data["runId"],
288 "name": name,
289 "path": data["path"],
290 "event": data["event"],
291 "status": "completed",
292 "conclusion": data["conclusion"],
293 "head_sha": data["sha"],
294 "head_branch": head_branch,
295 "run_number": data["number"],
296 "html_url": format!("{SITE}/{}/{}/actions/runs/{}", repo.namespace, repo.name, data["runId"].as_str().unwrap_or_default()),
297 "pull_requests": data["pull"].as_u64().map(|n| vec![json!({ "number": n })]).unwrap_or_default(),
298 },
299 "workflow": { "name": name, "path": data["path"] },
300 "repository": payload::repository(repo),
301 "sender": payload::user(sender),
302 });
303 let mut subject = on_default(sha, payload, format!("After {name}"), None);
304 // Branch filters apply to the branch the followed run was on.
305 subject.filter_ref = format!("refs/heads/{head_branch}");
306 Some(subject)
307 }
308 "issues" | "issue_comment" => {
309 let Some(number) = data["number"].as_u64().map(|n| n as u32) else { return Ok(None) };
310 let Some(sha) = self.default_head(repo).await? else { return Ok(None) };
311 let issue: Outcome<IssueDetail> = g1t_kit::call(&self.work, "get_issue", &view(number)).await?;
312 let (issue_json, comments, title, on_pull) = match issue {
313 Outcome::Ok(detail) => (payload::issue(repo, &detail.issue), detail.comments, detail.issue.title.clone(), false),
314 Outcome::Fail(_) => {
315 let pull: Outcome<PullDetail> = g1t_kit::call(&self.work, "get_pull", &view(number)).await?;
316 let Outcome::Ok(detail) = pull else { return Ok(None) };
317 (payload::pull_as_issue(repo, &detail.pull), detail.comments, detail.pull.title.clone(), true)
318 }
319 };
320 let mut payload = json!({
321 "action": action,
322 "issue": issue_json,
323 "repository": payload::repository(repo),
324 "sender": payload::user(sender),
325 });
326 payload::changed(&mut payload, data);
327 if event_name == "issue_comment" {
328 let comment_id = data["commentId"].as_str();
329 let comment = comments.iter().find(|c| Some(c.id.as_str()) == comment_id).or(comments.last());
330 match comment {
331 Some(comment) => payload["comment"] = payload::comment(repo, number, comment, on_pull),
332 None => return Ok(None),
333 }
334 }
335 Some(on_default(sha, payload, title, on_pull.then_some(number)))
336 }
337 _ => None,
338 })
339 }
340
341 pub async fn on_event(&self, event: &Event) -> Result<()> {
342 let Some(repo_id) = event.repo_id.as_deref() else { return Ok(()) };
343 let mapped = github_events(&event.kind);
344 let pushed_default = event.kind == "git.push" && event.data["defaultBranch"].as_bool() == Some(true);
345 if mapped.is_empty() && !pushed_default {
346 return Ok(());
347 }
348 let Some((repo, ws)) = self.repo_by_id(repo_id).await? else { return Ok(()) };
349 if pushed_default {
350 self.sync(&repo, &ws).await?;
351 }
352 let sender = self.username(event.actor.as_deref()).await?.unwrap_or_else(|| repo.namespace.clone());
353 for (event_name, action) in mapped {
354 // Issues and comments start the default branch's workflows,
355 // which the synced table lists: when none listens, nothing is
356 // read from git. Agents make many of these events.
357 if matches!(event_name, "issues" | "issue_comment") && self.listens(repo_id, event_name).await? == Some(false) {
358 continue;
359 }
360 let Some(mut subject) = self.subject(event, event_name, action, &repo, &ws, &sender).await? else {
361 continue;
362 };
363 let read = self.read_workflows(&subject.source, &ws, subject.source_ref.as_deref()).await?;
364 // A pull request's head runs each workflow once, however many
365 // events say it is there (marked ready, and pushed).
366 let key = match subject.pull {
367 Some(number) if event_name.starts_with("pull_request") && event_name != "pull_request_review" => {
368 let phase = if action == Some("closed") { "closed" } else { "open" };
369 format!("{event_name}:{number}:{}:{phase}", subject.sha)
370 }
371 _ => event.id.clone(),
372 };
373 self.start_matching(&repo, &ws, read, &mut subject, event_name, action, &key, event.actor.as_deref(), &sender)
374 .await?;
375 }
376 Ok(())
377 }
378
379 #[allow(clippy::too_many_arguments)]
380 async fn start_matching(
381 &self,
382 repo: &Repo,
383 ws: &User,
384 read: Read,
385 subject: &mut Subject,
386 event_name: &str,
387 action: Option<&str>,
388 event_key: &str,
389 actor_id: Option<&str>,
390 sender: &str,
391 ) -> Result<()> {
392 for file in read.files {
393 let parsed = workflow::parse(&file.source);
394 let workflow = match parsed {
395 Ok(workflow) => workflow,
396 Err(problem) => {
397 // A push shows a broken workflow as a failed run, as GitHub does.
398 if event_name == "push" && file.source.contains("on") {
399 self.record_invalid(repo, &file.path, &file.source, subject, event_key, actor_id, sender, &problem)
400 .await?;
401 }
402 continue;
403 }
404 };
405 let Some(trigger) = workflow.trigger(event_name) else { continue };
406 // workflow_run follows the workflows it names.
407 if event_name == "workflow_run" {
408 let followed = subject.payload["workflow_run"]["name"].as_str().unwrap_or_default();
409 if !trigger.workflows.iter().any(|name| name == followed) {
410 continue;
411 }
412 }
413 if !trigger.wants_type(action) || !self.passes(repo, ws, trigger, subject, event_name).await? {
414 continue;
415 }
416 if self.disabled(&repo.id, &file.path).await? {
417 continue;
418 }
419 self.create_run(NewRun {
420 repo: repo.clone(),
421 path: file.path,
422 source: file.source,
423 info: self.run_info(repo, &workflow, event_name, subject, sender, actor_id),
424 workflow,
425 action: action.map(str::to_owned),
426 pull: subject.pull,
427 title: subject.title.clone(),
428 inputs: Map::new(),
429 event_key: event_key.to_owned(),
430 actor_id: actor_id.map(str::to_owned),
431 actor: Some(sender.to_owned()),
432 trusted: subject.trusted,
433 })
434 .await?;
435 }
436 Ok(())
437 }
438
439 /// Whether the branch, tag and path filters let the event through.
440 async fn passes(&self, repo: &Repo, ws: &User, trigger: &Trigger, subject: &mut Subject, event_name: &str) -> Result<bool> {
441 let git_ref = subject.filter_ref.as_str();
442 if let Some(tag) = git_ref.strip_prefix("refs/tags/") {
443 // A tag push runs a workflow that filters tags, or filters nothing.
444 if trigger.tags.is_set() {
445 if !trigger.tags.allows(tag) {
446 return Ok(false);
447 }
448 } else if trigger.branches.is_set() {
449 return Ok(false);
450 }
451 // Paths are not checked for tags, as on GitHub.
452 return Ok(true);
453 }
454 let branch = git_ref.strip_prefix("refs/heads/").unwrap_or(git_ref);
455 if trigger.branches.is_set() {
456 if !trigger.branches.allows(branch) {
457 return Ok(false);
458 }
459 } else if event_name == "push" && trigger.tags.is_set() {
460 return Ok(false);
461 }
462 if trigger.paths.is_set() {
463 if subject.paths.is_none() {
464 let (base, head) = subject.compare.clone().unwrap_or((None, subject.sha.clone()));
465 subject.paths = Some(self.changed_paths(repo, ws, base, head).await?);
466 }
467 if !trigger.paths.allows_paths(subject.paths.as_deref().unwrap_or_default()) {
468 return Ok(false);
469 }
470 }
471 Ok(true)
472 }
473
474 async fn disabled(&self, repo_id: &str, path: &str) -> Result<bool> {
475 let row = self
476 .db
477 .prepare("SELECT * FROM workflows WHERE repo_id = ? AND path = ?")
478 .bind(&[repo_id.into(), path.into()])?
479 .first::<WorkflowRow>(None)
480 .await?;
481 Ok(row.is_some_and(|row| row.state == "disabled"))
482 }
483
484 fn run_info(&self, repo: &Repo, workflow: &Workflow, event_name: &str, subject: &Subject, sender: &str, actor_id: Option<&str>) -> RunInfo {
485 RunInfo {
486 repository: format!("{}/{}", repo.namespace, repo.name),
487 repository_id: repo.id.clone(),
488 default_branch: repo.default_branch.clone(),
489 event_name: event_name.to_owned(),
490 event: subject.payload.clone(),
491 git_ref: subject.git_ref.clone(),
492 sha: subject.sha.clone(),
493 head_ref: subject.head_ref.clone(),
494 base_ref: subject.base_ref.clone(),
495 actor: sender.to_owned(),
496 actor_id: actor_id.unwrap_or_default().to_owned(),
497 triggering_actor: sender.to_owned(),
498 run_id: String::new(),
499 run_number: 0,
500 run_attempt: 1,
501 workflow: workflow.name.clone().unwrap_or_default(),
502 workflow_path: String::new(),
503 server_url: SITE.to_owned(),
504 api_url: API.to_owned(),
505 }
506 }
507
508 #[allow(clippy::too_many_arguments)]
509 async fn record_invalid(
510 &self,
511 repo: &Repo,
512 path: &str,
513 source: &str,
514 subject: &Subject,
515 event_key: &str,
516 actor_id: Option<&str>,
517 sender: &str,
518 problem: &str,
519 ) -> Result<()> {
520 let row = self.workflow_row(repo, path, path, source).await?;
521 self.record_failed_run(&row, subject.git_ref.as_str(), &subject.sha, event_key, actor_id, sender, problem).await
522 }
523
524 /// Scheduled workflows whose cron fires this minute, on the default branch.
525 pub async fn run_schedules(&self, minute: u64) -> Result<()> {
526 let rows = self
527 .db
528 .prepare("SELECT * FROM workflows WHERE state = 'active' AND crons != '[]' AND error IS NULL")
529 .all()
530 .await?
531 .results::<WorkflowRow>()?;
532 for row in rows {
533 let crons: Vec<String> = serde_json::from_str(&row.crons).unwrap_or_default();
534 let Some(cron) = crons.iter().find(|cron| g1t_actions::cron::Schedule::parse(cron).is_ok_and(|s| s.fires_at(minute))) else {
535 continue;
536 };
537 let Ok(workflow) = workflow::parse(&row.source) else { continue };
538 // Schedules wait while a repository is archived; a deleted one is not found.
539 let Some((repo, _ws)) = self.repo_by_id(&row.repo_id).await?.filter(|(repo, _)| !repo.archived()) else { continue };
540 let Some(sha) = self.default_head(&repo).await? else { continue };
541 let payload = json!({ "schedule": cron, "repository": payload::repository(&repo), "workflow": row.path });
542 let mut subject = Subject {
543 source: Self::repo_path(&repo),
544 source_ref: None,
545 git_ref: format!("refs/heads/{}", repo.default_branch),
546 sha,
547 head_ref: None,
548 base_ref: None,
549 pull: None,
550 filter_ref: String::new(),
551 paths: None,
552 compare: None,
553 payload,
554 title: format!("Scheduled: {cron}"),
555 trusted: true,
556 };
557 subject.filter_ref = subject.git_ref.clone();
558 let info = self.run_info(&repo, &workflow, "schedule", &subject, &repo.namespace, None);
559 self.create_run(NewRun {
560 repo: repo.clone(),
561 path: row.path.clone(),
562 source: row.source.clone(),
563 workflow,
564 info,
565 action: None,
566 pull: None,
567 title: subject.title.clone(),
568 inputs: Map::new(),
569 event_key: format!("schedule:{minute}"),
570 actor_id: None,
571 actor: None,
572 trusted: true,
573 })
574 .await?;
575 }
576 Ok(())
577 }
578
579 /// `dispatch`: someone with the Write role runs a workflow that has
580 /// `workflow_dispatch`.
581 pub async fn dispatch(&self, a: DispatchArgs) -> Result<Outcome<WorkflowRun>> {
582 let repo = check!(self.may(&a.actor, &a.repo, Capability::Run).await?);
583 if repo.archived() {
584 return Ok(fail(FailureCode::Forbidden, g1t_contracts::repos::archived_message(&repo.namespace, &repo.name)));
585 }
586 let Some(ws) = self.workspace_actor(&repo.namespace).await? else {
587 return Ok(fail(FailureCode::NotFound, "There is no such workspace."));
588 };
589 let git_ref = a.git_ref.clone().unwrap_or_else(|| repo.default_branch.clone());
590 let full_ref = if git_ref.starts_with("refs/") {
591 git_ref.clone()
592 } else {
593 // A branch if there is one by that name, otherwise a tag.
594 let branches: Outcome<Vec<g1t_contracts::repos::Branch>> = g1t_kit::call(
595 &self.repos,
596 "branches",
597 &g1t_contracts::repos::BranchesArgs {
598 path: Self::repo_path(&repo),
599 viewer: Some(ws.clone()),
600 },
601 )
602 .await?;
603 let is_branch = branches.into_result().unwrap_or_default().iter().any(|branch| branch.name == git_ref);
604 format!("refs/{}/{git_ref}", if is_branch { "heads" } else { "tags" })
605 };
606 let short = full_ref.trim_start_matches("refs/heads/").trim_start_matches("refs/tags/").to_owned();
607 let read = self.read_workflows(&Self::repo_path(&repo), &ws, Some(&short)).await?;
608 let Some(sha) = read.head.clone() else {
609 return Ok(fail(FailureCode::NotFound, format!("There is no branch or tag called {short}.")));
610 };
611 // A workflow is named by its file (`build.yml`), its path, or its id
612 // (`wfl_…`), which stands for the path it was read from.
613 let by_id = if a.workflow.starts_with("wfl_") {
614 self.db
615 .prepare("SELECT * FROM workflows WHERE repo_id = ? AND id = ?")
616 .bind(&[repo.id.as_str().into(), a.workflow.as_str().into()])?
617 .first::<WorkflowRow>(None)
618 .await?
619 .map(|row| row.path)
620 } else {
621 None
622 };
623 let named = by_id.as_deref().unwrap_or(&a.workflow);
624 let wanted = named.trim_start_matches(".g1t/workflows/");
625 let Some(file) = read.files.iter().find(|file| {
626 file.path.rsplit('/').next() == Some(wanted) || file.path == named
627 }) else {
628 return Ok(fail(FailureCode::NotFound, format!("There is no workflow {wanted} on {short}.")));
629 };
630 let workflow = match workflow::parse(&file.source) {
631 Ok(workflow) => workflow,
632 Err(problem) => return Ok(fail(FailureCode::Invalid, format!("The workflow does not read: {problem}"))),
633 };
634 let Some(trigger) = workflow.trigger("workflow_dispatch") else {
635 return Ok(fail(FailureCode::Invalid, "That workflow cannot be run by hand: it has no `workflow_dispatch` trigger."));
636 };
637 let inputs = check!(dispatch_inputs(trigger, &a.inputs));
638 let payload = json!({
639 "inputs": inputs,
640 "ref": full_ref,
641 "repository": payload::repository(&repo),
642 "sender": payload::user(&a.actor.username),
643 "workflow": file.path,
644 });
645 let subject = Subject {
646 source: Self::repo_path(&repo),
647 source_ref: Some(sha.clone()),
648 git_ref: full_ref.clone(),
649 sha,
650 head_ref: None,
651 base_ref: None,
652 pull: None,
653 filter_ref: full_ref,
654 paths: None,
655 compare: None,
656 payload,
657 title: format!("{} run by {}", workflow.display_name(&file.path), a.actor.username),
658 trusted: true,
659 };
660 let info = self.run_info(&repo, &workflow, "workflow_dispatch", &subject, &a.actor.username, Some(&a.actor.id));
661 let created = self
662 .create_run(NewRun {
663 repo: repo.clone(),
664 path: file.path.clone(),
665 source: file.source.clone(),
666 workflow,
667 info,
668 action: None,
669 pull: None,
670 title: subject.title.clone(),
671 inputs,
672 event_key: format!("dispatch:{}", new_id("dsp", now_ms())),
673 actor_id: Some(a.actor.id.clone()),
674 actor: Some(a.actor.username.clone()),
675 trusted: true,
676 })
677 .await?;
678 match created {
679 Some(id) => self.run_summary(&id).await,
680 None => Ok(fail(FailureCode::Conflict, "It did not start.")),
681 }
682 }
683}
684
685#[derive(serde::Deserialize)]
686#[serde(rename_all = "camelCase")]
687pub struct MergeGroupArgs {
688 pub repo_id: String,
689 pub entry: String,
690 pub sha: String,
691 pub head_ref: String,
692 #[serde(default)]
693 pub base_sha: Option<String>,
694 pub number: u32,
695 #[serde(default)]
696 pub ahead: Vec<u32>,
697}
698
699impl Actions {
700 /// `merge_group`: the merge queue built a state and its checks passed.
701 /// Starts the workflows that run `on: merge_group` on it, as GitHub's
702 /// queue does, and says how many started; the queue waits for their
703 /// statuses on that commit.
704 pub async fn merge_group(&self, a: MergeGroupArgs) -> Result<Outcome<Value>> {
705 let Some((repo, ws)) = self.repo_by_id(&a.repo_id).await? else {
706 return Ok(Outcome::Ok(json!({ "runs": 0 })));
707 };
708 let read = self.read_workflows(&Self::repo_path(&repo), &ws, Some(&a.sha)).await?;
709 let head_commit = self.commits(&repo, &ws, &a.sha, None).await?.pop();
710 let payload = json!({
711 "action": "checks_requested",
712 "merge_group": {
713 "head_sha": a.sha,
714 "head_ref": a.head_ref,
715 "base_sha": a.base_sha,
716 "base_ref": format!("refs/heads/{}", repo.default_branch),
717 "head_commit": head_commit.as_ref().map(|c| payload::commit(&repo, c)),
718 },
719 "repository": payload::repository(&repo),
720 "sender": payload::user(&repo.namespace),
721 });
722 let mut started = 0u32;
723 for file in read.files {
724 let Ok(workflow) = workflow::parse(&file.source) else { continue };
725 let Some(trigger) = workflow.trigger("merge_group") else { continue };
726 if !trigger.wants_type(Some("checks_requested")) || self.disabled(&repo.id, &file.path).await? {
727 continue;
728 }
729 // Branch filters on merge_group name the branch it merges into.
730 if trigger.branches.is_set() && !trigger.branches.allows(&repo.default_branch) {
731 continue;
732 }
733 let ahead = if a.ahead.is_empty() {
734 String::new()
735 } else {
736 format!(" after {}", a.ahead.iter().map(|n| format!("#{n}")).collect::<Vec<_>>().join(", "))
737 };
738 let subject = Subject {
739 source: Self::repo_path(&repo),
740 source_ref: Some(a.sha.clone()),
741 git_ref: a.head_ref.clone(),
742 sha: a.sha.clone(),
743 head_ref: None,
744 base_ref: Some(repo.default_branch.clone()),
745 pull: Some(a.number),
746 filter_ref: format!("refs/heads/{}", repo.default_branch),
747 paths: None,
748 compare: None,
749 payload: payload.clone(),
750 title: format!("Merge queue: #{}{ahead}", a.number),
751 trusted: true,
752 };
753 let info = self.run_info(&repo, &workflow, "merge_group", &subject, &repo.namespace, None);
754 let created = self
755 .create_run(NewRun {
756 repo: repo.clone(),
757 path: file.path.clone(),
758 source: file.source.clone(),
759 workflow,
760 info,
761 action: Some("checks_requested".to_owned()),
762 pull: Some(a.number),
763 title: subject.title.clone(),
764 inputs: Map::new(),
765 event_key: format!("merge_group:{}:{}", a.entry, a.sha),
766 actor_id: None,
767 actor: None,
768 trusted: true,
769 })
770 .await?;
771 if created.is_some() {
772 started += 1;
773 }
774 }
775 Ok(Outcome::Ok(json!({ "runs": started })))
776 }
777}
778
779/// The inputs of a manual run: what was given, checked against the
780/// workflow's declared inputs, with their defaults filled in.
781fn dispatch_inputs(trigger: &Trigger, given: &Map<String, Value>) -> Outcome<Map<String, Value>> {
782 let mut inputs = Map::new();
783 for (name, spec) in &trigger.inputs {
784 let kind = spec.get("type").and_then(Value::as_str).unwrap_or("string");
785 let value = given.get(name).cloned().or_else(|| spec.get("default").cloned());
786 let required = spec.get("required").and_then(Value::as_bool).unwrap_or(false);
787 let value = match value {
788 Some(Value::Null) | None if required => return fail(FailureCode::Invalid, format!("The input `{name}` is required.")),
789 Some(Value::Null) | None => match kind {
790 "boolean" => Value::Bool(false),
791 _ => Value::String(String::new()),
792 },
793 Some(value) => match kind {
794 "boolean" => Value::Bool(match &value {
795 Value::Bool(flag) => *flag,
796 Value::String(text) => text == "true",
797 _ => false,
798 }),
799 "number" => match &value {
800 Value::Number(_) => value,
801 Value::String(text) => match text.parse::<f64>().ok().and_then(serde_json::Number::from_f64) {
802 Some(number) => Value::Number(number),
803 None => return fail(FailureCode::Invalid, format!("The input `{name}` is a number.")),
804 },
805 _ => return fail(FailureCode::Invalid, format!("The input `{name}` is a number.")),
806 },
807 "choice" => {
808 let text = g1t_actions::expr::to_text(&value);
809 let options: Vec<String> =
810 spec.get("options").and_then(Value::as_array).map(|o| o.iter().map(g1t_actions::expr::to_text).collect()).unwrap_or_default();
811 if !options.is_empty() && !options.contains(&text) {
812 return fail(FailureCode::Invalid, format!("The input `{name}` is one of {}.", options.join(", ")));
813 }
814 Value::String(text)
815 }
816 _ => Value::String(g1t_actions::expr::to_text(&value)),
817 },
818 };
819 inputs.insert(name.clone(), value);
820 }
821 Outcome::Ok(inputs)
822}
823
824#[cfg(test)]
825mod tests {
826 use super::*;
827 use g1t_contracts::work::{Pull, g1t_author};
828 use g1t_contracts::{Membership, PrincipalKind};
829
830 fn repo() -> Repo {
831 serde_json::from_value(json!({
832 "id": "rep_1", "namespace": "acme", "name": "web", "description": null, "isPrivate": true,
833 "ownerId": "ws_1", "defaultBranch": "main", "forkOf": null, "createdAt": ""
834 }))
835 .unwrap()
836 }
837
838 fn person(id: &str, username: &str) -> User {
839 User { id: id.into(), username: username.into(), kind: PrincipalKind::User, ..User::default() }
840 }
841
842 fn made_for(asker: User) -> Pull {
843 serde_json::from_value(json!({
844 "id": "pr_1", "repoId": "rep_1", "number": 14, "issue": 12, "title": "Fix it", "body": null,
845 "agent": "g1t", "runtime": "hosted", "status": "open",
846 "fork": { "namespace": "pulls", "name": "pr_1" }, "forkRepoId": "rep_f",
847 "branch": null, "headCommit": "abc", "mergeBase": null, "mergedBy": null, "mergedAt": null,
848 "supersededBy": null, "checkStatus": null,
849 "author": g1t_author(), "requestedBy": asker,
850 "createdAt": "", "updatedAt": ""
851 }))
852 .unwrap()
853 }
854
855 #[test]
856 fn g1t_s_change_for_someone_is_trusted_as_they_are() {
857 // Stored people carry no memberships, so identity is asked about
858 // them; being g1t's change gives it nothing more.
859 let pull = made_for(person("usr_2", "ana"));
860 assert!(!trusted_outright(pull.owner(), &repo()));
861 // Someone known to be able to push is trusted at once.
862 let mut member = person("usr_1", "syntaqx");
863 member.workspaces.push(Membership::member("acme"));
864 let pull = made_for(member);
865 assert!(trusted_outright(pull.owner(), &repo()));
866 }
867
868 #[test]
869 fn the_payload_names_g1t_as_its_user_and_who_asked_for_it() {
870 let pull = made_for(person("usr_1", "syntaqx"));
871 let event = payload::pull(&repo(), &pull);
872 assert_eq!(event["user"]["login"], "g1t");
873 assert_eq!(event["user"]["type"], "Bot");
874 assert_eq!(event["requested_by"]["login"], "syntaqx");
875 assert_eq!(event["requested_by"]["type"], "User");
876 let as_issue = payload::pull_as_issue(&repo(), &pull);
877 assert_eq!(as_issue["user"]["login"], "g1t");
878 assert_eq!(as_issue["requested_by"]["login"], "syntaqx");
879 }
880
881 #[test]
882 fn a_pull_request_names_its_own_base_labels_and_milestone() {
883 let mut pull = made_for(person("usr_1", "syntaqx"));
884 let event = payload::pull(&repo(), &pull);
885 assert_eq!(event["base"]["ref"], repo().default_branch);
886 pull.base = Some("release/1.x".into());
887 pull.labels = vec!["bug".into()];
888 pull.milestone = Some(g1t_contracts::work::MilestoneRef { number: 2, title: "1.1".into() });
889 let event = payload::pull(&repo(), &pull);
890 assert_eq!(event["base"]["ref"], "release/1.x");
891 assert_eq!(event["labels"], serde_json::json!([{ "name": "bug" }]));
892 assert_eq!(event["milestone"]["title"], "1.1");
893 let mut labeled = serde_json::json!({ "action": "labeled" });
894 payload::changed(&mut labeled, &serde_json::json!({ "label": { "name": "bug", "color": "d73a4a" } }));
895 assert_eq!(labeled["label"]["color"], "d73a4a");
896 }
897}