Skip to content

Commit 9592863

Browse files
authored
Merge pull request #306 from chaoss/fix/disabled_prs
Bypass collecting pull request data when PRs are disabled
2 parents 001eaf5 + 0496029 commit 9592863

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

collectoss/tasks/github/facade_github/contributor_interfaceable/contributor_interface.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def request_dict_from_endpoint(logger, session, url, timeout_wait=10):
107107

108108
return response_data
109109

110-
110+
@deprecated("Please use GithubDataAcess.endpoint_url() instead")
111111
def create_endpoint_from_email(email):
112112
# Note: I added "+type:user" to avoid having user owned organizations be returned
113113
# Also stopped splitting per note above.
@@ -117,7 +117,7 @@ def create_endpoint_from_email(email):
117117

118118
return url
119119

120-
120+
@deprecated("Please use GithubDataAcess.endpoint_url() instead")
121121
def create_endpoint_from_commit_sha(logger, commit_sha, repo_id):
122122
logger.debug(
123123
f"Trying to create endpoint from commit hash: {commit_sha}")

collectoss/tasks/github/pull_requests/tasks.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,15 @@ def retrieve_all_pr_data(repo_git: str, logger, key_auth, since): #-> Generator[
7575

7676
logger.debug(f"Collecting pull requests for {owner}/{repo}")
7777

78-
url = f"https://api.github.com/repos/{owner}/{repo}/pulls?state=all&direction=desc&sort=updated"
79-
8078
github_data_access = GithubDataAccess(key_auth, logger)
8179

80+
search_args = {"state": "all", "direction": "desc", "sort": "updated"}
81+
url = github_data_access.endpoint_url(f"repos/{owner}/{repo}/pulls", search_args)
82+
83+
if not github_data_access.check_prs_enabled(owner, repo):
84+
logger.info(f"{owner}/{repo}: Pull requests appear to be disabled for this repo. Skipping.")
85+
return
86+
8287
num_pages = github_data_access.get_resource_page_count(url)
8388

8489
logger.debug(f"{owner}/{repo}: Retrieving {num_pages} pages of pull requests")

collectoss/tasks/github/util/github_data_access.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ def __init__(self, key_manager, logger: logging.Logger, feature="rest"):
4444
self.key = None
4545
self.expired_keys_for_request = []
4646

47+
def endpoint_url(self, path: str, params: dict = None) -> str:
48+
"""Build a URL for a github endpoint using the specified path and query parameters
49+
50+
Args:
51+
path (str): the path to use (i.e. "/users/MoralCode")
52+
params (dict): optional query parameters to add to the url, as a dict
53+
54+
Returns:
55+
str: the full URL to the specified resource.
56+
"""
57+
# using pythons url processing library helps handle accidental
58+
# inclusion of query parameters in the path string, ensuring all query
59+
# parameters are properly encoded and escaped
60+
61+
if not path.startswith("/"):
62+
path = "/" + path
63+
64+
url = "https://api.github.com" + path
65+
66+
return self.__add_query_params(url, params or {})
67+
4768
def get_resource_count(self, url):
4869

4970
# set per_page to 100 explicitly so we know each page is 100 long
@@ -60,6 +81,20 @@ def get_resource_count(self, url):
6081

6182
return (100 * (num_pages -1)) + len(data)
6283

84+
def check_prs_enabled(self, owner: str, repo: str,) -> bool:
85+
"""
86+
Checks whether pull requests are enabled for a repository.
87+
Returns False if PRs are disabled (404 on /pulls) and true if there are PRs.
88+
"""
89+
try:
90+
url = self.endpoint_url(f"repos/{owner}/{repo}/pulls", {"per_page": "1"})
91+
self.get_resource_page_count(url)
92+
return True
93+
except UrlNotFoundException:
94+
self.logger.info(f"{owner}/{repo}: Pull requests are disabled. Skipping PR collection.")
95+
return False
96+
97+
6398
def paginate_resource(self, url):
6499

65100
response = self.make_request_with_retries(url)

0 commit comments

Comments
 (0)