git-bot-feedback 0.4.0

A library designed for CI tools that posts comments on a Pull Request.
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
use std::collections::{HashMap, HashSet};

use reqwest::{Method, Url};
use serde::Deserialize;
use serde_json::json;

use crate::{
    ReviewOptions,
    client::{ClientError, RestApiClient},
};

use super::{
    GithubApiClient,
    serde_structs::{ReviewState, ReviewSummary},
};

const QUERY_REVIEW_COMMENTS: &str = r#"query($owner: String!, $name: String!, $number: Int!, $afterThread: String, $afterComment: String) {
  repository(owner: $owner, name: $name) {
    pullRequest(number: $number) {
      reviewThreads(last: 100, after: $afterThread) {
        nodes {
          id
          isResolved
          isCollapsed
          comments(first: 100, after: $afterComment) {
            nodes {
              id
              body
              path
              line
              startLine
              originalLine
              originalStartLine
              pullRequestReview {
                id
                isMinimized
              }
            }
            pageInfo {
              endCursor
              hasNextPage
            }
          }
        }
        pageInfo {
          endCursor
          hasNextPage
        }
      }
    }
  }
}"#;

const RESOLVE_REVIEW_COMMENT: &str = r#"mutation($id: ID!) {
  resolveReviewThread(input: {threadId: $id, clientMutationId: "git-bot-feedback"}) {
    thread {
      id
    }
  }
}"#;

const DELETE_REVIEW_COMMENT: &str = r#"mutation($id: ID!) {
  deletePullRequestReviewComment(input: {id: $id, clientMutationId: "git-bot-feedback"}) {
    pullRequestReviewComment {
      id
    }
  }
}"#;

const HIDE_REVIEW_COMMENT: &str = r#"mutation($subjectId: ID!) {
  minimizeComment(input: {classifier:OUTDATED, subjectId: $subjectId, clientMutationId: "git-bot-feedback"}) {
    minimizedComment {
      isMinimized
    }
  }
}"#;

/// A constant string used as a payload to dismiss PR reviews.
const REVIEW_DISMISSAL: &str = r#"{"event":"DISMISS","message":"outdated review"}"#;

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct ThreadInfo {
    pub id: String,
    pub is_collapsed: bool,
    pub is_resolved: bool,
}

impl From<&QueryResponseReviewThread> for ThreadInfo {
    fn from(thread: &QueryResponseReviewThread) -> Self {
        Self {
            id: thread.id.clone(),
            is_collapsed: thread.is_collapsed,
            is_resolved: thread.is_resolved,
        }
    }
}

enum IdKind<'a> {
    Thread(&'a str),
    Comment(&'a str),
}
impl IdKind<'_> {
    fn value(&self) -> &str {
        match self {
            IdKind::Thread(id) => id,
            IdKind::Comment(id) => id,
        }
    }
}

impl std::fmt::Display for IdKind<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IdKind::Thread(id) => write!(f, "thread {id}"),
            IdKind::Comment(id) => write!(f, "comment {id}"),
        }
    }
}

pub struct ReviewThread {
    pub info: ThreadInfo,
    pub comments: Vec<QueryResponseReviewThreadComment>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PageInfo {
    has_next_page: bool,
    end_cursor: Option<String>,
}

#[derive(Debug, Deserialize, Clone, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub struct QueryResponsePrReview {
    pub id: String,
    pub is_minimized: bool,
}

