Skip to content

Commit

Permalink
Add another option for passing an already created GitHub Client
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
  • Loading branch information
rdimitrov committed May 31, 2024
1 parent 2f04baf commit 3a0d04b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func replaceCmd(cmd *cobra.Command, args []string) error {
// Create a new replacer
r := replacer.NewGitHubActionsReplacer(cfg).
WithUserRegex(cliFlags.Regex).
WithGitHubClient(os.Getenv(cli.GitHubTokenEnvKey))
WithGitHubClientFromToken(os.Getenv(cli.GitHubTokenEnvKey))

if cli.IsPath(pathOrRef) {
dir := filepath.Clean(pathOrRef)
Expand Down
2 changes: 1 addition & 1 deletion cmd/actions/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func list(cmd *cobra.Command, args []string) error {
// Create a new replacer
r := replacer.NewGitHubActionsReplacer(cfg).
WithUserRegex(cliFlags.Regex).
WithGitHubClient(os.Getenv(cli.GitHubTokenEnvKey))
WithGitHubClientFromToken(os.Getenv(cli.GitHubTokenEnvKey))

// List the references in the directory
res, err := r.ListPath(dir)
Expand Down
10 changes: 8 additions & 2 deletions pkg/replacer/replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,19 @@ func NewContainerImagesReplacer(cfg *config.Config) *Replacer {
}
}

// WithGitHubClient creates an authenticated GitHub client
func (r *Replacer) WithGitHubClient(token string) *Replacer {
// WithGitHubClientFromToken creates an authenticated GitHub client from a token
func (r *Replacer) WithGitHubClientFromToken(token string) *Replacer {
client := ghrest.NewClient(token)
r.rest = client
return r
}

// WithGitHubClient sets the GitHub client to use
func (r *Replacer) WithGitHubClient(client interfaces.REST) *Replacer {
r.rest = client
return r
}

// WithUserRegex sets a user-provided regex for the parser
func (r *Replacer) WithUserRegex(regex string) *Replacer {
if r.parser != nil {
Expand Down
55 changes: 43 additions & 12 deletions pkg/replacer/replacer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func TestReplacer_ParseGitHubActionString(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
r := NewGitHubActionsReplacer(&config.Config{}).WithGitHubClient(os.Getenv("GITHUB_TOKEN"))
r := NewGitHubActionsReplacer(&config.Config{}).WithGitHubClientFromToken(os.Getenv("GITHUB_TOKEN"))
got, err := r.ParseString(ctx, tt.args.action)
if tt.wantErr {
require.Error(t, err)
Expand Down Expand Up @@ -419,7 +419,7 @@ services:
wantErr: false,
},
{
name: "Multiple valid image references",
name: "Multiple valid image references with one commented",
before: `
version: v1
services:
Expand All @@ -446,6 +446,33 @@ services:
`,
modified: true,
},
{
name: "Valid image reference without specifying the tag",
before: `
apiVersion: v1
kind: Pod
metadata:
name: mount-host
namespace: playground
spec:
containers:
- name: mount-host
image: alpine
command: ["sleep"]
args: ["infinity"]
volumeMounts:
- name: host-root
mountPath: /host
readOnly: true
volumes:
- name: host-root
hostPath:
path: /
type: Directory
`,
expected: "",
modified: true,
},
}
for _, tt := range testCases {
tt := tt
Expand All @@ -454,21 +481,25 @@ services:
ctx := context.Background()
r := NewContainerImagesReplacer(&config.Config{})
modified, newContent, err := r.ParseFile(ctx, strings.NewReader(tt.before))
if tt.modified {
require.True(t, modified)
require.Equal(t, tt.expected, newContent)
} else {
require.False(t, modified)
require.Equal(t, tt.before, newContent)
}
if tt.wantErr {
require.False(t, modified)
require.Equal(t, tt.before, newContent)
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tt.expected, newContent)
if tt.modified {
require.True(t, modified)
if tt.expected != "" {
require.Equal(t, tt.expected, newContent)
} else {
require.NotEmpty(t, tt.before, newContent)
}
} else {
require.False(t, modified)
require.Equal(t, tt.before, newContent)
}

})
}
}
Expand Down Expand Up @@ -636,7 +667,7 @@ jobs:
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
r := NewGitHubActionsReplacer(&config.Config{}).WithGitHubClient(os.Getenv(cli.GitHubTokenEnvKey))
r := NewGitHubActionsReplacer(&config.Config{}).WithGitHubClientFromToken(os.Getenv(cli.GitHubTokenEnvKey))
if tt.useCustomRegex {
r = r.WithUserRegex(tt.regex)
}
Expand Down Expand Up @@ -721,7 +752,7 @@ func TestReplacer_WithGitHubClient(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
r = r.WithGitHubClient(tt.token)
r = r.WithGitHubClientFromToken(tt.token)
require.NotNil(t, r)
require.IsType(t, ghrest.NewClient(tt.token), r.rest)
})
Expand Down
3 changes: 0 additions & 3 deletions pkg/utils/ghrest/ghrest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ import (
"io"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/h2non/gock.v1"
)

const ContextTimeout = 4 * time.Second

// nolint:gocyclo
func TestClientFunctions(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 3a0d04b

Please sign in to comment.