cpp_linter/rest_api/
mod.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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//! This module is the home of functionality that uses the REST API of various git-based
//! servers.
//!
//! Currently, only Github is supported.

use std::fmt::Debug;
use std::future::Future;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::Duration;

// non-std crates
use anyhow::{anyhow, Error, Result};
use chrono::DateTime;
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::{Client, IntoUrl, Method, Request, Response, Url};

// project specific modules
pub mod github;
use crate::cli::FeedbackInput;
use crate::common_fs::{FileFilter, FileObj};

pub static COMMENT_MARKER: &str = "<!-- cpp linter action -->\n";
pub static USER_OUTREACH: &str = concat!(
    "\n\nHave any feedback or feature suggestions? [Share it here.]",
    "(https://github.com/cpp-linter/cpp-linter-action/issues)"
);
pub static USER_AGENT: &str = concat!("cpp-linter/", env!("CARGO_PKG_VERSION"));

/// A structure to contain the different forms of headers that
/// describe a REST API's rate limit status.
#[derive(Debug, Clone)]
pub struct RestApiRateLimitHeaders {
    /// The header key of the rate limit's reset time.
    pub reset: String,
    /// The header key of the rate limit's remaining attempts.
    pub remaining: String,
    /// The header key of the rate limit's "backoff" time interval.
    pub retry: String,
}

/// A custom trait that templates necessary functionality with a Git server's REST API.
pub trait RestApiClient {
    /// A way to set output variables specific to cpp_linter executions in CI.
    fn set_exit_code(
        &self,
        checks_failed: u64,
        format_checks_failed: Option<u64>,
        tidy_checks_failed: Option<u64>,
    ) -> u64;

    /// A convenience method to create the headers attached to all REST API calls.
    ///
    /// If an authentication token is provided (via environment variable),
    /// this method shall include the relative information.
    fn make_headers() -> Result<HeaderMap<HeaderValue>>;

    /// Construct a HTTP request to be sent.
    ///
    /// The idea here is that this method is called before [`RestApiClient::send_api_request()`].
    /// ```ignore
    /// let request = Self::make_api_request(
    ///     &self.client,
    ///     "https://example.com",
    ///     Method::GET,
    ///     None,
    ///     None
    /// );
    /// let response = Self::send_api_request(
    ///     self.client.clone(),
    ///     request,
    ///     self.rest_api_headers.clone(),
    ///     0, // start recursion count at 0 (max iterations is 4)
    /// );
    /// match response.await {
    ///     Some(res) => {/* handle response */}
    ///     None => {/* handle failure */}
    /// }
    /// ```
    fn make_api_request(
        client: &Client,
        url: impl IntoUrl,
        method: Method,
        data: Option<String>,
        headers: Option<HeaderMap>,
    ) -> Result<Request> {
        let mut req = client.request(method, url);
        if let Some(h) = headers {
            req = req.headers(h);
        }
        if let Some(d) = data {
            req = req.body(d);
        }
        // RequestBuilder only fails to `build()` if there is a malformed `url`. We
        // should be safe here because of this function's `url` parameter type.
        req.build().map_err(Error::from)
    }

