Skip to content

Commit 53e7906

Browse files
authored
Merge pull request #389 from FoamyGuy/requests_timeout
Requests timeout
2 parents 50bd148 + 6adb833 commit 53e7906

13 files changed

+69
-36
lines changed

adabot/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-FileCopyrightText: 2023 Tim Cocks
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""AdaBot is a friendly helper bot that works across the web to make people's
6+
lives better."""
7+
8+
REQUESTS_TIMEOUT = 30

adabot/arduino_libraries.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import requests
1313

14-
from adabot import github_requests as gh_reqs
14+
from adabot import github_requests as gh_reqs, REQUESTS_TIMEOUT
1515

1616
logger = logging.getLogger(__name__)
1717
ch = logging.StreamHandler(stream=sys.stdout)
@@ -86,7 +86,8 @@ def is_arduino_library(repo):
8686
+ repo["name"]
8787
+ "/"
8888
+ repo["default_branch"]
89-
+ "/library.properties"
89+
+ "/library.properties",
90+
timeout=REQUESTS_TIMEOUT,
9091
)
9192
return lib_prop_file.ok
9293

@@ -116,7 +117,8 @@ def validate_library_properties(repo):
116117
+ repo["name"]
117118
+ "/"
118119
+ repo["default_branch"]
119-
+ "/library.properties"
120+
+ "/library.properties",
121+
timeout=REQUESTS_TIMEOUT,
120122
)
121123
if not lib_prop_file.ok:
122124
# print("{} skipped".format(repo["name"]))
@@ -193,7 +195,8 @@ def validate_actions(repo):
193195
+ repo["name"]
194196
+ "/"
195197
+ repo["default_branch"]
196-
+ "/.github/workflows/githubci.yml"
198+
+ "/.github/workflows/githubci.yml",
199+
timeout=REQUESTS_TIMEOUT,
197200
)
198201
return repo_has_actions.ok
199202

@@ -309,7 +312,10 @@ def main(verbosity=1, output_file=None): # pylint: disable=missing-function-doc
309312
logger.setLevel("CRITICAL")
310313

311314
try:
312-
reply = requests.get("http://downloads.arduino.cc/libraries/library_index.json")
315+
reply = requests.get(
316+
"http://downloads.arduino.cc/libraries/library_index.json",
317+
timeout=REQUESTS_TIMEOUT,
318+
)
313319
if not reply.ok:
314320
logging.error(
315321
"Could not fetch http://downloads.arduino.cc/libraries/library_index.json"

adabot/circuitpython_libraries.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import github as pygithub
1818
import requests
1919

20-
from adabot import github_requests as gh_reqs
20+
from adabot import github_requests as gh_reqs, REQUESTS_TIMEOUT
2121
from adabot import pypi_requests as pypi
2222
from adabot.lib import circuitpython_library_validators as cirpy_lib_vals
2323
from adabot.lib import common_funcs
@@ -223,7 +223,7 @@ def run_library_checks(validators, kw_args, error_depth):
223223
resp = requests.get(
224224
"https://raw.githubusercontent.com/adafruit/"
225225
"CircuitPython_Community_Bundle/main/.gitmodules",
226-
timeout=30,
226+
timeout=REQUESTS_TIMEOUT,
227227
)
228228
community_bundle_submodules = resp.text
229229
community_library_count = community_bundle_submodules.count("submodule")

adabot/circuitpython_library_download_stats.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from google.cloud import bigquery
1818
import google.oauth2.service_account
1919

20-
from adabot import github_requests as gh_reqs
20+
from adabot import github_requests as gh_reqs, REQUESTS_TIMEOUT
2121
from adabot.lib import common_funcs
2222

2323
# Setup ArgumentParser
@@ -60,7 +60,7 @@
6060
def retrieve_piwheels_stats():
6161
"""Get data dump of piwheels download stats"""
6262
stats = {}
63-
response = requests.get(PIWHEELS_PACKAGES_URL)
63+
response = requests.get(PIWHEELS_PACKAGES_URL, timeout=REQUESTS_TIMEOUT)
6464
if response.ok:
6565
packages = response.json()
6666
stats = {

adabot/circuitpython_library_patches.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import sh
1414
from sh.contrib import git
1515

16+
from adabot import REQUESTS_TIMEOUT
1617
from adabot.lib import common_funcs
1718

1819

@@ -100,7 +101,8 @@ def get_patches(run_local):
100101
return_list = []
101102
if not run_local:
102103
contents = requests.get(
103-
"https://api.github.com/repos/adafruit/adabot/contents/patches"
104+
"https://api.github.com/repos/adafruit/adabot/contents/patches",
105+
timeout=REQUESTS_TIMEOUT
104106
)
105107
if contents.ok:
106108
for patch in contents.json():

adabot/circuitpython_library_release.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def make_release(new_tag, logger, test_run=False):
4848

4949
def get_pypi_name():
5050
"""
51-
return the shorthand pypi project name
51+
return the shorthand project name used for pypi, docs, etc.
5252
"""
5353
data = toml.load("pyproject.toml")
5454

@@ -126,7 +126,7 @@ def get_release_info():
126126
}
127127

128128

129-
def get_compare_url(tag_name):
129+
def get_compare_url(tag_name, compare_to_tag_name="main"):
130130
"""
131131
Get the URL to the GitHub compare page for the latest release compared
132132
to current main.
@@ -138,7 +138,9 @@ def get_compare_url(tag_name):
138138
if not remote_url.startswith("https"):
139139
return "Sorry, Unknown Remotes"
140140

