Skip to content

Commit ad5ba4a

Browse files
committed
Fix linting issues
1 parent b621836 commit ad5ba4a

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

src/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN")
1212
GITHUB_APP_TOKEN = os.environ.get("GITHUB_APP_TOKEN")
13-
APP_AUTH_ID = "Iv1.029716a4d1524dcd"
13+
APP_AUTH_ID = "Iv1.029716a4d1524dcd"

src/helpers.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,23 @@ def get_token(logger):
5353

5454
if token is not None:
5555
return token
56-
elif args.mode == "pat-auth":
56+
if args.mode == "pat-auth":
5757
token = GITHUB_TOKEN
58-
elif args.mode == "app-auth":
58+
return token
59+
if args.mode == "app-auth":
5960
token = GITHUB_APP_TOKEN
6061
if not token:
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.")
63-
raise Exception("Github APP token not found.")
62+
logger.info("Obtain the Github App token by accessing: http://localhost:8000/auth")
63+
logger.info("and set GITHUB_APP_TOKEN as environment variable.")
64+
raise RuntimeError("Github APP token not found.")
6465
return token
6566

6667
def authenticate_with_device_flow(logger):
68+
"""Initiates Github Device Authentication flow
69+
70+
Returns:
71+
token: Github App token for the authorized user
72+
"""
6773
device_code_url = "https://github.com/login/device/code"
6874
client_id = APP_AUTH_ID
6975
payload = {
@@ -76,7 +82,7 @@ def authenticate_with_device_flow(logger):
7682
}
7783

7884
try:
79-
response = requests.post(device_code_url, json=payload, headers=headers)
85+
response = requests.post(device_code_url, json=payload, headers=headers, timeout=10)
8086
if response.status_code == 200:
8187
data = response.json()
8288
device_code = data['device_code']
@@ -86,29 +92,37 @@ def authenticate_with_device_flow(logger):
8692
logger.info(f"Activate GitHub authentication at: {verification_uri}")
8793
logger.info(f"Enter activation code: {user_code}")
8894

89-
logger.info(f"Waiting 30 seconds for the user to authorize the device...")
95+
logger.info("Waiting 30 seconds for the user to authorize the device...")
9096
time.sleep(30)
9197

9298
token_url = "https://github.com/login/oauth/access_token"
9399
token_response = requests.post(token_url, json={
94100
"client_id": client_id,
95101
"device_code": device_code,
96102
"grant_type": "urn:ietf:params:oauth:grant-type:device_code"
97-
}, headers={"Accept": "application/json"})
103+
}, headers={"Accept": "application/json"}, timeout=10)
98104

99105
token_data = token_response.json()
100106
token_value = token_data.get("access_token")
101107

102108
if token_value is not None:
103109
logger.info("Successfully obtained access token.")
104-
logger.info(f"Please set env var GITHUB_APP_TOKEN={token_value} and restart the app.")
105-
else:
106-
logger.error(f"Failed to obtain access token. Status: {token_response.status_code}, Response: {token_response.text}")
110+
logger.info(
111+
"Please set env var GITHUB_APP_TOKEN=%s and restart the app.",
112+
token_value
113+
)
107114
return None
108-
else:
109-
logger.error(f"Failed to initiate device flow: {response.status_code} {response.text}")
115+
logger.error(
116+
"Failed to obtain access token. "
117+
"Status: %s, Response: %s",
118+
token_response.status_code,
119+
token_response.text
120+
)
110121
return None
111122

123+
logger.error(f"Failed to initiate device flow: {response.status_code} {response.text}")
124+
return None
125+
112126
except Exception as e:
113127
logger.exception(f"Error during device flow authentication: {str(e)}")
114128
return None

src/routes.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,21 @@ def index():
8686
def auth():
8787
"""Endpoint for handling Device Flow authentication."""
8888
try:
89-
threading.Thread(target=authenticate_with_device_flow, args=(logger,)).start()
90-
91-
return make_response("Authentication process started. Please check your console for instructions.", 200)
92-
except Exception as e:
89+
threading.Thread(
90+
target=authenticate_with_device_flow,
91+
args=(logger,)
92+
).start()
93+
94+
msg = (
95+
"Authentication process started. "
96+
"Please check your console for instructions."
97+
)
98+
return make_response(msg, 200)
99+
100+
except RuntimeError as e:
93101
logger.error("An error occurred during authentication: %s", str(e))
94102
return make_response("Authentication error.", 500)
95-
103+
96104

97105
@app.route('/health')
98106
def health():
@@ -152,23 +160,22 @@ def limit():
152160
'rate_limit': rate
153161
}
154162
logger.info("Request URI: %s Response Code: %d",
155-
redact_token(request.full_path), response.status_code)
163+
redact_token(request.full_path), response.status_code)
156164
else:
157-
logger.warning("Missing parameter(s) or Environment Variable")
165+
logger.warning("Missing parameter(s) or Environment Variable")
158166
response = {'status': 'ok', 'rate_limit': {
159167
'error': 'Failed to retrieve rate limit information'}}
160168

161169
return jsonify(response)
162170

163171

164-
@app.route('/token')
165-
def token():
166-
if access_token is None:
167-
return jsonify({"error": "Token is not set."}), 401
168-
return jsonify({"Token": access_token})
169-
170172
@app.route('/favicon.ico')
171173
def favicon():
174+
"""Handle favicon reuquests from browser
175+
176+
Returns:
177+
http_status: 204
178+
"""
172179
return '', 204
173180

174181

0 commit comments

Comments
 (0)