notbot 0.6.13

Matrix chatbot, primarily used around the Warsaw Hackerspace channels and spaces
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! Posts notifications about new change requests in Gerrit
//!
//! # Configuration
//!
//! [`GerritConfig`]
//!
//! ```toml
//! [module."notbot::gerrit".instances.example]
//! instance_url = "https://gerrit.example.org"
//! query = [
//!     [ "status", "open" ],
//!     [ "project", "hscloud" ],
//!     [ "-is", "wip" ],
//! ]
//! limit = 5
//! feed_rooms = [
//!     "#bottest:example.net",
//! ]
//!
//! [module."notbot::gerrit"]
//! feed_interval = 5
//! ```
//!
//! # Usage
//!
//! Worker function [`gerrit_feeds`] provides updates about new CRs to configured rooms.

use crate::prelude::*;

use gerrit_api::{ChangeInfo, gerrit_fetch};
use tokio::time::{Duration, interval};

use askama::Template;

/// Configuration of a specific queried Gerrit instance
#[derive(Clone, Debug, Deserialize)]
pub struct GerritInstance {
    /// Base instance URL.
    pub instance_url: String,
    /// Changes query
    #[serde(default = "query")]
    pub query: Vec<(String, String)>,
    /// Limit
    #[serde(default = "limit")]
    pub limit: u64,
    /// Room to post updates to.
    pub feed_rooms: Vec<String>,
}

fn query() -> Vec<(String, String)> {
    [("status", "open"), ("project", "hscloud"), ("-is", "wip")]
        .iter()
        .map(|(k, v)| (k.s(), v.s()))
        .collect()
}

const fn limit() -> u64 {
    5
}

/// General module configuration
#[derive(Clone, Debug, Deserialize)]
pub struct GerritConfig {
    /// How often the feeds should be checked, in minutes.
    #[serde(default = "feed_interval")]
    pub feed_interval: u64,
    /// Map of Gerrit instances to query and observe
    pub instances: HashMap<String, GerritInstance>,
}

/// Default value for feed checking interval: 5 minutes
#[must_use]
pub const fn feed_interval() -> u64 {
    5
}

pub(crate) fn workers(mx: &Client, config: &Config) -> anyhow::Result<Vec<WorkerInfo>> {
    info!("registering workers");
    let gerrit_config: GerritConfig = config.typed_module_config(module_path!())?;
    Ok(vec![WorkerInfo::new(
        "gerrit",
        "observes a gerrit instance feed for new changes",
        "gerrit",
        mx.clone(),
        gerrit_config,
        gerrit_feeds,
    )])
}

