Skip to content

Commit 72ea7ac

Browse files
committed
github: Remove redundant options
Signed-off-by: Matheus Pimenta <[email protected]>
1 parent 66a0184 commit 72ea7ac

File tree

6 files changed

+63
-78
lines changed

6 files changed

+63
-78
lines changed

git/github/client.go

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ import (
3333
)
3434

3535
const (
36-
AppIDKey = "githubAppID"
37-
AppInstallationIDKey = "githubAppInstallationID"
38-
AppPrivateKey = "githubAppPrivateKey"
39-
AppBaseUrlKey = "githubAppBaseURL"
36+
KeyAppID = "githubAppID"
37+
KeyAppInstallationID = "githubAppInstallationID"
38+
KeyAppPrivateKey = "githubAppPrivateKey"
39+
KeyAppBaseURL = "githubAppBaseURL"
4040

4141
AccessTokenUsername = "x-access-token"
4242
)
@@ -111,51 +111,22 @@ func New(opts ...OptFunc) (*Client, error) {
111111
return p, nil
112112
}
113113

114-
// WithInstallationID configures the installation ID of the GitHub App.
115-
func WithInstllationID(installationID string) OptFunc {
116-
return func(p *Client) {
117-
p.installationID = installationID
118-
}
119-
}
120-
121-
// WithAppID configures the app ID of the GitHub App.
122-
func WithAppID(appID string) OptFunc {
123-
return func(p *Client) {
124-
p.appID = appID
125-
}
126-
}
127-
128-
// WithPrivateKey configures the private key of the GitHub App.
129-
func WithPrivateKey(pk []byte) OptFunc {
130-
return func(p *Client) {
131-
p.privateKey = pk
132-
}
133-
}
134-
135-
// WithAppBaseURL configures the GitHub API endpoint to use to fetch GitHub App
136-
// installation token.
137-
func WithAppBaseURL(appBaseURL string) OptFunc {
138-
return func(p *Client) {
139-
p.apiURL = appBaseURL
140-
}
141-
}
142-
143114
// WithAppData configures the client using data from a map
144115
func WithAppData(appData map[string][]byte) OptFunc {
145116
return func(p *Client) {
146-
val, ok := appData[AppIDKey]
117+
val, ok := appData[KeyAppID]
147118
if ok {
148119
p.appID = string(val)
149120
}
150-
val, ok = appData[AppInstallationIDKey]
121+
val, ok = appData[KeyAppInstallationID]
151122
if ok {
152123
p.installationID = string(val)
153124
}
154-
val, ok = appData[AppPrivateKey]
125+
val, ok = appData[KeyAppPrivateKey]
155126
if ok {
156127
p.privateKey = val
157128
}
158-
val, ok = appData[AppBaseUrlKey]
129+
val, ok = appData[KeyAppBaseURL]
159130
if ok {
160131
p.apiURL = string(val)
161132
}
@@ -249,10 +220,10 @@ func GetCredentials(ctx context.Context, opts ...OptFunc) (string, string, error
249220

250221
func (p *Client) buildCacheKey() string {
251222
keyParts := []string{
252-
fmt.Sprintf("%s=%s", AppIDKey, p.appID),
253-
fmt.Sprintf("%s=%s", AppInstallationIDKey, p.installationID),
254-
fmt.Sprintf("%s=%s", AppBaseUrlKey, p.apiURL),
255-
fmt.Sprintf("%s=%s", AppPrivateKey, string(p.privateKey)),
223+
fmt.Sprintf("%s=%s", KeyAppID, p.appID),
224+
fmt.Sprintf("%s=%s", KeyAppInstallationID, p.installationID),
225+
fmt.Sprintf("%s=%s", KeyAppBaseURL, p.apiURL),
226+
fmt.Sprintf("%s=%s", KeyAppPrivateKey, string(p.privateKey)),
256227
}
257228
rawKey := strings.Join(keyParts, ",")
258229
hash := sha256.Sum256([]byte(rawKey))

git/github/client_test.go

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,33 @@ func TestClient_Options(t *testing.T) {
4545
opts []OptFunc
4646
wantErr error
4747
}{
48-
{
49-
name: "Create new client",
50-
opts: []OptFunc{WithInstllationID(installationID), WithAppID(appID), WithPrivateKey(kp.PrivateKey)},
51-
},
5248
{
5349
name: "Create new client with proxy",
54-
opts: []OptFunc{WithInstllationID(installationID), WithAppID(appID), WithPrivateKey(kp.PrivateKey), WithProxyURL((proxy))},
50+
opts: []OptFunc{
51+
WithAppData(map[string][]byte{
52+
KeyAppID: []byte(appID),
53+
KeyAppInstallationID: []byte(installationID),
54+
KeyAppPrivateKey: kp.PrivateKey,
55+
}),
56+
WithProxyURL(proxy),
57+
},
5558
},
5659
{
57-
name: "Create new client with custom api url",
58-
opts: []OptFunc{WithAppBaseURL(gitHubEnterpriseURL), WithInstllationID(installationID), WithAppID(appID), WithPrivateKey(kp.PrivateKey)},
60+
name: "Create new client",
61+
opts: []OptFunc{WithAppData(map[string][]byte{
62+
KeyAppID: []byte(appID),
63+
KeyAppInstallationID: []byte(installationID),
64+
KeyAppPrivateKey: kp.PrivateKey,
65+
})},
5966
},
6067
{
61-
name: "Create new client with app data",
68+
name: "Create new client with custom api url",
6269
opts: []OptFunc{WithAppData(map[string][]byte{
63-
AppIDKey: []byte(appID),
64-
AppInstallationIDKey: []byte(installationID),
65-
AppPrivateKey: kp.PrivateKey,
66-
},
67-
)},
70+
KeyAppID: []byte(appID),
71+
KeyAppInstallationID: []byte(installationID),
72+
KeyAppBaseURL: []byte(gitHubEnterpriseURL),
73+
KeyAppPrivateKey: kp.PrivateKey,
74+
})},
6875
},
6976
{
7077
name: "Create new client with empty data",
@@ -74,56 +81,56 @@ func TestClient_Options(t *testing.T) {
7481
{
7582
name: "Create new client with app data with missing AppID Key",
7683
opts: []OptFunc{WithAppData(map[string][]byte{
77-
AppInstallationIDKey: []byte(installationID),
78-
AppPrivateKey: kp.PrivateKey,
84+
KeyAppInstallationID: []byte(installationID),
85+
KeyAppPrivateKey: kp.PrivateKey,
7986
},
8087
)},
8188
wantErr: errors.New("app ID must be provided to use github app authentication"),
8289
},
8390
{
8491
name: "Create new client with app data with missing AppInstallationID Key",
8592
opts: []OptFunc{WithAppData(map[string][]byte{
86-
AppIDKey: []byte("123"),
87-
AppPrivateKey: kp.PrivateKey,
93+
KeyAppID: []byte("123"),
94+
KeyAppPrivateKey: kp.PrivateKey,
8895
},
8996
)},
9097
wantErr: errors.New("app installation ID must be provided to use github app authentication"),
9198
},
9299
{
93100
name: "Create new client with app data with missing private Key",
94101
opts: []OptFunc{WithAppData(map[string][]byte{
95-
AppIDKey: []byte(appID),
96-
AppInstallationIDKey: []byte(installationID),
102+
KeyAppID: []byte(appID),
103+
KeyAppInstallationID: []byte(installationID),
97104
},
98105
)},
99106
wantErr: errors.New("private key must be provided to use github app authentication"),
100107
},
101108
{
102109
name: "Create new client with invalid appID in app data",
103110
opts: []OptFunc{WithAppData(map[string][]byte{
104-
AppIDKey: []byte("abc"),
105-
AppInstallationIDKey: []byte(installationID),
106-
AppPrivateKey: kp.PrivateKey,
111+
KeyAppID: []byte("abc"),
112+
KeyAppInstallationID: []byte(installationID),
113+
KeyAppPrivateKey: kp.PrivateKey,
107114
},
108115
)},
109116
wantErr: errors.New("invalid app id, err: strconv.Atoi: parsing \"abc\": invalid syntax"),
110117
},
111118
{
112119
name: "Create new client with invalid installationID in app data",
113120
opts: []OptFunc{WithAppData(map[string][]byte{
114-
AppIDKey: []byte(appID),
115-
AppInstallationIDKey: []byte("abc"),
116-
AppPrivateKey: kp.PrivateKey,
121+
KeyAppID: []byte(appID),
122+
KeyAppInstallationID: []byte("abc"),
123+
KeyAppPrivateKey: kp.PrivateKey,
117124
},
118125
)},
119126
wantErr: errors.New("invalid app installation id, err: strconv.Atoi: parsing \"abc\": invalid syntax"),
120127
},
121128
{
122129
name: "Create new client with invalid private key in app data",
123130
opts: []OptFunc{WithAppData(map[string][]byte{
124-
AppIDKey: []byte(appID),
125-
AppInstallationIDKey: []byte(installationID),
126-
AppPrivateKey: []byte(" "),
131+
KeyAppID: []byte(appID),
132+
KeyAppInstallationID: []byte(installationID),
133+
KeyAppPrivateKey: []byte(" "),
127134
},
128135
)},
129136
wantErr: errors.New("could not parse private key: invalid key: Key must be a PEM encoded PKCS1 or PKCS8 key"),
@@ -230,7 +237,12 @@ func TestClient_GetCredentials(t *testing.T) {
230237
kp, err := ssh.GenerateKeyPair(ssh.RSA_4096)
231238
g.Expect(err).ToNot(HaveOccurred())
232239
opts := []OptFunc{
233-
WithAppBaseURL(srv.URL), WithInstllationID("123"), WithAppID("456"), WithPrivateKey(kp.PrivateKey),
240+
WithAppData(map[string][]byte{
241+
KeyAppID: []byte("123"),
242+
KeyAppInstallationID: []byte("456"),
243+
KeyAppBaseURL: []byte(srv.URL),
244+
KeyAppPrivateKey: kp.PrivateKey,
245+
}),
234246
}
235247
opts = append(opts, tt.opts...)
236248

git/gogit/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
1515
github.com/elazarl/goproxy v1.7.2
1616
github.com/fluxcd/gitkit v0.6.0
17-
github.com/fluxcd/pkg/git v0.33.0
17+
github.com/fluxcd/pkg/git v0.34.0
1818
github.com/fluxcd/pkg/gittestserver v0.18.0
1919
github.com/fluxcd/pkg/ssh v0.20.0
2020
github.com/fluxcd/pkg/version v0.9.0

git/internal/e2e/github_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,11 @@ func TestGitHubE2E(t *testing.T) {
149149
var data map[string][]byte
150150
authOptions, err = git.NewAuthOptions(*repoURL, data)
151151
username, password, err := pkggithub.GetCredentials(context.Background(),
152-
pkggithub.WithAppID(githubAppID),
153-
pkggithub.WithInstllationID(githubAppInstallID),
154-
pkggithub.WithPrivateKey(githubAppPrivateKey))
152+
pkggithub.WithAppData(map[string][]byte{
153+
pkggithub.KeyAppID: []byte(githubAppID),
154+
pkggithub.KeyAppInstallationID: []byte(githubAppInstallID),
155+
pkggithub.KeyAppPrivateKey: githubAppPrivateKey,
156+
}))
155157
if err != nil {
156158
return nil, nil, err
157159
}

git/internal/e2e/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ replace (
1313

1414
require (
1515
github.com/fluxcd/go-git-providers v0.22.0
16-
github.com/fluxcd/pkg/git v0.33.0
17-
github.com/fluxcd/pkg/git/gogit v0.36.0
16+
github.com/fluxcd/pkg/git v0.34.0
17+
github.com/fluxcd/pkg/git/gogit v0.37.0
1818
github.com/fluxcd/pkg/gittestserver v0.18.0
1919
github.com/fluxcd/pkg/ssh v0.20.0
2020
github.com/go-git/go-git/v5 v5.16.2

oci/tests/integration/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ require (
2020
github.com/fluxcd/pkg/apis/meta v1.17.0
2121
github.com/fluxcd/pkg/auth v0.21.0
2222
github.com/fluxcd/pkg/cache v0.10.0
23-
github.com/fluxcd/pkg/git v0.33.0
24-
github.com/fluxcd/pkg/git/gogit v0.36.0
23+
github.com/fluxcd/pkg/git v0.34.0
24+
github.com/fluxcd/pkg/git/gogit v0.37.0
2525
github.com/fluxcd/pkg/runtime v0.69.0
2626
github.com/fluxcd/test-infra/tftestenv v0.0.0-20250626232827-e0ca9c3f8d7b
2727
github.com/go-git/go-git/v5 v5.16.2

0 commit comments

Comments
 (0)