From c4e1361220ddf2abab7355ba9e36efbacff52ebc Mon Sep 17 00:00:00 2001
From: Holly Gong <39108850+hogo6002@users.noreply.github.com>
Date: Tue, 11 Jul 2023 00:30:47 +1000
Subject: [PATCH] Add past contributors for pr helper (#10664)
---
infra/pr_helper.py | 36 +++++++++++++++++++++++++++++++++++-
1 file changed, 35 insertions(+), 1 deletion(-)
diff --git a/infra/pr_helper.py b/infra/pr_helper.py
index 871d08ed8..b0de91390 100644
--- a/infra/pr_helper.py
+++ b/infra/pr_helper.py
@@ -31,6 +31,7 @@ API_URL = 'https://api.github.com'
BASE_URL = f'{API_URL}/repos/{OWNER}/{REPO}'
BRANCH = 'master'
CRITICALITY_SCORE_PATH = '/home/runner/go/bin/criticality_score'
+COMMITS_LIMIT = 50 # Only process the most recent 50 commits.
def get_criticality_score(repo_url):
@@ -118,10 +119,15 @@ def main():
# Checks the previous commits.
commit_sha = github.has_author_modified_project(project_path)
if commit_sha is None:
+ history_message = ''
+ contributors = github.get_past_contributors(project_path)
+ if contributors:
+ history_message = 'The past contributors are: '
+ history_message += ', '.join(contributors)
message += (
f'{pr_author} is a new contributor to '
f'[{project_path}]({project_url}). The PR must be approved by known '
- 'contributors before it can be merged.
')
+ f'contributors before it can be merged. {history_message}
')
is_ready_for_merge = False
continue
@@ -223,6 +229,34 @@ class GithubHandler:
return None
return pr_response.json()[0]['number']
+ def get_past_contributors(self, project_path):
+ """Returns a list of past contributors of a certain project."""
+ commits_response = requests.get(f'{BASE_URL}/commits?path={project_path}',
+ headers=self._headers)
+
+ if not commits_response.ok:
+ return []
+ commits = commits_response.json()
+ contributors: dict[str, bool] = {}
+ for i, commit in enumerate(commits):
+ if i >= COMMITS_LIMIT:
+ break
+
+ login = commit['author']['login']
+ verified = commit['commit']['verification']['verified']
+ if login not in contributors:
+ contributors[login] = verified
+ if verified:
+ # Override previous verification bit.
+ contributors[login] = True
+
+ all_contributors = []
+ for login, verified in contributors.items():
+ login_verify = login if verified else f'{login} (unverified)'
+ all_contributors.append(login_verify)
+
+ return all_contributors
+
def is_author_internal_member(self):
"""Returns if the author is an internal member."""
response = requests.get(f'{BASE_URL}/contents/infra/MAINTAINERS.csv',