checks.rs

  1use std::{fmt, ops::Not as _};
  2
  3use itertools::Itertools as _;
  4
  5use crate::{
  6    git::{CommitDetails, CommitList},
  7    github::{
  8        CommitAuthor, GitHubClient, GitHubUser, GithubLogin, PullRequestComment, PullRequestData,
  9        PullRequestReview, ReviewState,
 10    },
 11    report::Report,
 12};
 13
 14const ZED_ZIPPY_COMMENT_APPROVAL_PATTERN: &str = "@zed-zippy approve";
 15const ZED_ZIPPY_GROUP_APPROVAL: &str = "@zed-industries/approved";
 16
 17#[derive(Debug)]
 18pub enum ReviewSuccess {
 19    ApprovingComment(Vec<PullRequestComment>),
 20    CoAuthored(Vec<CommitAuthor>),
 21    ExternalMergedContribution { merged_by: GitHubUser },
 22    PullRequestReviewed(Vec<PullRequestReview>),
 23}
 24
 25impl ReviewSuccess {
 26    pub(crate) fn reviewers(&self) -> anyhow::Result<String> {
 27        let reviewers = match self {
 28            Self::CoAuthored(authors) => authors.iter().map(ToString::to_string).collect_vec(),
 29            Self::PullRequestReviewed(reviews) => reviews
 30                .iter()
 31                .filter_map(|review| review.user.as_ref())
 32                .map(|user| format!("@{}", user.login))
 33                .collect_vec(),
 34            Self::ApprovingComment(comments) => comments
 35                .iter()
 36                .map(|comment| format!("@{}", comment.user.login))
 37                .collect_vec(),
 38            Self::ExternalMergedContribution { merged_by } => {
 39                vec![format!("@{}", merged_by.login)]
 40            }
 41        };
 42
 43        let reviewers = reviewers.into_iter().unique().collect_vec();
 44
 45        reviewers
 46            .is_empty()
 47            .not()
 48            .then(|| reviewers.join(", "))
 49            .ok_or_else(|| anyhow::anyhow!("Expected at least one reviewer"))
 50    }
 51}
 52
 53impl fmt::Display for ReviewSuccess {
 54    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
 55        match self {
 56            Self::CoAuthored(_) => formatter.write_str("Co-authored by an organization member"),
 57            Self::PullRequestReviewed(_) => {
 58                formatter.write_str("Approved by an organization review")
 59            }
 60            Self::ApprovingComment(_) => {
 61                formatter.write_str("Approved by an organization approval comment")
 62            }
 63            Self::ExternalMergedContribution { .. } => {
 64                formatter.write_str("External merged contribution")
 65            }
 66        }
 67    }
 68}
 69
 70#[derive(Debug)]
 71pub enum ReviewFailure {
 72    // todo: We could still query the GitHub API here to search for one
 73    NoPullRequestFound,
 74    Unreviewed,
 75    UnableToDetermineReviewer,
 76    Other(anyhow::Error),
 77}
 78
 79impl fmt::Display for ReviewFailure {
 80    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
 81        match self {
 82            Self::NoPullRequestFound => formatter.write_str("No pull request found"),
 83            Self::Unreviewed => formatter
 84                .write_str("No qualifying organization approval found for the pull request"),
 85            Self::UnableToDetermineReviewer => formatter.write_str("Could not determine reviewer"),
 86            Self::Other(error) => write!(formatter, "Failed to inspect review state: {error}"),
 87        }
 88    }
 89}
 90
 91pub(crate) type ReviewResult = Result<ReviewSuccess, ReviewFailure>;
 92
 93impl<E: Into<anyhow::Error>> From<E> for ReviewFailure {
 94    fn from(err: E) -> Self {
 95        Self::Other(anyhow::anyhow!(err))
 96    }
 97}
 98
 99pub struct Reporter<'a> {
