cpp_linter/rest_api/github/
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
//! This module holds functionality specific to using Github's REST API.
//!
//! In the root module, we just implement the RestApiClient trait.
//! In other (private) submodules we implement behavior specific to Github's REST API.

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

// non-std crates
use anyhow::{Context, Result};
use reqwest::{
    header::{HeaderMap, HeaderValue, AUTHORIZATION},
    Client, Method, Url,
};
use serde_json;

// project specific modules/crates
use super::{RestApiClient, RestApiRateLimitHeaders};
use crate::clang_tools::clang_format::tally_format_advice;
use crate::clang_tools::clang_tidy::tally_tidy_advice;
use crate::cli::{FeedbackInput, ThreadComments};
use crate::common_fs::{FileFilter, FileObj};
use crate::git::{get_diff, open_repo, parse_diff, parse_diff_from_buf};

// private submodules.
mod serde_structs;
mod specific_api;
use serde_structs::{GithubChangedFile, PushEventFiles};

/// A structure to work with Github REST API.
pub struct GithubApiClient {
    /// The HTTP request client to be used for all REST API calls.
    client: Client,

    /// The CI run's event payload from the webhook that triggered the workflow.
    pull_request: i64,

    /// The name of the event that was triggered when running cpp_linter.
    pub event_name: String,

    /// The value of the `GITHUB_API_URL` environment variable.
    api_url: Url,

    /// The value of the `GITHUB_REPOSITORY` environment variable.
    repo: Option<String>,

    /// The value of the `GITHUB_SHA` environment variable.
    sha: Option<String>,

    /// The value of the `ACTIONS_STEP_DEBUG` environment variable.
    pub debug_enabled: bool,

    /// The response header names that describe the rate limit status.
    rate_limit_headers: RestApiRateLimitHeaders,
}

// implement the RestApiClient trait for the GithubApiClient
impl RestApiClient for GithubApiClient {
    fn set_exit_code(
        &self,
        checks_failed: u64,
        format_checks_failed: Option<u64>,
        tidy_checks_failed: Option<u64>,
    ) -> u64 {
        if let Ok(gh_out) = env::var("GITHUB_OUTPUT") {
            if let Ok(mut gh_out_file) = OpenOptions::new().append(true).open(gh_out) {
                for (prompt, value) in [
                    ("checks-failed", Some(checks_failed)),
                    ("format-checks-failed", format_checks_failed),
                    ("tidy-checks-failed", tidy_checks_failed),
                ] {
                    if let Err(e) = writeln!(gh_out_file, "{prompt}={}", value.unwrap_or(0),) {
                        log::error!("Could not write to GITHUB_OUTPUT file: {}", e);
                        break;
                    }
                }
                if let Err(e) = gh_out_file.flush() {
                    log::debug!("Failed to flush buffer to GITHUB_OUTPUT file: {e:?}");
                }
            } else {
                log::debug!("GITHUB_OUTPUT file could not be opened");
            }
        }
        log::info!(
            "{} clang-format-checks-failed",
            format_checks_failed.unwrap_or(0)
        );
        log::info!(
            "{} clang-tidy-checks-failed",
            tidy_checks_failed.unwrap_or(0)
        );
        log::info!("{checks_failed} checks-failed");
        checks_failed
    }

    fn make_headers() -> Result<HeaderMap<HeaderValue>> {
        let mut headers = HeaderMap::new();
        headers.insert(
            "Accept",
            HeaderValue::from_str("application/vnd.github.raw+json")?,
        );
        if let Ok(token) = env::var("GITHUB_TOKEN") {
            log::debug!("Using auth token from GITHUB_TOKEN environment variable");
            let mut val = HeaderValue::from_str(format!("token {token}").as_str())?;
            val.set_sensitive(true);
            headers.insert(AUTHORIZATION, val);
        }
        Ok(headers)
    }

