Repository navigation
Expand file tree
/
Copy pathgithub_token.go
More file actions
340 lines (307 loc) · 14.8 KB
/
Copy pathgithub_token.go
File metadata and controls
340 lines (307 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package workflow
import (
"strings"
"github.com/github/gh-aw/pkg/logger"
)
var tokenLog = logger.New("workflow:github_token")
var copilotCodingAgentGitHubTokenSecrets = []string{
"GH_AW_AGENT_TOKEN",
"GH_AW_GITHUB_TOKEN",
"GITHUB_TOKEN",
}
func wrapGitHubExpression(expression string) string {
return "${{ " + strings.TrimSpace(expression) + " }}"
}
func combineTokenExpressions(primaryExpression, fallbackExpression string) string {
combined := BuildOr(
&ExpressionNode{Expression: stripExpressionWrapper(primaryExpression)},
&ExpressionNode{Expression: stripExpressionWrapper(fallbackExpression)},
)
return wrapGitHubExpression(RenderCondition(combined))
}
// resolveGitHubToken returns the GitHub token to use, with precedence:
// 1. Custom token passed as parameter (e.g., from tool-specific config)
// 2. Default fallback: ${{ secrets.GH_AW_GITHUB_MCP_SERVER_TOKEN || secrets.GH_AW_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
func resolveGitHubToken(customToken string) string {
if customToken != "" {
tokenLog.Print("Using custom GitHub token")
return customToken
}
tokenLog.Print("Using default GitHub token fallback")
return "${{ secrets.GH_AW_GITHUB_MCP_SERVER_TOKEN || secrets.GH_AW_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}"
}
// resolveSafeOutputGitHubToken returns the GitHub token to use for safe output operations, with precedence:
// 1. Custom token passed as parameter (e.g., from per-output config)
// 2. Default fallback: ${{ secrets.GH_AW_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
// This simpler chain ensures safe outputs use: safe outputs token -> GH_AW_GITHUB_TOKEN -> GitHub Actions token
func resolveSafeOutputGitHubToken(customToken string) string {
if customToken != "" {
tokenLog.Print("Using custom safe output GitHub token")
return customToken
}
tokenLog.Print("Using default safe output GitHub token (GH_AW_GITHUB_TOKEN || GITHUB_TOKEN)")
return "${{ secrets.GH_AW_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}"
}
// getEffectiveMaintenanceGitHubToken returns the configured GitHub token secret
// expression to use for maintenance compile-workflows operations.
//
// No fallback chain is applied here. Maintenance compile PR mode must use the
// explicitly configured secret so the generated workflow does not silently fall
// back to a token without permission to write workflow files.
func getEffectiveMaintenanceGitHubToken(secretName string) string {
secretName = strings.TrimSpace(secretName)
if secretName == "" {
tokenLog.Print("No maintenance compile GitHub token secret configured")
return ""
}
tokenLog.Printf("Using configured maintenance compile GitHub token secret %q", secretName)
return wrapGitHubExpression("secrets." + secretName)
}
// getEffectiveCopilotRequestsToken returns the GitHub token to use for Copilot-related operations,
// with precedence:
// 1. Custom token passed as parameter (e.g., from safe-outputs config github-token field)
// 2. secrets.COPILOT_GITHUB_TOKEN (recommended token for Copilot operations)
// Note: The default GITHUB_TOKEN is NOT included as a fallback because it does not have
// permission to create agent sessions, assign issues to bots, or add bots as reviewers.
// This is used for safe outputs that interact with GitHub Copilot features:
// - create-agent-session
// - assigning "copilot" to issues
// - adding "copilot" as PR reviewer
func getEffectiveCopilotRequestsToken(customToken string) string {
if customToken != "" {
tokenLog.Print("Using custom Copilot GitHub token")
return customToken
}
return "${{ secrets.COPILOT_GITHUB_TOKEN }}"
}
// getEffectiveCopilotCodingAgentGitHubToken returns the GitHub token to use for agent assignment operations,
// with precedence:
// 1. Custom token passed as parameter (e.g., from safe-outputs config github-token field)
// 2. secrets.GH_AW_AGENT_TOKEN (recommended token for agent assignment with elevated permissions)
// 3. secrets.GH_AW_GITHUB_TOKEN (fallback with potentially sufficient permissions)
// 4. secrets.GITHUB_TOKEN (last resort, may lack permissions for bot assignment)
// Note: Assigning bots (like copilot-swe-agent) requires permissions that GITHUB_TOKEN may not have.
// It's recommended to configure GH_AW_AGENT_TOKEN or GH_AW_GITHUB_TOKEN with appropriate permissions.
func getEffectiveCopilotCodingAgentGitHubToken(customToken string) string {
if customToken != "" {
tokenLog.Print("Using custom agent GitHub token")
return customToken
}
tokenLog.Printf("Using default agent GitHub token fallback chain (%s)", strings.Join(copilotCodingAgentGitHubTokenSecrets, " || "))
expressions := make([]string, 0, len(copilotCodingAgentGitHubTokenSecrets))
for _, secret := range copilotCodingAgentGitHubTokenSecrets {
expressions = append(expressions, "secrets."+secret)
}
return wrapGitHubExpression(strings.Join(expressions, " || "))
}
// Match the assignment token's precedence without exposing a custom expression or secret value.
func getEffectiveCopilotCodingAgentGitHubTokenSource(customToken string) string {
if customToken != "" {
return `"configured"`
}
expressions := make([]string, 0, len(copilotCodingAgentGitHubTokenSecrets)+1)
for _, secret := range copilotCodingAgentGitHubTokenSecrets {
expressions = append(expressions, "secrets."+secret+" != '' && '"+secret+"'")
}
expressions = append(expressions, "'unavailable'")
return wrapGitHubExpression(strings.Join(expressions, " || "))
}
// getEffectiveProjectGitHubToken returns the GitHub token to use for GitHub Projects v2 operations,
// with precedence:
// 1. Custom token passed as parameter (e.g., from safe-outputs.update-project.github-token)
// 2. secrets.GH_AW_PROJECT_GITHUB_TOKEN (required token for Projects v2 operations)
// Note: GitHub Projects v2 requires a PAT (classic with project + repo scopes, or fine-grained
// with Projects: Read+Write) or GitHub App. The default GITHUB_TOKEN cannot access Projects v2.
// You must configure GH_AW_PROJECT_GITHUB_TOKEN or provide a custom token for Projects v2 operations.
// No fallback to GITHUB_TOKEN is provided as it will never work for Projects v2 operations.
func getEffectiveProjectGitHubToken(customToken string) string {
if customToken != "" {
tokenLog.Print("Using custom project GitHub token")
return customToken
}
tokenLog.Print("Using GH_AW_PROJECT_GITHUB_TOKEN for project operations")
return "${{ secrets.GH_AW_PROJECT_GITHUB_TOKEN }}"
}
// resolvePRCheckoutToken returns the token to use for PR checkout and git operations.
// Applies the following precedence (highest to lowest):
// 1. Per-config PAT: create-pull-request.github-token
// 2. Per-config PAT: push-to-pull-request-branch.github-token
// 3. Checkout-scoped safe-output GitHub App token (if configured for matching checkout)
// 4. safe-outputs GitHub App minted token (if a safe-outputs github-app is configured)
// 5. safe-outputs level PAT: safe-outputs.github-token
// 6. Default fallback via resolveSafeOutputGitHubToken()
//
// Per-config tokens take precedence over the GitHub App so that individual operations
// can override the app-wide authentication with a dedicated PAT when needed.
//
// Returns:
// - token: the effective GitHub Actions token expression to use for git operations
// - isCustom: true when a custom non-default token was explicitly configured (per-config PAT, app, or safe-outputs PAT)
func resolvePRCheckoutToken(safeOutputs *SafeOutputsConfig, checkoutMgr *CheckoutManager) (token string, isCustom bool) {
if safeOutputs == nil {
return resolveSafeOutputGitHubToken(""), false
}
var createPRToken string
if safeOutputs.CreatePullRequests != nil {
createPRToken = safeOutputs.CreatePullRequests.GitHubToken
}
var pushToPRBranchToken string
if safeOutputs.PushToPullRequestBranch != nil {
pushToPRBranchToken = safeOutputs.PushToPullRequestBranch.GitHubToken
}
// Per-config PAT tokens take highest precedence (overrides GitHub App).
// head-github-token is intentionally excluded: it is a fork-write credential
// scoped to head-repo only and must not be used for the shared checkout or
// upstream API calls.
perConfigToken := createPRToken
if perConfigToken == "" {
perConfigToken = pushToPRBranchToken
}
if perConfigToken != "" {
return resolveSafeOutputGitHubToken(perConfigToken), true
}
if checkoutMgr != nil {
targetRepo := resolvePRCheckoutTargetRepo(safeOutputs)
if checkoutToken, ok := checkoutMgr.ResolveSafeOutputCheckoutTokenExpression(targetRepo); ok {
return checkoutToken, true
}
}
// GitHub App token takes precedence over the safe-outputs level PAT
if safeOutputs.TokenBroker {
return "${{ steps.safe-outputs-app-token.outputs.token }}", true
}
if safeOutputs.GitHubApp != nil {
// When the global app-token minting step may be absent (ignore-if-missing, or no
// enabled handler consuming the global app), keep a fallback so the expression
// still resolves to a usable token instead of an empty string.
if safeOutputs.GitHubApp.shouldIgnoreMissingKey() || !safeOutputsGlobalAppTokenMinted(safeOutputs) {
fallback := resolveSafeOutputGitHubToken("")
if safeOutputs.GitHubToken != "" {
fallback = combineTokenExpressions(safeOutputs.GitHubToken, fallback)
}
return combineTokenExpressions(
"${{ steps.safe-outputs-app-token.outputs.token }}",
fallback,
), true
}
//nolint:gosec // G101: False positive - this is a GitHub Actions expression template placeholder, not a hardcoded credential
return "${{ steps.safe-outputs-app-token.outputs.token }}", true
}
if safeOutputs.GitHubToken != "" {
return resolveSafeOutputGitHubToken(safeOutputs.GitHubToken), true
}
return resolveSafeOutputGitHubToken(""), false
}
func resolvePRCheckoutTargetRepo(safeOutputs *SafeOutputsConfig) string {
if safeOutputs == nil {
return ""
}
if safeOutputs.CreatePullRequests != nil {
if repo := strings.TrimSpace(safeOutputs.CreatePullRequests.HeadRepoSlug); repo != "" && repo != "*" {
return repo
}
if repo := strings.TrimSpace(safeOutputs.CreatePullRequests.TargetRepoSlug); repo != "" && repo != "*" {
return repo
}
}
if safeOutputs.PushToPullRequestBranch != nil {
if repo := strings.TrimSpace(safeOutputs.PushToPullRequestBranch.HeadRepoSlug); repo != "" && repo != "*" {
return repo
}
if repo := strings.TrimSpace(safeOutputs.PushToPullRequestBranch.TargetRepoSlug); repo != "" && repo != "*" {
return repo
}
}
return ""
}
// resolveStaticCheckoutToken returns the effective checkout token as a static GitHub Actions
// expression (secret reference or default). Unlike resolvePRCheckoutToken, this function
// never returns a step-output expression because step outputs are not accessible outside the job
// they were created in.
//
// Token precedence:
// 1. checkout.github-token override
// 2. create-pull-request.github-token
// 3. push-to-pull-request-branch.github-token
// 4. safe-outputs.github-token
// 5. Default fallback (GH_AW_GITHUB_TOKEN || GITHUB_TOKEN)
func resolveStaticCheckoutToken(safeOutputs *SafeOutputsConfig, checkoutMgr *CheckoutManager) string {
if checkoutMgr != nil {
override := checkoutMgr.GetDefaultCheckoutOverride()
if override != nil && override.token != "" {
return resolveSafeOutputGitHubToken(override.token)
}
}
if safeOutputs == nil {
return resolveSafeOutputGitHubToken("")
}
if safeOutputs.CreatePullRequests != nil && safeOutputs.CreatePullRequests.HeadGitHubToken != "" {
return resolveSafeOutputGitHubToken(safeOutputs.CreatePullRequests.HeadGitHubToken)
}
if safeOutputs.CreatePullRequests != nil && safeOutputs.CreatePullRequests.GitHubToken != "" {
return resolveSafeOutputGitHubToken(safeOutputs.CreatePullRequests.GitHubToken)
}
if safeOutputs.PushToPullRequestBranch != nil && safeOutputs.PushToPullRequestBranch.HeadGitHubToken != "" {
return resolveSafeOutputGitHubToken(safeOutputs.PushToPullRequestBranch.HeadGitHubToken)
}
if safeOutputs.PushToPullRequestBranch != nil && safeOutputs.PushToPullRequestBranch.GitHubToken != "" {
return resolveSafeOutputGitHubToken(safeOutputs.PushToPullRequestBranch.GitHubToken)
}
if safeOutputs.GitHubToken != "" {
return resolveSafeOutputGitHubToken(safeOutputs.GitHubToken)
}
return resolveSafeOutputGitHubToken("")
}
// resolveProjectToken resolves the project token using precedence:
// 1. Per-config token (e.g., update-project/create-project/create-project-status-update)
// 2. safe-outputs.github-token
// 3. GH_AW_PROJECT_GITHUB_TOKEN fallback via getEffectiveProjectGitHubToken()
func resolveProjectToken(perConfigToken string, safeOutputsToken string) string {
token := perConfigToken
if token == "" {
token = safeOutputsToken
}
return getEffectiveProjectGitHubToken(token)
}
// resolveProjectURLAndToken resolves project URL/token from project-related safe output config.
// Priority: update-project > create-project-status-update > create-project.
func resolveProjectURLAndToken(safeOutputs *SafeOutputsConfig) (projectURL, projectToken string) {
if safeOutputs == nil {
return "", ""
}
safeOutputsToken := safeOutputs.GitHubToken
if safeOutputs.UpdateProjects != nil && safeOutputs.UpdateProjects.Project != "" {
projectURL = safeOutputs.UpdateProjects.Project
projectToken = resolveProjectToken(safeOutputs.UpdateProjects.GitHubToken, safeOutputsToken)
tokenLog.Printf("Setting GH_AW_PROJECT_URL from update-project config: %s", projectURL)
tokenLog.Printf("Setting GH_AW_PROJECT_GITHUB_TOKEN from update-project config")
return
}
if safeOutputs.CreateProjectStatusUpdates != nil && safeOutputs.CreateProjectStatusUpdates.Project != "" {
projectURL = safeOutputs.CreateProjectStatusUpdates.Project
projectToken = resolveProjectToken(safeOutputs.CreateProjectStatusUpdates.GitHubToken, safeOutputsToken)
tokenLog.Printf("Setting GH_AW_PROJECT_URL from create-project-status-update config: %s", projectURL)
tokenLog.Printf("Setting GH_AW_PROJECT_GITHUB_TOKEN from create-project-status-update config")
return
}
if safeOutputs.CreateProjects != nil {
projectToken = resolveProjectToken(safeOutputs.CreateProjects.GitHubToken, safeOutputsToken)
tokenLog.Printf("Setting GH_AW_PROJECT_GITHUB_TOKEN from create-project config")
}
return
}
// getEffectiveCITriggerGitHubToken returns the GitHub token to use for CI trigger operations
// (pushing empty commits to trigger workflow runs), with precedence:
// 1. Custom token passed as parameter (e.g., from github-token-for-extra-empty-commit field)
// 2. secrets.GH_AW_CI_TRIGGER_TOKEN (recommended token for CI trigger operations)
// Note: The default GITHUB_TOKEN is NOT included as a fallback because events created with
// GITHUB_TOKEN do not trigger other workflow runs (GitHub Actions security feature).
// This is used when pushing an empty commit to trigger CI checks on PRs created by workflows.
func getEffectiveCITriggerGitHubToken(customToken string) string {
if customToken != "" {
tokenLog.Print("Using custom CI trigger GitHub token")
return customToken
}
tokenLog.Print("Using GH_AW_CI_TRIGGER_TOKEN for CI trigger operations")
return "${{ secrets.GH_AW_CI_TRIGGER_TOKEN }}"
}