/// Process list of changes returned by queries at specified intervals, and
/// post information about new changes to rooms.
///
/// # Errors
/// Will return `Err` if rendering messages fails.
pub async fn gerrit_feeds(mx: Client, module_config: GerritConfig) -> anyhow::Result<()> {
    let mut interval = interval(Duration::from_secs(60 * module_config.feed_interval));
    let mut first_loop: HashMap<String, bool> = HashMap::default();
    let mut change_ids: HashMap<String, Vec<String>> = HashMap::default();
    let mut known_users: HashMap<(String, u64), String> = HashMap::default();

    for name in module_config.instances.keys() {
        first_loop.insert(name.to_owned(), true);
        change_ids.insert(name.to_owned(), vec![]);
    }

    loop {
        interval.tick().await;
        for (name, instance) in &module_config.instances {
            let changes_url = format!(
                r"{base_url}/changes/?q={query}&limit={limit}",
                base_url = instance.instance_url,
                limit = instance.limit,
                query = instance
                    .query
                    .iter()
                    .map(|(k, v)| format!("{k}:{v}"))
                    .collect::<Vec<String>>()
                    .join("+")
            );

            let data: Vec<ChangeInfo> = match gerrit_fetch(&changes_url).await {
                Ok(v) => v,
                Err(e) => {
                    error!("fetching/decoding changes failed: {e}");
                    continue;
                }
            };

            if first_loop
                .get(name)
                .ok_or_else(|| anyhow!("wtf? iterating over previously unknown instance?"))?
                .to_owned()
            {
                let current_ids: Vec<String> = data.iter().map(|d| d.id.clone()).collect();
                change_ids.insert(name.clone(), current_ids);
                first_loop.insert(name.to_owned(), false);
                continue;
            }

            let known_ids = change_ids
                .get_mut(name)
                .ok_or_else(|| anyhow!("wtf? iterating over previously unknown instance?"))?;
            let mut post_changes = vec![];

            for change in data.clone() {
                if !known_users.contains_key(&(name.to_owned(), change.clone().owner.account_id)) {
                    let user_url = format!(
                        "{instance}/accounts/{user_id}/username",
                        instance = instance.instance_url,
                        user_id = change.owner.account_id,
                    );

                    trace!("user url: {user_url}");
                    let username: String = match gerrit_fetch(&user_url).await {
                        Ok(v) => v,
                        Err(e) => {
                            error!("fetching/decoding username failed: {e}");
                            format!("id:{}", change.owner.account_id)
                        }
                    };

                    trace!("username found: {username}");

                    known_users.insert((name.to_owned(), change.owner.account_id), username);
                }

                if known_ids.contains(&change.id) {
                    continue;
                };

                known_ids.push(change.id.clone());
                post_changes.push(change);
            }

            if post_changes.is_empty() {
                continue;
            };

            let render_items = RenderItems {
                instance_url: instance.instance_url.clone(),
                instance_name: name.to_owned(),
                known_users: known_users.clone(),
                items: post_changes,
            };

            let message = RoomMessageEventContent::text_html(
                render_items.as_plain().render()?,
                render_items.as_formatted().render()?,
            );

            for room_name in &instance.feed_rooms {
                let Ok(room) = maybe_get_room(&mx, room_name).await else {
                    continue;
                };

                if let Err(e) = room.send(message.clone()).await {
                    error!("failed to send message: {e}");
                }
            }
        }
    }
}

#[derive(Template)]
#[template(
    path = "matrix/gerrit-message.html",
    blocks = ["formatted", "plain"],
)]
struct RenderItems {
    instance_url: String,
    instance_name: String,
    known_users: HashMap<(String, u64), String>,
    items: Vec<ChangeInfo>,
}

pub mod gerrit_api {
    //! Helper functions for interacting with Gerrit json APIs
    use anyhow::bail;
    use reqwest::ClientBuilder;
    use serde::de;
    use serde_derive::Deserialize;
    use serde_json::Value;
    use std::collections::HashMap;

    /// Strips the `)]}'` prefix from provided text before attempting to deserialize it.
    ///
    /// # Errors
    /// Will return `Err` if:
    /// * data does not start with the expected gerrit prefix.
    /// * deserializing data fails.
    pub fn gerrit_decode_json<D: de::DeserializeOwned>(text: &str) -> anyhow::Result<D> {
        match text.strip_prefix(")]}'") {
            None => bail!("this does not look like a gerrit response"),
            Some(data) => Ok(serde_json::from_str(data)?),
        }
    }

    /// Fetches contents of the provided url, and attempts to decode them as a Gerrit response
    ///
    /// # Errors
    /// Will return `Err` if:
    /// * fetching data fails.
    /// * decoding by [`gerrit_decode_json`] fails
    pub async fn gerrit_fetch<D: de::DeserializeOwned>(url: &str) -> anyhow::Result<D> {
        let client = ClientBuilder::new()
            .redirect(reqwest::redirect::Policy::none())
            .build()?;

        let text = client.get(url).send().await?.text().await?;
        gerrit_decode_json(text.as_str())
    }

    impl ChangeInfo {
        /// Calculate "size class" of a change
        ///
        /// Original implementation: <https://github.com/GerritCodeReview/gerrit/blob/287467f353b37ff68588adef0d1315a49845b09b/polygerrit-ui/app/elements/change-list/gr-change-list-item/gr-change-list-item.ts#L48-L53>
        #[must_use]
        pub const fn change_size(&self) -> &str {
            match self.insertions + self.deletions {
                ..10 => "[XS]",
                10..50 => "[S]",
                50..250 => "[M]",
                250..1000 => "[L]",
                1000.. => "[XL]",
            }
        }
    }

