cpp_linter/rest_api/github/
specific_api.rs

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
//! This submodule implements functionality exclusively specific to Github's REST API.

use std::{
    collections::HashMap,
    env,
    fs::OpenOptions,
    io::{Read, Write},
    sync::{Arc, Mutex},
};

use anyhow::{anyhow, Context, Result};
use reqwest::{Client, Method, Url};

use crate::{
    clang_tools::{clang_format::summarize_style, ReviewComments},
    cli::FeedbackInput,
    common_fs::FileObj,
    rest_api::{RestApiRateLimitHeaders, COMMENT_MARKER, USER_AGENT},
};

use super::{
    serde_structs::{
        FullReview, PullRequestInfo, ReviewComment, ReviewDiffComment, ThreadComment,
        REVIEW_DISMISSAL,
    },
    GithubApiClient, RestApiClient,
};

impl GithubApiClient {
    /// Instantiate a [`GithubApiClient`] object.
    pub fn new() -> Result<Self> {
        let event_name = env::var("GITHUB_EVENT_NAME").unwrap_or(String::from("unknown"));
        let pull_request = {
            match event_name.as_str() {
                "pull_request" => {
                    // GITHUB_*** env vars cannot be overwritten in CI runners on GitHub.
                    let event_payload_path = env::var("GITHUB_EVENT_PATH")?;
                    // event payload JSON file can be overwritten/removed in CI runners
                    let file_buf = &mut String::new();
                    OpenOptions::new()
                        .read(true)
                        .open(event_payload_path.clone())?
                        .read_to_string(file_buf)
                        .with_context(|| {
                            format!("Failed to read event payload at {event_payload_path}")
                        })?;
                    let payload =
                        serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(
                            file_buf,
                        )
                        .with_context(|| "Failed to deserialize event payload")?;
                    payload["number"].as_i64().unwrap_or(-1)
                }
                _ => -1,
            }
        };
        // GITHUB_*** env vars cannot be overwritten in CI runners on GitHub.
        let gh_api_url = env::var("GITHUB_API_URL").unwrap_or("https://api.github.com".to_string());
        let api_url = Url::parse(gh_api_url.as_str())?;

        Ok(GithubApiClient {
            client: Client::builder()
                .default_headers(Self::make_headers()?)
                .user_agent(USER_AGENT)
                .build()?,
            pull_request,
            event_name,
            api_url,
            repo: env::var("GITHUB_REPOSITORY").ok(),
            sha: env::var("GITHUB_SHA").ok(),
            debug_enabled: env::var("ACTIONS_STEP_DEBUG").is_ok_and(|val| &val == "true"),
            rate_limit_headers: RestApiRateLimitHeaders {
                reset: "x-ratelimit-reset".to_string(),
                remaining: "x-ratelimit-remaining".to_string(),
                retry: "retry-after".to_string(),
            },
        })
    }

    /// Append step summary to CI workflow's summary page.
    pub fn post_step_summary(&self, comment: &String) {
        if let Ok(gh_out) = env::var("GITHUB_STEP_SUMMARY") {
            // step summary MD file can be overwritten/removed in CI runners
            if let Ok(mut gh_out_file) = OpenOptions::new().append(true).open(gh_out) {
                if let Err(e) = writeln!(gh_out_file, "\n{}\n", comment) {
                    log::error!("Could not write to GITHUB_STEP_SUMMARY file: {}", e);
                }
            } else {
                log::error!("GITHUB_STEP_SUMMARY file could not be opened");
            }
        }
    }