100    commits: CommitList,
101    github_client: &'a GitHubClient,
102}
103
104impl<'a> Reporter<'a> {
105    pub fn new(commits: CommitList, github_client: &'a GitHubClient) -> Self {
106        Self {
107            commits,
108            github_client,
109        }
110    }
111
112    /// Method that checks every commit for compliance
113    async fn check_commit(&self, commit: &CommitDetails) -> Result<ReviewSuccess, ReviewFailure> {
114        let Some(pr_number) = commit.pr_number() else {
115            return Err(ReviewFailure::NoPullRequestFound);
116        };
117
118        let pull_request = self.github_client.get_pull_request(pr_number).await?;
119
120        if let Some(approval) = self.check_pull_request_approved(&pull_request).await? {
121            return Ok(approval);
122        }
123
124        if let Some(approval) = self
125            .check_approving_pull_request_comment(&pull_request)
126            .await?
127        {
128            return Ok(approval);
129        }
130
131        if let Some(approval) = self.check_commit_co_authors(commit).await? {
132            return Ok(approval);
133        }
134
135        // if let Some(approval) = self.check_external_merged_pr(pr_number).await? {
136        //     return Ok(approval);
137        // }
138
139        Err(ReviewFailure::Unreviewed)
140    }
141
142    async fn check_commit_co_authors(
143        &self,
144        commit: &CommitDetails,
145    ) -> Result<Option<ReviewSuccess>, ReviewFailure> {
146        if commit.co_authors().is_some()
147            && let Some(commit_authors) = self
148                .github_client
149                .get_commit_authors([commit.sha()])
150                .await?
151                .get(commit.sha())
152                .and_then(|authors| authors.co_authors())
153        {
154            let mut org_co_authors = Vec::new();
155            for co_author in commit_authors {
156                if let Some(github_login) = co_author.user()
157                    && self
158                        .github_client
159                        .check_org_membership(github_login)
160                        .await?
161                {
162                    org_co_authors.push(co_author.clone());
163                }
164            }
165
166            Ok(org_co_authors
167                .is_empty()
168                .not()
169                .then_some(ReviewSuccess::CoAuthored(org_co_authors)))
170        } else {
171            Ok(None)
172        }
173    }
174
175    #[allow(unused)]
176    async fn check_external_merged_pr(
177        &self,
178        pull_request: PullRequestData,
179    ) -> Result<Option<ReviewSuccess>, ReviewFailure> {
180        if let Some(user) = pull_request.user
181            && self
182                .github_client
183                .check_org_membership(&GithubLogin::new(user.login))
184                .await?
185                .not()
186        {
187            pull_request.merged_by.map_or(
188                Err(ReviewFailure::UnableToDetermineReviewer),
189                |merged_by| {
190                    Ok(Some(ReviewSuccess::ExternalMergedContribution {
191                        merged_by,
192                    }))
193                },
194            )
195        } else {
196            Ok(None)
197        }
198    }
199
200    async fn check_pull_request_approved(
201        &self,
202        pull_request: &PullRequestData,
203    ) -> Result<Option<ReviewSuccess>, ReviewFailure> {
204        let pr_reviews = self
205            .github_client
206            .get_pull_request_reviews(pull_request.number)
207            .await?;
208
209        if !pr_reviews.is_empty() {
210            let mut org_approving_reviews = Vec::new();
211            for review in pr_reviews {
212                if let Some(github_login) = review.user.as_ref()
213                    && pull_request
214                        .user
215                        .as_ref()
216                        .is_none_or(|pr_user| pr_user.login != github_login.login)
217                    && review
218                        .state
219                        .is_some_and(|state| state == ReviewState::Approved)
220                    && self
221                        .github_client
222                        .check_org_membership(&GithubLogin::new(github_login.login.clone()))
223                        .await?
224                {
225                    org_approving_reviews.push(review);
226                }
227            }
228
229            Ok(org_approving_reviews
230                .is_empty()
231                .not()
232                .then_some(ReviewSuccess::PullRequestReviewed(org_approving_reviews)))
233        } else {
234            Ok(None)
235        }
236    }
237
238    async fn check_approving_pull_request_comment(
239        &self,
240        pull_request: &PullRequestData,
241    ) -> Result<Option<ReviewSuccess>, ReviewFailure> {
242        let other_comments = self
243            .github_client
244            .get_pull_request_comments(pull_request.number)
245            .await?;
246
247        if !other_comments.is_empty() {
248            let mut org_approving_comments = Vec::new();
249
250            for comment in other_comments {
251                if pull_request
252                    .user
253                    .as_ref()
254                    .is_some_and(|pr_author| pr_author.login != comment.user.login)
255                    && comment.body.as_ref().is_some_and(|body| {
256                        body.contains(ZED_ZIPPY_COMMENT_APPROVAL_PATTERN)
257                            || body.contains(ZED_ZIPPY_GROUP_APPROVAL)
258                    })
259                    && self
260                        .github_client
261                        .check_org_membership(&GithubLogin::new(comment.user.login.clone()))
262                        .await?
263                {
264                    org_approving_comments.push(comment);
265                }
266            }
267
268            Ok(org_approving_comments
269                .is_empty()
270                .not()
271                .then_some(ReviewSuccess::ApprovingComment(org_approving_comments)))
272        } else {
273            Ok(None)
274        }
275    }
276
277    pub async fn generate_report(mut self) -> anyhow::Result<Report> {
278        let mut report = Report::new();
279
280        let commits_to_check = std::mem::take(&mut self.commits);
281        let total_commits = commits_to_check.len();
282
283        for (i, commit) in commits_to_check.into_iter().enumerate() {
284            println!(
285                "Checking commit {:?} ({current}/{total})",
286                commit.sha().short(),
287                current = i + 1,
288                total = total_commits
289            );
290
291            let review_result = self.check_commit(&commit).await;
292
293            if let Err(err) = &review_result {
294                println!("Commit {:?} failed review: {:?}", commit.sha().short(), err);
295            }
296
297            report.add(commit, review_result);
298        }
299
300        Ok(report)
301    }
302}
303
304#[cfg(test)]
305mod tests {
306    use std::rc::Rc;
307    use std::str::FromStr;
308
309    use crate::git::{CommitDetails, CommitList, CommitSha};
310    use crate::github::{
311        AuthorsForCommits, GitHubApiClient, GitHubClient, GitHubUser, GithubLogin,
312        PullRequestComment, PullRequestData, PullRequestReview, ReviewState,
313    };
314
315    use super::{Reporter, ReviewFailure, ReviewSuccess};
316
317    struct MockGitHubApi {
318        pull_request: PullRequestData,
319        reviews: Vec<PullRequestReview>,
320        comments: Vec<PullRequestComment>,
321        commit_authors_json: serde_json::Value,
322        org_members: Vec<String>,
323    }
324
325    #[async_trait::async_trait(?Send)]
326    impl GitHubApiClient for MockGitHubApi {
327        async fn get_pull_request(&self, _pr_number: u64) -> anyhow::Result<PullRequestData> {
328            Ok(self.pull_request.clone())
329        }
330
331        async fn get_pull_request_reviews(
332            &self,
333            _pr_number: u64,
334        ) -> anyhow::Result<Vec<PullRequestReview>> {
335            Ok(self.reviews.clone())
336        }
337
338        async fn get_pull_request_comments(
339            &self,
340            _pr_number: u64,
341        ) -> anyhow::Result<Vec<PullRequestComment>> {
342            Ok(self.comments.clone())
343        }
344
345        async fn get_commit_authors(
346            &self,
347            _commit_shas: &[&CommitSha],
348        ) -> anyhow::Result<AuthorsForCommits> {
349            serde_json::from_value(self.commit_authors_json.clone()).map_err(Into::into)
350        }
351
352        async fn check_org_membership(&self, login: &GithubLogin) -> anyhow::Result<bool> {
353            Ok(self
354                .org_members
355                .iter()
356                .any(|member| member == login.as_str()))
357        }
358
359        async fn ensure_pull_request_has_label(
360            &self,
361            _label: &str,
362            _pr_number: u64,
363        ) -> anyhow::Result<()> {
364            Ok(())
365        }
366    }
367
368    fn make_commit(
369        sha: &str,
370        author_name: &str,
371        author_email: &str,
372        title: &str,
373        body: &str,
374    ) -> CommitDetails {
375        let formatted = format!(
376            "{sha}|field-delimiter|{author_name}|field-delimiter|{author_email}|field-delimiter|\
377             {title}|body-delimiter|{body}|commit-delimiter|"
378        );
379        CommitList::from_str(&formatted)
380            .expect("test commit should parse")
381            .into_iter()
382            .next()
383            .expect("should have one commit")
384    }
385
386    fn review(login: &str, state: ReviewState) -> PullRequestReview {
387        PullRequestReview {
388            user: Some(GitHubUser {
389                login: login.to_owned(),
390            }),
391            state: Some(state),
392        }
393    }
394
395    fn comment(login: &str, body: &str) -> PullRequestComment {
396        PullRequestComment {
397            user: GitHubUser {
398                login: login.to_owned(),
399            },
400            body: Some(body.to_owned()),
401        }
402    }
403
404    struct TestScenario {
405        pull_request: PullRequestData,
406        reviews: Vec<PullRequestReview>,
407        comments: Vec<PullRequestComment>,
408        commit_authors_json: serde_json::Value,
409        org_members: Vec<String>,
410        commit: CommitDetails,
411    }
412
413    impl TestScenario {
414        fn single_commit() -> Self {
415            Self {
416                pull_request: PullRequestData {
417                    number: 1234,
418                    user: Some(GitHubUser {
419                        login: "alice".to_owned(),
420                    }),
421                    merged_by: None,
422                },
423                reviews: vec![],
424                comments: vec![],
425                commit_authors_json: serde_json::json!({}),
426                org_members: vec![],
427                commit: make_commit(
428                    "abc12345abc12345",
429                    "Alice",
430                    "alice@test.com",
431                    "Fix thing (#1234)",
432                    "",
433                ),
434            }
435        }
436
437        fn with_reviews(mut self, reviews: Vec<PullRequestReview>) -> Self {
438            self.reviews = reviews;
439            self
440        }
441
442        fn with_comments(mut self, comments: Vec<PullRequestComment>) -> Self {
443            self.comments = comments;
444            self
445        }
446
447        fn with_org_members(mut self, members: Vec<&str>) -> Self {
448            self.org_members = members.into_iter().map(str::to_owned).collect();
449            self
450        }
451
452        fn with_commit_authors_json(mut self, json: serde_json::Value) -> Self {
453            self.commit_authors_json = json;
454            self
455        }
456
457        fn with_commit(mut self, commit: CommitDetails) -> Self {
458            self.commit = commit;
459            self
460        }
461
462        async fn run_scenario(self) -> Result<ReviewSuccess, ReviewFailure> {
463            let mock = MockGitHubApi {
464                pull_request: self.pull_request,
465                reviews: self.reviews,
466                comments: self.comments,
467                commit_authors_json: self.commit_authors_json,
468                org_members: self.org_members,
469            };
470            let client = GitHubClient::new(Rc::new(mock));
471            let reporter = Reporter::new(CommitList::default(), &client);
472            reporter.check_commit(&self.commit).await
473        }
474    }
475
476    #[tokio::test]
477    async fn approved_review_by_org_member_succeeds() {
478        let result = TestScenario::single_commit()
479            .with_reviews(vec![review("bob", ReviewState::Approved)])
480            .with_org_members(vec!["bob"])
481            .run_scenario()
482            .await;
483        assert!(matches!(result, Ok(ReviewSuccess::PullRequestReviewed(_))));
484    }
485
486    #[tokio::test]
487    async fn non_approved_review_state_is_not_accepted() {
488        let result = TestScenario::single_commit()
489            .with_reviews(vec![review("bob", ReviewState::Other)])
490            .with_org_members(vec!["bob"])
491            .run_scenario()
492            .await;
493        assert!(matches!(result, Err(ReviewFailure::Unreviewed)));
494    }
495
496    #[tokio::test]
497    async fn review_by_non_org_member_is_not_accepted() {
498        let result = TestScenario::single_commit()
499            .with_reviews(vec![review("bob", ReviewState::Approved)])
500            .run_scenario()
501            .await;
502        assert!(matches!(result, Err(ReviewFailure::Unreviewed)));
503    }
504
505    #[tokio::test]
506    async fn pr_author_own_approval_review_is_rejected() {
507        let result = TestScenario::single_commit()
508            .with_reviews(vec![review("alice", ReviewState::Approved)])
509            .with_org_members(vec!["alice"])
510            .run_scenario()
511            .await;
512        assert!(matches!(result, Err(ReviewFailure::Unreviewed)));
513    }
514
515    #[tokio::test]
516    async fn pr_author_own_approval_comment_is_rejected() {
517        let result = TestScenario::single_commit()
518            .with_comments(vec![comment("alice", "@zed-zippy approve")])
519            .with_org_members(vec!["alice"])
520            .run_scenario()
521            .await;
522        assert!(matches!(result, Err(ReviewFailure::Unreviewed)));
523    }
524
525    #[tokio::test]
526    async fn approval_comment_by_org_member_succeeds() {
527        let result = TestScenario::single_commit()
528            .with_comments(vec![comment("bob", "@zed-zippy approve")])
529            .with_org_members(vec!["bob"])
530            .run_scenario()
531            .await;
532        assert!(matches!(result, Ok(ReviewSuccess::ApprovingComment(_))));
533    }
534
535    #[tokio::test]
536    async fn group_approval_comment_by_org_member_succeeds() {
537        let result = TestScenario::single_commit()
538            .with_comments(vec![comment("bob", "@zed-industries/approved")])
539            .with_org_members(vec!["bob"])
540            .run_scenario()
541            .await;
542        assert!(matches!(result, Ok(ReviewSuccess::ApprovingComment(_))));
543    }
544
545    #[tokio::test]
546    async fn comment_without_approval_pattern_is_not_accepted() {
547        let result = TestScenario::single_commit()
548            .with_comments(vec![comment("bob", "looks good")])
549            .with_org_members(vec!["bob"])
550            .run_scenario()
551            .await;
552        assert!(matches!(result, Err(ReviewFailure::Unreviewed)));
553    }
554
555    #[tokio::test]
556    async fn commit_without_pr_number_is_no_pr_found() {
557        let result = TestScenario::single_commit()
558            .with_commit(make_commit(
559                "abc12345abc12345",
560                "Alice",
561                "alice@test.com",
562                "Fix thing without PR number",
563                "",
564            ))
565            .run_scenario()
566            .await;
567        assert!(matches!(result, Err(ReviewFailure::NoPullRequestFound)));
568    }
569
570    #[tokio::test]
571    async fn pr_review_takes_precedence_over_comment() {
572        let result = TestScenario::single_commit()
573            .with_reviews(vec![review("bob", ReviewState::Approved)])
574            .with_comments(vec![comment("charlie", "@zed-zippy approve")])
575            .with_org_members(vec!["bob", "charlie"])
576            .run_scenario()
577            .await;
578        assert!(matches!(result, Ok(ReviewSuccess::PullRequestReviewed(_))));
579    }
580
581    #[tokio::test]
582    async fn comment_takes_precedence_over_co_author() {
583        let result = TestScenario::single_commit()
584            .with_comments(vec![comment("bob", "@zed-zippy approve")])
585            .with_commit_authors_json(serde_json::json!({
586                "abc12345abc12345": {
587                    "author": {
588                        "name": "Alice",
589                        "email": "alice@test.com",
590                        "user": { "login": "alice" }
591                    },
592                    "authors": [{
593                        "name": "Charlie",
594                        "email": "charlie@test.com",
595                        "user": { "login": "charlie" }
596                    }]
597                }
598            }))
599            .with_commit(make_commit(
600                "abc12345abc12345",
601                "Alice",
602                "alice@test.com",
603                "Fix thing (#1234)",
604                "Co-authored-by: Charlie <charlie@test.com>",
605            ))
606            .with_org_members(vec!["bob", "charlie"])
607            .run_scenario()
608            .await;
609        assert!(matches!(result, Ok(ReviewSuccess::ApprovingComment(_))));
610    }
611
612    #[tokio::test]
613    async fn co_author_org_member_succeeds() {
614        let result = TestScenario::single_commit()
615            .with_commit_authors_json(serde_json::json!({
616                "abc12345abc12345": {
617                    "author": {
618                        "name": "Alice",
619                        "email": "alice@test.com",
620                        "user": { "login": "alice" }
621                    },
622                    "authors": [{
623                        "name": "Bob",
624                        "email": "bob@test.com",
625                        "user": { "login": "bob" }
626                    }]
627                }
628            }))
629            .with_commit(make_commit(
630                "abc12345abc12345",
631                "Alice",
632                "alice@test.com",
633                "Fix thing (#1234)",
634                "Co-authored-by: Bob <bob@test.com>",
635            ))
636            .with_org_members(vec!["bob"])
637            .run_scenario()
638            .await;
639        assert!(matches!(result, Ok(ReviewSuccess::CoAuthored(_))));
640    }
641
642    #[tokio::test]
643    async fn no_reviews_no_comments_no_coauthors_is_unreviewed() {
644        let result = TestScenario::single_commit().run_scenario().await;
645        assert!(matches!(result, Err(ReviewFailure::Unreviewed)));
646    }
647}