141-
compare_url = remote_url.replace(".git", f"/compare/{tag_name}...main")
141+
compare_url = remote_url.replace(
142+
".git", f"/compare/{tag_name}...{compare_to_tag_name}"
143+
)
142144
return compare_url
143145

144146

adabot/lib/assign_hacktober_label.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import datetime
1212
import requests
1313

14-
from adabot import github_requests as gh_reqs
14+
from adabot import github_requests as gh_reqs, REQUESTS_TIMEOUT
1515
from adabot.lib import common_funcs
1616

1717
cli_args = argparse.ArgumentParser(description="Hacktoberfest Label Assigner")
@@ -73,7 +73,9 @@ def get_open_issues(repo):
7373
)
7474

7575
if response.links.get("next"):
76-
response = requests.get(response.links["next"]["url"])
76+
response = requests.get(
77+
response.links["next"]["url"], timeout=REQUESTS_TIMEOUT
78+
)
7779
else:
7880
break
7981

adabot/lib/circuitpython_library_validators.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import parse
2222

2323
import github as pygithub
24-
from adabot import github_requests as gh_reqs
24+
from adabot import github_requests as gh_reqs, REQUESTS_TIMEOUT
2525
from adabot.lib import common_funcs
2626
from adabot.lib import assign_hacktober_label as hacktober
2727

@@ -231,7 +231,7 @@ def rtd_yml_base(self):
231231
"%20if%20cookiecutter.sphinx_docs%20in%20%5B'y'%2C%20'yes'%5D%20%25"
232232
"%7D.readthedocs.yaml%7B%25%20endif%20%25%7D"
233233
)
234-
rtd_yml = requests.get(rtd_yml_dl_url)
234+
rtd_yml = requests.get(rtd_yml_dl_url, timeout=REQUESTS_TIMEOUT)
235235
if rtd_yml.ok:
236236
try:
237237
self._rtd_yaml_base = yaml.safe_load(rtd_yml.text)
@@ -255,7 +255,7 @@ def pcc_versions(self):
255255
"circuitpython/main/%7B%7B%20cookiecutter.__dirname%20%7D%7D/.pre-"
256256
"commit-config.yaml"
257257
)
258-
pcc_yml = requests.get(pcc_yml_dl_url)
258+
pcc_yml = requests.get(pcc_yml_dl_url, timeout=REQUESTS_TIMEOUT)
259259
if pcc_yml.ok:
260260
try:
261261
pcc_yaml_base = yaml.safe_load(pcc_yml.text)
@@ -463,7 +463,7 @@ def _filter_file_diffs(filenames):
463463
def _validate_readme(self, download_url):
464464
# We use requests because file contents are hosted by
465465
# githubusercontent.com, not the API domain.
466-
contents = requests.get(download_url, timeout=30)
466+
contents = requests.get(download_url, timeout=REQUESTS_TIMEOUT)
467467
if not contents.ok:
468468
return [ERROR_README_DOWNLOAD_FAILED]
469469

