ai6_api/source.rs
1//! The Source kind: a tracker or CI system that supplies work, and the items it hands over.
2//!
3//! A [`Source`] provider polls one external system for work. The Station turns each
4//! [`SourceItem`] into a queued brief; what is written back to a tracker goes through the
5//! [`Tracker`](crate::tracker::Tracker) kind.
6
7use guatiao::library::ProviderError;
8use guatiao::{FromValue, Schema, ToValue};
9use serde::{Deserialize, Serialize};
10
11/// One unit of work found at a source.
12#[derive(ToValue, FromValue, Schema, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
13#[map(crate = ::guatiao)]
14pub struct SourceItem {
15 /// The provider-local identity, stable across polls (for example `owner/repo#12`).
16 pub id: String,
17 /// The item title as written at the source. Untrusted text.
18 pub title: String,
19 /// The item description as written at the source. Untrusted text.
20 pub body: String,
21 /// A link to the item.
22 pub url: Option<String>,
23 /// Labels or tags currently on the item.
24 pub labels: Vec<String>,
25 /// The author's name at the source.
26 pub author: Option<String>,
27 /// When the source last changed the item, as the provider spells it (ISO 8601).
28 pub updated: String,
29 /// The item's short code at the tracker, as a person writes it: `AI6-64`, `63`. It goes
30 /// into the Mission ID, made safe there; `None` uses the item's `id` instead.
31 pub code: Option<String>,
32 /// What names the tracker for a `code` that does not name it itself (`gh` for GitHub's
33 /// `63`); `None` for one that does (Jira's `AI6-64`). The Station replaces it with the
34 /// instance's name when the provider has several instances in the Operation.
35 pub code_tag: Option<String>,
36 /// The event the item reports, in the provider's words (`check_failed`), when it is one.
37 /// An item that reports an event an authorized Sleeper waits for fires that Sleeper and
38 /// is not a Report of its own. Set only by the provider, never from the item's text.
39 pub event: Option<String>,
40}
41
42/// A request for the items that changed since a cursor.
43#[derive(ToValue, FromValue, Schema, Debug, Clone, PartialEq, Eq)]
44#[map(crate = ::guatiao)]
45pub struct SourcePoll {
46 /// The cursor returned by the previous poll; `None` on the first poll.
47 pub cursor: Option<String>,
48}
49
50/// The items found by one poll and where to resume.
51#[derive(ToValue, FromValue, Schema, Debug, Clone, PartialEq, Eq)]
52#[map(crate = ::guatiao)]
53pub struct SourceBatch {
54 /// Items that match the Operation's filters, oldest change first.
55 pub items: Vec<SourceItem>,
56 /// The cursor to present on the next poll; `None` keeps the stored one.
57 pub cursor: Option<String>,
58}
59
60/// What one Source instance's own settings say, which the Station cannot read from a key it
61/// does not know the name of: the provider answers, with no request to the tracker.
62#[derive(ToValue, FromValue, Schema, Debug, Clone, Default, PartialEq, Eq)]
63#[map(crate = ::guatiao)]
64pub struct SourceFacts {
65 /// The `https` page of the repository or project, or a Jira site's root; `None` when the
66 /// provider names none, or the instance is not served over `https`.
67 pub web: Option<String>,
68 /// The project's key at the tracker, when it has one (a Jira project's); `None` for a
69 /// repository, or an instance that reads by query alone.
70 pub project: Option<String>,
71 /// The labels an item needs to be taken in. An Agent never sets one, and nothing AI6 creates
72 /// carries one by itself.
73 #[map(default)]
74 pub intake_labels: Vec<String>,
75 /// The host names the tracker is reached at and writes its links to, which an Agent's links
76 /// may name.
77 #[map(default)]
78 pub hosts: Vec<String>,
79 /// The environment variables its credentials are read from, and the ones the tracker's own
80 /// command line tools read: a Run is given none of them and their values are masked.
81 #[map(default)]
82 pub credential_variables: Vec<String>,
83}
84
85/// A tracker or CI system that supplies work. What is written back to a tracker goes through
86/// the [`Tracker`](super::tracker::Tracker) kind.
87#[guatiao::kind(name = "ai6_source")]
88pub trait Source: Send + Sync {
89 /// What this instance's settings say of its web page, intake labels, hosts and credential
90 /// variables, with no request to the tracker. The default says nothing.
91 fn facts(&self) -> Result<SourceFacts, ProviderError> {
92 Ok(SourceFacts::default())
93 }
94
95 /// Returns the items that changed since `request.cursor` and match the filters the
96 /// provider was configured with. Blocks for the duration of the network exchange.
97 fn poll(&self, request: SourcePoll) -> Result<SourceBatch, ProviderError>;
98
99 /// The page where people read this instance's issues as a board, from its own settings and
100 /// with no request to the tracker; `None` when the provider names none.
101 fn board_url(&self) -> Result<Option<String>, ProviderError> {
102 Ok(None)
103 }
104
105 /// The text of the project's issue templates, read through the tracker: a hidden comment an
106 /// item carries from one of them is the template's own, and vetting does not hold it.
107 /// Empty when the provider has no templates or the project has none.
108 fn issue_templates(&self) -> Result<Vec<String>, ProviderError> {
109 Ok(Vec::new())
110 }
111}
112
113#[cfg(test)]
114mod tests {
115 use super::*;
116
117 // A changed required signature affects every built source provider.
118 #[test]
119 fn floor_hashes_are_pinned() {
120 assert_eq!(SourceVtable::FLOOR_HASH, 2228427855);
121 }
122}