Skip to content

Commit 78aa6a5

Browse files
ashmckenzieArchish27
authored andcommitted
Merge branch '789-personalaccesstoken-lint' into 'main'
Lint fixes for personalaccesstoken package Closes #789 See merge request https://gitlab.com/gitlab-org/gitlab-shell/-/merge_requests/1146 Merged-by: Ash McKenzie <[email protected]> Approved-by: Ash McKenzie <[email protected]> Reviewed-by: Ash McKenzie <[email protected]> Co-authored-by: Archish <[email protected]>
2 parents c5394a4 + 8de7929 commit 78aa6a5

File tree

4 files changed

+17
-25
lines changed

4 files changed

+17
-25
lines changed

internal/command/personalaccesstoken/personalaccesstoken.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package personalaccesstoken handles operations related to personal access tokens,
2+
// including parsing arguments, requesting tokens, and formatting responses.
13
package personalaccesstoken
24

35
import (
@@ -22,6 +24,7 @@ const (
2224
expiresDateFormat = "2006-01-02"
2325
)
2426

27+
// Command represents a command to manage personal access tokens.
2528
type Command struct {
2629
Config *config.Config
2730
Args *commandargs.Shell
@@ -35,6 +38,7 @@ type tokenArgs struct {
3538
ExpiresDate string // Calculated, a TTL is passed from command-line.
3639
}
3740

41+
// Execute processes the command, requests a personal access token, and prints the result.
3842
func (c *Command) Execute(ctx context.Context) (context.Context, error) {
3943
err := c.parseTokenArgs()
4044
if err != nil {
@@ -50,16 +54,16 @@ func (c *Command) Execute(ctx context.Context) (context.Context, error) {
5054
return ctx, err
5155
}
5256

53-
fmt.Fprint(c.ReadWriter.Out, "Token: "+response.Token+"\n")
54-
fmt.Fprint(c.ReadWriter.Out, "Scopes: "+strings.Join(response.Scopes, ",")+"\n")
55-
fmt.Fprint(c.ReadWriter.Out, "Expires: "+response.ExpiresAt+"\n")
57+
_, _ = fmt.Fprint(c.ReadWriter.Out, "Token: "+response.Token+"\n")
58+
_, _ = fmt.Fprint(c.ReadWriter.Out, "Scopes: "+strings.Join(response.Scopes, ",")+"\n")
59+
_, _ = fmt.Fprint(c.ReadWriter.Out, "Expires: "+response.ExpiresAt+"\n")
5660

5761
return ctx, nil
5862
}
5963

6064
func (c *Command) parseTokenArgs() error {
6165
if len(c.Args.SshArgs) < 3 || len(c.Args.SshArgs) > 4 {
62-
return errors.New(usageText)
66+
return errors.New(usageText) // nolint:stylecheck // usageText is customer facing
6367
}
6468

6569
var rectfiedScopes []string
@@ -86,7 +90,7 @@ func (c *Command) parseTokenArgs() error {
8690

8791
TTL, err := strconv.Atoi(rawTTL)
8892
if err != nil || TTL < 0 {
89-
return fmt.Errorf("Invalid value for days_ttl: '%s'", rawTTL)
93+
return fmt.Errorf("Invalid value for days_ttl: '%s'", rawTTL) //nolint:stylecheck //message is customer facing
9094
}
9195

9296
c.TokenArgs.ExpiresDate = time.Now().AddDate(0, 0, TTL+1).Format(expiresDateFormat)

internal/command/personalaccesstoken/personalaccesstoken_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"testing"
1111

12+
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
1314

1415
"gitlab.com/gitlab-org/gitlab-shell/v14/client/testserver"
@@ -28,7 +29,7 @@ func setup(t *testing.T) {
2829
b, err := io.ReadAll(r.Body)
2930
defer r.Body.Close()
3031

31-
require.NoError(t, err)
32+
assert.NoError(t, err)
3233

3334
var requestBody *personalaccesstoken.RequestBody
3435
json.Unmarshal(b, &requestBody)
@@ -177,7 +178,7 @@ func TestExecute(t *testing.T) {
177178
},
178179
{
179180
desc: "With unknown configured scopes",
180-
PATConfig: config.PATConfig{AllowedScopes: []string{"read_reposotory"}},
181+
PATConfig: config.PATConfig{AllowedScopes: []string{"read_reposotory"}}, //nolint:misspell //testing purpose
181182
arguments: &commandargs.Shell{
182183
GitlabKeyId: "default",
183184
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
@@ -191,18 +192,18 @@ func TestExecute(t *testing.T) {
191192
PATConfig: config.PATConfig{AllowedScopes: []string{"read_api", "read_repository"}},
192193
arguments: &commandargs.Shell{
193194
GitlabKeyId: "default",
194-
SshArgs: []string{cmdname, "newtoken", "read_api,read_reposotory"},
195+
SshArgs: []string{cmdname, "newtoken", "read_api,read_reposotory"}, //nolint:misspell //testing purpose
195196
},
196197
expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" +
197198
"Scopes: read_api\n" +
198199
"Expires: 9001-11-17\n",
199200
},
200201
{
201202
desc: "With matching unknown requested scopes",
202-
PATConfig: config.PATConfig{AllowedScopes: []string{"read_api", "read_reposotory"}},
203+
PATConfig: config.PATConfig{AllowedScopes: []string{"read_api", "read_reposotory"}}, //nolint:misspell //testing purpose
203204
arguments: &commandargs.Shell{
204205
GitlabKeyId: "invalidscope",
205-
SshArgs: []string{cmdname, "newtoken", "read_reposotory"},
206+
SshArgs: []string{cmdname, "newtoken", "read_reposotory"}, //nolint:misspell //testing purpose
206207
},
207208
expectedError: "Invalid scope: 'read_reposotory'. Valid scopes are: [\"api\", \"create_runner\", \"k8s_proxy\", \"read_api\", \"read_registry\", \"read_repository\", \"read_user\", \"write_registry\", \"write_repository\"]",
208209
},

internal/gitlabnet/personalaccesstoken/client_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"testing"
99

10+
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
1112
"gitlab.com/gitlab-org/gitlab-shell/v14/client"
1213
"gitlab.com/gitlab-org/gitlab-shell/v14/client/testserver"
@@ -27,7 +28,7 @@ func initialize(t *testing.T) {
2728
b, err := io.ReadAll(r.Body)
2829
defer r.Body.Close()
2930

30-
require.NoError(t, err)
31+
assert.NoError(t, err)
3132

3233
var requestBody *RequestBody
3334
json.Unmarshal(b, &requestBody)

support/lint_last_known_acceptable.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,6 @@ internal/command/lfstransfer/lfstransfer_test.go:1436:5: go-require: do not use
155155
internal/command/lfstransfer/lfstransfer_test.go:1451:5: go-require: do not use require in http handlers (testifylint)
156156
internal/command/lfstransfer/lfstransfer_test.go:1453:5: go-require: do not use require in http handlers (testifylint)
157157
internal/command/lfstransfer/lfstransfer_test.go:1454:5: go-require: do not use require in http handlers (testifylint)
158-
internal/command/personalaccesstoken/personalaccesstoken.go:1:1: package-comments: should have a package comment (revive)
159-
internal/command/personalaccesstoken/personalaccesstoken.go:25:6: exported: exported type Command should have comment or be unexported (revive)
160-
internal/command/personalaccesstoken/personalaccesstoken.go:38:1: exported: exported method Command.Execute should have comment or be unexported (revive)
161-
internal/command/personalaccesstoken/personalaccesstoken.go:53:12: Error return value of `fmt.Fprint` is not checked (errcheck)
162-
internal/command/personalaccesstoken/personalaccesstoken.go:54:12: Error return value of `fmt.Fprint` is not checked (errcheck)
163-
internal/command/personalaccesstoken/personalaccesstoken.go:55:12: Error return value of `fmt.Fprint` is not checked (errcheck)
164-
internal/command/personalaccesstoken/personalaccesstoken.go:62:10: ST1005: error strings should not be capitalized (stylecheck)
165-
internal/command/personalaccesstoken/personalaccesstoken.go:89:10: ST1005: error strings should not be capitalized (stylecheck)
166-
internal/command/personalaccesstoken/personalaccesstoken_test.go:31:5: go-require: do not use require in http handlers (testifylint)
167-
internal/command/personalaccesstoken/personalaccesstoken_test.go:180:62: `reposotory` is a misspelling of `repository` (misspell)
168-
internal/command/personalaccesstoken/personalaccesstoken_test.go:194:63: `reposotory` is a misspelling of `repository` (misspell)
169-
internal/command/personalaccesstoken/personalaccesstoken_test.go:202:74: `reposotory` is a misspelling of `repository` (misspell)
170-
internal/command/personalaccesstoken/personalaccesstoken_test.go:205:54: `reposotory` is a misspelling of `repository` (misspell)
171158
internal/command/readwriter/readwriter.go:1:1: package-comments: should have a package comment (revive)
172159
internal/command/readwriter/readwriter.go:7:6: exported: exported type ReadWriter should have comment or be unexported (revive)
173160
internal/command/receivepack/gitalycall_test.go:24:4: S1038: should use t.Logf(...) instead of t.Log(fmt.Sprintf(...)) (gosimple)
@@ -243,7 +230,6 @@ internal/gitlabnet/client.go:27:1: exported: exported function ParseJSON should
243230
internal/gitlabnet/client.go:35:1: exported: exported function ParseIP should have comment or be unexported (revive)
244231
internal/gitlabnet/healthcheck/client_test.go:19:41: unused-parameter: parameter 'r' seems to be unused, consider removing or renaming it as _ (revive)
245232
internal/gitlabnet/lfstransfer/client.go:137: internal/gitlabnet/lfstransfer/client.go:137: Line contains TODO/BUG/FIXME/NOTE/OPTIMIZE/HACK: "FIXME: This causes tests to fail" (godox)
246-
internal/gitlabnet/personalaccesstoken/client_test.go:30:5: go-require: do not use require in http handlers (testifylint)
247233
internal/sshd/server_config_test.go:5:2: SA1019: "crypto/dsa" has been deprecated since Go 1.16 because it shouldn't be used: DSA is a legacy algorithm, and modern alternatives such as Ed25519 (implemented by package crypto/ed25519) should be used instead. Keys with 1024-bit moduli (L1024N160 parameters) are cryptographically weak, while bigger keys are not widely supported. Note that FIPS 186-5 no longer approves DSA for signature generation. (staticcheck)
248234
internal/sshd/sshd.go:268:6: func `extractDataFromContext` is unused (unused)
249235
internal/testhelper/requesthandlers/requesthandlers.go:25:5: go-require: do not use require in http handlers (testifylint)

0 commit comments

Comments
 (0)