Skip to main content

ai6_api/
tracker.rs

1//! Trackers: the Registry's contract with an issue tracker for reading, commenting,
2//! labelling and creating issues.
3//!
4//! A [`Tracker`] is the second kind a tracker provider serves beside [`Source`]. The
5//! provider is built from the same `sources.<name>` settings table, so one table and one
6//! token configure both. Every call is blocking and names its issue by the provider-local
7//! item ID a [`SourceItem`] carries (`owner/repo#12`, `GUA-12`).
8//!
9//! [`Source`]: crate::source::Source
10//! [`SourceItem`]: crate::source::SourceItem
11
12use guatiao::library::ProviderError;
13use guatiao::{FromValue, Schema, ToValue};
14
15/// What one tracker instance can do, discovered from the provider and the tracker.
16///
17/// Answers about the repository or project are the same for every issue and may be cached
18/// by the caller for a while; `transitions` depends on the issue it was asked about.
19#[derive(ToValue, FromValue, Schema, Debug, Clone, Default, PartialEq, Eq)]
20#[map(crate = ::guatiao)]
21pub struct TrackerCapabilities {
22    /// New comments can be posted.
23    pub comment: bool,
24    /// A posted comment can be edited in place.
25    pub edit_comment: bool,
26    /// The labels the repository or project has; empty when `free_labels`.
27    pub labels: Vec<String>,
28    /// Labels are free text, so any label is accepted and `labels` lists none.
29    pub free_labels: bool,
30    /// The workflow transitions open on the issue asked about; empty without a workflow.
31    pub transitions: Vec<String>,
32    /// Assignees can be changed.
33    pub assign: bool,
34    /// An issue can be closed and reopened.
35    pub close: bool,
36    /// Issues can be created.
37    pub create: bool,
38    /// The issue types a created issue may have; empty when the tracker has none.
39    pub issue_types: Vec<String>,
40    /// The priorities a created issue may carry; empty when the tracker has none.
41    pub priorities: Vec<String>,
42    /// How a child issue is attached to its parent: `none`, `sub_issues`, `subtasks` or
43    /// `parent`.
44    pub children: String,
45    /// The ways two issues are linked besides naming each other in text: `issue_links`
46    /// and `remote_links`; empty when only text can.
47    pub links: Vec<String>,
48    /// Comments made after a moment can be listed.
49    pub replies: bool,
50    /// Whether the repository or project is `public`, `private` or `unknown`.
51    pub visibility: String,
52    /// A comment can carry a hidden marker the provider finds again.
53    pub markers: bool,
54}
55
56/// One issue as the tracker holds it. Titles and bodies are untrusted text.
57#[derive(ToValue, FromValue, Schema, Debug, Clone, Default, PartialEq, Eq)]
58#[map(crate = ::guatiao)]
59pub struct TrackerIssue {
60    /// The provider-local identity, as [`SourceItem::id`](crate::source::SourceItem::id).
61    pub id: String,
62    /// The tracker's own numeric or opaque ID, when linking children needs it.
63    pub native_id: String,
64    /// The title.
65    pub title: String,
66    /// The body, as Markdown or text.
67    pub body: String,
68    /// `open` or `closed`.
69    pub state: String,
70    /// Labels currently on the issue.
71    pub labels: Vec<String>,
72    /// Assignee logins or account IDs.
73    pub assignees: Vec<String>,
74    /// A link to the issue.
75    pub url: Option<String>,
76    /// When the tracker last changed the issue (ISO 8601).
77    pub updated: String,
78    /// The parent issue's ID, when the tracker says it has one.
79    pub parent: Option<String>,
80    /// The author's login or account ID, for the author rule of repliers.
81    pub author_id: Option<String>,
82    /// The priority, in the tracker's words, when it has one.
83    pub priority: Option<String>,
84    /// The issue's short code, as [`SourceItem::code`](crate::source::SourceItem::code): what
85    /// a Mission it starts is numbered by.
86    pub code: Option<String>,
87    /// What names the tracker for a `code` that does not name it itself, as
88    /// [`SourceItem::code_tag`](crate::source::SourceItem::code_tag).
89    pub code_tag: Option<String>,
90}
91
92/// A request for comments made after a moment on a set of issues.
93#[derive(ToValue, FromValue, Schema, Debug, Clone, Default, PartialEq, Eq)]
94#[map(crate = ::guatiao)]
95pub struct ReplyPoll {
96    /// The issues to read; empty reads nothing.
97    pub items: Vec<String>,
98    /// The cursor the previous batch returned; `None` reads every comment.
99    pub since: Option<String>,
100}
101
102/// What starts the ID of a reply that is a pull request's review comment, which GitHub numbers
103/// apart from the conversation's comments. Such a reply is read as review text (source
104/// `review`), not as a comment on the issue.
105pub const REVIEW_REPLY_PREFIX: &str = "review:";
106
107/// One comment on an issue.
108#[derive(ToValue, FromValue, Schema, Debug, Clone, Default, PartialEq, Eq)]
109#[map(crate = ::guatiao)]
110pub struct Reply {
111    /// The tracker's comment ID.
112    pub id: String,
113    /// The issue it was made on.
114    pub item: String,
115    /// The author's login or account ID.
116    pub author_id: String,
117    /// The author's display name.
118    pub author: String,
119    /// Whether the author may answer for the issue: the issue's author, a repository
120    /// owner, member or collaborator, a Jira reporter or assignee, or someone the
121    /// settings name.
122    pub permitted: bool,
123    /// Whether AI6 wrote the comment, by the mark it leaves on what it writes and never by its
124    /// author: the identity AI6 posts as may be a person's, whose replies count.
125    pub own: bool,
126    /// The comment text. Untrusted.
127    pub body: String,
128    /// When it was made (ISO 8601).
129    pub created: String,
130}
131
132/// The comments a poll found and where to resume.
133#[derive(ToValue, FromValue, Schema, Debug, Clone, Default, PartialEq, Eq)]
134#[map(crate = ::guatiao)]
135pub struct ReplyBatch {
136    /// Comments after the cursor, oldest first.
137    pub replies: Vec<Reply>,
138    /// The cursor for the next poll; `None` keeps the one sent.
139    pub cursor: Option<String>,
140}
141
142/// A comment to post or edit.
143#[derive(ToValue, FromValue, Schema, Debug, Clone, Default, PartialEq, Eq)]
144#[map(crate = ::guatiao)]
145pub struct TrackerPost {
146    /// The issue.
147    pub item: String,
148    /// The comment text, as Markdown.
149    pub body: String,
150    /// The comment to replace, when this edits one.
151    pub edit: Option<String>,
152    /// A short name for the mark the provider leaves on every comment; a default without one.
153    pub marker: Option<String>,
154}
155
156/// The comment a post made or edited.
157#[derive(ToValue, FromValue, Schema, Debug, Clone, Default, PartialEq, Eq)]
158#[map(crate = ::guatiao)]
159pub struct PostedComment {
160    /// The tracker's comment ID, kept to edit it later.
161    pub id: String,
162    /// A link to the comment.
163    pub url: Option<String>,
164}
165
166/// Changes to an issue's labels, workflow state and assignees.
167#[derive(ToValue, FromValue, Schema, Debug, Clone, Default, PartialEq, Eq)]
168#[map(crate = ::guatiao)]
169pub struct TrackerUpdate {
170    /// The issue.
171    pub item: String,
172    /// Labels to add.
173    pub add_labels: Vec<String>,
174    /// Labels to remove.
175    pub remove_labels: Vec<String>,
176    /// A workflow transition, by ID or name (Jira).
177    pub transition: Option<String>,
178    /// Assignees to add.
179    pub add_assignees: Vec<String>,
180    /// Assignees to remove.
181    pub remove_assignees: Vec<String>,
182    /// `open` or `closed` (GitHub); another value is refused.
183    pub state: Option<String>,
184}
185
186/// An issue to create.
187#[derive(ToValue, FromValue, Schema, Debug, Clone, Default, PartialEq, Eq)]
188#[map(crate = ::guatiao)]
189pub struct IssueSpec {
190    /// The title.
191    pub title: String,
192    /// The body as Markdown; the provider words it for the tracker.
193    pub body: String,
194    /// Labels to set; the provider keeps those the tracker has.
195    pub labels: Vec<String>,
196    /// The issue type; empty takes the instance's `issue_type`.
197    pub issue_type: Option<String>,
198    /// The priority, already in the target tracker's words.
199    pub priority: Option<String>,
200    /// The parent issue, when the new one is a child.
201    pub parent: Option<String>,
202}
203
204/// A link between two issues.
205#[derive(ToValue, FromValue, Schema, Debug, Clone, Default, PartialEq, Eq)]
206#[map(crate = ::guatiao)]
207pub struct TrackerLink {
208    /// The issue in this tracker the link is made on.
209    pub from: String,
210    /// The other issue, by ID in this tracker, when it lives here.
211    pub to: Option<String>,
212    /// The link's meaning: `related`, `child`, `parent` or `mirror`.
213    pub relation: String,
214    /// The other issue's URL, when it lives in another tracker (a remote link).
215    pub url: Option<String>,
216}
217
218/// An issue tracker that is read, commented on and written to on demand.
219///
220/// A call the instance cannot make fails with `GUATIAO_ERR_UNSUPPORTED` and a message
221/// naming what it can do; an item outside the instance's repository or project fails with
222/// `GUATIAO_ERR_BAD_VALUE` and a message that starts `foreign item`.
223#[guatiao::kind(name = "ai6_tracker")]
224pub trait Tracker: Send + Sync {
225    /// Returns what the instance can do; `item` adds the answers that depend on one issue.
226    fn capabilities(&self, item: Option<String>) -> Result<TrackerCapabilities, ProviderError>;
227
228    /// Reads one issue.
229    fn issue(&self, item: String) -> Result<TrackerIssue, ProviderError>;
230
231    /// Lists the comments made after `request.since` on the issues it names.
232    fn replies(&self, request: ReplyPoll) -> Result<ReplyBatch, ProviderError>;
233
234    /// The open pull request whose head is `branch` in this instance's repository, as its
235    /// item ID; `None` when there is none or the instance has no pull requests.
236    fn pull_for_branch(&self, branch: String) -> Result<Option<String>, ProviderError> {
237        let _ = branch;
238        Ok(None)
239    }
240
241    /// Posts a comment, or edits the one `post.edit` names.
242    fn post(&self, post: TrackerPost) -> Result<PostedComment, ProviderError>;
243
244    /// Changes labels, workflow state or assignees.
245    fn update(&self, update: TrackerUpdate) -> Result<(), ProviderError>;
246
247    /// Creates an issue and returns it, with its parent when one was attached.
248    fn create(&self, spec: IssueSpec) -> Result<TrackerIssue, ProviderError>;
249
250    /// Links two issues, or an issue and an address elsewhere.
251    fn link(&self, link: TrackerLink) -> Result<(), ProviderError>;
252}
253
254/// A live read of an Operation's trackers that Universal Exports asks the Headquarters Station
255/// for, because only it holds tracker tokens: one item with its latest comments, or the items
256/// the Operation's sources find.
257#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq, Eq)]
258#[serde(deny_unknown_fields)]
259pub struct SourceRead {
260    /// Universal Exports' identity for the read.
261    pub id: String,
262    /// The Operation whose `sources` are read.
263    pub operation: String,
264    /// The instance (`sources.<name>`); with no `item`, `None` lists every instance's items.
265    #[serde(default)]
266    pub tracker: Option<String>,
267    /// The item to read, as its instance names it (`owner/repo#12`, `AI6-12`); `None` lists.
268    #[serde(default)]
269    pub item: Option<String>,
270    /// Only items whose title holds this text, ignoring case.
271    #[serde(default)]
272    pub query: Option<String>,
273    /// The most comments to answer, the latest.
274    pub comments: u32,
275    /// The most items to list.
276    pub items: u32,
277    /// Asks for each instance's board page instead of any item: `boards` of the answer, from
278    /// the providers' own settings, with no request to a tracker.
279    #[serde(default)]
280    pub boards: bool,
281}
282
283/// The board of one tracker instance, as its Source provider names it.
284#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq, Eq)]
285#[serde(deny_unknown_fields)]
286pub struct SourceBoard {
287    /// The instance (the key under `sources`).
288    pub tracker: String,
289    /// The provider serving it.
290    pub provider: String,
291    /// The `https` page where people read its issues as a board.
292    pub url: String,
293}
294
295/// One item as a [`SourceRead`] answers it. Its text is untrusted.
296#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default, PartialEq, Eq)]
297#[serde(deny_unknown_fields)]
298pub struct SourceReadItem {
299    /// The instance it is in.
300    pub tracker: String,
301    /// Its ID in that instance.
302    pub id: String,
303    /// Its title.
304    pub title: String,
305    /// `open` or `closed`, when the read says.
306    #[serde(default)]
307    pub state: Option<String>,
308    /// Its labels.
309    #[serde(default)]
310    pub labels: Vec<String>,
311    /// Its assignees, as the tracker names them.
312    #[serde(default)]
313    pub assignees: Vec<String>,
314    /// A link to it.
315    #[serde(default)]
316    pub url: Option<String>,
317    /// When the tracker last changed it (ISO 8601).
318    #[serde(default)]
319    pub updated: String,
320    /// Who wrote it, when the tracker says.
321    #[serde(default)]
322    pub author: Option<String>,
323    /// Its body, when one item is read.
324    #[serde(default)]
325    pub body: Option<String>,
326}
327
328/// One comment as a [`SourceRead`] answers it. Its text is untrusted.
329#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default, PartialEq, Eq)]
330#[serde(deny_unknown_fields)]
331pub struct SourceReadComment {
332    /// The author's display name.
333    pub author: String,
334    /// `ai6` (AI6 wrote it), `answers` (someone who may answer for the item: its author, an
335    /// owner, member or collaborator, a reporter or assignee) or `other`.
336    pub role: String,
337    /// When it was made (ISO 8601).
338    pub at: String,
339    /// The comment text.
340    pub body: String,
341}
342
343/// What the Headquarters Station found for a [`SourceRead`], or why it could not read.
344#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default, PartialEq, Eq)]
345#[serde(deny_unknown_fields)]
346pub struct SourceReadAnswer {
347    /// The item read, for a read of one.
348    #[serde(default)]
349    pub item: Option<SourceReadItem>,
350    /// Its latest comments, oldest first.
351    #[serde(default)]
352    pub comments: Vec<SourceReadComment>,
353    /// The items listed, for a list.
354    #[serde(default)]
355    pub items: Vec<SourceReadItem>,
356    /// The boards of the instances asked about, for a read that asked for them.
357    #[serde(default)]
358    pub boards: Vec<SourceBoard>,
359    /// Why nothing was read, in a sentence.
360    #[serde(default)]
361    pub error: Option<String>,
362}
363
364impl SourceReadComment {
365    /// The comment `reply` with its author's role.
366    pub fn of(reply: Reply) -> Self {
367        let role = if reply.own {
368            "ai6"
369        } else if reply.permitted {
370            "answers"
371        } else {
372            "other"
373        };
374        Self {
375            author: reply.author,
376            role: role.to_owned(),
377            at: reply.created,
378            body: reply.body,
379        }
380    }
381}
382
383#[cfg(test)]
384mod tests {
385    use super::*;
386
387    // A changed required signature affects every built tracker provider.
388    #[test]
389    fn floor_hashes_are_pinned() {
390        assert_eq!(TrackerVtable::FLOOR_HASH, 11669220);
391    }
392}