Skip to content

Commit 7be597f

Browse files
committed
Improve logging and remove unwanted dependencies
1 parent 27ef5bb commit 7be597f

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ curl -X GET http://localhost:8000/limit?token=<your_token>
212212
* Activate [Python virtualenv](https://python.land/virtual-environments/virtualenv)
213213

214214
```bash
215-
python -m venv venv
215+
python3 -m venv venv
216216
source venv/bin/activate
217217
```
218218

@@ -229,5 +229,5 @@ pip install -r requirements.txt
229229
* set necessary env variable to authenticate with Github (see Prerequisites)
230230
* export BASIC_AUTH_USERNAME=<user>
231231
* export BASIC_AUTH_PASSWORD=<pass>
232-
* python app.py --mode [pat-auth|app-auth] # pat-auth is the default mode if no mode is set
232+
* python3 app.py --mode [pat-auth|app-auth] # pat-auth is the default mode if no mode is set
233233
```

src/helpers.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def redact_token(uri):
3434
return re.sub(r'(\?|&)token=.*?(&|$)', r'\1token=<REDACTED>\2', uri)
3535

3636

37-
def get_token():
37+
def get_token(logger):
3838
"""Sets the GitHub API token based on the selected mode
3939
4040
Returns:
@@ -58,8 +58,8 @@ def get_token():
5858
elif args.mode == "app-auth":
5959
token = GITHUB_APP_TOKEN
6060
if not token:
61-
print(f"\nObtain the Github App token by accessing: http://localhost:8000/auth")
62-
print(f"and set GITHUB_APP_TOKEN as environment variable.\n")
61+
logger.info(f"Obtain the Github App token by accessing: http://localhost:8000/auth")
62+
logger.info(f"and set GITHUB_APP_TOKEN as environment variable.")
6363
raise Exception("Github APP token not found.")
6464
return token
6565

@@ -83,10 +83,10 @@ def authenticate_with_device_flow(logger):
8383
user_code = data['user_code']
8484
verification_uri = data['verification_uri']
8585

86-
print(f"\nActivate GitHub authentication at: {verification_uri}")
87-
print(f"Enter activation code: {user_code}")
86+
logger.info(f"Activate GitHub authentication at: {verification_uri}")
87+
logger.info(f"Enter activation code: {user_code}")
8888

89-
print(f"\nWaiting 30 seconds for the user to authorize the device...\n")
89+
logger.info(f"Waiting 30 seconds for the user to authorize the device...")
9090
time.sleep(30)
9191

9292
token_url = "https://github.com/login/oauth/access_token"
@@ -101,8 +101,7 @@ def authenticate_with_device_flow(logger):
101101

102102
if token_value is not None:
103103
logger.info("Successfully obtained access token.")
104-
print(f"Please set env var GITHUB_APP_TOKEN={token_value}")
105-
print(f"and restart the app.")
104+
logger.info(f"Please set env var GITHUB_APP_TOKEN={token_value} and restart the app.")
106105
else:
107106
logger.error(f"Failed to obtain access token. Status: {token_response.status_code}, Response: {token_response.text}")
108107
return None

src/requirements.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
requests
22
flask
33
Flask-BasicAuth
4-
waitress
5-
PyJWT
6-
cryptography
4+
waitress

src/routes.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from helpers import get_token, get_all_workflow_runs, redact_token, authenticate_with_device_flow
1111
from config import BASIC_AUTH_USERNAME, BASIC_AUTH_PASSWORD, TIMEOUT
1212

13-
logging.basicConfig(level=logging.INFO)
13+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
1414
logger = logging.getLogger(__name__)
1515

1616
app = Flask('github-cctray')
@@ -30,7 +30,7 @@ def index():
3030
"""
3131
owner = request.args.get("owner") or request.form.get('owner')
3232
repo = request.args.get("repo") or request.form.get('repo')
33-
token = get_token()
33+
token = get_token(logger=logger)
3434

3535
if not owner or not repo or not token:
3636
logger.warning("Missing parameter(s) or Environment Variable")
@@ -113,7 +113,6 @@ def health():
113113
'status': 'ok',
114114
'version': f'{latest_version}'
115115
}
116-
117116
return jsonify(response)
118117

119118

@@ -125,7 +124,7 @@ def limit():
125124
Returns:
126125
flask.Response: JSON response containing rate limiting information.
127126
"""
128-
token = get_token()
127+
token = get_token(logger=logger)
129128
headers = {
130129
'Accept': 'application/vnd.github+json',
131130
"Authorization": f"Bearer {token}",
@@ -152,7 +151,10 @@ def limit():
152151
'status': 'ok',
153152
'rate_limit': rate
154153
}
154+
logger.info("Request URI: %s Response Code: %d",
155+
redact_token(request.full_path), response.status_code)
155156
else:
157+
logger.warning("Missing parameter(s) or Environment Variable")
156158
response = {'status': 'ok', 'rate_limit': {
157159
'error': 'Failed to retrieve rate limit information'}}
158160

0 commit comments

Comments
 (0)