From a65c8d82cb6d1682d4cd5b711d4981be192240bd Mon Sep 17 00:00:00 2001 From: Faye Date: Wed, 21 Aug 2024 16:58:53 +0200 Subject: [PATCH] Update tests for new command --- cmd/kosli/attestSonar_test.go | 128 +++++++++++------- cmd/kosli/attestation.go | 2 +- .../sonarcloud/.scannerwork/report-task.txt | 7 + .../sonarqube/.scannerwork/report-task.txt | 6 + internal/requests/requests.go | 2 +- internal/sonar/sonar.go | 5 +- 6 files changed, 100 insertions(+), 50 deletions(-) create mode 100644 cmd/kosli/testdata/sonar/sonarcloud/.scannerwork/report-task.txt create mode 100644 cmd/kosli/testdata/sonar/sonarqube/.scannerwork/report-task.txt diff --git a/cmd/kosli/attestSonar_test.go b/cmd/kosli/attestSonar_test.go index 6e438da8a..4168b9944 100644 --- a/cmd/kosli/attestSonar_test.go +++ b/cmd/kosli/attestSonar_test.go @@ -12,7 +12,18 @@ import ( * The sonar API token for SonarCloud and SonarQube will always be different, so we need * to have a separate test suite for each version of the command. This means we can easily * skip the SonarQube tests when we're testing SonarCloud (with the SonarCloud API token), - * and vice-versa. */ + * and vice-versa. + * Note that if you want to run the SonarQube tests, there are a few steps to take: + * 1. Set the environment variable SONARQUBE to something (value doesn't matter) + * so we know which test suite to use. + * 2. Set up an instance of SonarQube (e.g. on localhost), with a project that has been + * scanned at least once. + * 3. Replace testdata/sonar/sonarqube/.scannerwork/report-task.txt with the report-task.txt + * from your sonarqube project (this should be located in a .scannerwork folder in + * the base directory of your project) + * 4. In the final two tests, where the CE-task-url flag is provided, replace the current + * CE-task-url with the one for your project's scan. This can be found in your + * report-task.txt file. */ type AttestSonarCommandTestSuite struct { flowName string @@ -32,8 +43,8 @@ type AttestSonarQubeCommandTestSuite struct { func (suite *AttestSonarCommandTestSuite) SetupTest() { testHelpers.SkipIfEnvVarUnset(suite.T(), []string{"KOSLI_SONAR_API_TOKEN"}) - // If we have the sonarqube url set, we're testing SonarQube and therefore should skip the SonarCloud tests - testHelpers.SkipIfEnvVarSet(suite.T(), []string{"KOSLI_SONARQUBE_URL"}) + // If we have SONARQUBE set (e.g. to true), we're testing SonarQube and therefore should skip the SonarCloud tests + testHelpers.SkipIfEnvVarSet(suite.T(), []string{"SONARQUBE"}) suite.flowName = "attest-sonar" suite.trailName = "test-123" suite.artifactFingerprint = "7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9" @@ -49,7 +60,7 @@ func (suite *AttestSonarCommandTestSuite) SetupTest() { } func (suite *AttestSonarQubeCommandTestSuite) SetupTest() { - testHelpers.SkipIfEnvVarUnset(suite.T(), []string{"KOSLI_SONAR_API_TOKEN", "KOSLI_SONARQUBE_URL"}) + testHelpers.SkipIfEnvVarUnset(suite.T(), []string{"KOSLI_SONAR_API_TOKEN", "SONARQUBE"}) suite.flowName = "attest-sonar" suite.trailName = "test-123" suite.artifactFingerprint = "7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9" @@ -69,63 +80,92 @@ func (suite *AttestSonarCommandTestSuite) TestAttestSonarCmd() { { wantError: true, name: "fails when more arguments are provided", - cmd: fmt.Sprintf("attest sonar foo bar --sonar-working-dir testdata/sonar/.scannerwork %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar foo bar --sonar-working-dir testdata/sonar/sonarcloud/.scannerwork %s", suite.defaultKosliArguments), golden: "Error: accepts at most 1 arg(s), received 2 [foo bar]\n", }, { wantError: true, name: "fails when both --fingerprint and --artifact-type", - cmd: fmt.Sprintf("attest sonar testdata/file1 --fingerprint xxxx --artifact-type file --name bar --commit HEAD --origin-url example.com --sonar-working-dir testdata/sonar/.scannerwork %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar testdata/file1 --fingerprint xxxx --artifact-type file --name bar --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarcloud/.scannerwork %s", suite.defaultKosliArguments), golden: "Error: only one of --fingerprint, --artifact-type is allowed\n", }, { wantError: true, name: "fails when --fingerprint is not valid", - cmd: fmt.Sprintf("attest sonar --name foo-s --fingerprint xxxx --commit HEAD --origin-url example.com --sonar-working-dir testdata/sonar/.scannerwork %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar --name foo-s --fingerprint xxxx --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarcloud/.scannerwork %s", suite.defaultKosliArguments), golden: "Error: xxxx is not a valid SHA256 fingerprint. It should match the pattern ^([a-f0-9]{64})$\nUsage: kosli attest sonar [IMAGE-NAME | FILE-PATH | DIR-PATH] [flags]\n", }, { wantError: true, name: "attesting against an artifact that does not exist fails", - cmd: fmt.Sprintf("attest sonar --fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo --commit HEAD --origin-url example.com --sonar-working-dir testdata/sonar/.scannerwork %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar --fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarcloud/.scannerwork %s", suite.defaultKosliArguments), golden: "Error: Artifact with fingerprint '1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9' does not exist in flow 'attest-sonar' belonging to organization 'docs-cmd-test-user'\n", }, { name: "can attest sonar against an artifact using artifact name and --artifact-type", - cmd: fmt.Sprintf("attest sonar testdata/file1 --artifact-type file --name foo --commit HEAD --origin-url example.com --sonar-working-dir testdata/sonar/.scannerwork %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar testdata/file1 --artifact-type file --name foo --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarcloud/.scannerwork %s", suite.defaultKosliArguments), golden: "sonar attestation 'foo' is reported to trail: test-123\n", }, { name: "can attest sonar against an artifact using artifact name and --artifact-type when --name does not exist in the trail template", - cmd: fmt.Sprintf("attest sonar testdata/file1 --artifact-type file --name bar --commit HEAD --origin-url example.com --sonar-working-dir testdata/sonar/.scannerwork %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar testdata/file1 --artifact-type file --name bar --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarcloud/.scannerwork %s", suite.defaultKosliArguments), golden: "sonar attestation 'bar' is reported to trail: test-123\n", }, { name: "can attest sonar against an artifact using --fingerprint", - cmd: fmt.Sprintf("attest sonar --fingerprint 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo --commit HEAD --origin-url example.com --sonar-working-dir testdata/sonar/.scannerwork %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar --fingerprint 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarcloud/.scannerwork %s", suite.defaultKosliArguments), golden: "sonar attestation 'foo' is reported to trail: test-123\n", }, { name: "can attest sonar against a trail", - cmd: fmt.Sprintf("attest sonar --name bar --commit HEAD --origin-url example.com --sonar-working-dir testdata/sonar/.scannerwork %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar --name bar --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarcloud/.scannerwork %s", suite.defaultKosliArguments), golden: "sonar attestation 'bar' is reported to trail: test-123\n", }, { name: "can attest sonar against a trail when name is not found in the trail template", - cmd: fmt.Sprintf("attest sonar --name additional --commit HEAD --origin-url example.com --sonar-working-dir testdata/sonar/.scannerwork %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar --name additional --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarcloud/.scannerwork %s", suite.defaultKosliArguments), golden: "sonar attestation 'additional' is reported to trail: test-123\n", }, { name: "can attest sonar against an artifact it is created using dot syntax in --name", - cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url example.com --sonar-working-dir testdata/sonar/.scannerwork %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarcloud/.scannerwork %s", suite.defaultKosliArguments), golden: "sonar attestation 'foo' is reported to trail: test-123\n", }, { wantError: true, name: "trying to fetch data from SonarCloud with incorrect API token gives error", - cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url example.com --sonar-api-token xxxx --sonar-working-dir testdata/sonar/.scannerwork %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com --sonar-api-token xxxx --sonar-working-dir testdata/sonar/sonarcloud/.scannerwork %s", suite.defaultKosliArguments), golden: "Error: please check your API token is correct and you have the correct permissions in SonarCloud/SonarQube\n", }, + { + wantError: true, + name: "if no path to the scannerwork directory is provided and the command is not being run in the same base directory (and no CE task URL is provided), we get an error", + cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com %s", suite.defaultKosliArguments), + golden: "Error: report-task.txt not found. Check your working directory is set correctly: open .scannerwork/report-task.txt: no such file or directory\n", + }, + { + wantError: true, + name: "if incorrect path to the scannerwork directory is provided (and no CE task URL is provided), we get an error", + cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com --sonar-working-dir sonar/.scannerwork %s", suite.defaultKosliArguments), + golden: "Error: report-task.txt not found. Check your working directory is set correctly: open sonar/.scannerwork/report-task.txt: no such file or directory\n", + }, + { + name: "can retrieve scan results using provided CE task URL and attest them", + cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com --CE-task-url 'https://sonarcloud.io/api/ce/task?id=AZE2jzvUF2N-a1ygL1sM' %s", suite.defaultKosliArguments), + golden: "sonar attestation 'foo' is reported to trail: test-123\n", + }, + { + wantError: true, + name: "if incorrect CE task URL given, we get an error", + cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com --CE-task-url 'https://sonarcloud.io/api/ce/task?id=AZE2jzvUF2N-a1ygL1sm' %s", suite.defaultKosliArguments), + golden: "Error: analysis ID not found. Please check the ceTaskURL is correct\n", + }, + { + wantError: true, + name: "if outdated task given (i.e. we try to get results for an older scan that SonarCloud has deleted), we get an error", + cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com --CE-task-url 'https://sonarcloud.io/api/ce/task?id=AZERk4uWpzGpahwkB9ac' %s", suite.defaultKosliArguments), + golden: "Error: analysis with ID AZERk4xKSYJCvL0vWjio not found. Snapshot has most likely been deleted by Sonar\n", + }, } runTestCmd(suite.T(), tests) @@ -136,91 +176,85 @@ func (suite *AttestSonarQubeCommandTestSuite) TestAttestSonarQubeCmd() { { wantError: true, name: "fails when more arguments are provided", - cmd: fmt.Sprintf("attest sonar foo bar %s", suite.defaultKosliArguments), - golden: "Error: accepts at most 1 arg(s), received 2\n", + cmd: fmt.Sprintf("attest sonar foo bar --sonar-working-dir testdata/sonar/sonarqube/.scannerwork %s", suite.defaultKosliArguments), + golden: "Error: accepts at most 1 arg(s), received 2 [foo bar]\n", }, { wantError: true, name: "fails when both --fingerprint and --artifact-type", - cmd: fmt.Sprintf("attest sonar testdata/file1 --fingerprint xxxx --artifact-type file --name bar --commit HEAD --origin-url example.com %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar testdata/file1 --fingerprint xxxx --artifact-type file --name bar --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarqube/.scannerwork %s", suite.defaultKosliArguments), golden: "Error: only one of --fingerprint, --artifact-type is allowed\n", }, { wantError: true, name: "fails when --fingerprint is not valid", - cmd: fmt.Sprintf("attest sonar --name foo-s --fingerprint xxxx --commit HEAD --origin-url example.com --sonar-project-key cyber-dojo_differ %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar --name foo-s --fingerprint xxxx --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarqube/.scannerwork %s", suite.defaultKosliArguments), golden: "Error: xxxx is not a valid SHA256 fingerprint. It should match the pattern ^([a-f0-9]{64})$\nUsage: kosli attest sonar [IMAGE-NAME | FILE-PATH | DIR-PATH] [flags]\n", }, { wantError: true, name: "attesting against an artifact that does not exist fails", - cmd: fmt.Sprintf("attest sonar --fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo --commit HEAD --origin-url example.com --sonar-project-key test %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar --fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarqube/.scannerwork %s", suite.defaultKosliArguments), golden: "Error: Artifact with fingerprint '1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9' does not exist in flow 'attest-sonar' belonging to organization 'docs-cmd-test-user'\n", }, { name: "can attest sonar against an artifact using artifact name and --artifact-type", - cmd: fmt.Sprintf("attest sonar testdata/file1 --artifact-type file --name foo --commit HEAD --origin-url example.com --sonar-project-key test %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar testdata/file1 --artifact-type file --name foo --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarqube/.scannerwork %s", suite.defaultKosliArguments), golden: "sonar attestation 'foo' is reported to trail: test-123\n", }, { name: "can attest sonar against an artifact using artifact name and --artifact-type when --name does not exist in the trail template", - cmd: fmt.Sprintf("attest sonar testdata/file1 --artifact-type file --name bar --commit HEAD --origin-url example.com --sonar-project-key test %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar testdata/file1 --artifact-type file --name bar --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarqube/.scannerwork %s", suite.defaultKosliArguments), golden: "sonar attestation 'bar' is reported to trail: test-123\n", }, { name: "can attest sonar against an artifact using --fingerprint", - cmd: fmt.Sprintf("attest sonar --fingerprint 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo --commit HEAD --origin-url example.com --sonar-project-key test %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar --fingerprint 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarqube/.scannerwork %s", suite.defaultKosliArguments), golden: "sonar attestation 'foo' is reported to trail: test-123\n", }, { name: "can attest sonar against a trail", - cmd: fmt.Sprintf("attest sonar --name bar --commit HEAD --origin-url example.com --sonar-project-key test %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar --name bar --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarqube/.scannerwork %s", suite.defaultKosliArguments), golden: "sonar attestation 'bar' is reported to trail: test-123\n", }, { name: "can attest sonar against a trail when name is not found in the trail template", - cmd: fmt.Sprintf("attest sonar --name additional --commit HEAD --origin-url example.com --sonar-project-key test %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar --name additional --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarqube/.scannerwork %s", suite.defaultKosliArguments), golden: "sonar attestation 'additional' is reported to trail: test-123\n", }, { name: "can attest sonar against an artifact it is created using dot syntax in --name", - cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url example.com --sonar-project-key test %s", suite.defaultKosliArguments), + cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarqube/.scannerwork %s", suite.defaultKosliArguments), golden: "sonar attestation 'foo' is reported to trail: test-123\n", }, { wantError: true, - name: "trying to fetch data from SonarQube with incorrect SonarQube URL gives error", - cmd: fmt.Sprintf("attest sonar --name foo --commit HEAD --origin-url example.com --sonar-project-key test --sonarqube-url example.com/ %s", suite.defaultKosliArguments), - golden: "Error: Incorrect SonarQube URL\n", - }, - { - name: "can attest sonar with sonarqube url against an artifact it is created using dot syntax in --name", - cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url example.com --sonar-project-key test %s", suite.defaultKosliArguments), - golden: "sonar attestation 'foo' is reported to trail: test-123\n", + name: "trying to fetch data from SonarQube with incorrect API token gives error", + cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com --sonar-api-token xxxx --sonar-working-dir testdata/sonar/sonarqube/.scannerwork %s", suite.defaultKosliArguments), + golden: "Error: please check your API token is correct and you have the correct permissions in SonarCloud/SonarQube\n", }, { wantError: true, - name: "trying to fetch data from SonarQube with incorrect API token gives error", - cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url example.com --sonar-project-key test --sonar-api-token xxxx %s", suite.defaultKosliArguments), - golden: "Error: Incorrect API token or SonarQube URL\n", + name: "if no path to the scannerwork directory is provided and the command is not being run in the same base directory (and no CE task URL is provided), we get an error", + cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com %s", suite.defaultKosliArguments), + golden: "Error: report-task.txt not found. Check your working directory is set correctly: open .scannerwork/report-task.txt: no such file or directory\n", }, { wantError: true, - name: "trying to fetch data from SonarQube for a non-existent project gives error", - cmd: fmt.Sprintf("attest sonar --fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo --commit HEAD --origin-url example.com --sonar-project-key cyber-dojo_differ %s", suite.defaultKosliArguments), - golden: "Error: Component key 'cyber-dojo_differ' not found\n", + name: "if incorrect path to the scannerwork directory is provided (and no CE task URL is provided), we get an error", + cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com --sonar-working-dir sonar/.scannerwork %s", suite.defaultKosliArguments), + golden: "Error: report-task.txt not found. Check your working directory is set correctly: open sonar/.scannerwork/report-task.txt: no such file or directory\n", }, { - wantError: true, - name: "trying to fetch data from SonarQube for a non-existent branch gives error", - cmd: fmt.Sprintf("attest sonar --fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo --commit HEAD --origin-url example.com --sonar-project-key test --branch-name xx %s", suite.defaultKosliArguments), - golden: "Error: Component 'test' on branch 'xx' not found\n", + name: "can retrieve scan results using provided CE task URL and attest them", + cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com --CE-task-url 'http://localhost:9000/api/ce/task?id=9427d05e-a671-4942-95c4-ff0595b6f0fe' %s", suite.defaultKosliArguments), + golden: "sonar attestation 'foo' is reported to trail: test-123\n", }, { wantError: true, - name: "trying to fetch data from SonarQube for a non-existent pull-request gives error", - cmd: fmt.Sprintf("attest sonar --fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo --commit HEAD --origin-url example.com --sonar-project-key test --pull-request-id 5 %s", suite.defaultKosliArguments), - golden: "Error: Component 'test' of pull request '5' not found\n", + name: "if incorrect CE task URL given, we get an error", + cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com --CE-task-url 'http://localhost:9000/api/ce/task?id=9427d05e-a671-4942-95c4-ff0595b6f0ff' %s", suite.defaultKosliArguments), + golden: "Error: analysis ID not found. Please check the ceTaskURL is correct\n", }, } diff --git a/cmd/kosli/attestation.go b/cmd/kosli/attestation.go index d7fe697ad..bb96c728b 100644 --- a/cmd/kosli/attestation.go +++ b/cmd/kosli/attestation.go @@ -169,7 +169,7 @@ func newAttestationForm(payload interface{}, attachments []string) ( func wrapAttestationError(err error) error { if err != nil { - return fmt.Errorf(strings.Replace(err.Error(), "requires at least one of: artifact_fingerprint or git_commit_info.", + return fmt.Errorf("%s", strings.Replace(err.Error(), "requires at least one of: artifact_fingerprint or git_commit_info.", "requires at least one of: specifying the fingerprint (either by calculating it using the artifact name/path and --artifact-type, or by providing it using --fingerprint) or providing --commit (requires an available git repo to access commit details)", 1)) } return err diff --git a/cmd/kosli/testdata/sonar/sonarcloud/.scannerwork/report-task.txt b/cmd/kosli/testdata/sonar/sonarcloud/.scannerwork/report-task.txt new file mode 100644 index 000000000..8e87ea732 --- /dev/null +++ b/cmd/kosli/testdata/sonar/sonarcloud/.scannerwork/report-task.txt @@ -0,0 +1,7 @@ +organization=fayesgw +projectKey=FayeSGW_Hangman +serverUrl=https://sonarcloud.io +serverVersion=8.0.0.56246 +dashboardUrl=https://sonarcloud.io/dashboard?id=FayeSGW_Hangman +ceTaskId=AZE2jzvUF2N-a1ygL1sM +ceTaskUrl=https://sonarcloud.io/api/ce/task?id=AZE2jzvUF2N-a1ygL1sM diff --git a/cmd/kosli/testdata/sonar/sonarqube/.scannerwork/report-task.txt b/cmd/kosli/testdata/sonar/sonarqube/.scannerwork/report-task.txt new file mode 100644 index 000000000..cf761582b --- /dev/null +++ b/cmd/kosli/testdata/sonar/sonarqube/.scannerwork/report-task.txt @@ -0,0 +1,6 @@ +projectKey=test5 +serverUrl=http://localhost:9000 +serverVersion=10.6.0.92116 +dashboardUrl=http://localhost:9000/dashboard?id=test5 +ceTaskId=9427d05e-a671-4942-95c4-ff0595b6f0fe +ceTaskUrl=http://localhost:9000/api/ce/task?id=9427d05e-a671-4942-95c4-ff0595b6f0fe diff --git a/internal/requests/requests.go b/internal/requests/requests.go index f4eec2320..2704a1a70 100644 --- a/internal/requests/requests.go +++ b/internal/requests/requests.go @@ -233,7 +233,7 @@ func (c *Client) Do(p *RequestParams) (*HTTPResponse, error) { cleanedErrorMessage = fmt.Sprintf("%s", respBodyMap) } } - return nil, fmt.Errorf(cleanedErrorMessage) + return nil, fmt.Errorf("%s", cleanedErrorMessage) } return &HTTPResponse{string(body), resp}, nil } diff --git a/internal/sonar/sonar.go b/internal/sonar/sonar.go index 277f7d0cc..bfdc51402 100644 --- a/internal/sonar/sonar.go +++ b/internal/sonar/sonar.go @@ -42,7 +42,6 @@ type Branch struct { type QualityGate struct { Status string `json:"status"` Conditions []Condition `json:"conditions"` - //Name string `json:"name"` I cannot find a way to find out which quality gate was used for a specific scan } type Condition struct { @@ -203,6 +202,10 @@ func GetCETaskData(httpClient *http.Client, project *Project, sonarResults *Sona analysisId := taskResponseData.Task.AnalysisID sonarResults.Status = taskResponseData.Task.Status + if analysisId == "" { + return "", fmt.Errorf("analysis ID not found. Please check the ceTaskURL is correct") + } + if project.Url == "" { project.Url = fmt.Sprintf("%s/dashboard?id=%s", sonarResults.ServerUrl, project.Key) }