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    /// Extract an initial action wait so an asynchronous transport can own its observation.
69    pub fn action_wait(&self) -> Option<ActionWait> {
70        match &self.command {
71            Command::Inspect(Inspection {
72                read: Read::Run(show),
73                page,
74            }) if show.wait_for_action && page.is_initial() => Some(ActionWait {
75                run: show.run.clone(),
76                page: page.clone(),
77            }),
78            _ => None,
79        }
80    }
81
82    /// Execute an operation in the scope resolved by its transport.
83    pub async fn execute(
84        self,
85        context: &runs::Context,
86        cancel: CancellationToken,
87    ) -> Result<Output, Error> {
88        if let Some(wait) = self.action_wait() {
89            return Ok(Output::Inspection(
90                wait.wait(context, runs::ActionWaitBound::Inline, cancel)
91                    .await?,
92            ));
93        }
94        let command = match self.command {
95            Command::Verify(command) => command,
96            Command::Plugin(plugin) => {
97                use crate::config::plugins;
98                let result = match plugin {
99                    Plugin::Add(args) => PluginResult::Added(
100                        plugins::add(context.repo(), &args.path).map_err(Error::Config)?,
101                    ),
102                    Plugin::Remove(args) => {
103                        plugins::remove(context.repo(), &args.name).map_err(Error::Config)?;
104                        PluginResult::Removed { name: args.name }
105                    }
106                };
107                return Ok(Output::Plugin(result));
108            }
109            Command::Inspect(inspection) => {
110                return Ok(Output::Inspection(
111                    read::inspect(context, inspection, cancel).await?,
112                ));
113            }
114        };
115        Ok(match command {
116            Verify::Start(args) => {
117                let fingerprint = args.request_key.as_ref().map(|_| args.fingerprint());
118                let options = VerifyOptions {
119                    jobs: args.jobs.map(|limit| limit.get() as usize),
120                    modes: runs::RunModes {
121                        speculate: args.speculate,
122                    },
123                    comment: args.comment,
124                    profile_variables: args.profile_variables,
125                    ..Default::default()
126                };
127                Output::Observation(
128                    runs::start_with_args_hash(
129                        context,
130                        args.scope.selection(),
131                        &options,
132                        args.request_key,
133                        fingerprint,
134                    )
135                    .await?,
136                )
137            }
138            Verify::Cancel(run) => Output::Cancellation(runs::cancel(context, &run).await?),
139        })
140    }
141}