diff --git a/reusable_workflows/check_cla/check_cla_issue.py b/reusable_workflows/check_cla/check_cla_issue.py index 1994659..3a9abbf 100644 --- a/reusable_workflows/check_cla/check_cla_issue.py +++ b/reusable_workflows/check_cla/check_cla_issue.py @@ -20,7 +20,7 @@ def main() -> None: cla_signed = cla.check_if_cla_signed(issue, user) if not cla_signed: - cla.leave_failed_comment_on_issue(issue) + cla.comment_on_issue(issue) else: cla.handle_cla_signed(issue, user) diff --git a/reusable_workflows/check_cla/check_cla_pr.py b/reusable_workflows/check_cla/check_cla_pr.py index dde810e..f6f9533 100644 --- a/reusable_workflows/check_cla/check_cla_pr.py +++ b/reusable_workflows/check_cla/check_cla_pr.py @@ -12,30 +12,33 @@ APPROVED_LABEL = "cla:agreed" GH_WORKFLOW_LABEL = "cla:gh-wf-pending" +# keep all old bot names for backwards compatibility +CLA_BOT_NAMES = ["cla-idx-bot[bot]", "sa-github-api", "dfinity-droid-prod[bot]"] + class CLAHandler: def __init__(self, gh: github3.login) -> None: self.cla_repo = gh.repository(owner="dfinity", repository="cla") self.cla_link = f"{self.cla_repo.html_url}/blob/main/CLA.md" - @staticmethod - def check_if_comment_already_exists( - search_comment: str, comments: github3.structs.GitHubIterator + def check_comment_already_exists( + self, comments: github3.structs.GitHubIterator ) -> bool: for comment in comments: - if search_comment == comment.body: + if comment.user.login in CLA_BOT_NAMES: return True return False - def leave_failed_comment_on_issue(self, issue: GHIssue) -> None: + def comment_on_issue(self, issue: GHIssue): # check if bot has already left a message to avoid spam issue_comments = issue.comments() - if not self.check_if_comment_already_exists(messages.FAILED_COMMENT, issue_comments): + bot_comment = self.check_comment_already_exists(issue_comments) + if not bot_comment: issue.create_comment(messages.FAILED_COMMENT) - def comment_on_pr(self, pr: GHPullRequest, pr_comment: str) -> None: - pr_comments = pr.comments() - if not self.check_if_comment_already_exists(pr_comment, pr_comments): + def comment_on_pr(self, pr: GHPullRequest, pr_comment): + bot_comment = self.check_comment_already_exists(pr.issue_comments()) + if not bot_comment: pr.create_comment(pr_comment) def check_if_cla_signed(self, issue: GHIssue, user: str) -> bool: @@ -53,7 +56,7 @@ def check_if_cla_signed(self, issue: GHIssue, user: str) -> bool: def get_cla_issue(self, user: str) -> Optional[GHIssue]: for issue in self.cla_repo.issues(): - if issue.title == f"cla: @{user}": + if issue.title == f"cla: @{user}" and issue.user.login in CLA_BOT_NAMES: return issue print(f"No CLA issue for {user}") return None # to make linter happy diff --git a/reusable_workflows/tests/test_cla_issue.py b/reusable_workflows/tests/test_cla_issue.py index 2ac4150..5ce390b 100644 --- a/reusable_workflows/tests/test_cla_issue.py +++ b/reusable_workflows/tests/test_cla_issue.py @@ -33,7 +33,7 @@ def test_end_to_end_cla_signed(cla_mock, gh_login_mock): main() cla.check_if_cla_signed.assert_called_with(issue, "username") - cla.leave_failed_comment_on_issue.assert_not_called() + cla.comment_on_issue.assert_not_called() cla.handle_cla_signed.assert_called_once() @@ -56,7 +56,7 @@ def test_end_to_end_cla_not_signed(cla_mock, gh_login_mock): main() cla.check_if_cla_signed.assert_called_with(issue, "username") - cla.leave_failed_comment_on_issue.assert_called_once() + cla.comment_on_issue.assert_called_once() cla.handle_cla_signed.assert_not_called() diff --git a/reusable_workflows/tests/test_cla_pr.py b/reusable_workflows/tests/test_cla_pr.py index 1ab3621..65bff83 100644 --- a/reusable_workflows/tests/test_cla_pr.py +++ b/reusable_workflows/tests/test_cla_pr.py @@ -6,7 +6,6 @@ from shared.messages import ( AGREED_MESSAGE, CLA_AGREEMENT_MESSAGE, - FAILED_COMMENT, USER_AGREEMENT_MESSAGE, ) from check_cla.check_cla_pr import CLAHandler, main @@ -29,14 +28,15 @@ def test_bot_comment_exists(): cla = CLAHandler(mock.Mock()) comments_iterator = mock.Mock() comment1 = mock.Mock() - comment1.body = "comment1" + comment1.user.login = "username" comment2 = mock.Mock() - comment2.body = "comment2" + comment2.user.login = "sa-github-api" comments_iterator.__iter__ = mock.Mock( - return_value=iter([comment1, comment2]) + return_value=iter([comment1, comment2, comment1]) ) + # comments_iterator.return_value = [comment1, comment2, comment1] - bot_comment = cla.check_if_comment_already_exists("comment1", comments_iterator) + bot_comment = cla.check_comment_already_exists(comments_iterator) assert bot_comment is True @@ -45,25 +45,14 @@ def test_no_bot_comment(): cla = CLAHandler(mock.Mock()) issue_comments = mock.Mock() comment1 = mock.Mock() - comment1.body = "comment" - issue_comments.__iter__ = mock.Mock(return_value=iter([comment1])) + comment1.user.login = "username" + issue_comments.__iter__ = mock.Mock(return_value=iter([comment1, comment1])) - bot_comment = cla.check_if_comment_already_exists("comment2", issue_comments) + bot_comment = cla.check_comment_already_exists(issue_comments) assert bot_comment is False -def test_leave_failed_comment_on_issue(): - cla = CLAHandler(mock.Mock()) - issue = mock.Mock() - issue.comments.return_value = mock.Mock() - cla.check_if_comment_already_exists = mock.Mock(return_value=False) - - cla.leave_failed_comment_on_issue(issue) - - issue.create_comment.assert_called_once_with(FAILED_COMMENT) - - def test_cla_is_signed(capfd): cla = CLAHandler(mock.Mock()) issue = mock.Mock() @@ -99,6 +88,7 @@ def test_cla_is_not_signed(capfd): cla = CLAHandler(mock.Mock()) issue = mock.Mock() comment = mock.Mock() + comment.user.login = "bot" issue.comments.return_value = [mock.Mock(), comment] response = cla.check_if_cla_signed(issue, "username") @@ -113,6 +103,7 @@ def test_get_cla_issue_success(): cla_repo = mock.Mock() issue = mock.Mock() issue.title = "cla: @username" + issue.user.login = "sa-github-api" cla_repo.issues.return_value = [mock.Mock(), issue] cla.cla_repo = cla_repo