@@ -509,7 +509,7 @@ def _validate_py_for_u_modules(self, download_url):
509509
"""
510510
# We use requests because file contents are hosted by
511511
# githubusercontent.com, not the API domain.
512-
contents = requests.get(download_url, timeout=30)
512+
contents = requests.get(download_url, timeout=REQUESTS_TIMEOUT)
513513
if not contents.ok:
514514
return [ERROR_PYFILE_DOWNLOAD_FAILED]
515515

@@ -547,7 +547,7 @@ def _validate_actions_build_yml(self, actions_build_info):
547547
"""
548548

549549
download_url = actions_build_info["download_url"]
550-
contents = requests.get(download_url, timeout=30)
550+
contents = requests.get(download_url, timeout=REQUESTS_TIMEOUT)
551551
if not contents.ok:
552552
return [ERROR_PYFILE_DOWNLOAD_FAILED]
553553

@@ -557,7 +557,7 @@ def _validate_actions_build_yml(self, actions_build_info):
557557

558558
def _validate_pre_commit_config_yaml(self, file_info):
559559
download_url = file_info["download_url"]
560-
contents = requests.get(download_url, timeout=30)
560+
contents = requests.get(download_url, timeout=REQUESTS_TIMEOUT)
561561
if not contents.ok:
562562
return [ERROR_PYFILE_DOWNLOAD_FAILED]
563563

@@ -594,15 +594,15 @@ def _validate_pre_commit_config_yaml(self, file_info):
594594
def _validate_pyproject_toml(self, file_info):
595595
"""Check pyproject.toml for pypi compatibility"""
596596
download_url = file_info["download_url"]
597-
contents = requests.get(download_url, timeout=30)
597+
contents = requests.get(download_url, timeout=REQUESTS_TIMEOUT)
598598
if not contents.ok:
599599
return [ERROR_TOMLFILE_DOWNLOAD_FAILED]
600600
return []
601601

602602
def _validate_requirements_txt(self, repo, file_info, check_blinka=True):
603603
"""Check requirements.txt for pypi compatibility"""
604604
download_url = file_info["download_url"]
605-
contents = requests.get(download_url, timeout=30)
605+
contents = requests.get(download_url, timeout=REQUESTS_TIMEOUT)
606606
if not contents.ok:
607607
return [ERROR_PYFILE_DOWNLOAD_FAILED]
608608

@@ -717,7 +717,9 @@ def validate_contents(self, repo):
717717
if ".readthedocs.yaml" in files:
718718
filename = ".readthedocs.yaml"
719719
file_info = content_list[files.index(filename)]
720-
rtd_contents = requests.get(file_info["download_url"])
720+
rtd_contents = requests.get(
721+
file_info["download_url"], timeout=REQUESTS_TIMEOUT
722+
)
721723
if rtd_contents.ok:
722724
try:
723725
rtd_yml = yaml.safe_load(rtd_contents.text)
@@ -735,7 +737,9 @@ def validate_contents(self, repo):
735737
if len(self._pcc_versions) or self.pcc_versions != "":
736738
filename = ".pre-commit-config.yaml"
737739
file_info = content_list[files.index(filename)]
738-
pcc_contents = requests.get(file_info["download_url"])
740+
pcc_contents = requests.get(
741+
file_info["download_url"], timeout=REQUESTS_TIMEOUT
742+
)
739743
if pcc_contents.ok:
740744
try:
741745
pcc_yml = yaml.safe_load(pcc_contents.text)
@@ -888,7 +892,8 @@ def validate_readthedocs(self, repo):
888892
return []
889893
if not self.rtd_subprojects:
890894
rtd_response = requests.get(
891-
"https://readthedocs.org/api/v2/project/74557/subprojects/", timeout=15
895+
"https://readthedocs.org/api/v2/project/74557/subprojects/",
896+
timeout=REQUESTS_TIMEOUT,
892897
)
893898
if not rtd_response.ok:
894899
return [ERROR_RTD_SUBPROJECT_FAILED]
@@ -937,7 +942,7 @@ def validate_readthedocs(self, repo):
937942
url = f"https://readthedocs.org/api/v3/projects/{rtd_slug}/builds/"
938943
rtd_token = os.environ["RTD_TOKEN"]
939944
headers = {"Authorization": f"token {rtd_token}"}
940-
response = requests.get(url, headers=headers)
945+
response = requests.get(url, headers=headers, timeout=REQUESTS_TIMEOUT)
941946
json_response = response.json()
942947

