git-bot-feedback 0.5.3

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
use chrono::Utc;
use git_bot_feedback::{
    CommentKind, CommentPolicy, RestClientError, ThreadCommentOptions, client::init_client,
};
use mockito::{Matcher, Server};
use std::{env, fmt::Display, io::Write, path::Path};
use tempfile::{NamedTempFile, TempDir};

mod common;
use common::logger_init;

const MARKER: &str = "<!-- git-bot-feedback -->\n";
const SHA: &str = "deadbeef";
const REPO: &str = "2bndy5/git-bot-feedback";
const PR: i64 = 22;
const TOKEN: &str = "123456";
const MOCK_ASSETS_PATH: &str = "tests/assets/thread_comment/github/";

const RESET_RATE_LIMIT_HEADER: &str = "x-ratelimit-reset";
const REMAINING_RATE_LIMIT_HEADER: &str = "x-ratelimit-remaining";

#[derive(PartialEq, Clone, Copy, Debug)]
enum EventType {
    Push,
    PullRequest,
}

impl Display for EventType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Push => write!(f, "push"),
            Self::PullRequest => write!(f, "pull_request"),
        }
    }
}

struct TestParams {
    event_t: EventType,
    comment_policy: CommentPolicy,
    no_lgtm: bool,
    comment_kind: CommentKind,
    fail_get_existing_comments: bool,
    fail_dismissal: bool,
    fail_posting: bool,
    bad_existing_comments: bool,
    bad_pr_info: bool,
    no_pr_info_env_var: bool,
    no_token: bool,
    no_repo_env_var: bool,
    no_sha_env_var: bool,
    pr_locked: bool,
}

impl Default for TestParams {
    fn default() -> Self {
        Self {
            event_t: EventType::Push,
            comment_policy: CommentPolicy::Update,
            no_lgtm: false,
            comment_kind: CommentKind::Concerns,
            fail_get_existing_comments: false,
            fail_dismissal: false,
            fail_posting: false,
            bad_existing_comments: false,
            bad_pr_info: false,
            no_pr_info_env_var: false,
            no_token: false,
            no_repo_env_var: false,
            no_sha_env_var: false,
            pr_locked: false,
        }
    }
}

