MCPcopy Create free account
hub / github.com/github/gh-aw / GetGitHubToken

Function GetGitHubToken

pkg/parser/github.go:61–92  ·  view source on GitHub ↗

GetGitHubToken attempts to get GitHub token from environment or gh CLI

()

Source from the content-addressed store, hash-verified

59
60// GetGitHubToken attempts to get GitHub token from environment or gh CLI
61func GetGitHubToken() (string, error) {
62 githubLog.Print("Getting GitHub token")
63
64 // First try environment variable
65 if token := os.Getenv("GITHUB_TOKEN"); token != "" { //nolint:osgetenvlibrary
66 githubLog.Print("Found GITHUB_TOKEN environment variable")
67 return token, nil
68 }
69 if token := os.Getenv("GH_TOKEN"); token != "" { //nolint:osgetenvlibrary
70 githubLog.Print("Found GH_TOKEN environment variable")
71 return token, nil
72 }
73
74 // Fall back to gh auth token command
75 githubLog.Print("Attempting to get token from gh auth token command")
76 cmd := exec.Command("gh", "auth", "token")
77 // Note: gh auth token should respect GH_HOST environment variable for enterprise
78 output, err := cmd.Output()
79 if err != nil {
80 githubLog.Printf("Failed to get token from gh auth token: %v", err)
81 return "", fmt.Errorf("GITHUB_TOKEN environment variable not set and 'gh auth token' failed: %w", err)
82 }
83
84 token := strings.TrimSpace(string(output))
85 if token == "" {
86 githubLog.Print("gh auth token returned empty token")
87 return "", errors.New("GITHUB_TOKEN environment variable not set and 'gh auth token' returned empty token")
88 }
89
90 githubLog.Print("Successfully retrieved token from gh auth token")
91 return token, nil
92}

Callers 3

attachImportAuthHeaderFunction · 0.92
validateServerSecretsFunction · 0.92
buildGitHubBuiltinConfigFunction · 0.70

Calls 4

PrintMethod · 0.80
GetenvMethod · 0.80
PrintfMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected