Skip to content

Commit

Permalink
Add logic for key validation in report subcommand (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
subham-deepsource authored Mar 3, 2023
1 parent ee624ec commit 88f9575
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
48 changes: 40 additions & 8 deletions command/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ func NewCmdReport() *cobra.Command {
}

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

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

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

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

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

return cmd
}
Expand All @@ -81,10 +81,10 @@ func (opts *ReportOptions) Run() int {
// Command: report //
/////////////////////

reportCommandAnalyzerShortcode := opts.Analyzer
reportCommandKey := opts.Key
reportCommandAnalyzerShortcode := strings.TrimSpace(opts.Analyzer)
reportCommandKey := strings.TrimSpace(opts.Key)
reportCommandValue := opts.Value
reportCommandValueFile := opts.ValueFile
reportCommandValueFile := strings.TrimSpace(opts.ValueFile)

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

//////////////////
// Validate Key //
//////////////////

supportedKeys := map[string]bool{
"python": true,
"go": true,
"javascript": true,
"ruby": true,
"java": true,
"scala": true,
"php": true,
"csharp": true,
"cxx": true,
"rust": true,
}

allowedKeys := func(m map[string]bool) []string {
keys := make([]string, 0, len(supportedKeys))
for k := range m {
keys = append(keys, k)
}
return keys
}

if !supportedKeys[reportCommandKey] {
err = fmt.Errorf("DeepSource | Error | Invalid Key: %s (Supported Keys: %v)", reportCommandKey, allowedKeys(supportedKeys))
fmt.Fprintln(os.Stderr, err)
sentry.CaptureException(err)
return 1
}

//////////////////
// Validate DSN //
//////////////////
Expand Down
17 changes: 11 additions & 6 deletions command/report/tests/report_workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"os/exec"
"testing"

"github.com/google/go-cmp/cmp"
)

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

if string(requestBodyData) == string(req) {
if want, got := string(requestBodyData), string(req); want == got {
w.Write([]byte(successResponseBodyData))
} else {
if want != got {
log.Printf("Mismatch found:\nDiff: %s\n", cmp.Diff(want, got))
}
w.Write([]byte(errorResponseBodyData))
}
}
Expand All @@ -63,7 +68,7 @@ func TestReportKeyValueWorkflow(t *testing.T) {
// Read test artifact file
data, err := os.ReadFile("/tmp/python_coverage.xml")
if err != nil {
t.Errorf(err.Error())
t.Error(err)
}

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

if string(output) != outStr {
t.Errorf("Expected: %s, Got: %s", string(output), outStr)
if want := string(output); want != outStr {
t.Errorf("Expected: %s, Got: %s", want, outStr)
}
}

Expand Down Expand Up @@ -145,7 +150,7 @@ func TestReportKeyValueFileWorkflow(t *testing.T) {
t.Fatal(err)
}

if string(output) != outStr {
t.Errorf("Expected: %s, Got: %s", string(output), outStr)
if want := string(output); want != outStr {
t.Errorf("Expected: %s, Got: %s", want, outStr)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/deepsourcelabs/graphql v0.2.2
github.com/fatih/color v1.12.0
github.com/getsentry/sentry-go v0.6.0
github.com/google/go-cmp v0.5.5
github.com/owenrumney/go-sarif/v2 v2.1.0
github.com/pelletier/go-toml v1.9.2
github.com/pterm/pterm v0.12.23
Expand All @@ -23,7 +24,6 @@ require (
github.com/atomicgo/cursor v0.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/gookit/color v1.4.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
Expand Down

0 comments on commit 88f9575

Please sign in to comment.