    /// Structure describing information about a change returned from Gerrit
    ///
    /// Written based on [upstream documentation](https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#change-info)
    /// and responses from a gerrit instance this was developed for.
    ///
    /// Some fields that are documented in the documentation above as required, were missing in the
    /// gerrit instance this module was developed for.
    #[allow(missing_docs)]
    #[derive(Default, Debug, Clone, PartialEq, Eq, Deserialize)]
    pub struct ChangeInfo {
        pub id: String,
        // missing for us
        pub triplet_id: Option<String>,
        pub project: String,
        pub branch: String,
        pub topic: Option<String>,
        pub attention_set: Option<HashMap<String, AttentionSetInfo>>,
        pub removed_from_attention_set: Option<HashMap<String, AttentionSetInfo>>,
        pub hashtags: Option<Vec<String>>,
        pub custom_keyed_values: Option<HashMap<String, String>>,
        pub change_id: String,
        pub subject: String,
        pub status: String,
        pub created: String,
        pub updated: String,
        pub submitted: Option<String>,
        pub submitter: Option<String>,
        pub starred: Option<bool>,
        pub reviewed: Option<bool>,
        pub submit_type: Option<String>,
        pub mergeable: Option<String>,
        pub submittable: Option<String>,
        pub insertions: u64,
        pub deletions: u64,
        pub total_comment_count: u64,
        pub unresolved_comment_count: u64,
        #[serde(rename = "_number")]
        pub number: u64,
        // missing for us
        pub virtual_id_number: Option<u64>,
        pub owner: AccountInfo,
        pub actions: Option<ActionInfo>,
        pub requirements: Option<Vec<Requirement>>,
        pub submit_requirements: Option<Vec<SubmitRequirementResultInfo>>,
        // TODO:
        // https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#label-info
        pub labels: Option<HashMap<String, Value>>,
        pub permitted_labels: Option<Vec<String>>,
        // not even sure how to express that correctly
        // > A map of the removable labels that maps a label name to the map of values and reviewers ( AccountInfo entities) that are allowed to be removed from the change
        // HashMap<String, HashMap<String, AccountInfo>>?
        pub removable_labels: Option<HashMap<String, Value>>,
        pub removable_reviewers: Option<Vec<AccountInfo>>,
        pub reviewers: Option<HashMap<ReviewerState, Vec<AccountInfo>>>,
        pub pending_reviewers: Option<HashMap<ReviewerState, Vec<AccountInfo>>>,
        pub reviewer_updates: Option<Vec<ReviewerUpdateInfo>>,
        pub messages: Option<Vec<ChangeMessageInfo>>,
        // missing for us
        pub current_revision_number: Option<u64>,
        pub current_revision: Option<String>,
        // TODO:
        // https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#revision-info
        pub revisions: Option<HashMap<String, Value>>,
        pub meta_rev_id: Option<String>,
        pub tracking_ids: Option<Vec<TrackingIdInfo>>,
        #[serde(rename = "_more_changes")]
        pub more_changes: Option<bool>,
        pub problems: Option<Vec<ProblemInfo>>,
        pub is_private: Option<bool>,
        pub work_in_progress: Option<bool>,
        pub has_review_started: Option<bool>,
        pub revert_of: Option<u64>,
        pub submission_id: Option<String>,
        pub cherry_pick_of_change: Option<u64>,
        pub cherry_pick_of_patch_set: Option<u64>,
        pub contains_git_conflicts: Option<bool>,
    }

    #[allow(missing_docs)]
    #[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
    pub struct ProblemInfo {
        pub message: String,
        pub status: Option<ProblemInfoStatus>,
        pub outcome: Option<String>,
    }

    #[allow(missing_docs)]
    #[derive(Debug, Clone, Eq, Hash, PartialEq, Deserialize)]
    #[serde(rename_all = "SCREAMING_SNAKE_CASE")]
    pub enum ProblemInfoStatus {
        Fixed,
        FixFailed,
    }

