Add GitHub token environment variable support for Copilot (#31392)

Add support for environment variables as authentication alternatives to
OAuth flow for Copilot. Closes #31172

We can include the token in HTTPS request headers to hopefully resolve
the rate limiting issue in #9483. This change will be part of a separate
PR.

Release Notes:

- Added support for manually providing an OAuth token for GitHub Copilot
Chat by assigning the GH_COPILOT_TOKEN environment variable

---------

Co-authored-by: Bennet Bo Fenner <bennetbo@gmx.de>
This commit is contained in:
Clauses Kim 2025-06-09 10:39:44 +00:00 committed by GitHub
parent 78fd2685d5
commit 1fe10117b7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 37 additions and 11 deletions

View file

@ -408,24 +408,30 @@ impl Copilot {
let proxy_url = copilot_settings.proxy.clone()?;
let no_verify = copilot_settings.proxy_no_verify;
let http_or_https_proxy = if proxy_url.starts_with("http:") {
"HTTP_PROXY"
Some("HTTP_PROXY")
} else if proxy_url.starts_with("https:") {
"HTTPS_PROXY"
Some("HTTPS_PROXY")
} else {
log::error!(
"Unsupported protocol scheme for language server proxy (must be http or https)"
);
return None;
None
};
let mut env = HashMap::default();
env.insert(http_or_https_proxy.to_string(), proxy_url);
if let Some(true) = no_verify {
env.insert("NODE_TLS_REJECT_UNAUTHORIZED".to_string(), "0".to_string());
};
if let Some(proxy_type) = http_or_https_proxy {
env.insert(proxy_type.to_string(), proxy_url);
if let Some(true) = no_verify {
env.insert("NODE_TLS_REJECT_UNAUTHORIZED".to_string(), "0".to_string());
};
}
Some(env)
if let Ok(oauth_token) = env::var(copilot_chat::COPILOT_OAUTH_ENV_VAR) {
env.insert(copilot_chat::COPILOT_OAUTH_ENV_VAR.to_string(), oauth_token);
}
if env.is_empty() { None } else { Some(env) }
}
#[cfg(any(test, feature = "test-support"))]