    async fn get_list_of_changed_files(&self, file_filter: &FileFilter) -> Result<Vec<FileObj>> {
        if env::var("CI").is_ok_and(|val| val.as_str() == "true")
            && self.repo.is_some()
            && self.sha.is_some()
        {
            // get diff from Github REST API
            let is_pr = self.event_name == "pull_request";
            let pr = self.pull_request.to_string();
            let sha = self.sha.clone().unwrap();
            let url = self
                .api_url
                .join("repos/")?
                .join(format!("{}/", self.repo.as_ref().unwrap()).as_str())?
                .join(if is_pr { "pulls/" } else { "commits/" })?
                .join(if is_pr { pr.as_str() } else { sha.as_str() })?;
            let mut diff_header = HeaderMap::new();
            diff_header.insert("Accept", "application/vnd.github.diff".parse()?);
            log::debug!("Getting file changes from {}", url.as_str());
            let request = Self::make_api_request(
                &self.client,
                url.as_str(),
                Method::GET,
                None,
                Some(diff_header),
            )?;
            let response = Self::send_api_request(
                self.client.clone(),
                request,
                self.rate_limit_headers.to_owned(),
                0,
            )
            .await
            .with_context(|| "Failed to get list of changed files from GitHub server.")?;
            if response.status().is_success() {
                Ok(parse_diff_from_buf(&response.bytes().await?, file_filter))
            } else {
                let endpoint = if is_pr {
                    Url::parse(format!("{}/files", url.as_str()).as_str())?
                } else {
                    url
                };
                Self::log_response(response, "Failed to get full diff for event").await;
                log::debug!("Trying paginated request to {}", endpoint.as_str());
                self.get_changed_files_paginated(endpoint, file_filter)
                    .await
            }
        } else {
            // get diff from libgit2 API
            let repo = open_repo(".").with_context(|| {
                "Please ensure the repository is checked out before running cpp-linter."
            })?;
            let list = parse_diff(&get_diff(&repo)?, file_filter);
            Ok(list)
        }
    }

    async fn get_changed_files_paginated(
        &self,
        url: Url,
        file_filter: &FileFilter,
    ) -> Result<Vec<FileObj>> {
        let mut url = Some(Url::parse_with_params(url.as_str(), &[("page", "1")])?);
        let mut files = vec![];
        while let Some(ref endpoint) = url {
            let request =
                Self::make_api_request(&self.client, endpoint.as_str(), Method::GET, None, None)?;
            let response = Self::send_api_request(
                self.client.clone(),
                request,
                self.rate_limit_headers.clone(),
                0,
            )
            .await;
            if let Ok(response) = response {
                url = Self::try_next_page(response.headers());
                let files_list = if self.event_name != "pull_request" {
                    let json_value: PushEventFiles = serde_json::from_str(&response.text().await?)
                        .with_context(|| {
                            "Failed to deserialize list of changed files from json response"
                        })?;
                    json_value.files
                } else {
                    serde_json::from_str::<Vec<GithubChangedFile>>(&response.text().await?)
                        .with_context(|| {
                            "Failed to deserialize list of file changes from Pull Request event."
                        })?
                };
                for file in files_list {
                    if let Some(patch) = file.patch {
                        let diff = format!(
                            "diff --git a/{old} b/{new}\n--- a/{old}\n+++ b/{new}\n{patch}",
                            old = file.previous_filename.unwrap_or(file.filename.clone()),
                            new = file.filename,
                        );
                        if let Some(file_obj) =
                            parse_diff_from_buf(diff.as_bytes(), file_filter).first()
                        {
                            files.push(file_obj.to_owned());
                        }
                    }
                }
            }
        }
        Ok(files)
    }

