Skip to main content

artifactize_core/
verify_tools.rs

1//! Protocol-neutral implementation of the public Verify and inspection tools.
2mod catalog;
3mod error;
4mod input;
5mod page;
6mod read;
7mod tool;
8
9pub use catalog::{Hints, Tool, tools};
10pub use error::{ArgumentsError, Error};
11pub use input::Call;
12pub use page::{CursorError, Page};
13
14use serde::Serialize;
15use tokio_util::sync::CancellationToken;
16
17use crate::{project::VerifyOptions, runs, types::RunId};
18use input::{Command, Inspection, Plugin, Read, Verify};
19
20#[derive(Serialize)]
21#[serde(untagged)]
22pub enum Output {
23    Observation(runs::RunObservation),
24    Cancellation(runs::CancelAcknowledgement),
25    Inspection(Page),
26    Plugin(PluginResult),
27}
28
29#[derive(Serialize)]
30#[serde(tag = "action", rename_all = "lowercase")]
31pub enum PluginResult {
32    Added(crate::config::plugins::PluginInfo),
33    Removed {
34        #[serde(rename = "namespace")]
35        name: crate::config::PluginName,
36    },
37}
38
39/// Whether `name` is one of the public tools, served or not.
40pub fn is_tool(name: &str) -> bool {
41    tool::ToolId::parse(name).is_some()
42}
43
44/// The Run's `run_show` JSON, read at once and whole, with managed secrets scrubbed.
45pub async fn run_json(context: &runs::Context, run: &crate::types::RunId) -> Result<String, Error> {
46    read::run(context, run).await
47}
48
49/// An initial action wait; a cursor only reads the next page, never starts another wait.
50pub struct ActionWait {
51    pub run: RunId,
52    page: page::Query,
53}
54
55impl ActionWait {
56    pub async fn wait(
57        self,
58        context: &runs::Context,
59        bound: runs::ActionWaitBound,
60        cancel: CancellationToken,
61    ) -> Result<Page, Error> {
62        let summary = runs::wait_for_action(context, &self.run, bound, cancel).await?;
63        read::project_page(context, self.page, serde_json::to_string(&summary)?)
64    }
65}
66
67impl Call {
68    /// The existing Run this call names: the one `run_show` reads or `run_cancel` cancels.
69    pub fn run(&self) -> Option<&RunId> {
70        match &self.command {
71            Command::Inspect(Inspection {
72                read: Read::Run(show),
73                ..
74            }) => Some(&show.run),
75            Command::Verify(Verify::Cancel(run)) => Some(run),
76            _ => None,
77        }
78    }
79
80    /// Extract an initial action wait so an asynchronous transport can own its observation.
81    pub fn action_wait(&self) -> Option<ActionWait> {
82        match &self.command {
83            Command::Inspect(Inspection {
84                read: Read::Run(show),
85                page,
86            }) if show.wait_for_action && page.is_initial() => Some(ActionWait {
87                run: show.run.clone(),
88                page: page.clone(),
89            }),
90            _ => None,
91        }
92    }
93
94    /// Execute an operation in the scope resolved by its transport.
95    pub async fn execute(
96        self,
97        context: &runs::Context,
98        cancel: CancellationToken,
99    ) -> Result<Output, Error> {
100        if let Some(wait) = self.action_wait() {
101            return Ok(Output::Inspection(
102                wait.wait(context, runs::ActionWaitBound::Inline, cancel)
103                    .await?,
104            ));
105        }
106        let command = match self.command {
107            Command::Verify(command) => command,
108            Command::Plugin(plugin) => {
109                use crate::config::plugins;
110                let result = match plugin {
111                    Plugin::Add(args) => PluginResult::Added(
112                        plugins::add(context.repo(), &args.path).map_err(Error::Config)?,
113                    ),
114                    Plugin::Remove(args) => {
115                        plugins::remove(context.repo(), &args.name).map_err(Error::Config)?;
116                        PluginResult::Removed { name: args.name }
117                    }
118                };
119                return Ok(Output::Plugin(result));
120            }
121            Command::Inspect(inspection) => {
122                return Ok(Output::Inspection(
123                    read::inspect(context, inspection, cancel).await?,
124                ));
125            }
126        };
127        Ok(match command {
128            Verify::Start(args) => {
129                let fingerprint = args.request_key.as_ref().map(|_| args.fingerprint());
130                let options = VerifyOptions {
131                    jobs: args.jobs.map(|limit| limit.get() as usize),
132                    modes: runs::RunModes {
133                        speculate: args.speculate,
134                        ignore_gates: args.ignore_gates,
135                    },
136                    comment: args.comment,
137                    profile_variables: args.profile_variables,
138                    ..Default::default()
139                };
140                Output::Observation(
141                    runs::start_with_args_hash(
142                        context,
143                        args.scope.selection(),
144                        &options,
145                        args.request_key,
146                        fingerprint,
147                    )
148                    .await?,
149                )
150            }
151            Verify::Cancel(run) => Output::Cancellation(runs::cancel(context, &run).await?),
152        })
153    }
154}