    /// A convenience function to send HTTP requests and respect a REST API rate limits.
    ///
    /// This method must own all the data passed to it because asynchronous recursion is used.
    /// Recursion is needed when a secondary rate limit is hit. The server tells the client that
    /// it should back off and retry after a specified time interval.
    ///
    /// Note, pass `0` to the `retries` parameter, which is used to count recursive iterations.
    /// This function will recur a maximum of 4 times, and this only happens when the response's
    /// headers includes a retry interval.
    fn send_api_request(
        client: Client,
        request: Request,
        rate_limit_headers: RestApiRateLimitHeaders,
        retries: u8,
    ) -> impl Future<Output = Result<Response>> + Send {
        async move {
            static MAX_RETRIES: u8 = 5;
            for i in retries..MAX_RETRIES {
                let result = client
                    .execute(request.try_clone().ok_or(anyhow!(
                        "Failed to clone request object for recursive behavior"
                    ))?)
                    .await;
                if let Ok(response) = &result {
                    if [403u16, 429u16].contains(&response.status().as_u16()) {
                        // rate limit may have been exceeded

                        // check if primary rate limit was violated; panic if so.
                        let mut requests_remaining = None;
                        if let Some(remaining) =
                            response.headers().get(&rate_limit_headers.remaining)
                        {
                            if let Ok(count) = remaining.to_str() {
                                if let Ok(value) = count.parse::<i64>() {
                                    requests_remaining = Some(value);
                                } else {
                                    log::debug!(
                                    "Failed to parse i64 from remaining attempts about rate limit: {count}"
                                );
                                }
                            }
                        } else {
                            // NOTE: I guess it is sometimes valid for a request to
                            // not include remaining rate limit attempts
                            log::debug!(
                                "Response headers do not include remaining API usage count"
                            );
                        }
                        if requests_remaining.is_some_and(|v| v <= 0) {
                            if let Some(reset_value) =
                                response.headers().get(&rate_limit_headers.reset)
                            {
                                if let Ok(epoch) = reset_value.to_str() {
                                    if let Ok(value) = epoch.parse::<i64>() {
                                        if let Some(reset) = DateTime::from_timestamp(value, 0) {
                                            return Err(anyhow!(
                                                "REST API rate limit exceeded! Resets at {}",
                                                reset
                                            ));
                                        }
                                    } else {
                                        log::debug!(
                                        "Failed to parse i64 from reset time about rate limit: {epoch}"
                                    );
                                    }
                                }
                            } else {
                                log::debug!("Response headers does not include a reset timestamp");
                            }
                            return Err(anyhow!("REST API rate limit exceeded!"));
                        }

                        // check if secondary rate limit is violated; backoff and try again.
                        if let Some(retry_value) = response.headers().get(&rate_limit_headers.retry)
                        {
                            if let Ok(retry_str) = retry_value.to_str() {
                                if let Ok(retry) = retry_str.parse::<u64>() {
                                    let interval = Duration::from_secs(retry + (i as u64).pow(2));
                                    tokio::time::sleep(interval).await;
                                } else {
                                    log::debug!(
                                    "Failed to parse u64 from retry interval about rate limit: {retry_str}"
                                );
                                }
                            }
                            continue;
                        }
                    }
                    return result.map_err(Error::from);
                }
                return result.map_err(Error::from);
            }
            Err(anyhow!(
                "REST API secondary rate limit exceeded after {MAX_RETRIES} retries."
            ))
        }
    }

    /// A way to get the list of changed files using REST API calls. It is this method's
    /// job to parse diff blobs and return a list of changed files.
    ///
    /// The context of the file changes are subject to the type of event in which
    /// cpp_linter package is used.
    fn get_list_of_changed_files(
        &self,
        file_filter: &FileFilter,
    ) -> impl Future<Output = Result<Vec<FileObj>>>;

    /// A way to get the list of changed files using REST API calls that employ a paginated response.
    ///
    /// This is a helper to [`RestApiClient::get_list_of_changed_files()`] but takes a formulated URL
    /// endpoint based on the context of the triggering CI event.
    fn get_changed_files_paginated(
        &self,
        url: Url,
        file_filter: &FileFilter,
    ) -> impl Future<Output = Result<Vec<FileObj>>>;

    /// Makes a comment in MarkDown syntax based on the concerns in `format_advice` and
    /// `tidy_advice` about the given set of `files`.
    ///
    /// This method has a default definition and should not need to be redefined by
    /// implementors.
    ///
    /// Returns the markdown comment as a string as well as the total count of
    /// `format_checks_failed` and `tidy_checks_failed` (in respective order).
    fn make_comment(
        &self,
        files: &[Arc<Mutex<FileObj>>],
        format_checks_failed: u64,
        tidy_checks_failed: u64,
        max_len: Option<u64>,
    ) -> String {
        let mut comment = format!("{COMMENT_MARKER}# Cpp-Linter Report ");
        let mut remaining_length =
            max_len.unwrap_or(u64::MAX) - comment.len() as u64 - USER_OUTREACH.len() as u64;

        if format_checks_failed > 0 || tidy_checks_failed > 0 {
            let prompt = ":warning:\nSome files did not pass the configured checks!\n";
            remaining_length -= prompt.len() as u64;
            comment.push_str(prompt);
            if format_checks_failed > 0 {
                make_format_comment(
                    files,
                    &mut comment,
                    format_checks_failed,
                    &mut remaining_length,
                );
            }
            if tidy_checks_failed > 0 {
                make_tidy_comment(
                    files,
                    &mut comment,
                    tidy_checks_failed,
                    &mut remaining_length,
                );
            }
        } else {
            comment.push_str(":heavy_check_mark:\nNo problems need attention.");
        }
        comment.push_str(USER_OUTREACH);
        comment
    }

