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};
18use input::{Command, Plugin, 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
49impl Call {
50    /// Execute an operation in the scope resolved by its transport.
51    pub async fn execute(
52        self,
53        context: &runs::Context,
54        cancel: CancellationToken,
55    ) -> Result<Output, Error> {
56        let command = match self.command {
57            Command::Verify(command) => command,
58            Command::Plugin(plugin) => {
59                use crate::config::plugins;
60                let result = match plugin {
61                    Plugin::Add(args) => PluginResult::Added(
62                        plugins::add(context.repo(), &args.path).map_err(Error::Config)?,
63                    ),
64                    Plugin::Remove(args) => {
65                        plugins::remove(context.repo(), &args.name).map_err(Error::Config)?;
66                        PluginResult::Removed { name: args.name }
67                    }
68                };
69                return Ok(Output::Plugin(result));
70            }
71            Command::Inspect(inspection) => {
72                return Ok(Output::Inspection(
73                    read::inspect(context, inspection, cancel).await?,
74                ));
75            }
76        };
77        Ok(match command {
78            Verify::Start(args) => {
79                let options = VerifyOptions {
80                    jobs: args.jobs.map(|limit| limit.get() as usize),
81                    modes: runs::RunModes {
82                        speculate: args.speculate,
83                    },
84                    comment: args.comment.clone(),
85                    ..Default::default()
86                };
87                let fingerprint = args.request_key.as_ref().map(|_| args.fingerprint());
88                Output::Observation(
89                    runs::start_with_args_hash(
90                        context,
91                        args.scope.selection(),
92                        &options,
93                        args.request_key,
94                        fingerprint,
95                    )
96                    .await?,
97                )
98            }
99            Verify::Cancel(run) => Output::Cancellation(runs::cancel(context, &run).await?),
100        })
101    }
102}