#[derive(Debug, Deserialize, Clone, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub struct QueryResponseReviewThreadComment {
    pub id: String,
    pub body: String,
    pub path: String,
    pub line: Option<i64>,
    pub start_line: Option<i64>,
    pub original_line: i64,
    pub original_start_line: Option<i64>,
    pub pull_request_review: QueryResponsePrReview,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct QueryResponseReviewThreadComments {
    pub nodes: Vec<QueryResponseReviewThreadComment>,
    page_info: PageInfo,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct QueryResponseReviewThread {
    pub id: String,
    pub is_collapsed: bool,
    pub is_resolved: bool,
    pub comments: QueryResponseReviewThreadComments,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct QueryResponseReviewThreads {
    nodes: Vec<QueryResponseReviewThread>,
    page_info: PageInfo,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct QueryResponsePr {
    review_threads: QueryResponseReviewThreads,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct QueryResponseRepo {
    pull_request: QueryResponsePr,
}

#[derive(Debug, Deserialize)]
struct QueryResponseData {
    repository: QueryResponseRepo,
}

#[derive(Debug, Deserialize)]
struct QueryResponse {
    pub data: QueryResponseData,
}

impl GithubApiClient {
    /// Creates the list existing review thread comments to close.
    ///
    /// Set `no_dismissed` is `true` to ignore any already dismissed comments.
    pub(super) async fn get_existing_review_comments(
        &self,
        marker: &str,
        no_dismissed: bool,
    ) -> Result<Vec<ReviewThread>, ClientError> {
        let mut found_threads: HashMap<ThreadInfo, HashSet<QueryResponseReviewThreadComment>> =
            HashMap::new();
        // We should never reach the `default_value` in `.unwrap_or(default_value)` because
        // the repo name should always have a `/` to delimit the repo's owner and name.
        let (repo_owner, repo_name) = self.repo.split_once('/').unwrap_or(("", ""));
        let pr_number = self
            .pull_request
            .as_ref()
            .map(|i| i.number)
            .expect("PR reviews should only be fetched for PR events.");
        let mut after_thread = None;
        let mut after_comment = None;
        let mut has_next_page = true;
        let graphql_url = self.api_url.join("/graphql")?;
        while has_next_page {
            let variables = json!({
                "owner": repo_owner.to_string(),
                "name": repo_name.to_string(),
                "number": pr_number,
                "afterThread": after_thread,
                "afterComment": after_comment,
            });
            let req = self.make_api_request(
                &self.client,
                graphql_url.clone(),
                Method::POST,
                Some(json!({"query": QUERY_REVIEW_COMMENTS, "variables": variables}).to_string()),
                None,
            )?;
            match self
                .send_api_request(&self.client, req, &self.rate_limit_headers)
                .await
            {
                Err(e) => {
                    return Err(
                        e.add_request_context("get list of existing review thread comments")
                    );
                }
                Ok(response) => {
                    if !response.status().is_success() {
                        self.log_response(
                            response,
                            "Failed to get list of existing review thread comments",
                        )
                        .await;
                        break;
                    }
                    let text = response.text().await?;
                    match serde_json::from_str::<QueryResponse>(&text) {
                        Err(e) => {
                            return Err(ClientError::json(
                                "deserialize (GraphQL) list of existing review thread comments",
                                e,
                            ));
                        }
                        Ok(payload) => {
                            let threads_data = payload.data.repository.pull_request.review_threads;
                            let thread_pg_info = threads_data.page_info;
                            for thread in threads_data.nodes {
                                let comment_data = &thread.comments;
                                let comment_pg_info = &comment_data.page_info;
                                let thread_info = ThreadInfo::from(&thread);
                                for comment in &comment_data.nodes {
                                    if comment.body.starts_with(marker)
                                        && (!no_dismissed
                                            || (!thread.is_resolved && !thread.is_collapsed))
                                    {
                                        if let Some(item) = found_threads.get_mut(&thread_info) {
                                            item.insert(comment.clone());
                                        } else {
                                            let new_set = HashSet::from_iter([comment.clone()]);
                                            found_threads.insert(thread_info.clone(), new_set);
                                        }
                                    }
                                }
                                after_comment = if comment_pg_info.has_next_page {
                                    comment_pg_info.end_cursor.clone()
                                } else {
                                    None
                                };
                            }
                            if after_comment.is_none() {
                                if !thread_pg_info.has_next_page {
                                    has_next_page = false;
                                } else {
                                    after_thread = thread_pg_info.end_cursor;
                                }
                            }
                        }
                    }
                }
            }
        }
        let mut result = vec![];
        for (info, comments) in found_threads {
            result.push(ReviewThread {
                info,
                comments: Vec::from_iter(comments),
            });
        }
        Ok(result)
    }

    /// This will sort through the threads of PR reviews and return a list of
    /// bot comments to be kept.
    ///
    /// This will also resolve (or delete if `delete_review_comments` is `true`)
    /// any outdated unresolved comment.
    ///
    /// The returned list of strings are the IDs (as used in graphQL API) of
    /// the PR reviews that should be kept.
    pub(super) async fn check_reused_comments(
        &self,
        options: &mut ReviewOptions,
    ) -> Result<Vec<String>, ClientError> {
        let mut reused_reviews = vec![];
        let found_threads = self
            .get_existing_review_comments(&options.marker, !options.delete_review_comments)
            .await?;
        if found_threads.is_empty() {
            return Ok(reused_reviews);
        }

        // Keep already posted comments if they match new ones
        let mut existing_review_comments = HashSet::new();
        for thread in &found_threads {
            let mut keep_thread = false; // should we `keep` the whole thread?
            for comment in &thread.comments {
                let line_start = comment.start_line.or(comment.original_start_line);
                let line_end = comment.line.unwrap_or(comment.original_line);
                let mut keep = false; // should we `keep` the review comment?
                for suggestion in options.comments.iter() {
                    let proposed_comment =
                        if suggestion.comment.starts_with(options.marker.as_str()) {
                            suggestion.comment.clone()
                        } else {
                            format!("{}{}", options.marker, suggestion.comment)
                        };
                    if suggestion.path == comment.path
                        && suggestion.line_start.map(|i| i as i64) == line_start
                        && suggestion.line_end as i64 == line_end
                        && proposed_comment == comment.body
                        && !thread.info.is_resolved
                        && !thread.info.is_collapsed
                        && !comment.pull_request_review.is_minimized
                    {
                        log::info!(
                            "Using existing review comment: path='{}', line_start='{line_start:?}', line_end='{line_end}'",
                            comment.path,
                        );
                        reused_reviews.push(comment.pull_request_review.id.clone());
                        existing_review_comments.insert(suggestion.clone());
                        keep = true;
                        keep_thread = true;
                        break;
                    }
                }
                if !keep {
                    self.close_review_comment(
                        IdKind::Comment(comment.id.as_str()),
                        options.delete_review_comments,
                    )
                    .await?;
                }
            }
            if !keep_thread {
                // We don't delete the whole thread since there may be other non-bot comments in the thread.
                // Instead, we'll just mark it as resolved (effectively hiding/collapsing it).
                self.close_review_comment(IdKind::Thread(thread.info.id.as_str()), false)
                    .await?;
            }
        }
        options
            .comments
            .retain(|c| !existing_review_comments.contains(c));
        Ok(reused_reviews)
    }

    /// Resolve or Delete an existing review thread comment.
    ///
    /// Pass a thread `id` to resolve/delete the entire thread.
    /// A thread is a conversation focused on a single part of the diff.
    ///
    /// Pass a comment `id` to resolve/delete a specific comment within the thread.
    ///
    /// Pass `delete` as `true` to delete the review comment/thread, `false` to set it as resolved.
    /// Typically, it is undesirable to delete a thread since there may be other non-bot comments in the thread.
    async fn close_review_comment(&self, id: IdKind<'_>, delete: bool) -> Result<(), ClientError> {
        let (mutation, op) = if delete {
            (DELETE_REVIEW_COMMENT, "Delete")
        } else {
            (RESOLVE_REVIEW_COMMENT, "Resolve")
        };
        let request = self.make_api_request(
            &self.client,
            self.api_url.join("/graphql")?,
            Method::POST,
            Some(json!({"query": mutation, "variables": { "id": id.value() }}).to_string()),
            None,
        )?;
        match self
            .send_api_request(&self.client, request, &self.rate_limit_headers)
            .await
        {
            Ok(response) => {
                self.log_response(response, format!("Failed to {op} review {id}").as_str())
                    .await;
                Ok(())
            }
            Err(e) => Err(e.add_request_context(format!("{op} review {id}").as_str())),
        }
    }

    /// Hide and dismiss review that were previously created by this software.
    ///
    /// The `keep_reviews` parameter is a list of reviews' Node IDs to keep displayed.
    /// This also will dismiss any review (as "outdated") if it is not being kept.
    pub(super) async fn hide_outdated_reviews(
        &self,
        url: Url,
        keep_reviews: Vec<String>,
        marker: &str,
    ) -> Result<(), ClientError> {
        let mut next_page = Some(Url::parse_with_params(url.as_str(), [("page", "1")])?);
        let graphql_url = self.api_url.join("/graphql")?;
        while let Some(url) = next_page {
            let request =
                self.make_api_request(&self.client, url.clone(), Method::GET, None, None)?;
            let response = self
                .send_api_request(&self.client, request, &self.rate_limit_headers)
                .await;
            match response {
                Err(e) => {
                    return Err(e.add_request_context("get list of existing reviews"));
                }
                Ok(response) => {
                    next_page = self.try_next_page(response.headers());
                    let reviews =
                        serde_json::from_str::<Vec<ReviewSummary>>(response.text().await?.as_str())
                            .map_err(|e| {
                                ClientError::json("deserialize list of PR review comments", e)
                            })?;
                    for review in reviews {
                        if keep_reviews.contains(&review.node_id)
                            || review.body.as_ref().is_none_or(|b| !b.starts_with(marker))
                        {
                            // if the review is being reused or is not authored by this software, then
                            // leave it as is and skip to the next review.
                            continue;
                        }
                        let req = self.make_api_request(
                            &self.client,
                            graphql_url.clone(),
                            Method::POST,
                            Some(json!({"query": HIDE_REVIEW_COMMENT, "variables": {"subjectId": review.node_id}}).to_string()),
                            None
                        )?;
                        match self
                            .send_api_request(&self.client, req, &self.rate_limit_headers)
                            .await
                        {
                            Ok(result) => {
                                self.log_response(result, "Failed to hide outdated review comment")
                                    .await;
                            }
                            Err(e) => {
                                return Err(e.add_request_context("hide outdated review comment"));
                            }
                        }
                        if review.state != ReviewState::Dismissed {
                            let dismissal_url =
                                url.join(format!("reviews/{}/dismissals", review.id).as_str())?;
                            let dismiss_request = self.make_api_request(
                                &self.client,
                                dismissal_url,
                                Method::PUT,
                                Some(REVIEW_DISMISSAL.to_string()),
                                None,
                            )?;
                            match self
                                .send_api_request(
                                    &self.client,
                                    dismiss_request,
                                    &self.rate_limit_headers,
                                )
                                .await
                            {
                                Ok(result) => {
                                    self.log_response(result, "Failed to dismiss outdated review")
                                        .await;
                                }
                                Err(e) => {
                                    return Err(e.add_request_context("dismiss outdated review"));
                                }
                            }
                        }
                    }
                }
            }
        }
        Ok(())
    }
}