    /// A way to post feedback in the form of `thread_comments`, `file_annotations`, and
    /// `step_summary`.
    ///
    /// The given `files` should've been gathered from `get_list_of_changed_files()` or
    /// `list_source_files()`.
    ///
    /// The `format_advice` and `tidy_advice` should be a result of parsing output from
    /// clang-format and clang-tidy (see `capture_clang_tools_output()`).
    ///
    /// All other parameters correspond to CLI arguments.
    fn post_feedback(
        &self,
        files: &[Arc<Mutex<FileObj>>],
        user_inputs: FeedbackInput,
    ) -> impl Future<Output = Result<u64>>;

    /// Gets the URL for the next page in a paginated response.
    ///
    /// Returns [`None`] if current response is the last page.
    fn try_next_page(headers: &HeaderMap) -> Option<Url> {
        if let Some(links) = headers.get("link") {
            if let Ok(pg_str) = links.to_str() {
                let pages = pg_str.split(", ");
                for page in pages {
                    if page.ends_with("; rel=\"next\"") {
                        if let Some(link) = page.split_once(">;") {
                            let url = link.0.trim_start_matches("<").to_string();
                            if let Ok(next) = Url::parse(&url) {
                                return Some(next);
                            } else {
                                log::debug!("Failed to parse next page link from response header");
                            }
                        } else {
                            log::debug!("Response header link for pagination is malformed");
                        }
                    }
                }
            }
        }
        None
    }

    fn log_response(response: Response, context: &str) -> impl Future<Output = ()> + Send {
        async move {
            if let Err(e) = response.error_for_status_ref() {
                log::error!("{}: {e:?}", context.to_owned());
                if let Ok(text) = response.text().await {
                    log::error!("{text}");
                }
            }
        }
    }
}

fn make_format_comment(
    files: &[Arc<Mutex<FileObj>>],
    comment: &mut String,
    format_checks_failed: u64,
    remaining_length: &mut u64,
) {
    let opener = format!("\n<details><summary>clang-format reports: <strong>{} file(s) not formatted</strong></summary>\n\n", format_checks_failed);
    let closer = String::from("\n</details>");
    let mut format_comment = String::new();
    *remaining_length -= opener.len() as u64 + closer.len() as u64;
    for file in files {
        let file = file.lock().unwrap();
        if let Some(format_advice) = &file.format_advice {
            if !format_advice.replacements.is_empty() && *remaining_length > 0 {
                let note = format!("- {}\n", file.name.to_string_lossy().replace('\\', "/"));
                if (note.len() as u64) < *remaining_length {
                    format_comment.push_str(&note.to_string());
                    *remaining_length -= note.len() as u64;
                }
            }
        }
    }
    comment.push_str(&opener);
    comment.push_str(&format_comment);
    comment.push_str(&closer);
}