async fn setup(lib_root: &Path, test_params: &TestParams) {
    unsafe {
        env::set_var("GITHUB_ACTIONS", "true");
        env::set_var(
            "GITHUB_EVENT_NAME",
            test_params.event_t.to_string().as_str(),
        );
        if !test_params.no_repo_env_var {
            env::set_var("GITHUB_REPOSITORY", REPO);
        } else if env::var("GITHUB_REPOSITORY").is_ok() {
            env::remove_var("GITHUB_REPOSITORY");
        }
        if !test_params.no_sha_env_var {
            env::set_var("GITHUB_SHA", SHA);
        } else if env::var("GITHUB_SHA").is_ok() {
            env::remove_var("GITHUB_SHA");
        }
        if !test_params.no_token {
            env::set_var("GITHUB_TOKEN", TOKEN);
        }
        env::set_var("CI", "true");
        if env::var("ACTIONS_STEP_DEBUG").is_err() {
            env::set_var("ACTIONS_STEP_DEBUG", "true");
        }
    }
    let mut event_payload_path = NamedTempFile::new_in("./").unwrap();
    if test_params.event_t == EventType::PullRequest {
        let event_payload = if test_params.bad_pr_info {
            "EVENT_PAYLOAD".to_string()
        } else {
            serde_json::json!({
                "pull_request": {
                    "draft": false,
                    "state": "open",
                    "number": PR,
                    "locked": test_params.pr_locked,
                }
            })
            .to_string()
        };
        event_payload_path
            .write_all(event_payload.as_bytes())
            .expect("Failed to create mock event payload.");
        if !test_params.no_pr_info_env_var {
            unsafe {
                env::set_var("GITHUB_EVENT_PATH", event_payload_path.path());
            }
        } else if env::var("GITHUB_EVENT_PATH").is_ok() {
            unsafe {
                env::remove_var("GITHUB_EVENT_PATH");
            }
        }
    }

    let reset_timestamp = (Utc::now().timestamp() + 60).to_string();
    let asset_path = format!("{}/{MOCK_ASSETS_PATH}", lib_root.to_str().unwrap());

    let mut server = Server::new_async().await;
    unsafe {
        env::set_var("GITHUB_API_URL", server.url());
    }

    logger_init();
    log::set_max_level(log::LevelFilter::Debug);
    let client = match init_client() {
        Ok(c) => c,
        Err(e) => {
            if test_params.no_pr_info_env_var
                || test_params.no_repo_env_var
                || test_params.no_sha_env_var
            {
                assert!(matches!(e, RestClientError::EnvVar { .. }));
            } else if test_params.bad_pr_info {
                assert!(matches!(e, RestClientError::Json { .. }));
            } else {
                panic!("Unexpected error creating GithubApiClient: {e}");
            }
            return;
        }
    };
    assert!(client.is_debug_enabled());
    assert!(
        client
            .event_name()
            .is_some_and(|n| n == test_params.event_t.to_string())
    );

    let mut mocks = vec![];

    if !test_params.no_token {
        match test_params.event_t {
            EventType::Push => {
                let mut mock = server
                    .mock(
                        "GET",
                        format!("/repos/{REPO}/commits/{SHA}/comments").as_str(),
                    )
                    .match_header("Accept", "application/vnd.github.raw+json")
                    .match_body(Matcher::Any)
                    .match_query(Matcher::UrlEncoded("page".to_string(), "1".to_string()))
                    .with_header(REMAINING_RATE_LIMIT_HEADER, "50")
                    .with_header(RESET_RATE_LIMIT_HEADER, reset_timestamp.as_str())
                    .match_header("Authorization", format!("token {TOKEN}").as_str())
                    .with_status(if test_params.fail_get_existing_comments {
                        403
                    } else {
                        200
                    });
                if test_params.bad_existing_comments {
                    mock = mock.with_body(String::new());
                } else {
                    mock =
                        mock.with_body_from_file(format!("{asset_path}push_comments_{SHA}.json"));
                }
                mock = mock.create();
                mocks.push(mock);
            }
            EventType::PullRequest => {
                if !test_params.pr_locked {
                    let pr_endpoint = format!("/repos/{REPO}/issues/{PR}/comments");
                    for pg in ["1", "2"] {
                        let link = if pg == "1" {
                            format!("<{}{pr_endpoint}?page=2>; rel=\"next\"", server.url())
                        } else {
                            "".to_string()
                        };
                        mocks.push(
                            server
                                .mock("GET", pr_endpoint.as_str())
                                .match_header("Accept", "application/vnd.github.raw+json")
                                .match_header("Authorization", format!("token {TOKEN}").as_str())
                                .match_body(Matcher::Any)
                                .match_query(Matcher::UrlEncoded(
                                    "page".to_string(),
                                    pg.to_string(),
                                ))
                                .with_body_from_file(format!("{asset_path}pr_comments_pg{pg}.json"))
                                .with_header(REMAINING_RATE_LIMIT_HEADER, "50")
                                .with_header(RESET_RATE_LIMIT_HEADER, reset_timestamp.as_str())
                                .with_header("link", link.as_str())
                                .with_status(if test_params.fail_dismissal { 403 } else { 200 })
                                .create(),
                        );
                    }
                }
            }
        }
    }

    let comment_url = format!(
        "/repos/{REPO}{}/comments/76453652",
        if test_params.event_t == EventType::PullRequest {
            "/issues"
        } else {
            ""
        }
    );

    if !test_params.fail_get_existing_comments
        && !test_params.bad_existing_comments
        && !test_params.no_token
        && (test_params.event_t == EventType::Push || !test_params.pr_locked)
    {
        mocks.push(
            server
                .mock("DELETE", comment_url.as_str())
                .match_body(Matcher::Any)
                .match_header("Authorization", format!("token {TOKEN}").as_str())
                .with_status(if test_params.fail_dismissal { 403 } else { 200 })
                .with_header(REMAINING_RATE_LIMIT_HEADER, "50")
                .with_header(RESET_RATE_LIMIT_HEADER, reset_timestamp.as_str())
                .expect_at_least(1)
                .create(),
        );
    }

    let comment = match test_params.comment_kind {
        CommentKind::Concerns => "Attention".to_string(),
        CommentKind::Lgtm => "LGTM".to_string(),
    };
    let new_comment_match = Matcher::Regex(comment.clone());

    let posting_comment = match test_params.comment_kind {
        CommentKind::Concerns => true,
        CommentKind::Lgtm => !test_params.no_lgtm && !test_params.bad_existing_comments,
    };
    if posting_comment
        && (test_params.event_t == EventType::Push || !test_params.pr_locked)
        && !test_params.no_token
    {
        if test_params.bad_existing_comments
            || test_params.fail_get_existing_comments
            || test_params.comment_policy == CommentPolicy::Anew
        {
            let mut mock = server
                .mock(
                    "POST",
                    format!(
                        "/repos/{REPO}/{}/comments",
                        if test_params.event_t == EventType::PullRequest {
                            format!("issues/{PR}")
                        } else {
                            format!("commits/{SHA}")
                        }
                    )
                    .as_str(),
                )
                .match_body(new_comment_match)
                .with_header(REMAINING_RATE_LIMIT_HEADER, "50")
                .with_header(RESET_RATE_LIMIT_HEADER, reset_timestamp.as_str())
                .with_status(if test_params.fail_posting { 403 } else { 200 })
                .create();
            if !test_params.no_token {
                mock = mock.match_header("Authorization", format!("token {TOKEN}").as_str());
            }
            mocks.push(mock);
        } else {
            mocks.push(
                server
                    .mock("PATCH", comment_url.as_str())
                    .match_body(new_comment_match.clone())
                    .match_header("Authorization", format!("token {TOKEN}").as_str())
                    .with_status(if test_params.fail_posting { 403 } else { 200 })
                    .with_header(REMAINING_RATE_LIMIT_HEADER, "50")
                    .with_header(RESET_RATE_LIMIT_HEADER, reset_timestamp.as_str())
                    .create(),
            );
        }
    }

    let opts = ThreadCommentOptions {
        policy: test_params.comment_policy,
        comment,
        kind: test_params.comment_kind,
        marker: MARKER.to_string(),
        no_lgtm: test_params.no_lgtm,
    };
    client.start_log_group("posting comment");
    let result = client.post_thread_comment(opts).await;
    client.end_log_group("");
    if test_params.bad_existing_comments {
        assert!(matches!(result, Err(RestClientError::Json { .. })));
    } else if test_params.no_token {
        assert!(matches!(result, Err(RestClientError::EnvVar { .. })));
    } else {
        assert!(result.is_ok());
    }
    for mock in mocks {
        mock.assert();
    }
}

