-
Notifications
You must be signed in to change notification settings - Fork 454
Refactor token env lookup and schedule helpers to remove semantic drift #45298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package parser | ||
|
|
||
| import ( | ||
| "github.com/github/gh-aw/pkg/envutil" | ||
| "github.com/github/gh-aw/pkg/logger" | ||
| ) | ||
|
|
||
| func githubTokenFromEnv(log *logger.Logger) string { | ||
| if token := envutil.GetStringFromEnv("GITHUB_TOKEN", "", log); token != "" { | ||
| return token | ||
| } | ||
| return envutil.GetStringFromEnv("GH_TOKEN", "", log) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| //go:build !integration | ||
|
|
||
| package parser | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/github/gh-aw/pkg/logger" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestGithubTokenFromEnv(t *testing.T) { | ||
| t.Run("prefers GITHUB_TOKEN over GH_TOKEN", func(t *testing.T) { | ||
| t.Setenv("GITHUB_TOKEN", "github-token") | ||
| t.Setenv("GH_TOKEN", "gh-token") | ||
|
|
||
| assert.Equal(t, "github-token", githubTokenFromEnv(logger.New("parser:test"))) | ||
| }) | ||
|
|
||
| t.Run("falls back to GH_TOKEN when GITHUB_TOKEN is empty", func(t *testing.T) { | ||
| t.Setenv("GITHUB_TOKEN", "") | ||
| t.Setenv("GH_TOKEN", "gh-token") | ||
|
|
||
| assert.Equal(t, "gh-token", githubTokenFromEnv(nil)) | ||
| }) | ||
|
|
||
| t.Run("returns empty when neither token is set", func(t *testing.T) { | ||
| t.Setenv("GITHUB_TOKEN", "") | ||
| t.Setenv("GH_TOKEN", "") | ||
|
|
||
| assert.Empty(t, githubTokenFromEnv(nil)) | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,6 @@ func TestIsCronExpression(t *testing.T) { | |
| {"invalid expression", "invalid cron expression", false}, | ||
| {"empty string", "", false}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := IsCronExpression(tt.input) | ||
|
|
@@ -37,6 +36,28 @@ func TestIsCronExpression(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestIsNumericCronField(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| expected bool | ||
| }{ | ||
| {"single digit", "5", true}, | ||
| {"multiple digits", "15", true}, | ||
| {"empty", "", false}, | ||
| {"wildcard", "*", false}, | ||
| {"interval", "*/5", false}, | ||
| {"range", "1-5", false}, | ||
| {"list", "1,2", false}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| assert.Equal(t, tt.expected, isNumericCronField(tt.input)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestIsDailyCron(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] 💡 Suggested test skeletonfunc TestCronFields(t *testing.T) {
fields, ok := cronFields("0 0 * * *")
assert.True(t, ok)
assert.Equal(t, []string{"0", "0", "*", "*", "*"}, fields)
_, ok = cronFields("0 0 * *") // only 4 fields
assert.False(t, ok)
_, ok = cronFields("") // empty string
assert.False(t, ok)
}@copilot please address this. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[/tdd] The second and third test cases pass
nilas the logger while the first passeslogger.New("parser:test"). There is no documented reason for the inconsistency — ifnilis intentionally valid (i.e. the helper is safe to call without a logger), make that explicit with a comment or a dedicatedTestGithubTokenFromEnvNilLoggercase; otherwise uselogger.New(...)consistently to avoid masking nil-pointer panics during future edits.@copilot please address this.