From ad5ba4aca9dffba1a0b2adca6ac7b78b6bdd165c Mon Sep 17 00:00:00 2001 From: Mansab Uppal Date: Thu, 26 Sep 2024 11:37:31 +0200 Subject: [PATCH] Fix linting issues --- src/config.py | 2 +- src/helpers.py | 40 +++++++++++++++++++++++++++------------- src/routes.py | 33 ++++++++++++++++++++------------- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/config.py b/src/config.py index 525d24b..a630372 100644 --- a/src/config.py +++ b/src/config.py @@ -10,4 +10,4 @@ GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN") GITHUB_APP_TOKEN = os.environ.get("GITHUB_APP_TOKEN") -APP_AUTH_ID = "Iv1.029716a4d1524dcd" \ No newline at end of file +APP_AUTH_ID = "Iv1.029716a4d1524dcd" diff --git a/src/helpers.py b/src/helpers.py index 27599cb..440ad75 100644 --- a/src/helpers.py +++ b/src/helpers.py @@ -53,17 +53,23 @@ def get_token(logger): if token is not None: return token - elif args.mode == "pat-auth": + if args.mode == "pat-auth": token = GITHUB_TOKEN - elif args.mode == "app-auth": + return token + if args.mode == "app-auth": token = GITHUB_APP_TOKEN if not token: - 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.") + logger.info("Obtain the Github App token by accessing: http://localhost:8000/auth") + logger.info("and set GITHUB_APP_TOKEN as environment variable.") + raise RuntimeError("Github APP token not found.") return token def authenticate_with_device_flow(logger): + """Initiates Github Device Authentication flow + + Returns: + token: Github App token for the authorized user + """ device_code_url = "https://github.com/login/device/code" client_id = APP_AUTH_ID payload = { @@ -76,7 +82,7 @@ def authenticate_with_device_flow(logger): } try: - response = requests.post(device_code_url, json=payload, headers=headers) + response = requests.post(device_code_url, json=payload, headers=headers, timeout=10) if response.status_code == 200: data = response.json() device_code = data['device_code'] @@ -86,7 +92,7 @@ def authenticate_with_device_flow(logger): logger.info(f"Activate GitHub authentication at: {verification_uri}") logger.info(f"Enter activation code: {user_code}") - logger.info(f"Waiting 30 seconds for the user to authorize the device...") + logger.info("Waiting 30 seconds for the user to authorize the device...") time.sleep(30) token_url = "https://github.com/login/oauth/access_token" @@ -94,21 +100,29 @@ def authenticate_with_device_flow(logger): "client_id": client_id, "device_code": device_code, "grant_type": "urn:ietf:params:oauth:grant-type:device_code" - }, headers={"Accept": "application/json"}) + }, headers={"Accept": "application/json"}, timeout=10) token_data = token_response.json() token_value = token_data.get("access_token") if token_value is not None: logger.info("Successfully obtained access token.") - 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}") + logger.info( + "Please set env var GITHUB_APP_TOKEN=%s and restart the app.", + token_value + ) return None - else: - logger.error(f"Failed to initiate device flow: {response.status_code} {response.text}") + logger.error( + "Failed to obtain access token. " + "Status: %s, Response: %s", + token_response.status_code, + token_response.text + ) return None + logger.error(f"Failed to initiate device flow: {response.status_code} {response.text}") + return None + except Exception as e: logger.exception(f"Error during device flow authentication: {str(e)}") return None diff --git a/src/routes.py b/src/routes.py index 828cda5..7793947 100644 --- a/src/routes.py +++ b/src/routes.py @@ -86,13 +86,21 @@ def index(): def auth(): """Endpoint for handling Device Flow authentication.""" try: - threading.Thread(target=authenticate_with_device_flow, args=(logger,)).start() - - return make_response("Authentication process started. Please check your console for instructions.", 200) - except Exception as e: + threading.Thread( + target=authenticate_with_device_flow, + args=(logger,) + ).start() + + msg = ( + "Authentication process started. " + "Please check your console for instructions." + ) + return make_response(msg, 200) + + except RuntimeError as e: logger.error("An error occurred during authentication: %s", str(e)) return make_response("Authentication error.", 500) - + @app.route('/health') def health(): @@ -152,23 +160,22 @@ def limit(): 'rate_limit': rate } logger.info("Request URI: %s Response Code: %d", - redact_token(request.full_path), response.status_code) + redact_token(request.full_path), response.status_code) else: - logger.warning("Missing parameter(s) or Environment Variable") + logger.warning("Missing parameter(s) or Environment Variable") response = {'status': 'ok', 'rate_limit': { 'error': 'Failed to retrieve rate limit information'}} return jsonify(response) -@app.route('/token') -def token(): - if access_token is None: - return jsonify({"error": "Token is not set."}), 401 - return jsonify({"Token": access_token}) - @app.route('/favicon.ico') def favicon(): + """Handle favicon reuquests from browser + + Returns: + http_status: 204 + """ return '', 204