    /// Post file annotations.
    pub fn post_annotations(&self, files: &[Arc<Mutex<FileObj>>], style: &str) {
        let style_guide = summarize_style(style);

        // iterate over clang-format advice and post annotations
        for file in files {
            let file = file.lock().unwrap();
            if let Some(format_advice) = &file.format_advice {
                // assemble a list of line numbers
                let mut lines: Vec<usize> = Vec::new();
                for replacement in &format_advice.replacements {
                    if !lines.contains(&replacement.line) {
                        lines.push(replacement.line);
                    }
                }
                // post annotation if any applicable lines were formatted
                if !lines.is_empty() {
                    println!(
                            "::notice file={name},title=Run clang-format on {name}::File {name} does not conform to {style_guide} style guidelines. (lines {line_set})",
                            name = &file.name.to_string_lossy().replace('\\', "/"),
                            line_set = lines.iter().map(|val| val.to_string()).collect::<Vec<_>>().join(","),
                        );
                }
            } // end format_advice iterations

            // iterate over clang-tidy advice and post annotations
            // The tidy_advice vector is parallel to the files vector; meaning it serves as a file filterer.
            // lines are already filter as specified to clang-tidy CLI.
            if let Some(tidy_advice) = &file.tidy_advice {
                for note in &tidy_advice.notes {
                    if note.filename == file.name.to_string_lossy().replace('\\', "/") {
                        println!(
                            "::{severity} file={file},line={line},title={file}:{line}:{cols} [{diag}]::{info}",
                            severity = if note.severity == *"note" { "notice".to_string() } else {note.severity.clone()},
                            file = note.filename,
                            line = note.line,
                            cols = note.cols,
                            diag = note.diagnostic,
                            info = note.rationale,
                        );
                    }
                }
            }
        }
    }

    /// Update existing comment or remove old comment(s) and post a new comment
    pub async fn update_comment(
        &self,
        url: Url,
        comment: &String,
        no_lgtm: bool,
        is_lgtm: bool,
        update_only: bool,
    ) -> Result<()> {
        let comment_url = self
            .remove_bot_comments(&url, !update_only || (is_lgtm && no_lgtm))
            .await?;
        if !is_lgtm || !no_lgtm {
            let payload = HashMap::from([("body", comment)]);
            // log::debug!("payload body:\n{:?}", payload);
            let req_meth = if comment_url.is_some() {
                Method::PATCH
            } else {
                Method::POST
            };
            let request = Self::make_api_request(
                &self.client,
                comment_url.unwrap_or(url),
                req_meth,
                Some(serde_json::json!(&payload).to_string()),
                None,
            )?;
            match Self::send_api_request(
                self.client.clone(),
                request,
                self.rate_limit_headers.to_owned(),
                0,
            )
            .await
            {
                Ok(response) => {
                    Self::log_response(response, "Failed to post thread comment").await;
                }
                Err(e) => {
                    log::error!("Failed to post thread comment: {e:?}");
                }
            }
        }
        Ok(())
    }

