Skip to content

Commit 4e3d4a4

Browse files
committed
add copilot auto login through env vars
1 parent b454076 commit 4e3d4a4

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

lab_notebook_intelligence/ai_service_manager.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,14 @@
3131
)
3232
from lab_notebook_intelligence.base_chat_participant import BaseChatParticipant
3333
from lab_notebook_intelligence.config import NBIConfig
34-
from lab_notebook_intelligence.github_copilot_chat_participant import (
35-
GithubCopilotChatParticipant,
36-
)
34+
from lab_notebook_intelligence.github_copilot_chat_participant import GithubCopilotChatParticipant
3735
from lab_notebook_intelligence.llm_providers.github_copilot_llm_provider import (
3836
GitHubCopilotLLMProvider,
3937
)
4038
from lab_notebook_intelligence.llm_providers.litellm_compatible_llm_provider import (
4139
LiteLLMCompatibleLLMProvider,
4240
)
43-
from lab_notebook_intelligence.llm_providers.ollama_llm_provider import (
44-
OllamaLLMProvider,
45-
)
41+
from lab_notebook_intelligence.llm_providers.ollama_llm_provider import OllamaLLMProvider
4642
from lab_notebook_intelligence.llm_providers.openai_compatible_llm_provider import (
4743
OpenAICompatibleLLMProvider,
4844
)

lab_notebook_intelligence/github_copilot.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@
7878
websocket_connector: ThreadSafeWebSocketConnector = None
7979
github_login_status_change_updater_enabled = False
8080

81+
deprecated_user_data_file = os.path.join(os.path.expanduser("~"), ".jupyter", "nbi-data.json")
82+
user_data_file = os.path.join(os.path.expanduser("~"), ".jupyter", "nbi", "user-data.json")
83+
access_token_password = os.getenv("NBI_GH_ACCESS_TOKEN_PASSWORD", "nbi-access-token-password")
84+
85+
86+
def get_gh_access_token_from_env() -> str:
87+
access_token = os.environ.get("NBI_GH_ACCESS_TOKEN_ENCRYPTED")
88+
if access_token is not None:
89+
try:
90+
base64_bytes = base64.b64decode(access_token.encode("utf-8"))
91+
return decrypt_with_password(access_token_password, base64_bytes).decode("utf-8")
92+
except Exception as e:
93+
log.error(f"Failed to decrypt GitHub access token from environment variable: {e}")
94+
return None
95+
8196

8297
def enable_github_login_status_change_updater(enabled: bool):
8398
global github_login_status_change_updater_enabled
@@ -109,11 +124,6 @@ def get_login_status():
109124
return response
110125

111126

112-
deprecated_user_data_file = os.path.join(os.path.expanduser("~"), ".jupyter", "nbi-data.json")
113-
user_data_file = os.path.join(os.path.expanduser("~"), ".jupyter", "nbi", "user-data.json")
114-
access_token_password = os.getenv("NBI_GH_ACCESS_TOKEN_PASSWORD", "nbi-access-token-password")
115-
116-
117127
def read_stored_github_access_token() -> str:
118128
try:
119129
if os.path.exists(user_data_file):
@@ -186,7 +196,12 @@ def login_with_existing_credentials(store_access_token: bool):
186196
if github_auth["status"] is not LoginStatus.NOT_LOGGED_IN:
187197
return
188198

189-
if store_access_token:
199+
# Check for GitHub access token in environment variable
200+
github_access_token_provided = get_gh_access_token_from_env()
201+
if github_access_token_provided:
202+
log.info("Using GitHub access token from environment variable")
203+
remember_github_access_token = False # Do not store the token if it's from the environment
204+
elif store_access_token:
190205
github_access_token_provided = read_stored_github_access_token()
191206
remember_github_access_token = True
192207
else:
@@ -331,8 +346,7 @@ def wait_for_user_access_token_thread_func():
331346

332347
def get_token():
333348
global github_auth, github_access_token_provided, API_ENDPOINT, PROXY_ENDPOINT, TOKEN_REFRESH_INTERVAL
334-
access_token = github_auth["access_token"]
335-
349+
access_token = get_gh_access_token_from_env() or github_auth["access_token"]
336350
if access_token is None:
337351
return
338352

@@ -351,7 +365,6 @@ def get_token():
351365
)
352366

353367
resp_json = resp.json()
354-
355368
if resp.status_code == 401:
356369
github_access_token_provided = None
357370
logout()
@@ -393,7 +406,9 @@ def get_token_thread_func():
393406
return
394407
token = github_auth["token"]
395408
# update token if 10 seconds or less left to expiration
396-
if github_auth["access_token"] is not None and (
409+
access_token = get_gh_access_token_from_env() or github_auth["access_token"]
410+
411+
if access_token and (
397412
token is None
398413
or (dt.datetime.now() - github_auth["token_expires_at"]).total_seconds() > -10
399414
):

0 commit comments

Comments
 (0)