async fn test_comment(test_params: &TestParams) {
    let tmp_dir = TempDir::new().unwrap();
    let lib_root = env::current_dir().unwrap();
    env::set_current_dir(tmp_dir.path()).unwrap();
    setup(&lib_root, test_params).await;
    env::set_current_dir(lib_root.as_path()).unwrap();
    drop(tmp_dir);
}

#[tokio::test]
async fn new_push() {
    test_comment(&TestParams {
        comment_policy: CommentPolicy::Anew,
        ..Default::default()
    })
    .await;
}

#[tokio::test]
async fn new_pr() {
    test_comment(&TestParams {
        event_t: EventType::PullRequest,
        comment_policy: CommentPolicy::Anew,
        ..Default::default()
    })
    .await;
}

#[tokio::test]
async fn update_push() {
    test_comment(&TestParams::default()).await;
}

#[tokio::test]
async fn update_pr() {
    test_comment(&TestParams {
        event_t: EventType::PullRequest,
        ..Default::default()
    })
    .await;
}

#[tokio::test]
async fn new_push_no_lgtm() {
    test_comment(&TestParams {
        comment_policy: CommentPolicy::Anew,
        comment_kind: CommentKind::Lgtm,
        no_lgtm: true,
        ..Default::default()
    })
    .await;
}

#[tokio::test]
async fn update_push_no_lgtm() {
    test_comment(&TestParams {
        comment_kind: CommentKind::Lgtm,
        no_lgtm: true,
        ..Default::default()
    })
    .await;
}

#[tokio::test]
async fn new_pr_no_lgtm() {
    test_comment(&TestParams {
        comment_policy: CommentPolicy::Anew,
        event_t: EventType::PullRequest,
        no_lgtm: true,
        comment_kind: CommentKind::Lgtm,
        ..Default::default()
    })
    .await;
}

#[tokio::test]
async fn update_pr_no_lgtm() {
    test_comment(&TestParams {
        event_t: EventType::PullRequest,
        comment_kind: CommentKind::Lgtm,
        no_lgtm: true,
        ..Default::default()
    })
    .await;
}

#[tokio::test]
async fn fail_get_existing_comments() {
    test_comment(&TestParams {
        fail_get_existing_comments: true,
        ..Default::default()
    })
    .await;
}

#[tokio::test]
async fn fail_dismissal() {
    test_comment(&TestParams {
        fail_dismissal: true,
        ..Default::default()
    })
    .await;
}

#[tokio::test]
async fn fail_posting() {
    test_comment(&TestParams {
        fail_posting: true,
        ..Default::default()
    })
    .await;
}

#[tokio::test]
async fn bad_existing_comments() {
    test_comment(&TestParams {
        bad_existing_comments: true,
        comment_kind: CommentKind::Lgtm,
        ..Default::default()
    })
    .await;
}

#[tokio::test]
async fn bad_pr_info() {
    test_comment(&TestParams {
        event_t: EventType::PullRequest,
        bad_pr_info: true,
        ..Default::default()
    })
    .await;
}

#[tokio::test]
async fn no_pr_info_env() {
    test_comment(&TestParams {
        event_t: EventType::PullRequest,
        bad_pr_info: true,
        no_pr_info_env_var: true,
        ..Default::default()
    })
    .await;
}

#[tokio::test]
async fn no_token() {
    test_comment(&TestParams {
        no_token: true,
        ..Default::default()
    })
    .await;
}

#[tokio::test]
async fn no_repo_env() {
    test_comment(&TestParams {
        no_repo_env_var: true,
        ..Default::default()
    })
    .await;
}

#[tokio::test]
async fn no_sha_env() {
    test_comment(&TestParams {
        no_sha_env_var: true,
        ..Default::default()
    })
    .await;
}

#[tokio::test]
async fn pr_locked() {
    test_comment(&TestParams {
        event_t: EventType::PullRequest,
        pr_locked: true,
        ..Default::default()
    })
    .await;
}