Skip to content

Commit 88f9575

Browse files
Add logic for key validation in report subcommand (#201)
1 parent ee624ec commit 88f9575

3 files changed

Lines changed: 52 additions & 15 deletions

File tree

command/report/report.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ func NewCmdReport() *cobra.Command {
5252
}
5353

5454
// --repo, -r flag
55-
cmd.Flags().StringVar(&opts.Analyzer, "analyzer", "", "The analyzer shortcode whose test coverage is being reported")
55+
cmd.Flags().StringVar(&opts.Analyzer, "analyzer", "", "name of the analyzer to report the artifact to (example: test-coverage)")
5656

57-
cmd.Flags().StringVar(&opts.Key, "key", "", "The artifact being reported.tcv for test-coverage")
57+
cmd.Flags().StringVar(&opts.Key, "key", "", "shortcode of the language (example: go)")
5858

59-
cmd.Flags().StringVar(&opts.ValueFile, "value-file", "", "Path to the value file")
59+
cmd.Flags().StringVar(&opts.Value, "value", "", "value of the artifact")
6060

61-
cmd.Flags().StringVar(&opts.Value, "value", "", "Value of the artifact")
61+
cmd.Flags().StringVar(&opts.ValueFile, "value-file", "", "path to the artifact value file")
6262

6363
// --skip-verify flag to skip SSL certificate verification while reporting test coverage data.
64-
cmd.Flags().BoolVar(&opts.SkipCertificateVerification, "skip-verify", false, "Skip SSL certificate verification while sending the test coverage data")
64+
cmd.Flags().BoolVar(&opts.SkipCertificateVerification, "skip-verify", false, "skip SSL certificate verification while sending the test coverage data")
6565

6666
return cmd
6767
}
@@ -81,10 +81,10 @@ func (opts *ReportOptions) Run() int {
8181
// Command: report //
8282
/////////////////////
8383

84-
reportCommandAnalyzerShortcode := opts.Analyzer
85-
reportCommandKey := opts.Key
84+
reportCommandAnalyzerShortcode := strings.TrimSpace(opts.Analyzer)
85+
reportCommandKey := strings.TrimSpace(opts.Key)
8686
reportCommandValue := opts.Value
87-
reportCommandValueFile := opts.ValueFile
87+
reportCommandValueFile := strings.TrimSpace(opts.ValueFile)
8888

8989
// Get current path
9090
currentDir, err := os.Getwd()
@@ -97,6 +97,38 @@ func (opts *ReportOptions) Run() int {
9797
scope.SetExtra("currentDir", currentDir)
9898
})
9999

100+
//////////////////
101+
// Validate Key //
102+
//////////////////
103+
104+
supportedKeys := map[string]bool{
105+
"python": true,
106+
"go": true,
107+
"javascript": true,
108+
"ruby": true,
109+
"java": true,
110+
"scala": true,
111+
"php": true,
112+
"csharp": true,
113+
"cxx": true,
114+
"rust": true,
115+
}
116+
117+
allowedKeys := func(m map[string]bool) []string {
118+
keys := make([]string, 0, len(supportedKeys))
119+
for k := range m {
120+
keys = append(keys, k)
121+
}
122+
return keys
123+
}
124+
125+
if !supportedKeys[reportCommandKey] {
126+
err = fmt.Errorf("DeepSource | Error | Invalid Key: %s (Supported Keys: %v)", reportCommandKey, allowedKeys(supportedKeys))
127+
fmt.Fprintln(os.Stderr, err)
128+
sentry.CaptureException(err)
129+
return 1
130+
}
131+
100132
//////////////////
101133
// Validate DSN //
102134
//////////////////

command/report/tests/report_workflow_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"os"
99
"os/exec"
1010
"testing"
11+
12+
"github.com/google/go-cmp/cmp"
1113
)
1214

1315
// Workflow tested:
@@ -50,9 +52,12 @@ func graphQLAPIMock(w http.ResponseWriter, r *http.Request) {
5052
w.WriteHeader(http.StatusOK)
5153
w.Header().Set("Content-Type", "application/json")
5254

53-
if string(requestBodyData) == string(req) {
55+
if want, got := string(requestBodyData), string(req); want == got {
5456
w.Write([]byte(successResponseBodyData))
5557
} else {
58+
if want != got {
59+
log.Printf("Mismatch found:\nDiff: %s\n", cmp.Diff(want, got))
60+
}
5661
w.Write([]byte(errorResponseBodyData))
5762
}
5863
}
@@ -63,7 +68,7 @@ func TestReportKeyValueWorkflow(t *testing.T) {
6368
// Read test artifact file
6469
data, err := os.ReadFile("/tmp/python_coverage.xml")
6570
if err != nil {
66-
t.Errorf(err.Error())
71+
t.Error(err)
6772
}
6873

6974
cmd := exec.Command("/tmp/deepsource",
@@ -102,8 +107,8 @@ func TestReportKeyValueWorkflow(t *testing.T) {
102107
t.Fatal(err)
103108
}
104109

105-
if string(output) != outStr {
106-
t.Errorf("Expected: %s, Got: %s", string(output), outStr)
110+
if want := string(output); want != outStr {
111+
t.Errorf("Expected: %s, Got: %s", want, outStr)
107112
}
108113
}
109114

@@ -145,7 +150,7 @@ func TestReportKeyValueFileWorkflow(t *testing.T) {
145150
t.Fatal(err)
146151
}
147152

148-
if string(output) != outStr {
149-
t.Errorf("Expected: %s, Got: %s", string(output), outStr)
153+
if want := string(output); want != outStr {
154+
t.Errorf("Expected: %s, Got: %s", want, outStr)
150155
}
151156
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/deepsourcelabs/graphql v0.2.2
1111
github.com/fatih/color v1.12.0
1212
github.com/getsentry/sentry-go v0.6.0
13+
github.com/google/go-cmp v0.5.5
1314
github.com/owenrumney/go-sarif/v2 v2.1.0
1415
github.com/pelletier/go-toml v1.9.2
1516
github.com/pterm/pterm v0.12.23
@@ -23,7 +24,6 @@ require (
2324
github.com/atomicgo/cursor v0.0.1 // indirect
2425
github.com/davecgh/go-spew v1.1.1 // indirect
2526
github.com/fsnotify/fsnotify v1.4.7 // indirect
26-
github.com/google/go-cmp v0.5.5 // indirect
2727
github.com/gookit/color v1.4.2 // indirect
2828
github.com/hashicorp/hcl v1.0.0 // indirect
2929
github.com/inconshreveable/mousetrap v1.0.0 // indirect

0 commit comments

Comments
 (0)