diff --git a/infra/pr_helper.py b/infra/pr_helper.py index 83d66f16e851..8a0fb53c253d 100644 --- a/infra/pr_helper.py +++ b/infra/pr_helper.py @@ -130,14 +130,25 @@ def main(): # Checks if the author is in the contact list. if email: if is_known_contributor(content_dict, email): - # Checks if the email is verified. - verified_marker = ' (verified)' if verified else '' - message += ( - f'{pr_author}{verified_marker} is either the primary contact or ' - f'is in the CCs list of [{project_path}]({project_url}).
') if verified: - continue - + # The email is trustworthy: it is either the author's + # account-linked public profile email, or the first commit's + # author email with a GitHub-verified commit signature. + message += ( + f'{pr_author} (verified) is either the primary contact or ' + f'is in the CCs list of [{project_path}]({project_url}).
') + continue + # The email comes only from the author field of the first commit, + # which is metadata set by the committer and proves nothing about + # identity. Do not assert contact status based on it. + message += ( + f'The author email of the first commit of this PR matches an ' + f'entry in the contact list of [{project_path}]({project_url}), ' + f'but it could not be verified. Commit author emails are set ' + f'by the committer and are not proof of identity, so this ' + f'match is not treated as coming from the primary contact or ' + f'the CCs list.
') + # Checks the previous commits. commit_sha = github.has_author_modified_project(project_path) if commit_sha is None: @@ -204,8 +215,15 @@ def get_projects_path(self): def get_author_email(self): """Retrieves the author's email address for a pull request, - including non-public emails.""" - user_response = requests.get(f'{API_URL}/users/{self._pr_author}') + including non-public emails. + + Returns a (verified, email) pair. |verified| is True only when the + email is trustworthy: the account-linked public profile email, or + the first commit's author email when GitHub verified that commit's + signature. An unverified commit author email is raw metadata set by + the committer and must not be treated as proof of identity. + """ + user_response = requests.get(f'{API_URL}/users/{self._pr_author}') if user_response.ok: email = user_response.json()['email'] if email: diff --git a/infra/pr_helper_test.py b/infra/pr_helper_test.py index 36cbad8b2a33..9f1d99f1236b 100644 --- a/infra/pr_helper_test.py +++ b/infra/pr_helper_test.py @@ -247,5 +247,141 @@ def test_no_match(self): 'user@example.com')) +class _FakeGithubHandler: + """Minimal stand-in for GithubHandler to drive pr_helper.main().""" + + def __init__(self, verified, email, project_yaml, previous_commit_sha): + self._verified = verified + self._email = email + self._project_yaml = project_yaml + self._previous_commit_sha = previous_commit_sha + self.history_checked = False + + def is_author_internal_member(self): + """The fake author is never an internal member.""" + return False + + def get_pr_author(self): + """Returns the fake author's login.""" + return 'someauthor' + + def get_projects_path(self): + """Returns a single fake project path.""" + return ['projects/example'] + + def get_author_email(self): + """Returns the configured (verified, email) pair.""" + return self._verified, self._email + + def get_project_yaml(self, project_path): + """Returns the configured project.yaml content.""" + del project_path # Unused. + return self._project_yaml + + def has_author_modified_project(self, project_path): + """Records the check and returns the configured previous commit.""" + del project_path # Unused. + self.history_checked = True + return self._previous_commit_sha + + def get_past_contributors(self, project_path): + """Returns no past contributors.""" + del project_path # Unused. + return [] + + def get_pull_request_number(self, commit): + """Returns None: the fake previous commit has no associated PR.""" + del commit # Unused. + + +class MainContactVerificationTest(unittest.TestCase): + """Verify main() only asserts contact status for verified emails. + + A PR author's email can come from the author field of the PR's first + commit, which is metadata set by the committer and proves nothing about + identity. When that unverified email happens to match a project's + contact list, the bot must not claim the author is the primary contact + or a CC, and the match must not by itself make the PR ready to merge. + """ + + _PROJECT_YAML = { + 'primary_contact': 'owner@example.com', + 'vendor_ccs': [], + 'auto_ccs': ['cc@example.com'], + } + + def _run_main(self, handler): + """Runs pr_helper.main() with a fake handler, capturing save_env.""" + saved = {} + + def fake_save_env(message, is_ready_for_merge, is_internal=False): + saved['message'] = message + saved['is_ready_for_merge'] = is_ready_for_merge + saved['is_internal'] = is_internal + + with mock.patch.object(pr_helper, 'GithubHandler', return_value=handler): + with mock.patch.object(pr_helper, 'save_env', fake_save_env): + pr_helper.main() + return saved + + def test_verified_contact_is_asserted_and_ready(self): + """Verified contact match is asserted and skips the history check.""" + handler = _FakeGithubHandler(True, 'owner@example.com', self._PROJECT_YAML, + None) + saved = self._run_main(handler) + self.assertIn('is either the primary contact', saved['message']) + self.assertIn('(verified)', saved['message']) + self.assertNotIn('could not be verified', saved['message']) + self.assertTrue(saved['is_ready_for_merge']) + self.assertFalse(handler.history_checked) + + def test_unverified_contact_match_is_not_asserted(self): + """Unverified commit email matching primary_contact is not asserted.""" + handler = _FakeGithubHandler(False, 'owner@example.com', self._PROJECT_YAML, + None) + saved = self._run_main(handler) + self.assertNotIn('is either the primary contact', saved['message']) + self.assertIn('could not be verified', saved['message']) + self.assertFalse(saved['is_ready_for_merge']) + + def test_unverified_cc_match_is_not_asserted(self): + """Unverified commit email matching auto_ccs is not asserted either.""" + handler = _FakeGithubHandler(False, 'cc@example.com', self._PROJECT_YAML, + None) + saved = self._run_main(handler) + self.assertNotIn('is either the primary contact', saved['message']) + self.assertIn('could not be verified', saved['message']) + self.assertFalse(saved['is_ready_for_merge']) + + def test_unverified_match_with_history_ready_via_history_only(self): + """With a prior commit, readiness comes from history, not the match.""" + handler = _FakeGithubHandler(False, 'owner@example.com', self._PROJECT_YAML, + 'abc123') + saved = self._run_main(handler) + self.assertNotIn('is either the primary contact', saved['message']) + self.assertIn('could not be verified', saved['message']) + self.assertIn('has previously contributed', saved['message']) + self.assertTrue(saved['is_ready_for_merge']) + + def test_non_contact_past_contributor_unchanged(self): + """A non-contact author with history keeps the previous behaviour.""" + handler = _FakeGithubHandler(False, 'stranger@example.com', + self._PROJECT_YAML, 'abc123') + saved = self._run_main(handler) + self.assertIn('has previously contributed', saved['message']) + self.assertNotIn('primary contact', saved['message']) + self.assertNotIn('could not be verified', saved['message']) + self.assertTrue(saved['is_ready_for_merge']) + + def test_non_contact_new_contributor_unchanged(self): + """A non-contact author without history is still not ready.""" + handler = _FakeGithubHandler(False, 'stranger@example.com', + self._PROJECT_YAML, None) + saved = self._run_main(handler) + self.assertIn('is a new contributor', saved['message']) + self.assertNotIn('could not be verified', saved['message']) + self.assertFalse(saved['is_ready_for_merge']) + + if __name__ == '__main__': unittest.main()