Skip to content

Commit

Permalink
Improve logging and remove unwanted dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
mansab committed Sep 26, 2024
1 parent 27ef5bb commit 7be597f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ curl -X GET http://localhost:8000/limit?token=<your_token>
* Activate [Python virtualenv](https://python.land/virtual-environments/virtualenv)

```bash
python -m venv venv
python3 -m venv venv
source venv/bin/activate
```

Expand All @@ -229,5 +229,5 @@ pip install -r requirements.txt
* set necessary env variable to authenticate with Github (see Prerequisites)
* export BASIC_AUTH_USERNAME=<user>
* export BASIC_AUTH_PASSWORD=<pass>
* python app.py --mode [pat-auth|app-auth] # pat-auth is the default mode if no mode is set
* python3 app.py --mode [pat-auth|app-auth] # pat-auth is the default mode if no mode is set
```
15 changes: 7 additions & 8 deletions src/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def redact_token(uri):
return re.sub(r'(\?|&)token=.*?(&|$)', r'\1token=<REDACTED>\2', uri)


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

Expand All @@ -83,10 +83,10 @@ def authenticate_with_device_flow(logger):
user_code = data['user_code']
verification_uri = data['verification_uri']

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

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

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

if token_value is not None:
logger.info("Successfully obtained access token.")
print(f"Please set env var GITHUB_APP_TOKEN={token_value}")
print(f"and restart the app.")
logger.info(f"Please set env var GITHUB_APP_TOKEN={token_value} and restart the app.")
else:
logger.error(f"Failed to obtain access token. Status: {token_response.status_code}, Response: {token_response.text}")
return None
Expand Down
4 changes: 1 addition & 3 deletions src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
requests
flask
Flask-BasicAuth
waitress
PyJWT
cryptography
waitress
10 changes: 6 additions & 4 deletions src/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from helpers import get_token, get_all_workflow_runs, redact_token, authenticate_with_device_flow
from config import BASIC_AUTH_USERNAME, BASIC_AUTH_PASSWORD, TIMEOUT

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

app = Flask('github-cctray')
Expand All @@ -30,7 +30,7 @@ def index():
"""
owner = request.args.get("owner") or request.form.get('owner')
repo = request.args.get("repo") or request.form.get('repo')
token = get_token()
token = get_token(logger=logger)

if not owner or not repo or not token:
logger.warning("Missing parameter(s) or Environment Variable")
Expand Down Expand Up @@ -113,7 +113,6 @@ def health():
'status': 'ok',
'version': f'{latest_version}'
}

return jsonify(response)


Expand All @@ -125,7 +124,7 @@ def limit():
Returns:
flask.Response: JSON response containing rate limiting information.
"""
token = get_token()
token = get_token(logger=logger)
headers = {
'Accept': 'application/vnd.github+json',
"Authorization": f"Bearer {token}",
Expand All @@ -152,7 +151,10 @@ def limit():
'status': 'ok',
'rate_limit': rate
}
logger.info("Request URI: %s Response Code: %d",
redact_token(request.full_path), response.status_code)
else:
logger.warning("Missing parameter(s) or Environment Variable")
response = {'status': 'ok', 'rate_limit': {
'error': 'Failed to retrieve rate limit information'}}

Expand Down

0 comments on commit 7be597f

Please sign in to comment.