diff --git a/Makefile b/Makefile index 54d1d16b..1b589f13 100644 --- a/Makefile +++ b/Makefile @@ -5,9 +5,9 @@ build_local: cd cmd/deepsource && go build -tags static_all -o /tmp/deepsource . test: - CGO_ENABLED=0 go test -v ./command/report/tests/... -count=1 + CGO_ENABLED=1 go test -v ./command/report/tests/... -count=1 echo "\n====TESTING DEEPSOURCE PACKAGE====\n" - CGO_ENABLED=0 go test -v ./deepsource/tests/... + CGO_ENABLED=1 go test -v ./deepsource/tests/... echo "\n====TESTING CONFIG VALIDATOR PACKAGE====\n" go test -v ./configvalidator/... -count=1 echo "\n====CALCULATING TEST COVERAGE FOR ENTIRE PACKAGE====\n" diff --git a/command/report/constants.go b/command/report/constants.go index b13f79fd..5d7fc399 100644 --- a/command/report/constants.go +++ b/command/report/constants.go @@ -29,6 +29,7 @@ Notes: Documentation: https://deepsource.io/docs/cli#report ` - reportGraphqlQuery = "mutation($input: CreateArtifactInput!) {\r\n createArtifact(input: $input) {\r\n ok\r\n message\r\n error\r\n }\r\n}" - reportGraphqlQueryOld = "mutation($input: CreateArtifactInput!) {\r\n createArtifact(input: $input) {\r\n ok\r\n error\r\n }\r\n}" + reportGraphqlQuery = "mutation($input: CreateArtifactInput!) {\r\n createArtifact(input: $input) {\r\n ok\r\n message\r\n error\r\n }\r\n}" + reportGraphqlQueryOld = "mutation($input: CreateArtifactInput!) {\r\n createArtifact(input: $input) {\r\n ok\r\n error\r\n }\r\n}" + graphqlCheckCompressed = "query {\r\n __type(name: \"ArtifactMetadataInput\") {\r\n inputFields {\r\n name\r\n }\r\n }\r\n}" ) diff --git a/command/report/report.go b/command/report/report.go index 3e678ee9..24cfa3ca 100644 --- a/command/report/report.go +++ b/command/report/report.go @@ -1,6 +1,7 @@ package report import ( + "encoding/base64" "encoding/json" "errors" "fmt" @@ -8,6 +9,7 @@ import ( "strings" "time" + "github.com/DataDog/zstd" "github.com/MakeNowJust/heredoc" "github.com/deepsourcelabs/cli/utils" "github.com/getsentry/sentry-go" @@ -221,13 +223,74 @@ func (opts *ReportOptions) Run() int { artifactValue = string(valueBytes) } + // Query DeepSource API to check if compression is supported + q := ReportQuery{Query: graphqlCheckCompressed} + + qBytes, err := json.Marshal(q) + if err != nil { + fmt.Fprintln(os.Stderr, "DeepSource | Error | Failed to marshal query:", err) + sentry.CaptureException(err) + return 1 + } + + r, err := makeQuery( + dsnProtocol+"://"+dsnHost+"/graphql/cli/", + qBytes, + "application/json", + opts.SkipCertificateVerification, + ) + if err != nil { + fmt.Fprintln(os.Stderr, "DeepSource | Error | Failed to make query:", err) + sentry.CaptureException(err) + return 1 + } + + // res is a struct to unmarshal the response to check if compression is supported + var res struct { + Data struct { + Type struct { + InputFields []struct { + Name string `json:"name"` + } `json:"inputFields"` + } `json:"__type"` + } `json:"data"` + } + + err = json.Unmarshal(r, &res) + if err != nil { + fmt.Fprintln(os.Stderr, "DeepSource | Error | Failed to unmarshal response:", err) + sentry.CaptureException(err) + return 1 + } + + reportMeta := make(map[string]interface{}) + reportMeta["workDir"] = currentDir + + // Compress the value if compression is supported + for _, inputField := range res.Data.Type.InputFields { + if inputField.Name == "compressed" { + // Compress the byte array + var compressedBytes []byte + compressLevel := 20 + compressedBytes, err = zstd.CompressLevel(compressedBytes, []byte(artifactValue), compressLevel) + if err != nil { + fmt.Fprintln(os.Stderr, "DeepSource | Error | Failed to compress value file:", reportCommandValueFile) + sentry.CaptureException(err) + return 1 + } + + // Base64 encode the compressed byte array + artifactValue = base64.StdEncoding.EncodeToString(compressedBytes) + + // Set the compression flag + reportMeta["compressed"] = "True" + } + } + //////////////////// // Generate query // //////////////////// - reportMeta := make(map[string]string) - reportMeta["workDir"] = currentDir - queryInput := ReportQueryInput{ AccessToken: dsnAccessToken, CommitOID: headCommitOID, diff --git a/command/report/tests/golden_files/report_graphql_request_body.json b/command/report/tests/golden_files/report_graphql_request_body.json index a91c03bb..4dc42a7e 100644 --- a/command/report/tests/golden_files/report_graphql_request_body.json +++ b/command/report/tests/golden_files/report_graphql_request_body.json @@ -1 +1 @@ -{"query":"mutation($input: CreateArtifactInput!) {\r\n createArtifact(input: $input) {\r\n ok\r\n message\r\n error\r\n }\r\n}","variables":{"input":{"accessToken":"f59ab9314307","commitOid":"b7ff1a5ecb0dce0541b935224f852ee98570bbd4","reporter":"cli","reporterVersion":"v0.1.6","key":"python","data":"\u003c?xml version=\"1.0\" ?\u003e\n\u003ccoverage branch-rate=\"0.9333\" branches-covered=\"28\" branches-valid=\"30\" complexity=\"0\" line-rate=\"0.9839\" lines-covered=\"183\" lines-valid=\"186\" timestamp=\"1551180635459\" version=\"4.5.2\"\u003e\n \u003c!-- Generated by coverage.py: https://coverage.readthedocs.io --\u003e\n \u003c!-- Based on https://raw.githubusercontent.com/cobertura/web/master/htdocs/xml/coverage-04.dtd --\u003e\n \u003csources/\u003e\n \u003cpackages\u003e\n \u003cpackage branch-rate=\"0.9333\" complexity=\"0\" line-rate=\"0.9839\" name=\".Users.sanket.Code.s3tree.s3tree\"\u003e\n \u003cclasses\u003e\n \u003cclass branch-rate=\"1\" complexity=\"0\" filename=\"/code/s3tree/s3tree/__init__.py\" line-rate=\"1\" name=\"__init__.py\"\u003e\n \u003cmethods/\u003e\n \u003clines\u003e\n \u003cline hits=\"1\" number=\"4\"/\u003e\n \u003cline hits=\"1\" number=\"6\"/\u003e\n \u003cline hits=\"1\" number=\"7\"/\u003e\n \u003cline hits=\"1\" number=\"9\"/\u003e\n \u003cline hits=\"1\" number=\"10\"/\u003e\n \u003cline hits=\"1\" number=\"12\"/\u003e\n \u003c/lines\u003e\n \u003c/class\u003e\n \u003cclass branch-rate=\"1\" complexity=\"0\" filename=\"/code/s3tree/s3tree/__version__.py\" line-rate=\"1\" name=\"__version__.py\"\u003e\n \u003cmethods/\u003e\n \u003clines\u003e\n \u003cline hits=\"1\" number=\"4\"/\u003e\n \u003c/lines\u003e\n \u003c/class\u003e\n \u003cclass branch-rate=\"0.9375\" complexity=\"0\" filename=\"/code/s3tree/s3tree/core.py\" line-rate=\"0.9846\" name=\"core.py\"\u003e\n \u003cmethods/\u003e\n \u003clines\u003e\n \u003cline hits=\"1\" number=\"4\"/\u003e\n \u003cline hits=\"1\" number=\"5\"/\u003e\n \u003cline hits=\"1\" number=\"6\"/\u003e\n \u003cline hits=\"1\" number=\"7\"/\u003e\n \u003cline hits=\"1\" number=\"8\"/\u003e\n \u003cline hits=\"1\" number=\"10\"/\u003e\n \u003cline hits=\"1\" number=\"11\"/\u003e\n \u003cline hits=\"1\" number=\"13\"/\u003e\n \u003cline hits=\"1\" number=\"14\"/\u003e\n \u003cline hits=\"1\" number=\"17\"/\u003e\n \u003cline hits=\"1\" number=\"41\"/\u003e\n \u003cline hits=\"1\" number=\"43\"/\u003e\n \u003cline hits=\"1\" number=\"45\"/\u003e\n \u003cline hits=\"1\" number=\"49\"/\u003e\n \u003cline hits=\"1\" number=\"50\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"53\"/\u003e\n \u003cline hits=\"1\" number=\"54\"/\u003e\n \u003cline hits=\"1\" number=\"57\"/\u003e\n \u003cline hits=\"1\" number=\"61\"/\u003e\n \u003cline hits=\"1\" number=\"62\"/\u003e\n \u003cline hits=\"1\" number=\"65\"/\u003e\n \u003cline hits=\"1\" number=\"66\"/\u003e\n \u003cline hits=\"1\" number=\"69\"/\u003e\n \u003cline hits=\"1\" number=\"72\"/\u003e\n \u003cline hits=\"1\" number=\"73\"/\u003e\n \u003cline hits=\"1\" number=\"76\"/\u003e\n \u003cline hits=\"1\" number=\"78\"/\u003e\n \u003cline hits=\"1\" number=\"79\"/\u003e\n \u003cline hits=\"1\" number=\"81\"/\u003e\n \u003cline hits=\"1\" number=\"82\"/\u003e\n \u003cline hits=\"1\" number=\"84\"/\u003e\n \u003cline hits=\"1\" number=\"86\"/\u003e\n \u003cline hits=\"1\" number=\"90\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"94\"/\u003e\n \u003cline hits=\"1\" number=\"95\"/\u003e\n \u003cline hits=\"1\" number=\"99\"/\u003e\n \u003cline hits=\"1\" number=\"101\"/\u003e\n \u003cline hits=\"1\" number=\"104\"/\u003e\n \u003cline hits=\"1\" number=\"105\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"107\"/\u003e\n \u003cline hits=\"1\" number=\"108\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"110\"/\u003e\n \u003cline hits=\"1\" number=\"111\"/\u003e\n \u003cline hits=\"1\" number=\"113\"/\u003e\n \u003cline hits=\"1\" number=\"115\"/\u003e\n \u003cline hits=\"1\" number=\"120\"/\u003e\n \u003cline hits=\"1\" number=\"121\"/\u003e\n \u003cline hits=\"1\" number=\"122\"/\u003e\n \u003cline hits=\"1\" number=\"125\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"127\"/\u003e\n \u003cline hits=\"1\" number=\"128\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"50% (1/2)\" hits=\"1\" missing-branches=\"134\" number=\"130\"/\u003e\n \u003cline hits=\"1\" number=\"131\"/\u003e\n \u003cline hits=\"0\" number=\"134\"/\u003e\n \u003cline hits=\"1\" number=\"136\"/\u003e\n \u003cline hits=\"1\" number=\"139\"/\u003e\n \u003cline hits=\"1\" number=\"141\"/\u003e\n \u003cline hits=\"1\" number=\"144\"/\u003e\n \u003cline hits=\"1\" number=\"146\"/\u003e\n \u003cline hits=\"1\" number=\"149\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"151\"/\u003e\n \u003cline hits=\"1\" number=\"152\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"154\"/\u003e\n \u003cline hits=\"1\" number=\"155\"/\u003e\n \u003cline hits=\"1\" number=\"157\"/\u003e\n \u003c/lines\u003e\n \u003c/class\u003e\n \u003cclass branch-rate=\"1\" complexity=\"0\" filename=\"/code/s3tree/s3tree/exceptions.py\" line-rate=\"1\" name=\"exceptions.py\"\u003e\n \u003cmethods/\u003e\n \u003clines\u003e\n \u003cline hits=\"1\" number=\"5\"/\u003e\n \u003cline hits=\"1\" number=\"6\"/\u003e\n \u003cline hits=\"1\" number=\"7\"/\u003e\n \u003cline hits=\"1\" number=\"11\"/\u003e\n \u003cline hits=\"1\" number=\"14\"/\u003e\n \u003cline hits=\"1\" number=\"15\"/\u003e\n \u003cline hits=\"1\" number=\"16\"/\u003e\n \u003cline hits=\"1\" number=\"17\"/\u003e\n \u003cline hits=\"1\" number=\"20\"/\u003e\n \u003cline hits=\"1\" number=\"21\"/\u003e\n \u003cline hits=\"1\" number=\"22\"/\u003e\n \u003cline hits=\"1\" number=\"25\"/\u003e\n \u003cline hits=\"1\" number=\"28\"/\u003e\n \u003cline hits=\"1\" number=\"29\"/\u003e\n \u003cline hits=\"1\" number=\"30\"/\u003e\n \u003cline hits=\"1\" number=\"31\"/\u003e\n \u003cline hits=\"1\" number=\"34\"/\u003e\n \u003cline hits=\"1\" number=\"35\"/\u003e\n \u003cline hits=\"1\" number=\"36\"/\u003e\n \u003cline hits=\"1\" number=\"37\"/\u003e\n \u003cline hits=\"1\" number=\"40\"/\u003e\n \u003cline hits=\"1\" number=\"41\"/\u003e\n \u003cline hits=\"1\" number=\"42\"/\u003e\n \u003cline hits=\"1\" number=\"43\"/\u003e\n \u003c/lines\u003e\n \u003c/class\u003e\n \u003cclass branch-rate=\"1\" complexity=\"0\" filename=\"/code/s3tree/s3tree/models.py\" line-rate=\"0.9615\" name=\"models.py\"\u003e\n \u003cmethods/\u003e\n \u003clines\u003e\n \u003cline hits=\"1\" number=\"3\"/\u003e\n \u003cline hits=\"1\" number=\"5\"/\u003e\n \u003cline hits=\"1\" number=\"6\"/\u003e\n \u003cline hits=\"1\" number=\"7\"/\u003e\n \u003cline hits=\"1\" number=\"8\"/\u003e\n \u003cline hits=\"1\" number=\"9\"/\u003e\n \u003cline hits=\"1\" number=\"10\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"13\"/\u003e\n \u003cline hits=\"1\" number=\"14\"/\u003e\n \u003cline hits=\"1\" number=\"19\"/\u003e\n \u003cline hits=\"1\" number=\"20\"/\u003e\n \u003cline hits=\"1\" number=\"21\"/\u003e\n \u003cline hits=\"1\" number=\"23\"/\u003e\n \u003cline hits=\"1\" number=\"25\"/\u003e\n \u003cline hits=\"1\" number=\"27\"/\u003e\n \u003cline hits=\"1\" number=\"28\"/\u003e\n \u003cline hits=\"1\" number=\"30\"/\u003e\n \u003cline hits=\"1\" number=\"31\"/\u003e\n \u003cline hits=\"1\" number=\"37\"/\u003e\n \u003cline hits=\"1\" number=\"40\"/\u003e\n \u003cline hits=\"1\" number=\"45\"/\u003e\n \u003cline hits=\"1\" number=\"48\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"51\"/\u003e\n \u003cline hits=\"1\" number=\"52\"/\u003e\n \u003cline hits=\"1\" number=\"55\"/\u003e\n \u003cline hits=\"1\" number=\"57\"/\u003e\n \u003cline hits=\"1\" number=\"58\"/\u003e\n \u003cline hits=\"1\" number=\"59\"/\u003e\n \u003cline hits=\"1\" number=\"60\"/\u003e\n \u003cline hits=\"1\" number=\"61\"/\u003e\n \u003cline hits=\"1\" number=\"64\"/\u003e\n \u003cline hits=\"1\" number=\"67\"/\u003e\n \u003cline hits=\"1\" number=\"69\"/\u003e\n \u003cline hits=\"1\" number=\"72\"/\u003e\n \u003cline hits=\"1\" number=\"74\"/\u003e\n \u003cline hits=\"1\" number=\"77\"/\u003e\n \u003cline hits=\"1\" number=\"79\"/\u003e\n \u003cline hits=\"1\" number=\"82\"/\u003e\n \u003cline hits=\"1\" number=\"84\"/\u003e\n \u003cline hits=\"1\" number=\"86\"/\u003e\n \u003cline hits=\"1\" number=\"87\"/\u003e\n \u003cline hits=\"0\" number=\"90\"/\u003e\n \u003cline hits=\"0\" number=\"91\"/\u003e\n \u003cline hits=\"1\" number=\"93\"/\u003e\n \u003cline hits=\"1\" number=\"94\"/\u003e\n \u003cline hits=\"1\" number=\"96\"/\u003e\n \u003cline hits=\"1\" number=\"99\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"101\"/\u003e\n \u003cline hits=\"1\" number=\"102\"/\u003e\n \u003cline hits=\"1\" number=\"103\"/\u003e\n \u003cline hits=\"1\" number=\"105\"/\u003e\n \u003cline hits=\"1\" number=\"108\"/\u003e\n \u003c/lines\u003e\n \u003c/class\u003e\n \u003cclass branch-rate=\"0.5\" complexity=\"0\" filename=\"/code/s3tree/s3tree/types.py\" line-rate=\"1\" name=\"types.py\"\u003e\n \u003cmethods/\u003e\n \u003clines\u003e\n \u003cline hits=\"1\" number=\"3\"/\u003e\n \u003cline hits=\"1\" number=\"6\"/\u003e\n \u003cline hits=\"1\" number=\"8\"/\u003e\n \u003cline hits=\"1\" number=\"10\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"50% (1/2)\" hits=\"1\" missing-branches=\"14\" number=\"11\"/\u003e\n \u003cline hits=\"1\" number=\"12\"/\u003e\n \u003cline hits=\"1\" number=\"14\"/\u003e\n \u003cline hits=\"1\" number=\"17\"/\u003e\n \u003cline hits=\"1\" number=\"18\"/\u003e\n \u003cline hits=\"1\" number=\"21\"/\u003e\n \u003cline hits=\"1\" number=\"22\"/\u003e\n \u003cline hits=\"1\" number=\"23\"/\u003e\n \u003cline hits=\"1\" number=\"24\"/\u003e\n \u003c/lines\u003e\n \u003c/class\u003e\n \u003cclass branch-rate=\"1\" complexity=\"0\" filename=\"/code/s3tree/s3tree/utils.py\" line-rate=\"1\" name=\"utils.py\"\u003e\n \u003cmethods/\u003e\n \u003clines\u003e\n \u003cline hits=\"1\" number=\"3\"/\u003e\n \u003cline hits=\"1\" number=\"4\"/\u003e\n \u003cline hits=\"1\" number=\"5\"/\u003e\n \u003cline hits=\"1\" number=\"6\"/\u003e\n \u003cline hits=\"1\" number=\"8\"/\u003e\n \u003cline hits=\"1\" number=\"11\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"27\"/\u003e\n \u003cline hits=\"1\" number=\"28\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"30\"/\u003e\n \u003cline hits=\"1\" number=\"31\"/\u003e\n \u003cline hits=\"1\" number=\"34\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"37\"/\u003e\n \u003cline hits=\"1\" number=\"38\"/\u003e\n \u003cline hits=\"1\" number=\"40\"/\u003e\n \u003cline hits=\"1\" number=\"43\"/\u003e\n \u003cline hits=\"1\" number=\"56\"/\u003e\n \u003cline hits=\"1\" number=\"60\"/\u003e\n \u003cline hits=\"1\" number=\"65\"/\u003e\n \u003cline hits=\"1\" number=\"69\"/\u003e\n \u003cline hits=\"1\" number=\"73\"/\u003e\n \u003cline hits=\"1\" number=\"74\"/\u003e\n \u003cline hits=\"1\" number=\"76\"/\u003e\n \u003cline hits=\"1\" number=\"78\"/\u003e\n \u003cline hits=\"1\" number=\"81\"/\u003e\n \u003cline hits=\"1\" number=\"83\"/\u003e\n \u003c/lines\u003e\n \u003c/class\u003e\n \u003c/classes\u003e\n \u003c/package\u003e\n \u003c/packages\u003e\n\u003c/coverage\u003e\n","analyzer":"test-coverage","metadata":{"workDir":"/home/runner/code"}}}} \ No newline at end of file +{"query":"mutation($input: CreateArtifactInput!) {\r\n createArtifact(input: $input) {\r\n ok\r\n message\r\n error\r\n }\r\n}","variables":{"input":{"accessToken":"f59ab9314307","commitOid":"b7ff1a5ecb0dce0541b935224f852ee98570bbd4","reporter":"cli","reporterVersion":"v0.1.6","key":"python","data":"\u003c?xml version=\"1.0\" ?\u003e\n\u003ccoverage branch-rate=\"0.9333\" branches-covered=\"28\" branches-valid=\"30\" complexity=\"0\" line-rate=\"0.9839\" lines-covered=\"183\" lines-valid=\"186\" timestamp=\"1551180635459\" version=\"4.5.2\"\u003e\n \u003c!-- Generated by coverage.py: https://coverage.readthedocs.io --\u003e\n \u003c!-- Based on https://raw.githubusercontent.com/cobertura/web/master/htdocs/xml/coverage-04.dtd --\u003e\n \u003csources/\u003e\n \u003cpackages\u003e\n \u003cpackage branch-rate=\"0.9333\" complexity=\"0\" line-rate=\"0.9839\" name=\".Users.sanket.Code.s3tree.s3tree\"\u003e\n \u003cclasses\u003e\n \u003cclass branch-rate=\"1\" complexity=\"0\" filename=\"/code/s3tree/s3tree/__init__.py\" line-rate=\"1\" name=\"__init__.py\"\u003e\n \u003cmethods/\u003e\n \u003clines\u003e\n \u003cline hits=\"1\" number=\"4\"/\u003e\n \u003cline hits=\"1\" number=\"6\"/\u003e\n \u003cline hits=\"1\" number=\"7\"/\u003e\n \u003cline hits=\"1\" number=\"9\"/\u003e\n \u003cline hits=\"1\" number=\"10\"/\u003e\n \u003cline hits=\"1\" number=\"12\"/\u003e\n \u003c/lines\u003e\n \u003c/class\u003e\n \u003cclass branch-rate=\"1\" complexity=\"0\" filename=\"/code/s3tree/s3tree/__version__.py\" line-rate=\"1\" name=\"__version__.py\"\u003e\n \u003cmethods/\u003e\n \u003clines\u003e\n \u003cline hits=\"1\" number=\"4\"/\u003e\n \u003c/lines\u003e\n \u003c/class\u003e\n \u003cclass branch-rate=\"0.9375\" complexity=\"0\" filename=\"/code/s3tree/s3tree/core.py\" line-rate=\"0.9846\" name=\"core.py\"\u003e\n \u003cmethods/\u003e\n \u003clines\u003e\n \u003cline hits=\"1\" number=\"4\"/\u003e\n \u003cline hits=\"1\" number=\"5\"/\u003e\n \u003cline hits=\"1\" number=\"6\"/\u003e\n \u003cline hits=\"1\" number=\"7\"/\u003e\n \u003cline hits=\"1\" number=\"8\"/\u003e\n \u003cline hits=\"1\" number=\"10\"/\u003e\n \u003cline hits=\"1\" number=\"11\"/\u003e\n \u003cline hits=\"1\" number=\"13\"/\u003e\n \u003cline hits=\"1\" number=\"14\"/\u003e\n \u003cline hits=\"1\" number=\"17\"/\u003e\n \u003cline hits=\"1\" number=\"41\"/\u003e\n \u003cline hits=\"1\" number=\"43\"/\u003e\n \u003cline hits=\"1\" number=\"45\"/\u003e\n \u003cline hits=\"1\" number=\"49\"/\u003e\n \u003cline hits=\"1\" number=\"50\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"53\"/\u003e\n \u003cline hits=\"1\" number=\"54\"/\u003e\n \u003cline hits=\"1\" number=\"57\"/\u003e\n \u003cline hits=\"1\" number=\"61\"/\u003e\n \u003cline hits=\"1\" number=\"62\"/\u003e\n \u003cline hits=\"1\" number=\"65\"/\u003e\n \u003cline hits=\"1\" number=\"66\"/\u003e\n \u003cline hits=\"1\" number=\"69\"/\u003e\n \u003cline hits=\"1\" number=\"72\"/\u003e\n \u003cline hits=\"1\" number=\"73\"/\u003e\n \u003cline hits=\"1\" number=\"76\"/\u003e\n \u003cline hits=\"1\" number=\"78\"/\u003e\n \u003cline hits=\"1\" number=\"79\"/\u003e\n \u003cline hits=\"1\" number=\"81\"/\u003e\n \u003cline hits=\"1\" number=\"82\"/\u003e\n \u003cline hits=\"1\" number=\"84\"/\u003e\n \u003cline hits=\"1\" number=\"86\"/\u003e\n \u003cline hits=\"1\" number=\"90\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"94\"/\u003e\n \u003cline hits=\"1\" number=\"95\"/\u003e\n \u003cline hits=\"1\" number=\"99\"/\u003e\n \u003cline hits=\"1\" number=\"101\"/\u003e\n \u003cline hits=\"1\" number=\"104\"/\u003e\n \u003cline hits=\"1\" number=\"105\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"107\"/\u003e\n \u003cline hits=\"1\" number=\"108\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"110\"/\u003e\n \u003cline hits=\"1\" number=\"111\"/\u003e\n \u003cline hits=\"1\" number=\"113\"/\u003e\n \u003cline hits=\"1\" number=\"115\"/\u003e\n \u003cline hits=\"1\" number=\"120\"/\u003e\n \u003cline hits=\"1\" number=\"121\"/\u003e\n \u003cline hits=\"1\" number=\"122\"/\u003e\n \u003cline hits=\"1\" number=\"125\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"127\"/\u003e\n \u003cline hits=\"1\" number=\"128\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"50% (1/2)\" hits=\"1\" missing-branches=\"134\" number=\"130\"/\u003e\n \u003cline hits=\"1\" number=\"131\"/\u003e\n \u003cline hits=\"0\" number=\"134\"/\u003e\n \u003cline hits=\"1\" number=\"136\"/\u003e\n \u003cline hits=\"1\" number=\"139\"/\u003e\n \u003cline hits=\"1\" number=\"141\"/\u003e\n \u003cline hits=\"1\" number=\"144\"/\u003e\n \u003cline hits=\"1\" number=\"146\"/\u003e\n \u003cline hits=\"1\" number=\"149\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"151\"/\u003e\n \u003cline hits=\"1\" number=\"152\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"154\"/\u003e\n \u003cline hits=\"1\" number=\"155\"/\u003e\n \u003cline hits=\"1\" number=\"157\"/\u003e\n \u003c/lines\u003e\n \u003c/class\u003e\n \u003cclass branch-rate=\"1\" complexity=\"0\" filename=\"/code/s3tree/s3tree/exceptions.py\" line-rate=\"1\" name=\"exceptions.py\"\u003e\n \u003cmethods/\u003e\n \u003clines\u003e\n \u003cline hits=\"1\" number=\"5\"/\u003e\n \u003cline hits=\"1\" number=\"6\"/\u003e\n \u003cline hits=\"1\" number=\"7\"/\u003e\n \u003cline hits=\"1\" number=\"11\"/\u003e\n \u003cline hits=\"1\" number=\"14\"/\u003e\n \u003cline hits=\"1\" number=\"15\"/\u003e\n \u003cline hits=\"1\" number=\"16\"/\u003e\n \u003cline hits=\"1\" number=\"17\"/\u003e\n \u003cline hits=\"1\" number=\"20\"/\u003e\n \u003cline hits=\"1\" number=\"21\"/\u003e\n \u003cline hits=\"1\" number=\"22\"/\u003e\n \u003cline hits=\"1\" number=\"25\"/\u003e\n \u003cline hits=\"1\" number=\"28\"/\u003e\n \u003cline hits=\"1\" number=\"29\"/\u003e\n \u003cline hits=\"1\" number=\"30\"/\u003e\n \u003cline hits=\"1\" number=\"31\"/\u003e\n \u003cline hits=\"1\" number=\"34\"/\u003e\n \u003cline hits=\"1\" number=\"35\"/\u003e\n \u003cline hits=\"1\" number=\"36\"/\u003e\n \u003cline hits=\"1\" number=\"37\"/\u003e\n \u003cline hits=\"1\" number=\"40\"/\u003e\n \u003cline hits=\"1\" number=\"41\"/\u003e\n \u003cline hits=\"1\" number=\"42\"/\u003e\n \u003cline hits=\"1\" number=\"43\"/\u003e\n \u003c/lines\u003e\n \u003c/class\u003e\n \u003cclass branch-rate=\"1\" complexity=\"0\" filename=\"/code/s3tree/s3tree/models.py\" line-rate=\"0.9615\" name=\"models.py\"\u003e\n \u003cmethods/\u003e\n \u003clines\u003e\n \u003cline hits=\"1\" number=\"3\"/\u003e\n \u003cline hits=\"1\" number=\"5\"/\u003e\n \u003cline hits=\"1\" number=\"6\"/\u003e\n \u003cline hits=\"1\" number=\"7\"/\u003e\n \u003cline hits=\"1\" number=\"8\"/\u003e\n \u003cline hits=\"1\" number=\"9\"/\u003e\n \u003cline hits=\"1\" number=\"10\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"13\"/\u003e\n \u003cline hits=\"1\" number=\"14\"/\u003e\n \u003cline hits=\"1\" number=\"19\"/\u003e\n \u003cline hits=\"1\" number=\"20\"/\u003e\n \u003cline hits=\"1\" number=\"21\"/\u003e\n \u003cline hits=\"1\" number=\"23\"/\u003e\n \u003cline hits=\"1\" number=\"25\"/\u003e\n \u003cline hits=\"1\" number=\"27\"/\u003e\n \u003cline hits=\"1\" number=\"28\"/\u003e\n \u003cline hits=\"1\" number=\"30\"/\u003e\n \u003cline hits=\"1\" number=\"31\"/\u003e\n \u003cline hits=\"1\" number=\"37\"/\u003e\n \u003cline hits=\"1\" number=\"40\"/\u003e\n \u003cline hits=\"1\" number=\"45\"/\u003e\n \u003cline hits=\"1\" number=\"48\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"51\"/\u003e\n \u003cline hits=\"1\" number=\"52\"/\u003e\n \u003cline hits=\"1\" number=\"55\"/\u003e\n \u003cline hits=\"1\" number=\"57\"/\u003e\n \u003cline hits=\"1\" number=\"58\"/\u003e\n \u003cline hits=\"1\" number=\"59\"/\u003e\n \u003cline hits=\"1\" number=\"60\"/\u003e\n \u003cline hits=\"1\" number=\"61\"/\u003e\n \u003cline hits=\"1\" number=\"64\"/\u003e\n \u003cline hits=\"1\" number=\"67\"/\u003e\n \u003cline hits=\"1\" number=\"69\"/\u003e\n \u003cline hits=\"1\" number=\"72\"/\u003e\n \u003cline hits=\"1\" number=\"74\"/\u003e\n \u003cline hits=\"1\" number=\"77\"/\u003e\n \u003cline hits=\"1\" number=\"79\"/\u003e\n \u003cline hits=\"1\" number=\"82\"/\u003e\n \u003cline hits=\"1\" number=\"84\"/\u003e\n \u003cline hits=\"1\" number=\"86\"/\u003e\n \u003cline hits=\"1\" number=\"87\"/\u003e\n \u003cline hits=\"0\" number=\"90\"/\u003e\n \u003cline hits=\"0\" number=\"91\"/\u003e\n \u003cline hits=\"1\" number=\"93\"/\u003e\n \u003cline hits=\"1\" number=\"94\"/\u003e\n \u003cline hits=\"1\" number=\"96\"/\u003e\n \u003cline hits=\"1\" number=\"99\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"101\"/\u003e\n \u003cline hits=\"1\" number=\"102\"/\u003e\n \u003cline hits=\"1\" number=\"103\"/\u003e\n \u003cline hits=\"1\" number=\"105\"/\u003e\n \u003cline hits=\"1\" number=\"108\"/\u003e\n \u003c/lines\u003e\n \u003c/class\u003e\n \u003cclass branch-rate=\"0.5\" complexity=\"0\" filename=\"/code/s3tree/s3tree/types.py\" line-rate=\"1\" name=\"types.py\"\u003e\n \u003cmethods/\u003e\n \u003clines\u003e\n \u003cline hits=\"1\" number=\"3\"/\u003e\n \u003cline hits=\"1\" number=\"6\"/\u003e\n \u003cline hits=\"1\" number=\"8\"/\u003e\n \u003cline hits=\"1\" number=\"10\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"50% (1/2)\" hits=\"1\" missing-branches=\"14\" number=\"11\"/\u003e\n \u003cline hits=\"1\" number=\"12\"/\u003e\n \u003cline hits=\"1\" number=\"14\"/\u003e\n \u003cline hits=\"1\" number=\"17\"/\u003e\n \u003cline hits=\"1\" number=\"18\"/\u003e\n \u003cline hits=\"1\" number=\"21\"/\u003e\n \u003cline hits=\"1\" number=\"22\"/\u003e\n \u003cline hits=\"1\" number=\"23\"/\u003e\n \u003cline hits=\"1\" number=\"24\"/\u003e\n \u003c/lines\u003e\n \u003c/class\u003e\n \u003cclass branch-rate=\"1\" complexity=\"0\" filename=\"/code/s3tree/s3tree/utils.py\" line-rate=\"1\" name=\"utils.py\"\u003e\n \u003cmethods/\u003e\n \u003clines\u003e\n \u003cline hits=\"1\" number=\"3\"/\u003e\n \u003cline hits=\"1\" number=\"4\"/\u003e\n \u003cline hits=\"1\" number=\"5\"/\u003e\n \u003cline hits=\"1\" number=\"6\"/\u003e\n \u003cline hits=\"1\" number=\"8\"/\u003e\n \u003cline hits=\"1\" number=\"11\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"27\"/\u003e\n \u003cline hits=\"1\" number=\"28\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"30\"/\u003e\n \u003cline hits=\"1\" number=\"31\"/\u003e\n \u003cline hits=\"1\" number=\"34\"/\u003e\n \u003cline branch=\"true\" condition-coverage=\"100% (2/2)\" hits=\"1\" number=\"37\"/\u003e\n \u003cline hits=\"1\" number=\"38\"/\u003e\n \u003cline hits=\"1\" number=\"40\"/\u003e\n \u003cline hits=\"1\" number=\"43\"/\u003e\n \u003cline hits=\"1\" number=\"56\"/\u003e\n \u003cline hits=\"1\" number=\"60\"/\u003e\n \u003cline hits=\"1\" number=\"65\"/\u003e\n \u003cline hits=\"1\" number=\"69\"/\u003e\n \u003cline hits=\"1\" number=\"73\"/\u003e\n \u003cline hits=\"1\" number=\"74\"/\u003e\n \u003cline hits=\"1\" number=\"76\"/\u003e\n \u003cline hits=\"1\" number=\"78\"/\u003e\n \u003cline hits=\"1\" number=\"81\"/\u003e\n \u003cline hits=\"1\" number=\"83\"/\u003e\n \u003c/lines\u003e\n \u003c/class\u003e\n \u003c/classes\u003e\n \u003c/package\u003e\n \u003c/packages\u003e\n\u003c/coverage\u003e\n","analyzer":"test-coverage","metadata":{"workDir":"/home/runner/code","compressed":"True"}}}} \ No newline at end of file diff --git a/command/report/tests/golden_files/report_grqphql_artifactmetadatainput_response_success.json b/command/report/tests/golden_files/report_grqphql_artifactmetadatainput_response_success.json new file mode 100644 index 00000000..31b6e94c --- /dev/null +++ b/command/report/tests/golden_files/report_grqphql_artifactmetadatainput_response_success.json @@ -0,0 +1,14 @@ +{ + "data": { + "__type": { + "inputFields": [ + { + "name": "workDir" + }, + { + "name": "compressed" + } + ] + } + } +} diff --git a/command/report/tests/report_workflow_test.go b/command/report/tests/report_workflow_test.go index 010e5555..1d62e6d3 100644 --- a/command/report/tests/report_workflow_test.go +++ b/command/report/tests/report_workflow_test.go @@ -2,6 +2,8 @@ package tests import ( "bytes" + "encoding/base64" + "encoding/json" "io" "log" "net/http" @@ -9,6 +11,8 @@ import ( "os/exec" "testing" + "github.com/DataDog/zstd" + "github.com/deepsourcelabs/cli/command/report" "github.com/google/go-cmp/cmp" ) @@ -26,39 +30,97 @@ const ( ) func graphQLAPIMock(w http.ResponseWriter, r *http.Request) { - req, _ := io.ReadAll(r.Body) - - // Read test graphql request body artifact file - requestBodyData, err := os.ReadFile("./golden_files/report_graphql_request_body.json") + // Read request request request body + req, err := io.ReadAll(r.Body) if err != nil { log.Println(err) return } - // Read test graphql success response body artifact file - successResponseBodyData, err := os.ReadFile("./golden_files/report_graphql_success_response_body.json") - if err != nil { - log.Println(err) - return - } + log.Println("Request body: ", string(req)) - // Read test graphql error response body artifact file - errorResponseBodyData, err := os.ReadFile("./golden_files/report_graphql_error_response_body.json") - if err != nil { - log.Println(err) - return - } - - w.WriteHeader(http.StatusOK) - w.Header().Set("Content-Type", "application/json") + // Check if the request has ArtifactMetadataInput in body + if bytes.Contains(req, []byte("ArtifactMetadataInput")) { + log.Println("ArtifactMetadataInput found in request body") + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json") - if want, got := string(requestBodyData), string(req); want == got { + successResponseBodyData, err := os.ReadFile("./golden_files/report_grqphql_artifactmetadatainput_response_success.json") + if err != nil { + log.Println(err) + return + } w.Write([]byte(successResponseBodyData)) + } else { - if want != got { - log.Printf("Mismatch found:\nDiff: %s\n", cmp.Diff(want, got)) + + // Unmarshal request body into ReportQuery + var reportQuery report.ReportQuery + err = json.Unmarshal(req, &reportQuery) + if err != nil { + log.Println(err) + return + } + + requestData := reportQuery.Variables.Input.Data + + // Decode base64 encoded data + decodedData, err := base64.StdEncoding.DecodeString(requestData) + if err != nil { + log.Println(err) + return + } + + // Decompress zstd compressed data + decompressedData, err := zstd.Decompress(nil, decodedData) + if err != nil { + log.Println(err) + return + } + + // Create new ReportQeury object with decompressed data + reportQuery.Variables.Input.Data = string(decompressedData) + + // Read test graphql request body artifact file + requestBodyData, err := os.ReadFile("./golden_files/report_graphql_request_body.json") + if err != nil { + log.Println(err) + return + } + + // Unmarshal request body into ReportQuery + var requestReportQuery report.ReportQuery + err = json.Unmarshal(requestBodyData, &requestReportQuery) + if err != nil { + log.Println(err) + return + } + + // Read test graphql success response body artifact file + successResponseBodyData, err := os.ReadFile("./golden_files/report_graphql_success_response_body.json") + if err != nil { + log.Println(err) + return + } + + // Read test graphql error response body artifact file + errorResponseBodyData, err := os.ReadFile("./golden_files/report_graphql_error_response_body.json") + if err != nil { + log.Println(err) + return + } + + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json") + + if want, got := requestReportQuery, reportQuery; cmp.Equal(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)) } - w.Write([]byte(errorResponseBodyData)) } } diff --git a/go.mod b/go.mod index 30940e23..a9bfdd93 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.18 require ( github.com/AlecAivazis/survey/v2 v2.2.12 + github.com/DataDog/zstd v1.5.5 github.com/Jeffail/gabs/v2 v2.6.1 github.com/MakeNowJust/heredoc v1.0.0 github.com/cli/browser v1.1.0 diff --git a/go.sum b/go.sum index 7d4c7534..37630dda 100644 --- a/go.sum +++ b/go.sum @@ -18,6 +18,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/CloudyKit/fastprinter v0.0.0-20170127035650-74b38d55f37a/go.mod h1:EFZQ978U7x8IRnstaskI3IysnWY5Ao3QgZUKOXlsAdw= github.com/CloudyKit/jet v2.1.3-0.20180809161101-62edd43e4f88+incompatible/go.mod h1:HPYO+50pSWkPoj9Q/eq0aRGByCL6ScRlUmiEX5Zgm+w= +github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ= +github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw= github.com/Jeffail/gabs/v2 v2.6.1 h1:wwbE6nTQTwIMsMxzi6XFQQYRZ6wDc1mSdxoAN+9U4Gk= github.com/Jeffail/gabs/v2 v2.6.1/go.mod h1:xCn81vdHKxFUuWWAaD5jCTQDNPBMh5pPs9IJ+NcziBI= github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY=