Skip to content

Commit 5979bfd

Browse files
authored
feat(config): define LIBRARIAN_GITHUB_TOKEN as a constant (#2367)
This change introduces a constant for the LIBRARIAN_GITHUB_TOKEN environment variable to improve maintainability and reduce duplication. Fixes #2089.
1 parent d579dd7 commit 5979bfd

File tree

8 files changed

+17
-13
lines changed

8 files changed

+17
-13
lines changed

cmd/librarian/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ in '.librarian/state.yaml'.
8181
8282
Example with build and push:
8383
84-
SDK_LIBRARIAN_GITHUB_TOKEN=xxx librarian generate --push --build
84+
LIBRARIAN_GITHUB_TOKEN=xxx librarian generate --push --build
8585
8686
Usage:
8787

e2e_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestRunGenerate(t *testing.T) {
102102
}
103103

104104
cmd := exec.Command("go", cmdArgs...)
105-
cmd.Env = append(os.Environ(), "LIBRARIAN_GITHUB_TOKEN=fake-token")
105+
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=fake-token", config.LibrarianGithubToken))
106106
cmd.Env = append(cmd.Env, "LIBRARIAN_GITHUB_BASE_URL="+server.URL)
107107
cmd.Stderr = os.Stderr
108108
cmd.Stdout = os.Stdout
@@ -540,7 +540,7 @@ func TestReleaseInit(t *testing.T) {
540540

541541
cmd := exec.Command("go", cmdArgs...)
542542
cmd.Env = os.Environ()
543-
cmd.Env = append(cmd.Env, "LIBRARIAN_GITHUB_TOKEN=fake-token")
543+
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=fake-token", config.LibrarianGithubToken))
544544
cmd.Env = append(cmd.Env, "LIBRARIAN_GITHUB_BASE_URL="+server.URL)
545545
cmd.Stderr = os.Stderr
546546
cmd.Stdout = os.Stdout
@@ -741,7 +741,7 @@ libraries:
741741
}
742742

743743
cmd := exec.Command("go", cmdArgs...)
744-
cmd.Env = append(os.Environ(), "LIBRARIAN_GITHUB_TOKEN=fake-token")
744+
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=fake-token", config.LibrarianGithubToken))
745745
cmd.Stderr = os.Stderr
746746
cmd.Stdout = os.Stdout
747747
if err := cmd.Run(); err != nil {

internal/automation/trigger.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
cloudbuild "cloud.google.com/go/cloudbuild/apiv1/v2"
2828
"cloud.google.com/go/cloudbuild/apiv1/v2/cloudbuildpb"
2929
"github.com/googleapis/gax-go/v2"
30+
"github.com/googleapis/librarian/internal/config"
3031
"github.com/googleapis/librarian/internal/github"
3132
)
3233

@@ -73,7 +74,7 @@ func RunCommand(ctx context.Context, command string, projectId string, push bool
7374
wrappedClient := &wrappedCloudBuildClient{
7475
client: c,
7576
}
76-
ghClient := github.NewClient(os.Getenv("LIBRARIAN_GITHUB_TOKEN"), nil)
77+
ghClient := github.NewClient(os.Getenv(config.LibrarianGithubToken), nil)
7778
return runCommandWithClient(ctx, wrappedClient, ghClient, command, projectId, push, build, forceRun, time.Now())
7879
}
7980

internal/config/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ const (
5555
ReleaseInitResponse = "release-init-response.json"
5656
// LibrarianStateFile is the name of the pipeline state file.
5757
LibrarianStateFile = "state.yaml"
58+
// LibrarianGithubToken is the name of the env var used to store the github token.
59+
LibrarianGithubToken = "LIBRARIAN_GITHUB_TOKEN"
5860
)
5961

6062
// are variables so it can be replaced during testing.
@@ -248,7 +250,7 @@ type Config struct {
248250
func New(cmdName string) *Config {
249251
return &Config{
250252
CommandName: cmdName,
251-
GitHubToken: os.Getenv("LIBRARIAN_GITHUB_TOKEN"),
253+
GitHubToken: os.Getenv(LibrarianGithubToken),
252254
}
253255
}
254256

internal/config/config_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
func TestNew(t *testing.T) {
29-
t.Setenv("LIBRARIAN_GITHUB_TOKEN", "")
29+
t.Setenv(LibrarianGithubToken, "")
3030
for _, test := range []struct {
3131
name string
3232
envVars map[string]string
@@ -35,7 +35,7 @@ func TestNew(t *testing.T) {
3535
{
3636
name: "All environment variables set",
3737
envVars: map[string]string{
38-
"LIBRARIAN_GITHUB_TOKEN": "gh_token",
38+
LibrarianGithubToken: "gh_token",
3939
"LIBRARIAN_SYNC_AUTH_TOKEN": "sync_token",
4040
},
4141
want: Config{
@@ -54,7 +54,7 @@ func TestNew(t *testing.T) {
5454
{
5555
name: "Some environment variables set",
5656
envVars: map[string]string{
57-
"LIBRARIAN_GITHUB_TOKEN": "gh_token",
57+
LibrarianGithubToken: "gh_token",
5858
},
5959
want: Config{
6060
GitHubToken: "gh_token",

internal/librarian/flags.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package librarian
1616

1717
import (
1818
"flag"
19+
"fmt"
1920

2021
"github.com/googleapis/librarian/internal/config"
2122
)
@@ -86,9 +87,9 @@ If not specified, will search for all merged pull requests with the label
8687

8788
func addFlagPush(fs *flag.FlagSet, cfg *config.Config) {
8889
fs.BoolVar(&cfg.Push, "push", false,
89-
`If true, Librarian will create a commit and a pull request for the changes.
90+
fmt.Sprintf(`If true, Librarian will create a commit and a pull request for the changes.
9091
A GitHub token with push access must be provided via the
91-
LIBRARIAN_GITHUB_TOKEN environment variable.`)
92+
%s environment variable.`, config.LibrarianGithubToken))
9293
}
9394

9495
func addFlagRepo(fs *flag.FlagSet, cfg *config.Config) {

internal/librarian/help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ in '.librarian/state.yaml'.
7171
your local working directory for inspection.
7272
7373
Example with build and push:
74-
SDK_LIBRARIAN_GITHUB_TOKEN=xxx librarian generate --push --build`
74+
LIBRARIAN_GITHUB_TOKEN=xxx librarian generate --push --build`
7575

7676
releaseInitLongHelp = `The 'release init' command is the primary entry point for initiating
7777
a new release. It automates the creation of a release pull request by parsing

internal/librarian/tag_and_release.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type tagAndReleaseRunner struct {
5151

5252
func newTagAndReleaseRunner(cfg *config.Config) (*tagAndReleaseRunner, error) {
5353
if cfg.GitHubToken == "" {
54-
return nil, fmt.Errorf("`LIBRARIAN_GITHUB_TOKEN` must be set")
54+
return nil, fmt.Errorf("`%s` must be set", config.LibrarianGithubToken)
5555
}
5656
repo, err := parseRemote(cfg.Repo)
5757
if err != nil {

0 commit comments

Comments
 (0)