g1t/crates/contracts/src/automations.rs

109 lines3,513 bytesCodeBlame
1//! The automations service: rules in a repository's `.g1t/automations/`
2//! that act when something happens, in the way GitHub Actions' workflows
3//! do, but on g1t's events and with g1t's own steps.
4//!
5//! An automation says *on* which event (or a schedule, or by hand), *if*
6//! which conditions hold, *do* which steps: comment, label, put an agent
7//! on it, message the agent working on it, open, close or reopen an issue,
8//! post to a URL. Files on the default branch are the source of truth; each
9//! push reloads them. Every run is kept, step by step.
10//!
11//! Mirrors `packages/contracts/src/automations.ts`.
12
13use serde::{Deserialize, Serialize};
14
15use crate::repos::RepoPath;
16use crate::{User, Viewer};
17
18/// One automation, as read from its file.
19#[derive(Clone, Debug, Serialize, Deserialize)]
20#[serde(rename_all = "camelCase")]
21pub struct Automation {
22 pub id: String,
23 /// `owner/name`.
24 pub repo: String,
25 /// The file it comes from, such as `.g1t/automations/bugs.yml`.
26 pub path: String,
27 pub name: String,
28 /// What starts it, in words: `issue.opened`, `every Monday at 09:00`.
29 pub trigger: String,
30 /// Its conditions and steps, in words, one each.
31 pub conditions: Vec<String>,
32 pub steps: Vec<String>,
33 /// Whether it runs. Members can turn one off without changing its file.
34 pub enabled: bool,
35 /// Why the file could not be used, if it could not.
36 pub error: Option<String>,
37 /// Whether it can be run by hand.
38 pub manual: bool,
39 pub last_run: Option<AutomationRun>,
40}
41
42/// One step of a run, and how it went.
43#[derive(Clone, Debug, Serialize, Deserialize)]
44#[serde(rename_all = "camelCase")]
45pub struct StepResult {
46 pub step: String,
47 pub ok: bool,
48 pub detail: String,
49}
50
51#[derive(Clone, Debug, Serialize, Deserialize)]
52#[serde(rename_all = "camelCase")]
53pub struct AutomationRun {
54 pub id: String,
55 pub automation_id: String,
56 pub name: String,
57 /// What started it: an event type, `schedule` or `manual`.
58 pub event: String,
59 /// The issue or pull request it acted on.
60 pub number: Option<u32>,
61 /// `succeeded`, `failed` (a step failed) or `skipped` (and why, in
62 /// `reason`).
63 pub status: String,
64 pub reason: Option<String>,
65 pub steps: Vec<StepResult>,
66 /// Who started it by hand, or who caused the event.
67 pub actor: Option<String>,
68 /// RFC 3339.
69 pub started_at: String,
70}
71
72/// `list`: a repository's automations. Returns `Outcome<Vec<Automation>>`.
73/// Anyone who can see the repository.
74#[derive(Debug, Serialize, Deserialize)]
75pub struct ListArgs {
76 pub repo: RepoPath,
77 pub viewer: Viewer,
78}
79
80/// `runs`: a repository's latest runs, newest first, of one automation or
81/// all. Returns `Outcome<Vec<AutomationRun>>`.
82#[derive(Debug, Serialize, Deserialize)]
83pub struct RunsArgs {
84 pub repo: RepoPath,
85 pub viewer: Viewer,
86 #[serde(default)]
87 pub automation: Option<String>,
88}
89
90/// `run`: runs an automation now, on an issue or pull request if given.
91/// Returns `Outcome<AutomationRun>`. Members only.
92#[derive(Debug, Serialize, Deserialize)]
93pub struct RunArgs {
94 pub actor: User,
95 pub repo: RepoPath,
96 pub id: String,
97 #[serde(default)]
98 pub number: Option<u32>,
99}
100
101/// `set_enabled`: turns an automation on or off. Returns
102/// `Outcome<Automation>`. Members only.
103#[derive(Debug, Serialize, Deserialize)]
104pub struct SetEnabledArgs {
105 pub actor: User,
106 pub repo: RepoPath,
107 pub id: String,
108 pub enabled: bool,
109}