    /// Remove thread comments previously posted by cpp-linter.
    async fn remove_bot_comments(&self, url: &Url, delete: bool) -> Result<Option<Url>> {
        let mut comment_url = None;
        let mut comments_url = Some(Url::parse_with_params(url.as_str(), &[("page", "1")])?);
        let repo = format!(
            "repos/{}{}/comments",
            // if we got here, then we know it is on a CI runner as self.repo should be known
            self.repo.as_ref().expect("Repo name unknown."),
            if self.event_name == "pull_request" {
                "/issues"
            } else {
                ""
            },
        );
        let base_comment_url = self.api_url.join(&repo).unwrap();
        while let Some(ref endpoint) = comments_url {
            let request =
                Self::make_api_request(&self.client, endpoint.as_str(), Method::GET, None, None)?;
            let result = Self::send_api_request(
                self.client.clone(),
                request,
                self.rate_limit_headers.to_owned(),
                0,
            )
            .await;
            match result {
                Err(e) => {
                    log::error!("Failed to get list of existing thread comments: {e:?}");
                    return Ok(comment_url);
                }
                Ok(response) => {
                    if !response.status().is_success() {
                        Self::log_response(
                            response,
                            "Failed to get list of existing thread comments",
                        )
                        .await;
                        return Ok(comment_url);
                    }
                    comments_url = Self::try_next_page(response.headers());
                    let payload =
                        serde_json::from_str::<Vec<ThreadComment>>(&response.text().await?);
                    match payload {
                        Err(e) => {
                            log::error!(
                                "Failed to deserialize list of existing thread comments: {e:?}"
                            );
                            continue;
                        }
                        Ok(payload) => {
                            for comment in payload {
                                if comment.body.starts_with(COMMENT_MARKER) {
                                    log::debug!(
                                        "Found cpp-linter comment id {} from user {} ({})",
                                        comment.id,
                                        comment.user.login,
                                        comment.user.id,
                                    );
                                    let this_comment_url = Url::parse(
                                        format!("{base_comment_url}/{}", comment.id).as_str(),
                                    )?;
                                    if delete || comment_url.is_some() {
                                        // if not updating: remove all outdated comments
                                        // if updating: remove all outdated comments except the last one

                                        // use last saved comment_url (if not None) or current comment url
                                        let del_url = if let Some(last_url) = &comment_url {
                                            last_url
                                        } else {
                                            &this_comment_url
                                        };
                                        let req = Self::make_api_request(
                                            &self.client,
                                            del_url.as_str(),
                                            Method::DELETE,
                                            None,
                                            None,
                                        )?;
                                        match Self::send_api_request(
                                            self.client.clone(),
                                            req,
                                            self.rate_limit_headers.to_owned(),
                                            0,
                                        )
                                        .await
                                        {
                                            Ok(result) => {
                                                if !result.status().is_success() {
                                                    Self::log_response(
                                                        result,
                                                        "Failed to delete old thread comment",
                                                    )
                                                    .await;
                                                }
                                            }
                                            Err(e) => {
                                                log::error!(
                                                    "Failed to delete old thread comment: {e:?}"
                                                )
                                            }
                                        }
                                    }
                                    if !delete {
                                        comment_url = Some(this_comment_url)
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        Ok(comment_url)
    }

    /// Post a PR review with code suggestions.
    ///
    /// Note: `--no-lgtm` is applied when nothing is suggested.
    pub async fn post_review(
        &self,
        files: &[Arc<Mutex<FileObj>>],
        feedback_input: &FeedbackInput,
    ) -> Result<()> {
        let url = self
            .api_url
            .join("repos/")?
            .join(
                format!(
                    "{}/",
                    // if we got here, then we know self.repo should be known
                    self.repo.as_ref().ok_or(anyhow!("Repo name unknown"))?
                )
                .as_str(),
            )?
            .join("pulls/")?
            // if we got here, then we know that it is a self.pull_request is a valid value
            .join(self.pull_request.to_string().as_str())?;
        let request = Self::make_api_request(&self.client, url.as_str(), Method::GET, None, None)?;
        let response = Self::send_api_request(
            self.client.clone(),
            request,
            self.rate_limit_headers.clone(),
            0,
        );

        let url = Url::parse(format!("{}/", url).as_str())?.join("reviews")?;
        let dismissal = self.dismiss_outdated_reviews(&url);
        match response.await {
            Ok(response) => {
                match serde_json::from_str::<PullRequestInfo>(&response.text().await?) {
                    Err(e) => {
                        log::error!("Failed to deserialize PR info: {e:?}");
                        return dismissal.await;
                    }
                    Ok(pr_info) => {
                        if pr_info.draft || pr_info.state != "open" {
                            return dismissal.await;
                        }
                    }
                }
            }
            Err(e) => {
                log::error!("Failed to get PR info from {e:?}");
                return dismissal.await;
            }
        }

        let summary_only = ["true", "on", "1"].contains(
            &env::var("CPP_LINTER_PR_REVIEW_SUMMARY_ONLY")
                .unwrap_or("false".to_string())
                .as_str(),
        );

        let mut review_comments = ReviewComments::default();
        for file in files {
            let file = file.lock().unwrap();
            file.make_suggestions_from_patch(&mut review_comments, summary_only)?;
        }
        let has_no_changes =
            review_comments.full_patch[0].is_empty() && review_comments.full_patch[1].is_empty();
        if has_no_changes && feedback_input.no_lgtm {
            log::debug!("Not posting an approved review because `no-lgtm` is true");
            return dismissal.await;
        }
        let mut payload = FullReview {
            event: if feedback_input.passive_reviews {
                String::from("COMMENT")
            } else if has_no_changes {
                String::from("APPROVE")
            } else {
                String::from("REQUEST_CHANGES")
            },
            body: String::new(),
            comments: vec![],
        };
        payload.body = review_comments.summarize();
        if !summary_only {
            payload.comments = {
                let mut comments = vec![];
                for comment in review_comments.comments {
                    comments.push(ReviewDiffComment::from(comment));
                }
                comments
            };
        }
        dismissal.await?; // free up the `url` variable
        let request = Self::make_api_request(
            &self.client,
            url.clone(),
            Method::POST,
            Some(
                serde_json::to_string(&payload)
                    .with_context(|| "Failed to serialize PR review to json string")?,
            ),
            None,
        )?;
        match Self::send_api_request(
            self.client.clone(),
            request,
            self.rate_limit_headers.clone(),
            0,
        )
        .await
        {
            Ok(response) => {
                if !response.status().is_success() {
                    Self::log_response(response, "Failed to post a new PR review").await;
                }
            }
            Err(e) => {
                log::error!("Failed to post a new PR review: {e:?}");
            }
        }
        Ok(())
    }

    /// Dismiss any outdated reviews generated by cpp-linter.
    async fn dismiss_outdated_reviews(&self, url: &Url) -> Result<()> {
        let mut url_ = Some(Url::parse_with_params(url.as_str(), [("page", "1")])?);
        while let Some(ref endpoint) = url_ {
            let request =
                Self::make_api_request(&self.client, endpoint.as_str(), Method::GET, None, None)?;
            let result = Self::send_api_request(
                self.client.clone(),
                request,
                self.rate_limit_headers.clone(),
                0,
            )
            .await;
            match result {
                Err(e) => {
                    log::error!("Failed to get a list of existing PR reviews: {e:?}");
                    return Ok(());
                }
                Ok(response) => {
                    if !response.status().is_success() {
                        Self::log_response(response, "Failed to get a list of existing PR reviews")
                            .await;
                        return Ok(());
                    }
                    url_ = Self::try_next_page(response.headers());
                    match serde_json::from_str::<Vec<ReviewComment>>(&response.text().await?) {
                        Err(e) => {
                            log::error!("Unable to serialize JSON about review comments: {e:?}");
                            return Ok(());
                        }
                        Ok(payload) => {
                            for review in payload {
                                if let Some(body) = &review.body {
                                    if body.starts_with(COMMENT_MARKER)
                                        && !(["PENDING", "DISMISSED"]
                                            .contains(&review.state.as_str()))
                                    {
                                        // dismiss outdated review
                                        if let Ok(dismiss_url) = url.join(
                                            format!("reviews/{}/dismissals", review.id).as_str(),
                                        ) {
                                            if let Ok(req) = Self::make_api_request(
                                                &self.client,
                                                dismiss_url,
                                                Method::PUT,
                                                Some(REVIEW_DISMISSAL.to_string()),
                                                None,
                                            ) {
                                                match Self::send_api_request(
                                                    self.client.clone(),
                                                    req,
                                                    self.rate_limit_headers.clone(),
                                                    0,
                                                )
                                                .await
                                                {
                                                    Ok(result) => {
                                                        if !result.status().is_success() {
                                                            Self::log_response(
                                                                result,
                                                                "Failed to dismiss outdated review",
                                                            )
                                                            .await;
                                                        }
                                                    }
                                                    Err(e) => {
                                                        log::error!(
                                                            "Failed to dismiss outdated review: {e:}"
                                                        );
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        Ok(())
    }
}