    async fn post_feedback(
        &self,
        files: &[Arc<Mutex<FileObj>>],
        feedback_inputs: FeedbackInput,
    ) -> Result<u64> {
        let tidy_checks_failed = tally_tidy_advice(files);
        let format_checks_failed = tally_format_advice(files);
        let mut comment = None;

        if feedback_inputs.file_annotations {
            self.post_annotations(files, feedback_inputs.style.as_str());
        }
        if feedback_inputs.step_summary {
            comment =
                Some(self.make_comment(files, format_checks_failed, tidy_checks_failed, None));
            self.post_step_summary(comment.as_ref().unwrap());
        }
        self.set_exit_code(
            format_checks_failed + tidy_checks_failed,
            Some(format_checks_failed),
            Some(tidy_checks_failed),
        );

        if feedback_inputs.thread_comments != ThreadComments::Off {
            // post thread comment for PR or push event
            if comment.as_ref().is_some_and(|c| c.len() > 65535) || comment.is_none() {
                comment = Some(self.make_comment(
                    files,
                    format_checks_failed,
                    tidy_checks_failed,
                    Some(65535),
                ));
            }
            if let Some(repo) = &self.repo {
                let is_pr = self.event_name == "pull_request";
                let pr = self.pull_request.to_string() + "/";
                let sha = self.sha.clone().unwrap() + "/";
                let comments_url = self
                    .api_url
                    .join("repos/")?
                    .join(format!("{}/", repo).as_str())?
                    .join(if is_pr { "issues/" } else { "commits/" })?
                    .join(if is_pr { pr.as_str() } else { sha.as_str() })?
                    .join("comments")?;

                self.update_comment(
                    comments_url,
                    &comment.unwrap(),
                    feedback_inputs.no_lgtm,
                    format_checks_failed + tidy_checks_failed == 0,
                    feedback_inputs.thread_comments == ThreadComments::Update,
                )
                .await?;
            }
        }
        if self.event_name == "pull_request"
            && (feedback_inputs.tidy_review || feedback_inputs.format_review)
        {
            self.post_review(files, &feedback_inputs).await?;
        }
        Ok(format_checks_failed + tidy_checks_failed)
    }
}

#[cfg(test)]
mod test {
    use std::{
        default::Default,
        env,
        io::Read,
        path::{Path, PathBuf},
        sync::{Arc, Mutex},
    };

    use regex::Regex;
    use tempfile::{tempdir, NamedTempFile};

    use super::GithubApiClient;
    use crate::{
        clang_tools::{
            clang_format::{FormatAdvice, Replacement},
            clang_tidy::{TidyAdvice, TidyNotification},
        },
        cli::FeedbackInput,
        common_fs::{FileFilter, FileObj},
        logger,
        rest_api::{RestApiClient, USER_OUTREACH},
    };

    // ************************* tests for step-summary and output variables