    #[allow(missing_docs)]
    #[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
    pub struct TrackingIdInfo {
        pub system: String,
        pub id: String,
    }

    #[allow(missing_docs)]
    #[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
    pub struct ChangeMessageInfo {
        pub id: String,
        pub author: Option<AccountInfo>,
        pub real_author: Option<AccountInfo>,
        // timestamp with a known format, we could do something nicer here
        pub date: String,
        pub message: String,
        pub accounts_in_message: Vec<AccountInfo>,
        pub tag: Option<String>,
        #[serde(rename = "_revision_number")]
        pub revision_number: Option<u64>,
    }

    #[allow(missing_docs)]
    #[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
    pub struct ReviewerUpdateInfo {
        pub updated: String,
        pub updated_by: AccountInfo,
        pub reviewer: AccountInfo,
        pub state: ReviewerState,
    }

    #[allow(missing_docs)]
    #[derive(Debug, Clone, Eq, Hash, PartialEq, Deserialize)]
    #[serde(rename_all = "SCREAMING_SNAKE_CASE")]
    pub enum ReviewerState {
        Reviewer,
        Cc,
        Removed,
    }

    #[allow(missing_docs)]
    #[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
    pub struct SubmitRequirementResultInfo {
        pub name: String,
        pub description: Option<String>,
        pub status: SubmitRequirementStatus,
        pub is_legacy: Option<bool>,
        pub applicability_expression_result: Option<SubmitRequirementExpressionInfo>,
        pub submittability_expression_result: SubmitRequirementExpressionInfo,
        pub override_expression_result: Option<SubmitRequirementExpressionInfo>,
    }

    #[allow(missing_docs)]
    #[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
    pub struct SubmitRequirementExpressionInfo {
        pub expression: Option<String>,
        pub fulfilled: bool,
        pub status: SubmitRequirementExpressionInfoStatus,
        pub passing_atoms: Option<Vec<String>>,
        pub failing_atoms: Option<Vec<String>>,
        pub atom_explanations: Option<HashMap<String, String>>,
        pub error_message: Option<String>,
    }

    #[allow(missing_docs)]
    #[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
    #[serde(rename_all = "SCREAMING_SNAKE_CASE")]
    pub enum SubmitRequirementExpressionInfoStatus {
        Pass,
        Fail,
        Error,
        NotEvaluated,
    }

    #[allow(missing_docs)]
    #[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
    #[serde(rename_all = "SCREAMING_SNAKE_CASE")]
    pub enum SubmitRequirementStatus {
        Satisfied,
        Unsatisfied,
        Overridden,
        NotApplicable,
        Error,
        Forced,
    }

    #[allow(missing_docs)]
    #[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
    pub struct Requirement {
        pub status: RequirementStatus,
        pub fallback_text: String,
        #[serde(rename = "type")]
        pub type_field: String,
    }

    #[allow(missing_docs)]
    #[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
    #[serde(rename_all = "SCREAMING_SNAKE_CASE")]
    pub enum RequirementStatus {
        Ok,
        NotReady,
        RuleError,
    }

    #[allow(missing_docs)]
    #[derive(Default, Debug, Clone, PartialEq, Eq, Deserialize)]
    pub struct AttentionSetInfo {
        pub account: AccountInfo,
        pub last_update: String,
        pub reason: String,
    }

    #[allow(missing_docs)]
    #[derive(Default, Debug, Clone, PartialEq, Eq, Deserialize)]
    pub struct AccountInfo {
        #[serde(rename = "_account_id")]
        pub account_id: u64,
    }

    #[allow(missing_docs)]
    #[derive(Default, Debug, Clone, PartialEq, Eq, Deserialize)]
    pub struct ActionInfo {
        pub method: Option<String>,
        pub label: Option<String>,
        pub title: Option<String>,
        pub enabled: Option<bool>,
        pub enabled_options: Option<Vec<String>>,
    }
}