Skip to content

Commit 316b3f7

Browse files
Merge branch 'master' into self-updater
Signed-off-by: Sourya Vatsyayan <sourya@deepsource.io>
2 parents 71812c4 + 51594ab commit 316b3f7

21 files changed

Lines changed: 439 additions & 60 deletions

File tree

.deepsource.toml

Lines changed: 0 additions & 13 deletions
This file was deleted.

command/auth/login/login.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/MakeNowJust/heredoc"
88
"github.com/deepsourcelabs/cli/command/cmddeps"
9+
"github.com/deepsourcelabs/cli/command/cmdutil"
910
"github.com/deepsourcelabs/cli/config"
1011
"github.com/deepsourcelabs/cli/deepsource"
1112
"github.com/deepsourcelabs/cli/internal/cli/args"
@@ -59,7 +60,7 @@ func NewCmdLoginWithDeps(deps *cmddeps.Deps) *cobra.Command {
5960
Long: doc,
6061
Args: args.NoArgs,
6162
RunE: func(cmd *cobra.Command, args []string) error {
62-
return opts.Run()
63+
return opts.Run(cmd)
6364
},
6465
}
6566

@@ -72,7 +73,7 @@ func NewCmdLoginWithDeps(deps *cmddeps.Deps) *cobra.Command {
7273
return cmd
7374
}
7475

