Skip to content

Commit 26eb86d

Browse files
authored
Merge pull request #102 from tannewt/hide_api_key
Replace api key in error message
2 parents fc4488a + 27a3f2f commit 26eb86d

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

adabot/github_requests.py

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@
3131
import datetime
3232
import os
3333
import requests
34+
import sys
3435
import time
36+
import traceback
3537

38+
TIMEOUT = 60
3639

3740
def _fix_url(url):
3841
if url.startswith("/"):
@@ -57,7 +60,17 @@ def _fix_kwargs(kwargs):
5760
return kwargs
5861

5962
def get(url, **kwargs):
60-
response = requests.get(_fix_url(url), timeout=30, **_fix_kwargs(kwargs))
63+
ok = True
64+
try:
65+
response = requests.get(_fix_url(url), timeout=TIMEOUT, **_fix_kwargs(kwargs))
66+
except Exception as e:
67+
exception_text = traceback.format_exc()
68+
if "ADABOT_GITHUB_ACCESS_TOKEN" in os.environ:
69+
exception_text = exception_text.replace(os.environ["ADABOT_GITHUB_ACCESS_TOKEN"], "[secure]")
70+
print(exception_text, file=sys.stderr)
71+
ok = False
72+
if not ok:
73+
raise RuntimeError("See print for error text that as been sanitized for secrets")
6174
if "X-RateLimit-Remaining" in response.headers:
6275
remaining = int(response.headers["X-RateLimit-Remaining"])
6376
if remaining <= 1:
@@ -78,13 +91,41 @@ def get(url, **kwargs):
7891
return response
7992

8093
def post(url, **kwargs):
81-
return requests.post(_fix_url(url), timeout=30, **_fix_kwargs(kwargs))
94+
try:
95+
return requests.post(_fix_url(url), timeout=TIMEOUT, **_fix_kwargs(kwargs))
96+
except Exception as e:
97+
exception_text = traceback.format_exc()
98+
if "ADABOT_GITHUB_ACCESS_TOKEN" in os.environ:
99+
exception_text = exception_text.replace(os.environ["ADABOT_GITHUB_ACCESS_TOKEN"], "[secure]")
100+
print(exception_text, file=sys.stderr)
101+
raise RuntimeError("See print for error text that as been sanitized for secrets")
82102

83103
def put(url, **kwargs):
84-
return requests.put(_fix_url(url), timeout=30, **_fix_kwargs(kwargs))
104+
try:
105+
return requests.put(_fix_url(url), timeout=TIMEOUT, **_fix_kwargs(kwargs))
106+
except Exception as e:
107+
exception_text = traceback.format_exc()
108+
if "ADABOT_GITHUB_ACCESS_TOKEN" in os.environ:
109+
exception_text = exception_text.replace(os.environ["ADABOT_GITHUB_ACCESS_TOKEN"], "[secure]")
110+
print(exception_text, file=sys.stderr)
111+
raise RuntimeError("See print for error text that as been sanitized for secrets")
85112

86113
def patch(url, **kwargs):
87-
return requests.patch(_fix_url(url), timeout=30, **_fix_kwargs(kwargs))
114+
try:
115+
return requests.patch(_fix_url(url), timeout=TIMEOUT, **_fix_kwargs(kwargs))
116+
except Exception as e:
117+
exception_text = traceback.format_exc()
118+
if "ADABOT_GITHUB_ACCESS_TOKEN" in os.environ:
119+
exception_text = exception_text.replace(os.environ["ADABOT_GITHUB_ACCESS_TOKEN"], "[secure]")
120+
print(exception_text, file=sys.stderr)
121+
raise RuntimeError("See print for error text that as been sanitized for secrets")
88122

89123
def delete(url, **kwargs):
90-
return requests.delete(_fix_url(url), timeout=30, **_fix_kwargs(kwargs))
124+
try:
125+
return requests.delete(_fix_url(url), timeout=TIMEOUT, **_fix_kwargs(kwargs))
126+
except Exception as e:
127+
exception_text = traceback.format_exc()
128+
if "ADABOT_GITHUB_ACCESS_TOKEN" in os.environ:
129+
exception_text = exception_text.replace(os.environ["ADABOT_GITHUB_ACCESS_TOKEN"], "[secure]")
130+
print(exception_text, file=sys.stderr)
131+
raise RuntimeError("See print for error text that as been sanitized for secrets")

0 commit comments

Comments
 (0)