    async fn create_comment(
        is_lgtm: bool,
        fail_gh_out: bool,
        fail_summary: bool,
    ) -> (String, String) {
        let tmp_dir = tempdir().unwrap();
        let rest_api_client = GithubApiClient::new().unwrap();
        logger::init().unwrap();
        if env::var("ACTIONS_STEP_DEBUG").is_ok_and(|var| var == "true") {
            assert!(rest_api_client.debug_enabled);
            log::set_max_level(log::LevelFilter::Debug);
        }
        let mut files = vec![];
        if !is_lgtm {
            for _i in 0..65535 {
                let filename = String::from("tests/demo/demo.cpp");
                let mut file = FileObj::new(PathBuf::from(&filename));
                let notes = vec![TidyNotification {
                    filename,
                    line: 0,
                    cols: 0,
                    severity: String::from("note"),
                    rationale: String::from("A test dummy rationale"),
                    diagnostic: String::from("clang-diagnostic-warning"),
                    suggestion: vec![],
                    fixed_lines: vec![],
                }];
                file.tidy_advice = Some(TidyAdvice {
                    notes,
                    patched: None,
                });
                let replacements = vec![Replacement {
                    offset: 0,
                    length: 0,
                    value: Some(String::new()),
                    line: 1,
                    cols: 1,
                }];
                file.format_advice = Some(FormatAdvice {
                    replacements,
                    patched: None,
                });
                files.push(Arc::new(Mutex::new(file)));
            }
        }
        let feedback_inputs = FeedbackInput {
            style: if is_lgtm {
                String::new()
            } else {
                String::from("file")
            },
            step_summary: true,
            ..Default::default()
        };
        let mut step_summary_path = NamedTempFile::new_in(tmp_dir.path()).unwrap();
        env::set_var(
            "GITHUB_STEP_SUMMARY",
            if fail_summary {
                Path::new("not-a-file.txt")
            } else {
                step_summary_path.path()
            },
        );
        let mut gh_out_path = NamedTempFile::new_in(tmp_dir.path()).unwrap();
        env::set_var(
            "GITHUB_OUTPUT",
            if fail_gh_out {
                Path::new("not-a-file.txt")
            } else {
                gh_out_path.path()
            },
        );
        rest_api_client
            .post_feedback(&files, feedback_inputs)
            .await
            .unwrap();
        let mut step_summary_content = String::new();
        step_summary_path
            .read_to_string(&mut step_summary_content)
            .unwrap();
        if !fail_summary {
            assert!(&step_summary_content.contains(USER_OUTREACH));
        }
        let mut gh_out_content = String::new();
        gh_out_path.read_to_string(&mut gh_out_content).unwrap();
        if !fail_gh_out {
            assert!(gh_out_content.starts_with("checks-failed="));
        }
        (step_summary_content, gh_out_content)
    }

    #[tokio::test]
    async fn check_comment_concerns() {
        let (comment, gh_out) = create_comment(false, false, false).await;
        assert!(&comment.contains(":warning:\nSome files did not pass the configured checks!\n"));
        let fmt_pattern = Regex::new(r"format-checks-failed=(\d+)\n").unwrap();
        let tidy_pattern = Regex::new(r"tidy-checks-failed=(\d+)\n").unwrap();
        for pattern in [fmt_pattern, tidy_pattern] {
            let number = pattern
                .captures(&gh_out)
                .expect("found no number of checks-failed")
                .get(1)
                .unwrap()
                .as_str()
                .parse::<u64>()
                .unwrap();
            assert!(number > 0);
        }
    }

    #[tokio::test]
    async fn check_comment_lgtm() {
        env::set_var("ACTIONS_STEP_DEBUG", "true");
        let (comment, gh_out) = create_comment(true, false, false).await;
        assert!(comment.contains(":heavy_check_mark:\nNo problems need attention."));
        assert_eq!(
            gh_out,
            "checks-failed=0\nformat-checks-failed=0\ntidy-checks-failed=0\n"
        );
    }

    #[tokio::test]
    async fn fail_gh_output() {
        env::set_var("ACTIONS_STEP_DEBUG", "true");
        let (comment, gh_out) = create_comment(true, true, false).await;
        assert!(&comment.contains(":heavy_check_mark:\nNo problems need attention."));
        assert!(gh_out.is_empty());
    }

    #[tokio::test]
    async fn fail_gh_summary() {
        env::set_var("ACTIONS_STEP_DEBUG", "true");
        let (comment, gh_out) = create_comment(true, false, true).await;
        assert!(comment.is_empty());
        assert_eq!(
            gh_out,
            "checks-failed=0\nformat-checks-failed=0\ntidy-checks-failed=0\n"
        );
    }

    #[tokio::test]
    async fn fail_get_local_diff() {
        env::set_var("CI", "false");
        let tmp_dir = tempdir().unwrap();
        env::set_current_dir(tmp_dir.path()).unwrap();
        let rest_client = GithubApiClient::new().unwrap();
        let files = rest_client
            .get_list_of_changed_files(&FileFilter::new(&[], vec![]))
            .await;
        assert!(files.is_err())
    }
}