943948
error_message = json_response.get("detail")
@@ -981,7 +986,7 @@ def validate_core_driver_page(self, repo):
981986
"https://raw.githubusercontent.com/adafruit/Adafruit_CircuitPython_Bundle/"
982987
"main/docs/drivers.rst"
983988
),
984-
timeout=15,
989+
timeout=REQUESTS_TIMEOUT,
985990
)
986991
if not driver_page.ok:
987992
return [ERROR_DRIVERS_PAGE_DOWNLOAD_FAILED]

adabot/lib/common_funcs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import os
1313
import re
1414
import requests
15-
from adabot import github_requests as gh_reqs
15+
from adabot import github_requests as gh_reqs, REQUESTS_TIMEOUT
1616
from adabot import pypi_requests as pypi
1717

1818
CORE_REPO_URL = "/repos/adafruit/circuitpython"
@@ -96,7 +96,7 @@ def get_bundle_submodules():
9696
# master branch of the bundle is the canonical source of the bundle release.
9797
result = requests.get(
9898
"https://raw.githubusercontent.com/adafruit/Adafruit_CircuitPython_Bundle/main/.gitmodules",
99-
timeout=15,
99+
timeout=REQUESTS_TIMEOUT,
100100
)
101101
if result.status_code != 200:
102102
# output_handler("Failed to access bundle .gitmodules file from GitHub!", quiet=True)

adabot/pypi_requests.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import requests
1111

12+
from adabot import REQUESTS_TIMEOUT
13+
1214

1315
def _fix_url(url):
1416
if url.startswith("/"):
@@ -18,4 +20,4 @@ def _fix_url(url):
1820

1921
def get(url, **kwargs):
2022
"""Process a GET request from pypi.org"""
21-
return requests.get(_fix_url(url), timeout=30, **kwargs)
23+
return requests.get(_fix_url(url), timeout=REQUESTS_TIMEOUT, **kwargs)

tools/docs_status.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import requests
2222
from github.Repository import Repository
2323
from github.ContentFile import ContentFile
24+
2425
from iterate_libraries import (
2526
iter_remote_bundle_with_func,
2627
RemoteLibFunc_IterResult,
2728
)
29+
from adabot import REQUESTS_TIMEOUT
2830

2931

3032
def check_docs_status(
@@ -64,7 +66,7 @@ def check_docs_status(
6466
# GET the latest documentation build runs
6567
url = f"https://readthedocs.org/api/v3/projects/{rtd_slug}/builds/"
6668
headers = {"Authorization": f"token {rtd_token}"}
67-
response = requests.get(url, headers=headers)
69+
response = requests.get(url, headers=headers, timeout=REQUESTS_TIMEOUT)
6870
json_response: dict[str, Any] = response.json()
6971

7072
# Return the results of the latest run

tools/file_compare.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import requests
2121
from requests.structures import CaseInsensitiveDict
2222

23+
from adabot import REQUESTS_TIMEOUT
2324
from adabot.lib.common_funcs import list_repos
2425

2526

@@ -54,9 +55,9 @@ def compare(git_file: str, token: Optional[str] = None) -> list:
5455
headers = CaseInsensitiveDict()
5556
headers["Authorization"] = f"token {token}"
5657

57-
resp = requests.get(url, headers=headers)
58+
resp = requests.get(url, headers=headers, timeout=REQUESTS_TIMEOUT)
5859
else:
59-
resp = requests.get(url)
60+
resp = requests.get(url, timeout=REQUESTS_TIMEOUT)
6061

6162
if resp.status_code != 200:
6263
print(name)

tools/find_text.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import requests
2121

22+
from adabot import REQUESTS_TIMEOUT
2223
from adabot.lib.common_funcs import list_repos
2324

2425
argumentList = sys.argv[1:]
@@ -129,7 +130,9 @@ def prettyprint(info, results):
129130

130131
for repo in all_repos:
131132
INFO = "getting {} for: {}".format(FILE, repo["name"])
132-
response = requests.get(URL_TEMPLATE.format(repo["name"], FILE))
133+
response = requests.get(
134+
URL_TEMPLATE.format(repo["name"], FILE), timeout=REQUESTS_TIMEOUT
135+
)
133136
result = []
134137
if response.status_code == 404:
135138
RESULTS["file_not_found"].append(repo["html_url"])

0 commit comments

Comments
 (0)