fn make_tidy_comment(
    files: &[Arc<Mutex<FileObj>>],
    comment: &mut String,
    tidy_checks_failed: u64,
    remaining_length: &mut u64,
) {
    let opener = format!(
        "\n<details><summary>clang-tidy reports: <strong>{} concern(s)</strong></summary>\n\n",
        tidy_checks_failed
    );
    let closer = String::from("\n</details>");
    let mut tidy_comment = String::new();
    *remaining_length -= opener.len() as u64 + closer.len() as u64;
    for file in files {
        let file = file.lock().unwrap();
        if let Some(tidy_advice) = &file.tidy_advice {
            for tidy_note in &tidy_advice.notes {
                let file_path = PathBuf::from(&tidy_note.filename);
                if file_path == file.name {
                    let mut tmp_note = format!("- {}\n\n", tidy_note.filename);
                    tmp_note.push_str(&format!(
                        "   <strong>{filename}:{line}:{cols}:</strong> {severity}: [{diagnostic}]\n   > {rationale}\n{concerned_code}",
                        filename = tidy_note.filename,
                        line = tidy_note.line,
                        cols = tidy_note.cols,
                        severity = tidy_note.severity,
                        diagnostic = tidy_note.diagnostic_link(),
                        rationale = tidy_note.rationale,
                        concerned_code = if tidy_note.suggestion.is_empty() {String::from("")} else {
                            format!("\n   ```{ext}\n   {suggestion}\n   ```\n",
                                ext = file_path.extension().unwrap_or_default().to_string_lossy(),
                                suggestion = tidy_note.suggestion.join("\n   "),
                            ).to_string()
                        },
                    ).to_string());

                    if (tmp_note.len() as u64) < *remaining_length {
                        tidy_comment.push_str(&tmp_note);
                        *remaining_length -= tmp_note.len() as u64;
                    }
                }
            }
        }
    }
    comment.push_str(&opener);
    comment.push_str(&tidy_comment);
    comment.push_str(&closer);
}

/// This module tests the silent errors' debug logs
/// from `try_next_page()` and `send_api_request()` functions.
#[cfg(test)]
mod test {
    use std::sync::{Arc, Mutex};

    use anyhow::{anyhow, Result};
    use chrono::Utc;
    use mockito::{Matcher, Server};
    use reqwest::{
        header::{HeaderMap, HeaderValue},
        Client,
    };
    use reqwest::{Method, Url};

    use crate::{
        cli::FeedbackInput,
        common_fs::{FileFilter, FileObj},
        logger,
    };

    use super::{RestApiClient, RestApiRateLimitHeaders};

    /// A dummy struct to impl RestApiClient
    #[derive(Default)]
    struct TestClient {}

    impl RestApiClient for TestClient {
        fn set_exit_code(
            &self,
            _checks_failed: u64,
            _format_checks_failed: Option<u64>,
            _tidy_checks_failed: Option<u64>,
        ) -> u64 {
            0
        }

        fn make_headers() -> Result<HeaderMap<HeaderValue>> {
            Err(anyhow!("Not implemented"))
        }

        async fn get_list_of_changed_files(
            &self,
            _file_filter: &FileFilter,
        ) -> Result<Vec<FileObj>> {
            Err(anyhow!("Not implemented"))
        }

        async fn get_changed_files_paginated(
            &self,
            _url: reqwest::Url,
            _file_filter: &FileFilter,
        ) -> Result<Vec<FileObj>> {
            Err(anyhow!("Not implemented"))
        }

        async fn post_feedback(
            &self,
            _files: &[Arc<Mutex<FileObj>>],
            _user_inputs: FeedbackInput,
        ) -> Result<u64> {
            Err(anyhow!("Not implemented"))
        }
    }

    #[tokio::test]
    async fn dummy_coverage() {
        assert!(TestClient::make_headers().is_err());
        let dummy = TestClient::default();
        assert_eq!(dummy.set_exit_code(1, None, None), 0);
        assert!(dummy
            .get_list_of_changed_files(&FileFilter::new(&[], vec![]))
            .await
            .is_err());
        assert!(dummy
            .get_changed_files_paginated(
                Url::parse("https://example.net").unwrap(),
                &FileFilter::new(&[], vec![])
            )
            .await
            .is_err());
        assert!(dummy
            .post_feedback(&[], FeedbackInput::default())
            .await
            .is_err())
    }

    // ************************************************* try_next_page() tests

    #[test]
    fn bad_link_header() {
        let mut headers = HeaderMap::with_capacity(1);
        assert!(headers
            .insert("link", HeaderValue::from_str("; rel=\"next\"").unwrap())
            .is_none());
        logger::init().unwrap();
        log::set_max_level(log::LevelFilter::Debug);
        let result = TestClient::try_next_page(&headers);
        assert!(result.is_none());
    }

