Skip to content

Commit 0807094

Browse files
feat: enhance GitHub authentication handling in Copilot module
- Added support for optional GitHub external authentication using the `coder_external_auth` data source. - Updated README to clarify the authentication methods available. - Simplified the start script to automatically handle GitHub authentication without prompting the user. This improves the user experience by streamlining the authentication process.
1 parent c4f210b commit 0807094

File tree

3 files changed

+9
-29
lines changed

3 files changed

+9
-29
lines changed

registry/coder-labs/modules/copilot/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ module "copilot" {
165165

166166
The module supports multiple authentication methods (in priority order):
167167

168-
1. **[Coder External Auth](https://coder.com/docs/admin/external-auth) (Recommended)** - Automatic if GitHub external auth is configured in Coder
168+
1. **[Coder External Auth](https://coder.com/docs/admin/external-auth) (Recommended)** - Automatic if GitHub external auth is configured in Coder with the ID github, otherwise you can change it
169169
2. **Direct Token** - Pass `github_token` variable (OAuth or Personal Access Token)
170170
3. **Interactive** - Copilot prompts for login via `/login` command if no auth found
171171

registry/coder-labs/modules/copilot/main.tf

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ variable "post_install_script" {
190190
data "coder_workspace" "me" {}
191191
data "coder_workspace_owner" "me" {}
192192

193+
data "coder_external_auth" "github" {
194+
id = var.external_auth_id
195+
optional = true
196+
}
197+
193198
locals {
194199
workdir = trimsuffix(var.workdir, "/")
195200
app_slug = "copilot"
@@ -230,13 +235,6 @@ resource "coder_env" "copilot_model" {
230235
value = var.copilot_model
231236
}
232237

233-
resource "coder_env" "github_token" {
234-
count = var.github_token != "" ? 1 : 0
235-
agent_id = var.agent_id
236-
name = "GITHUB_TOKEN"
237-
value = var.github_token
238-
}
239-
240238
module "agentapi" {
241239
source = "registry.coder.com/coder/agentapi/coder"
242240
version = "1.1.1"
@@ -265,6 +263,7 @@ module "agentapi" {
265263
echo -n '${base64encode(local.start_script)}' | base64 -d > /tmp/start.sh
266264
chmod +x /tmp/start.sh
267265
266+
GITHUB_TOKEN='${var.github_token != "" ? var.github_token : data.coder_external_auth.github.access_token}' \
268267
ARG_WORKDIR='${local.workdir}' \
269268
ARG_AI_PROMPT='${base64encode(var.ai_prompt)}' \
270269
ARG_SYSTEM_PROMPT='${base64encode(var.system_prompt)}' \

registry/coder-labs/modules/copilot/scripts/start.sh

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -98,30 +98,11 @@ setup_github_authentication() {
9898

9999
if [ -n "${GITHUB_TOKEN:-}" ]; then
100100
export GH_TOKEN="$GITHUB_TOKEN"
101-
echo "Using GitHub token from module configuration"
101+
echo "✓ GitHub token configured"
102102
return 0
103103
fi
104104

105-
if command_exists coder; then
106-
local github_token
107-
if github_token=$(coder external-auth access-token "${ARG_EXTERNAL_AUTH_ID:-github}" 2> /dev/null); then
108-
if [ -n "$github_token" ] && [ "$github_token" != "null" ]; then
109-
export GITHUB_TOKEN="$github_token"
110-
export GH_TOKEN="$github_token"
111-
echo "✓ Using Coder external auth OAuth token"
112-
return 0
113-
fi
114-
fi
115-
fi
116-
117-
if command_exists gh && gh auth status > /dev/null 2>&1; then
118-
echo "✓ Using GitHub CLI OAuth authentication"
119-
return 0
120-
fi
121-
122-
echo "⚠ No GitHub authentication available"
123-
echo " Copilot will prompt for login during first use"
124-
echo " Use the '/login' command in Copilot to authenticate"
105+
echo "✓ Copilot will handle authentication automatically"
125106
return 0
126107
}
127108

0 commit comments

Comments
 (0)