75-
func (opts *LoginOptions) Run() (err error) {
76+
func (opts *LoginOptions) Run(cmd *cobra.Command) (err error) {
7677
var cfgMgr *config.Manager
7778
if opts.deps != nil && opts.deps.ConfigMgr != nil {
7879
cfgMgr = opts.deps.ConfigMgr
@@ -94,6 +95,9 @@ func (opts *LoginOptions) Run() (err error) {
9495
opts.User = cfg.User
9596
opts.TokenExpired = cfg.IsExpired()
9697

98+
// Resolve skip-tls-verify: flag > env > config
99+
cfg.SkipTLSVerify = cmdutil.ResolveSkipTLSVerify(cmd, cfg.SkipTLSVerify)
100+
97101
opts.verifyTokenWithServer(cfg, svc)
98102

99103
if opts.Interactive {

command/auth/login/tests/login_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,69 @@ func TestLoginVerifyTokenServerReject(t *testing.T) {
245245
}
246246
}
247247

248+
func TestLoginPATSkipTLSVerifyNotPersisted(t *testing.T) {
249+
cfgMgr := testutil.CreateExpiredTestConfigManager(t, "", "deepsource.com", "")
250+
251+
deps := &cmddeps.Deps{
252+
ConfigMgr: cfgMgr,
253+
Client: newMockViewerClient(t),
254+
}
255+
256+
cmd := loginCmd.NewCmdLoginWithDeps(deps)
257+
cmd.SetArgs([]string{"--with-token", "dsp_noskip"})
258+
259+
if err := cmd.Execute(); err != nil {
260+
t.Fatalf("unexpected error: %v", err)
261+
}
262+
263+
cfg, err := cfgMgr.Load()
264+
if err != nil {
265+
t.Fatalf("failed to load config: %v", err)
266+
}
267+
if cfg.SkipTLSVerify {
268+
t.Error("expected SkipTLSVerify=false when flag is not set")
269+
}
270+
}
271+
272+
func TestLoginPATSkipTLSVerifyFromConfig(t *testing.T) {
273+
// Write a config with SkipTLSVerify already set to true
274+
tmpDir := t.TempDir()
275+
fs := adapters.NewOSFileSystem()
276+
cfgMgr := config.NewManager(fs, func() (string, error) {
277+
return tmpDir, nil
278+
})
279+
280+
// Use manager.Write to create valid TOML with expired token + SkipTLSVerify
281+
initialCfg := &config.CLIConfig{
282+
Host: "enterprise.example.com",
283+
Token: "",
284+
SkipTLSVerify: true,
285+
}
286+
if err := cfgMgr.Write(initialCfg); err != nil {
287+
t.Fatalf("failed to write initial config: %v", err)
288+
}
289+
290+
deps := &cmddeps.Deps{
291+
ConfigMgr: cfgMgr,
292+
Client: newMockViewerClient(t),
293+
}
294+
295+
cmd := loginCmd.NewCmdLoginWithDeps(deps)
296+
cmd.SetArgs([]string{"--with-token", "dsp_skipyes"})
297+
298+
if err := cmd.Execute(); err != nil {
299+
t.Fatalf("unexpected error: %v", err)
300+
}
301+
302+
cfg, err := cfgMgr.Load()
303+
if err != nil {
304+
t.Fatalf("failed to load config: %v", err)
305+
}
306+
if !cfg.SkipTLSVerify {
307+
t.Error("expected SkipTLSVerify=true to be preserved from config after login")
308+
}
309+
}
310+
248311
func TestLoginPATInvalidToken(t *testing.T) {
249312
cfgMgr := testutil.CreateExpiredTestConfigManager(t, "", "deepsource.com", "")
250313

command/auth/status/status.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import (
99

1010
"github.com/MakeNowJust/heredoc"
1111
"github.com/deepsourcelabs/cli/command/cmddeps"
12+
"github.com/deepsourcelabs/cli/command/cmdutil"
1213
"github.com/deepsourcelabs/cli/config"
1314
"github.com/deepsourcelabs/cli/deepsource"
1415
dsuser "github.com/deepsourcelabs/cli/deepsource/user"
1516
"github.com/deepsourcelabs/cli/internal/cli/args"
1617
"github.com/deepsourcelabs/cli/internal/cli/style"
1718
clierrors "github.com/deepsourcelabs/cli/internal/errors"
1819
"github.com/spf13/cobra"
20+
1921
)
2022

2123
type AuthStatusOptions struct {
@@ -47,13 +49,13 @@ func NewCmdStatusWithDeps(deps *cmddeps.Deps) *cobra.Command {
4749
Args: args.NoArgs,
4850
RunE: func(cmd *cobra.Command, args []string) error {
4951
opts := AuthStatusOptions{deps: deps}
50-
return opts.Run()
52+
return opts.Run(cmd)
5153
},
5254
}
5355
return cmd
5456
}
5557

56-
func (opts *AuthStatusOptions) Run() error {
58+
func (opts *AuthStatusOptions) Run(cmd *cobra.Command) error {
5759
cfgMgr := opts.configManager()
5860
cfg, err := cfgMgr.Load()
5961
if err != nil {
@@ -68,7 +70,7 @@ func (opts *AuthStatusOptions) Run() error {
6870
return nil
6971
}
7072

71-
client, err := opts.apiClient(cfg, cfgMgr)
73+
client, err := opts.apiClient(cmd, cfg, cfgMgr)
7274
if err != nil {
7375
style.Warnf(opts.stdout(), "Could not connect to DeepSource to verify authentication")
7476
return nil
@@ -100,14 +102,15 @@ func (opts *AuthStatusOptions) configManager() *config.Manager {
100102
return config.DefaultManager()
101103
}
102104

103-
func (opts *AuthStatusOptions) apiClient(cfg *config.CLIConfig, cfgMgr *config.Manager) (*deepsource.Client, error) {
105+
func (opts *AuthStatusOptions) apiClient(cmd *cobra.Command, cfg *config.CLIConfig, cfgMgr *config.Manager) (*deepsource.Client, error) {
104106
if opts.deps != nil && opts.deps.Client != nil {
105107
return opts.deps.Client, nil
106108
}
107109
return deepsource.New(deepsource.ClientOpts{
108-
Token: cfg.Token,
109-
HostName: cfg.Host,
110-
OnTokenRefreshed: cfgMgr.TokenRefreshCallback(),
110+
Token: cfg.Token,
111+
HostName: cfg.Host,
112+
InsecureSkipVerify: cmdutil.ResolveSkipTLSVerify(cmd, cfg.SkipTLSVerify),
113+
OnTokenRefreshed: cfgMgr.TokenRefreshCallback(),
111114
})
112115
}
113116

command/cmdutil/tls.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cmdutil
2+
3+
import "github.com/spf13/cobra"
4+
5+
// ResolveSkipTLSVerify returns true if TLS verification should be skipped.
6+
// Priority: --skip-tls-verify flag > config value (which includes env var).
7+
func ResolveSkipTLSVerify(cmd *cobra.Command, cfgValue bool) bool {
8+
if cmd != nil {
9+
if f := cmd.Root().PersistentFlags().Lookup("skip-tls-verify"); f != nil && f.Changed {
10+
return true
11+
}
12+
}
13+
return cfgValue
14+
}

command/cmdutil/tls_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package cmdutil
2+
3+
import (
4+
"testing"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func TestResolveSkipTLSVerify(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
setupCmd func() *cobra.Command
13+
cfgValue bool
14+
want bool
15+
}{
16+
{
17+
name: "nil cmd, cfgValue false",
18+
setupCmd: func() *cobra.Command { return nil },
19+
cfgValue: false,
20+
want: false,
21+
},
22+
{
23+
name: "nil cmd, cfgValue true",
24+
setupCmd: func() *cobra.Command { return nil },
25+
cfgValue: true,
26+
want: true,
27+
},
28+
{
29+
name: "cmd without skip-tls-verify flag, cfgValue false",
30+
setupCmd: func() *cobra.Command {
31+
return &cobra.Command{Use: "test"}
32+
},
33+
cfgValue: false,
34+
want: false,
35+
},
36+
{
37+
name: "cmd without skip-tls-verify flag, cfgValue true",
38+
setupCmd: func() *cobra.Command {
39+
return &cobra.Command{Use: "test"}
40+
},
41+
cfgValue: true,
42+
want: true,
43+
},
44+
{
45+
name: "cmd with flag not set, cfgValue false",
46+
setupCmd: func() *cobra.Command {
47+
root := &cobra.Command{Use: "root"}
48+
root.PersistentFlags().Bool("skip-tls-verify", false, "")
49+
child := &cobra.Command{Use: "child"}
50+
root.AddCommand(child)
51+
return child
52+
},
53+
cfgValue: false,
54+
want: false,
55+
},
56+
{
57+
name: "cmd with flag not set, cfgValue true",
58+
setupCmd: func() *cobra.Command {
59+
root := &cobra.Command{Use: "root"}
60+
root.PersistentFlags().Bool("skip-tls-verify", false, "")
61+
child := &cobra.Command{Use: "child"}
62+
root.AddCommand(child)
63+
return child
64+
},
65+
cfgValue: true,
66+
want: true,
67+
},
68+
{
69+
name: "cmd with flag set, cfgValue false",
70+
setupCmd: func() *cobra.Command {
71+
root := &cobra.Command{Use: "root"}
72+
root.PersistentFlags().Bool("skip-tls-verify", false, "")
73+
child := &cobra.Command{Use: "child"}
74+
root.AddCommand(child)
75+
// Simulate the flag being set on the command line
76+
root.PersistentFlags().Set("skip-tls-verify", "true")
77+
return child
78+
},
79+
cfgValue: false,
80+
want: true,
81+
},
82+
{
83+
name: "cmd with flag set, cfgValue true",
84+
setupCmd: func() *cobra.Command {
85+
root := &cobra.Command{Use: "root"}
86+
root.PersistentFlags().Bool("skip-tls-verify", false, "")
87+
child := &cobra.Command{Use: "child"}
88+
root.AddCommand(child)
89+
root.PersistentFlags().Set("skip-tls-verify", "true")
90+
return child
91+
},
92+
cfgValue: true,
93+
want: true,
94+
},
95+
}
96+
97+
for _, tt := range tests {
98+
t.Run(tt.name, func(t *testing.T) {
99+
cmd := tt.setupCmd()
100+
got := ResolveSkipTLSVerify(cmd, tt.cfgValue)
101+
if got != tt.want {
102+
t.Errorf("ResolveSkipTLSVerify() = %v, want %v", got, tt.want)
103+
}
104+
})
105+
}
106+
}

command/flags_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ type flagExpectation struct {
1515
defaultValue string
1616
}
1717

18+
func TestRootSkipTLSVerifyFlag(t *testing.T) {
19+
cmd := NewCmdRoot()
20+
f := cmd.PersistentFlags().Lookup("skip-tls-verify")
21+
if f == nil {
22+
t.Fatal("expected skip-tls-verify persistent flag on root command, got nil")
23+
}
24+
if f.DefValue != "false" {
25+
t.Errorf("expected default value %q, got %q", "false", f.DefValue)
26+
}
27+
}
28+
1829
func TestFlagDefaults(t *testing.T) {
1930
tests := []struct {
2031
name string

command/issues/issues.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func NewCmdIssuesWithDeps(deps *cmddeps.Deps) *cobra.Command {
9292
Short: "View issues in a repository",
9393
Long: doc,
9494
RunE: func(cmd *cobra.Command, _ []string) error {
95-
return opts.Run(cmd.Context())
95+
return opts.Run(cmd.Context(), cmd)
9696
},
9797
}
9898

@@ -203,7 +203,7 @@ func flagUsageLine(f *pflag.Flag) string {
203203
return line
204204
}
205205

206-
func (opts *IssuesOptions) initClientAndConfig() (*deepsource.Client, *vcs.RemoteData, error) {
206+
func (opts *IssuesOptions) initClientAndConfig(cmd *cobra.Command) (*deepsource.Client, *vcs.RemoteData, error) {
207207
var cfgMgr *config.Manager
208208
if opts.deps != nil && opts.deps.ConfigMgr != nil {
209209
cfgMgr = opts.deps.ConfigMgr
@@ -228,18 +228,19 @@ func (opts *IssuesOptions) initClientAndConfig() (*deepsource.Client, *vcs.Remot
228228
return opts.deps.Client, remote, nil
229229
}
230230
client, err := deepsource.New(deepsource.ClientOpts{
231-
Token: cfg.Token,
232-
HostName: cfg.Host,
233-
OnTokenRefreshed: cfgMgr.TokenRefreshCallback(),
231+
Token: cfg.Token,
232+
HostName: cfg.Host,
233+
InsecureSkipVerify: cmdutil.ResolveSkipTLSVerify(cmd, cfg.SkipTLSVerify),
234+
OnTokenRefreshed: cfgMgr.TokenRefreshCallback(),
234235
})
235236
if err != nil {
236237
return nil, nil, err
237238
}
238239
return client, remote, nil
239240
}
240241

241-
func (opts *IssuesOptions) Run(ctx context.Context) error {
242-
client, remote, err := opts.initClientAndConfig()
242+
func (opts *IssuesOptions) Run(ctx context.Context, cmd *cobra.Command) error {
243+
client, remote, err := opts.initClientAndConfig(cmd)
243244
if err != nil {
244245
return err
245246
}

command/metrics/metrics.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func NewCmdMetricsWithDeps(deps *cmddeps.Deps) *cobra.Command {
8787
Short: "View repository metrics",
8888
Long: doc,
8989
RunE: func(cmd *cobra.Command, _ []string) error {
90-
return opts.Run(cmd.Context())
90+
return opts.Run(cmd.Context(), cmd)
9191
},
9292
}
9393

@@ -116,7 +116,7 @@ func NewCmdMetricsWithDeps(deps *cmddeps.Deps) *cobra.Command {
116116
return cmd
117117
}
118118

119-
func (opts *MetricsOptions) Run(ctx context.Context) error {
119+
func (opts *MetricsOptions) Run(ctx context.Context, cmd *cobra.Command) error {
120120
var cfgMgr *config.Manager
121121
if opts.deps != nil && opts.deps.ConfigMgr != nil {
122122
cfgMgr = opts.deps.ConfigMgr
@@ -142,9 +142,10 @@ func (opts *MetricsOptions) Run(ctx context.Context) error {
142142
client = opts.deps.Client
143143
} else {
144144
client, err = deepsource.New(deepsource.ClientOpts{
145-
Token: cfg.Token,
146-
HostName: cfg.Host,
147-
OnTokenRefreshed: cfgMgr.TokenRefreshCallback(),
145+
Token: cfg.Token,
146+
HostName: cfg.Host,
147+
InsecureSkipVerify: cmdutil.ResolveSkipTLSVerify(cmd, cfg.SkipTLSVerify),
148+
OnTokenRefreshed: cfgMgr.TokenRefreshCallback(),
148149
})
149150
if err != nil {
150151
return err

0 commit comments

Comments
 (0)