11"""Helpers Module"""
2- import datetime
32import time
43import re
54import base64
65import argparse
76from concurrent .futures import ThreadPoolExecutor
8- import jwt
97import requests
108from flask import request
119from config import (
@@ -65,7 +63,7 @@ def get_token():
6563 raise Exception ("Github APP token not found." )
6664 return token
6765
68- def authenticate_with_device_flow ():
66+ def authenticate_with_device_flow (logger ):
6967 device_code_url = "https://github.com/login/device/code"
7068 client_id = APP_AUTH_ID
7169 payload = {
@@ -75,52 +73,46 @@ def authenticate_with_device_flow():
7573
7674 headers = {
7775 "Accept" : "application/json"
78- }
79-
80- response = requests .post (device_code_url , json = payload , headers = headers )
81- if response .status_code == 200 :
82- data = response .json ()
83- device_code = data ['device_code' ]
84- user_code = data ['user_code' ]
85- verification_uri = data ['verification_uri' ]
86- print (f"\n Activate the app by accessing: { verification_uri } " )
87- print (f"and enter this activation code: { user_code } " )
88-
89- token_url = "https://github.com/login/oauth/access_token"
90- max_attempts = 2
91- attempt = 0
92- delay = 5
93-
94- while attempt < max_attempts :
95- attempt += 1
96-
76+ }
77+
78+ try :
79+ response = requests .post (device_code_url , json = payload , headers = headers )
80+ if response .status_code == 200 :
81+ data = response .json ()
82+ device_code = data ['device_code' ]
83+ user_code = data ['user_code' ]
84+ verification_uri = data ['verification_uri' ]
85+
86+ print (f"\n Activate GitHub authentication at: { verification_uri } " )
87+ print (f"Enter activation code: { user_code } " )
88+
89+ print (f"\n Waiting 30 seconds for the user to authorize the device...\n " )
90+ time .sleep (30 )
91+
92+ token_url = "https://github.com/login/oauth/access_token"
9793 token_response = requests .post (token_url , json = {
9894 "client_id" : client_id ,
9995 "device_code" : device_code ,
10096 "grant_type" : "urn:ietf:params:oauth:grant-type:device_code"
10197 }, headers = {"Accept" : "application/json" })
10298
103- if token_response .status_code == 200 :
104- token_data = token_response .json ()
105- return token_data .get ("access_token" )
106- elif token_response .status_code == 400 :
107- error_data = token_response .json ()
108- if error_data .get ("error" ) == "authorization_pending" :
109- print (f"Authorization pending... Attempt { attempt } /{ max_attempts } . Retrying in { delay } seconds." )
110- time .sleep (delay )
111- else :
112- print (f"Error: { error_data .get ('error_description' )} " )
113- return None
99+ token_data = token_response .json ()
100+ token_value = token_data .get ("access_token" )
101+
102+ if token_value is not None :
103+ 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." )
114106 else :
115- print (f"Unexpected response : { token_response .status_code } { token_response .text } " )
107+ logger . error (f"Failed to obtain access token. Status : { token_response .status_code } , Response: { token_response .text } " )
116108 return None
109+ else :
110+ logger .error (f"Failed to initiate device flow: { response .status_code } { response .text } " )
111+ return None
117112
118- print ("Maximum attempts reached. Please try again." )
113+ except Exception as e :
114+ logger .exception (f"Error during device flow authentication: { str (e )} " )
119115 return None
120- else :
121- raise Exception (f"Failed to initiate device flow: { response .status_code } { response .text } " )
122-
123-
124116
125117def get_workflows (owner , repo , headers ):
126118 """Get the workflows for a given owner and repo from the GitHub API.
0 commit comments