    #[test]
    fn bad_link_domain() {
        let mut headers = HeaderMap::with_capacity(1);
        assert!(headers
            .insert(
                "link",
                HeaderValue::from_str("<not a domain>; rel=\"next\"").unwrap()
            )
            .is_none());
        logger::init().unwrap();
        log::set_max_level(log::LevelFilter::Debug);
        let result = TestClient::try_next_page(&headers);
        assert!(result.is_none());
    }

    // ************************************************* Rate Limit Tests

    #[derive(Default)]
    struct RateLimitTestParams {
        secondary: bool,
        has_remaining_count: bool,
        bad_remaining_count: bool,
        has_reset_timestamp: bool,
        bad_reset_timestamp: bool,
        has_retry_interval: bool,
        bad_retry_interval: bool,
    }

    async fn simulate_rate_limit(test_params: &RateLimitTestParams) {
        let rate_limit_headers = RestApiRateLimitHeaders {
            reset: "reset".to_string(),
            remaining: "remaining".to_string(),
            retry: "retry".to_string(),
        };
        logger::init().unwrap();
        log::set_max_level(log::LevelFilter::Debug);

        let mut server = Server::new_async().await;
        let client = Client::new();
        let reset_timestamp = (Utc::now().timestamp() + 60).to_string();
        let mut mock = server
            .mock("GET", "/")
            .match_body(Matcher::Any)
            .expect_at_least(1)
            .expect_at_most(5)
            .with_status(429);
        if test_params.has_remaining_count {
            mock = mock.with_header(
                &rate_limit_headers.remaining,
                if test_params.secondary {
                    "1"
                } else if test_params.bad_remaining_count {
                    "X"
                } else {
                    "0"
                },
            );
        }
        if test_params.has_reset_timestamp {
            mock = mock.with_header(
                &rate_limit_headers.reset,
                if test_params.bad_reset_timestamp {
                    "X"
                } else {
                    &reset_timestamp
                },
            );
        }
        if test_params.secondary && test_params.has_retry_interval {
            mock.with_header(
                &rate_limit_headers.retry,
                if test_params.bad_retry_interval {
                    "X"
                } else {
                    "0"
                },
            )
            .create();
        } else {
            mock.create();
        }
        let request =
            TestClient::make_api_request(&client, server.url(), Method::GET, None, None).unwrap();
        TestClient::send_api_request(client.clone(), request, rate_limit_headers.clone(), 0)
            .await
            .unwrap();
    }

    #[tokio::test]
    #[should_panic(expected = "REST API secondary rate limit exceeded")]
    async fn rate_limit_secondary() {
        simulate_rate_limit(&RateLimitTestParams {
            secondary: true,
            has_retry_interval: true,
            ..Default::default()
        })
        .await;
    }

    #[tokio::test]
    #[should_panic(expected = "REST API secondary rate limit exceeded")]
    async fn rate_limit_bad_retry() {
        simulate_rate_limit(&RateLimitTestParams {
            secondary: true,
            has_retry_interval: true,
            bad_retry_interval: true,
            ..Default::default()
        })
        .await;
    }

    #[tokio::test]
    #[should_panic(expected = "REST API rate limit exceeded!")]
    async fn rate_limit_primary() {
        simulate_rate_limit(&RateLimitTestParams {
            has_remaining_count: true,
            has_reset_timestamp: true,
            ..Default::default()
        })
        .await;
    }

    #[tokio::test]
    #[should_panic(expected = "REST API rate limit exceeded!")]
    async fn rate_limit_no_reset() {
        simulate_rate_limit(&RateLimitTestParams {
            has_remaining_count: true,
            ..Default::default()
        })
        .await;
    }

    #[tokio::test]
    #[should_panic(expected = "REST API rate limit exceeded!")]
    async fn rate_limit_bad_reset() {
        simulate_rate_limit(&RateLimitTestParams {
            has_remaining_count: true,
            has_reset_timestamp: true,
            bad_reset_timestamp: true,
            ..Default::default()
        })
        .await;
    }

    #[tokio::test]
    async fn rate_limit_bad_count() {
        simulate_rate_limit(&RateLimitTestParams {
            has_remaining_count: true,
            bad_remaining_count: true,
            ..Default::default()
        })
        .await;
    }
}