From 74b2d501b229e81fc17a846fc06ffe1c7b869891 Mon Sep 17 00:00:00 2001 From: tiagobcx Date: Mon, 25 Sep 2023 15:57:25 +0100 Subject: [PATCH 01/11] adding the support for gitlab pr decoration --- cmd/main.go | 3 +- internal/commands/util/help_test.go | 2 +- internal/commands/util/pr.go | 84 +++++++++++++++++++++++++++-- internal/params/binds.go | 1 + internal/params/envs.go | 1 + internal/params/flags.go | 14 ++--- internal/params/keys.go | 1 + internal/wrappers/pr-http.go | 22 +++++++- internal/wrappers/pr.go | 10 ++++ test/integration/pr_test.go | 59 ++++++++++++++++++-- 10 files changed, 182 insertions(+), 15 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 756a2902d..72ad6852d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -40,6 +40,7 @@ func main() { codebashing := viper.GetString(params.CodeBashingPathKey) bfl := viper.GetString(params.BflPathKey) prDecorationGithubPath := viper.GetString(params.PRDecorationGithubPathKey) + prDecorationGitlabPath := viper.GetString(params.PRDecorationGitlabPathKey) descriptionsPath := viper.GetString(params.DescriptionsPathKey) tenantConfigurationPath := viper.GetString(params.TenantConfigurationPathKey) resultsPdfPath := viper.GetString(params.ResultsPdfReportPathKey) @@ -66,7 +67,7 @@ func main() { bitBucketServerWrapper := bitbucketserver.NewBitbucketServerWrapper() gitLabWrapper := wrappers.NewGitLabWrapper() bflWrapper := wrappers.NewBflHTTPWrapper(bfl) - prWrapper := wrappers.NewHTTPPRWrapper(prDecorationGithubPath) + prWrapper := wrappers.NewHTTPPRWrapper(prDecorationGithubPath, prDecorationGitlabPath) learnMoreWrapper := wrappers.NewHTTPLearnMoreWrapper(descriptionsPath) tenantConfigurationWrapper := wrappers.NewHTTPTenantConfigurationWrapper(tenantConfigurationPath) jwtWrapper := wrappers.NewJwtWrapper() diff --git a/internal/commands/util/help_test.go b/internal/commands/util/help_test.go index c3b6119fd..1d8bfe01a 100644 --- a/internal/commands/util/help_test.go +++ b/internal/commands/util/help_test.go @@ -4,7 +4,7 @@ import ( "testing" ) -//TODO: can we assert something? +// TODO: can we assert something? func TestRootHelpFunc(t *testing.T) { cmd := NewConfigCommand() cmd.Long = "" diff --git a/internal/commands/util/pr.go b/internal/commands/util/pr.go index 68d49b1ed..d0400ee47 100644 --- a/internal/commands/util/pr.go +++ b/internal/commands/util/pr.go @@ -11,8 +11,9 @@ import ( ) const ( - failedCreatingPrDecoration = "Failed creating PR Decoration" - errorCodeFormat = "%s: CODE: %d, %s\n" + failedCreatingGithubPrDecoration = "Failed creating github PR Decoration" + failedCreatingGitlabPrDecoration = "Failed creating gitlab PR Decoration" + errorCodeFormat = "%s: CODE: %d, %s\n" ) func NewPRDecorationCommand(prWrapper wrappers.PRWrapper) *cobra.Command { @@ -27,8 +28,10 @@ func NewPRDecorationCommand(prWrapper wrappers.PRWrapper) *cobra.Command { } prDecorationGithub := PRDecorationGithub(prWrapper) + prDecorationGitlab := PRDecorationGitlab(prWrapper) cmd.AddCommand(prDecorationGithub) + cmd.AddCommand(prDecorationGitlab) return cmd } @@ -71,6 +74,47 @@ func PRDecorationGithub(prWrapper wrappers.PRWrapper) *cobra.Command { return prDecorationGithub } +func PRDecorationGitlab(prWrapper wrappers.PRWrapper) *cobra.Command { + prDecorationGitlab := &cobra.Command{ + Use: "gitlab", + Short: "Decorate gitlab PR with vulnerabilities", + Long: "Decorate gitlab PR with vulnerabilities", + Example: heredoc.Doc( + ` + $ cx utils pr gitlab --scan-id --token --namespace --repo-name + --iid --gitlab-project < gitlab project ID > + `, + ), + Annotations: map[string]string{ + "command:doc": heredoc.Doc( + ` + `, + ), + }, + RunE: runPRDecorationGitlab(prWrapper), + } + + prDecorationGitlab.Flags().String(params.ScanIDFlag, "", "Scan ID to retrieve results from") + prDecorationGitlab.Flags().String(params.SCMTokenFlag, "", params.GithubTokenUsage) + prDecorationGitlab.Flags().String(params.NamespaceFlag, "", params.NamespaceFlagUsage) + prDecorationGitlab.Flags().String(params.RepoNameFlag, "", params.RepoNameFlagUsage) + prDecorationGitlab.Flags().Int(params.PRIidFlag, 0, params.PRNumberFlagUsage) + prDecorationGitlab.Flags().Int(params.PRGitlabProjectFlag, 0, params.PRNumberFlagUsage) + + // Set the value for token to mask the scm token + _ = viper.BindPFlag(params.SCMTokenFlag, prDecorationGitlab.Flags().Lookup(params.SCMTokenFlag)) + + // mark all fields as required\ + _ = prDecorationGitlab.MarkFlagRequired(params.ScanIDFlag) + _ = prDecorationGitlab.MarkFlagRequired(params.SCMTokenFlag) + _ = prDecorationGitlab.MarkFlagRequired(params.NamespaceFlag) + _ = prDecorationGitlab.MarkFlagRequired(params.RepoNameFlag) + _ = prDecorationGitlab.MarkFlagRequired(params.PRIidFlag) + _ = prDecorationGitlab.MarkFlagRequired(params.PRGitlabProjectFlag) + + return prDecorationGitlab +} + func runPRDecoration(prWrapper wrappers.PRWrapper) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { scanID, _ := cmd.Flags().GetString(params.ScanIDFlag) @@ -94,7 +138,41 @@ func runPRDecoration(prWrapper wrappers.PRWrapper) func(cmd *cobra.Command, args } if errorModel != nil { - return errors.Errorf(errorCodeFormat, failedCreatingPrDecoration, errorModel.Code, errorModel.Message) + return errors.Errorf(errorCodeFormat, failedCreatingGithubPrDecoration, errorModel.Code, errorModel.Message) + } + + logger.Print(prResponse) + + return nil + } +} + +func runPRDecorationGitlab(prWrapper wrappers.PRWrapper) func(cmd *cobra.Command, args []string) error { + return func(cmd *cobra.Command, args []string) error { + scanID, _ := cmd.Flags().GetString(params.ScanIDFlag) + scmTokenFlag, _ := cmd.Flags().GetString(params.SCMTokenFlag) + namespaceFlag, _ := cmd.Flags().GetString(params.NamespaceFlag) + repoNameFlag, _ := cmd.Flags().GetString(params.RepoNameFlag) + iIDFlag, _ := cmd.Flags().GetInt(params.PRIidFlag) + gitlabProjectIdFlag, _ := cmd.Flags().GetInt(params.PRGitlabProjectFlag) + + prModel := &wrappers.GitlabPRModel{ + ScanID: scanID, + ScmToken: scmTokenFlag, + Namespace: namespaceFlag, + RepoName: repoNameFlag, + IiD: iIDFlag, + GitlabProjectID: gitlabProjectIdFlag, + } + + prResponse, errorModel, err := prWrapper.PostGitlabPRDecoration(prModel) + + if err != nil { + return err + } + + if errorModel != nil { + return errors.Errorf(errorCodeFormat, failedCreatingGitlabPrDecoration, errorModel.Code, errorModel.Message) } logger.Print(prResponse) diff --git a/internal/params/binds.go b/internal/params/binds.go index 902ece957..6a790baff 100644 --- a/internal/params/binds.go +++ b/internal/params/binds.go @@ -26,6 +26,7 @@ var EnvVarsBinds = []struct { {KicsResultsPredicatesPathKey, KicsResultsPredicatesPathEnv, "api/kics-results-predicates"}, {BflPathKey, BflPathEnv, "api/bfl"}, {PRDecorationGithubPathKey, PRDecorationGithubPathEnv, "api/flow-publisher/pr/github"}, + {PRDecorationGitlabPathKey, PRDecorationGitlabPathEnv, "api/flow-publisher/pr/gitlab"}, {DescriptionsPathKey, DescriptionsPathEnv, "api/queries/descriptions"}, {TenantConfigurationPathKey, TenantConfigurationPathEnv, "api/configuration/tenant"}, {UploadsPathKey, UploadsPathEnv, "api/uploads"}, diff --git a/internal/params/envs.go b/internal/params/envs.go index 1a82d4fa8..2c55459fe 100644 --- a/internal/params/envs.go +++ b/internal/params/envs.go @@ -28,6 +28,7 @@ const ( KicsResultsPredicatesPathEnv = "CX_KICS_RESULTS_PREDICATES_PATH" BflPathEnv = "CX_BFL_PATH" PRDecorationGithubPathEnv = "CX_PR_DECORATION_GITHUB_PATH" + PRDecorationGitlabPathEnv = "CX_PR_DECORATION_GITLAB_PATH" SastRmPathEnv = "CX_SAST_RM_PATH" UploadsPathEnv = "CX_UPLOADS_PATH" TokenExpirySecondsEnv = "CX_TOKEN_EXPIRY_SECONDS" diff --git a/internal/params/flags.go b/internal/params/flags.go index f12785d8e..3f8f175ff 100644 --- a/internal/params/flags.go +++ b/internal/params/flags.go @@ -148,12 +148,14 @@ const ( ScaFilterUsage = "SCA filter" // PR decoration flags - NamespaceFlag = "namespace" - NamespaceFlagUsage = "Github namespace is required to post the comments" - RepoNameFlag = "repo-name" - RepoNameFlagUsage = "Github repository details" - PRNumberFlag = "pr-number" - PRNumberFlagUsage = "Pull Request number for posting notifications and comments" + NamespaceFlag = "namespace" + NamespaceFlagUsage = "Github namespace is required to post the comments" + RepoNameFlag = "repo-name" + RepoNameFlagUsage = "Github repository details" + PRNumberFlag = "pr-number" + PRNumberFlagUsage = "Pull Request number for posting notifications and comments" + PRIidFlag = "iid" + PRGitlabProjectFlag = "gitlab-project-id" // Chat ChatAPIKey = "chat-apikey" diff --git a/internal/params/keys.go b/internal/params/keys.go index cd3fe39aa..28c448a33 100644 --- a/internal/params/keys.go +++ b/internal/params/keys.go @@ -25,6 +25,7 @@ var ( KicsResultsPathKey = strings.ToLower(KicsResultsPathEnv) BflPathKey = strings.ToLower(BflPathEnv) PRDecorationGithubPathKey = strings.ToLower(PRDecorationGithubPathEnv) + PRDecorationGitlabPathKey = strings.ToLower(PRDecorationGitlabPathEnv) UploadsPathKey = strings.ToLower(UploadsPathEnv) SastRmPathKey = strings.ToLower(SastRmPathEnv) AccessKeyIDConfigKey = strings.ToLower(AccessKeyIDEnv) diff --git a/internal/wrappers/pr-http.go b/internal/wrappers/pr-http.go index 2dbd0d512..fa70f1bd4 100644 --- a/internal/wrappers/pr-http.go +++ b/internal/wrappers/pr-http.go @@ -17,11 +17,13 @@ const ( type PRHTTPWrapper struct { githubPath string + gitlabPath string } -func NewHTTPPRWrapper(githubPath string) PRWrapper { +func NewHTTPPRWrapper(githubPath, gitlabPath string) PRWrapper { return &PRHTTPWrapper{ githubPath: githubPath, + gitlabPath: gitlabPath, } } @@ -43,6 +45,24 @@ func (r *PRHTTPWrapper) PostPRDecoration(model *PRModel) ( return handlePRResponseWithBody(resp, err) } +func (r *PRHTTPWrapper) PostGitlabPRDecoration(model *GitlabPRModel) ( + string, + *WebError, + error, +) { + clientTimeout := viper.GetUint(commonParams.ClientTimeoutKey) + jsonBytes, err := json.Marshal(model) + if err != nil { + return "", nil, err + } + resp, err := SendHTTPRequestWithJSONContentType(http.MethodPost, r.gitlabPath, bytes.NewBuffer(jsonBytes), true, clientTimeout) + if err != nil { + return "", nil, err + } + defer resp.Body.Close() + return handlePRResponseWithBody(resp, err) +} + func handlePRResponseWithBody(resp *http.Response, err error) (string, *WebError, error) { if err != nil { return "", nil, err diff --git a/internal/wrappers/pr.go b/internal/wrappers/pr.go index 8b8d1ea38..7158fd4da 100644 --- a/internal/wrappers/pr.go +++ b/internal/wrappers/pr.go @@ -12,6 +12,16 @@ type PRModel struct { PrNumber int `json:"prNumber"` } +type GitlabPRModel struct { + ScanID string `json:"scanId"` + ScmToken string `json:"scmToken"` + Namespace string `json:"namespace"` + RepoName string `json:"repoName"` + IiD int `json:"iid"` + GitlabProjectID int `json:"gitlabProjectID"` +} + type PRWrapper interface { PostPRDecoration(model *PRModel) (string, *WebError, error) + PostGitlabPRDecoration(model *GitlabPRModel) (string, *WebError, error) } diff --git a/test/integration/pr_test.go b/test/integration/pr_test.go index a06d5e59a..7742cbe26 100644 --- a/test/integration/pr_test.go +++ b/test/integration/pr_test.go @@ -15,9 +15,14 @@ const ( prGithubNamespace = "PR_GITHUB_NAMESPACE" prGithubNumber = "PR_GITHUB_NUMBER" prGithubRepoName = "PR_GITHUB_REPO_NAME" + prGitlabRepoName = "PR_GITLAB_REPO_NAME" + prGitlabToken = "PR_GITLAB_TOKEN" + prGitlabNamespace = "PR_GITLAB_NAMESPACE" + prGitlabProjectId = "PR_GITLAB_PROJECT_ID" + prGitlabIid = "PR_GITLAB_IID" ) -func TestPRDecorationSuccessCase(t *testing.T) { +func TestPRGithubDecorationSuccessCase(t *testing.T) { scanID, _ := getRootScan(t) args := []string{ @@ -39,7 +44,7 @@ func TestPRDecorationSuccessCase(t *testing.T) { assert.NilError(t, err, "Error should be nil") } -func TestPRDecorationFailure(t *testing.T) { +func TestPRGithubDecorationFailure(t *testing.T) { args := []string{ "utils", "pr", @@ -56,5 +61,53 @@ func TestPRDecorationFailure(t *testing.T) { os.Getenv(prGithubRepoName), } err, _ := executeCommand(t, args...) - assert.ErrorContains(t, err, "Failed creating PR Decoration") + assert.ErrorContains(t, err, "Failed creating github PR Decoration") +} + +func TestPRGitlabDecorationSuccessCase(t *testing.T) { + scanID, _ := getRootScan(t) + + args := []string{ + "utils", + "pr", + "gitlab", + flag(params.ScanIDFlag), + scanID, + flag(params.SCMTokenFlag), + os.Getenv(prGitlabToken), + flag(params.NamespaceFlag), + os.Getenv(prGitlabNamespace), + flag(params.RepoNameFlag), + os.Getenv(prGitlabRepoName), + flag(params.PRGitlabProjectFlag), + os.Getenv(prGitlabProjectId), + flag(params.PRIidFlag), + os.Getenv(prGitlabIid), + } + err, _ := executeCommand(t, args...) + assert.NilError(t, err, "Error should be nil") +} + +func TestPRGitlabDecorationFailure(t *testing.T) { + scanID, _ := getRootScan(t) + + args := []string{ + "utils", + "pr", + "gitlab", + flag(params.ScanIDFlag), + "", + flag(params.SCMTokenFlag), + os.Getenv(prGitlabToken), + flag(params.NamespaceFlag), + os.Getenv(prGitlabNamespace), + flag(params.RepoNameFlag), + os.Getenv(prGitlabRepoName), + flag(params.PRGitlabProjectFlag), + os.Getenv(prGitlabProjectId), + flag(params.PRIidFlag), + os.Getenv(prGitlabIid), + } + err, _ := executeCommand(t, args...) + assert.ErrorContains(t, err, "Failed creating gitlab PR Decoration") } From 4b9566f9cf36580b1ba822e75441ed4b7851cd95 Mon Sep 17 00:00:00 2001 From: tiagobcx Date: Mon, 25 Sep 2023 16:37:23 +0100 Subject: [PATCH 02/11] fixing flag usage --- internal/commands/util/pr.go | 18 ++++++++++-------- internal/params/flags.go | 18 ++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/internal/commands/util/pr.go b/internal/commands/util/pr.go index d0400ee47..430758176 100644 --- a/internal/commands/util/pr.go +++ b/internal/commands/util/pr.go @@ -1,6 +1,8 @@ package util import ( + "fmt" + "github.com/MakeNowJust/heredoc" "github.com/checkmarx/ast-cli/internal/logger" "github.com/checkmarx/ast-cli/internal/params" @@ -57,8 +59,8 @@ func PRDecorationGithub(prWrapper wrappers.PRWrapper) *cobra.Command { prDecorationGithub.Flags().String(params.ScanIDFlag, "", "Scan ID to retrieve results from") prDecorationGithub.Flags().String(params.SCMTokenFlag, "", params.GithubTokenUsage) - prDecorationGithub.Flags().String(params.NamespaceFlag, "", params.NamespaceFlagUsage) - prDecorationGithub.Flags().String(params.RepoNameFlag, "", params.RepoNameFlagUsage) + prDecorationGithub.Flags().String(params.NamespaceFlag, "", fmt.Sprintf(params.NamespaceFlagUsage, "Github")) + prDecorationGithub.Flags().String(params.RepoNameFlag, "", fmt.Sprintf(params.RepoNameFlagUsage, "Github")) prDecorationGithub.Flags().Int(params.PRNumberFlag, 0, params.PRNumberFlagUsage) // Set the value for token to mask the scm token @@ -82,7 +84,7 @@ func PRDecorationGitlab(prWrapper wrappers.PRWrapper) *cobra.Command { Example: heredoc.Doc( ` $ cx utils pr gitlab --scan-id --token --namespace --repo-name - --iid --gitlab-project < gitlab project ID > + --iid --gitlab-project `, ), Annotations: map[string]string{ @@ -95,11 +97,11 @@ func PRDecorationGitlab(prWrapper wrappers.PRWrapper) *cobra.Command { } prDecorationGitlab.Flags().String(params.ScanIDFlag, "", "Scan ID to retrieve results from") - prDecorationGitlab.Flags().String(params.SCMTokenFlag, "", params.GithubTokenUsage) - prDecorationGitlab.Flags().String(params.NamespaceFlag, "", params.NamespaceFlagUsage) - prDecorationGitlab.Flags().String(params.RepoNameFlag, "", params.RepoNameFlagUsage) - prDecorationGitlab.Flags().Int(params.PRIidFlag, 0, params.PRNumberFlagUsage) - prDecorationGitlab.Flags().Int(params.PRGitlabProjectFlag, 0, params.PRNumberFlagUsage) + prDecorationGitlab.Flags().String(params.SCMTokenFlag, "", params.GitLabTokenUsage) + prDecorationGitlab.Flags().String(params.NamespaceFlag, "", fmt.Sprintf(params.NamespaceFlagUsage, "Gitlab")) + prDecorationGitlab.Flags().String(params.RepoNameFlag, "", fmt.Sprintf(params.RepoNameFlagUsage, "Gitlab")) + prDecorationGitlab.Flags().Int(params.PRIidFlag, 0, params.PRIidFlagUsage) + prDecorationGitlab.Flags().Int(params.PRGitlabProjectFlag, 0, params.PRGitlabProjectFlagUsage) // Set the value for token to mask the scm token _ = viper.BindPFlag(params.SCMTokenFlag, prDecorationGitlab.Flags().Lookup(params.SCMTokenFlag)) diff --git a/internal/params/flags.go b/internal/params/flags.go index 3f8f175ff..a40b0adb5 100644 --- a/internal/params/flags.go +++ b/internal/params/flags.go @@ -148,14 +148,16 @@ const ( ScaFilterUsage = "SCA filter" // PR decoration flags - NamespaceFlag = "namespace" - NamespaceFlagUsage = "Github namespace is required to post the comments" - RepoNameFlag = "repo-name" - RepoNameFlagUsage = "Github repository details" - PRNumberFlag = "pr-number" - PRNumberFlagUsage = "Pull Request number for posting notifications and comments" - PRIidFlag = "iid" - PRGitlabProjectFlag = "gitlab-project-id" + NamespaceFlag = "namespace" + NamespaceFlagUsage = "%s namespace is required to post the comments" + RepoNameFlag = "repo-name" + RepoNameFlagUsage = "%s repository details" + PRNumberFlag = "pr-number" + PRNumberFlagUsage = "Pull Request number for posting notifications and comments" + PRIidFlag = "mr-iid" + PRIidFlagUsage = "Gitlab IID (internal ID) of the merge request" + PRGitlabProjectFlag = "gitlab-project-id" + PRGitlabProjectFlagUsage = "Gitlab project ID" // Chat ChatAPIKey = "chat-apikey" From 0d374f20f4e13c6756042dcde7f526c4d6c2192b Mon Sep 17 00:00:00 2001 From: tiagobcx Date: Thu, 12 Oct 2023 11:31:52 +0100 Subject: [PATCH 03/11] go lint fix --- internal/commands/util/pr.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/commands/util/pr.go b/internal/commands/util/pr.go index 430758176..1fb8b79b9 100644 --- a/internal/commands/util/pr.go +++ b/internal/commands/util/pr.go @@ -156,7 +156,7 @@ func runPRDecorationGitlab(prWrapper wrappers.PRWrapper) func(cmd *cobra.Command namespaceFlag, _ := cmd.Flags().GetString(params.NamespaceFlag) repoNameFlag, _ := cmd.Flags().GetString(params.RepoNameFlag) iIDFlag, _ := cmd.Flags().GetInt(params.PRIidFlag) - gitlabProjectIdFlag, _ := cmd.Flags().GetInt(params.PRGitlabProjectFlag) + gitlabProjectIDFlag, _ := cmd.Flags().GetInt(params.PRGitlabProjectFlag) prModel := &wrappers.GitlabPRModel{ ScanID: scanID, @@ -164,7 +164,7 @@ func runPRDecorationGitlab(prWrapper wrappers.PRWrapper) func(cmd *cobra.Command Namespace: namespaceFlag, RepoName: repoNameFlag, IiD: iIDFlag, - GitlabProjectID: gitlabProjectIdFlag, + GitlabProjectID: gitlabProjectIDFlag, } prResponse, errorModel, err := prWrapper.PostGitlabPRDecoration(prModel) From bc2b4f14e619d80ad784a653113c5e9440936603 Mon Sep 17 00:00:00 2001 From: tiagobcx Date: Thu, 12 Oct 2023 11:45:14 +0100 Subject: [PATCH 04/11] fixing unit tests --- internal/commands/util/pr_test.go | 8 ++++++++ internal/wrappers/mock/pr-mock.go | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/internal/commands/util/pr_test.go b/internal/commands/util/pr_test.go index 2a68ebc1e..012ade408 100644 --- a/internal/commands/util/pr_test.go +++ b/internal/commands/util/pr_test.go @@ -13,3 +13,11 @@ func TestNewPRDecorationCommandMustExist(t *testing.T) { err := cmd.Execute() assert.ErrorContains(t, err, "scan-id") } + +func TestNewMRDecorationCommandMustExist(t *testing.T) { + cmd := PRDecorationGitlab(nil) + assert.Assert(t, cmd != nil, "MR decoration command must exist") + + err := cmd.Execute() + assert.ErrorContains(t, err, "scan-id") +} \ No newline at end of file diff --git a/internal/wrappers/mock/pr-mock.go b/internal/wrappers/mock/pr-mock.go index b41d3cdec..11f38ef29 100644 --- a/internal/wrappers/mock/pr-mock.go +++ b/internal/wrappers/mock/pr-mock.go @@ -14,3 +14,7 @@ func (pr *PRMockWrapper) PostPRDecoration(model *wrappers.PRModel) ( ) { return "PR comment created successfully.", nil, nil } + +func (pr *PRMockWrapper)PostGitlabPRDecoration(model **wrappers.GitlabPRModel) (string, *wrappers.WebError, error){ + return "MR comment created successfully.", nil, nil +} \ No newline at end of file From 9fc7c3a7bc517527823ff1982da873246584f32e Mon Sep 17 00:00:00 2001 From: tiagobcx Date: Thu, 12 Oct 2023 11:47:22 +0100 Subject: [PATCH 05/11] go lint fix --- internal/commands/util/pr_test.go | 2 +- internal/wrappers/mock/pr-mock.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/commands/util/pr_test.go b/internal/commands/util/pr_test.go index 012ade408..d731dd53c 100644 --- a/internal/commands/util/pr_test.go +++ b/internal/commands/util/pr_test.go @@ -20,4 +20,4 @@ func TestNewMRDecorationCommandMustExist(t *testing.T) { err := cmd.Execute() assert.ErrorContains(t, err, "scan-id") -} \ No newline at end of file +} diff --git a/internal/wrappers/mock/pr-mock.go b/internal/wrappers/mock/pr-mock.go index 11f38ef29..bd6291f67 100644 --- a/internal/wrappers/mock/pr-mock.go +++ b/internal/wrappers/mock/pr-mock.go @@ -15,6 +15,6 @@ func (pr *PRMockWrapper) PostPRDecoration(model *wrappers.PRModel) ( return "PR comment created successfully.", nil, nil } -func (pr *PRMockWrapper)PostGitlabPRDecoration(model **wrappers.GitlabPRModel) (string, *wrappers.WebError, error){ +func (pr *PRMockWrapper) PostGitlabPRDecoration(model **wrappers.GitlabPRModel) (string, *wrappers.WebError, error) { return "MR comment created successfully.", nil, nil -} \ No newline at end of file +} From 90bd4337921d48eff2a140dd9b3078526f606ba7 Mon Sep 17 00:00:00 2001 From: tiagobcx Date: Thu, 12 Oct 2023 11:53:47 +0100 Subject: [PATCH 06/11] fixing tests --- internal/commands/util/pr.go | 2 +- internal/wrappers/mock/pr-mock.go | 2 +- test/integration/pr_test.go | 3 +-- test/integration/util_command.go | 3 ++- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/commands/util/pr.go b/internal/commands/util/pr.go index 1fb8b79b9..aeaf1bc7b 100644 --- a/internal/commands/util/pr.go +++ b/internal/commands/util/pr.go @@ -14,7 +14,7 @@ import ( const ( failedCreatingGithubPrDecoration = "Failed creating github PR Decoration" - failedCreatingGitlabPrDecoration = "Failed creating gitlab PR Decoration" + failedCreatingGitlabPrDecoration = "Failed creating gitlab MR Decoration" errorCodeFormat = "%s: CODE: %d, %s\n" ) diff --git a/internal/wrappers/mock/pr-mock.go b/internal/wrappers/mock/pr-mock.go index bd6291f67..4d5a464aa 100644 --- a/internal/wrappers/mock/pr-mock.go +++ b/internal/wrappers/mock/pr-mock.go @@ -15,6 +15,6 @@ func (pr *PRMockWrapper) PostPRDecoration(model *wrappers.PRModel) ( return "PR comment created successfully.", nil, nil } -func (pr *PRMockWrapper) PostGitlabPRDecoration(model **wrappers.GitlabPRModel) (string, *wrappers.WebError, error) { +func (pr *PRMockWrapper) PostGitlabPRDecoration(model *wrappers.GitlabPRModel) (string, *wrappers.WebError, error) { return "MR comment created successfully.", nil, nil } diff --git a/test/integration/pr_test.go b/test/integration/pr_test.go index 7742cbe26..c742d4a84 100644 --- a/test/integration/pr_test.go +++ b/test/integration/pr_test.go @@ -89,7 +89,6 @@ func TestPRGitlabDecorationSuccessCase(t *testing.T) { } func TestPRGitlabDecorationFailure(t *testing.T) { - scanID, _ := getRootScan(t) args := []string{ "utils", @@ -109,5 +108,5 @@ func TestPRGitlabDecorationFailure(t *testing.T) { os.Getenv(prGitlabIid), } err, _ := executeCommand(t, args...) - assert.ErrorContains(t, err, "Failed creating gitlab PR Decoration") + assert.ErrorContains(t, err, "Failed creating gitlab MR Decoration") } diff --git a/test/integration/util_command.go b/test/integration/util_command.go index 681025ba1..aafd4e55d 100644 --- a/test/integration/util_command.go +++ b/test/integration/util_command.go @@ -72,6 +72,7 @@ func createASTIntegrationTestCommand(t *testing.T) *cobra.Command { bfl := viper.GetString(params.BflPathKey) learnMore := viper.GetString(params.DescriptionsPathKey) prDecorationGithubPath := viper.GetString(params.PRDecorationGithubPathKey) + prDecorationGitlabPath := viper.GetString(params.PRDecorationGitlabPathKey) tenantConfigurationPath := viper.GetString(params.TenantConfigurationPathKey) resultsPdfPath := viper.GetString(params.ResultsPdfReportPathKey) resultsSbomPath := viper.GetString(params.ResultsSbomReportPathKey) @@ -98,7 +99,7 @@ func createASTIntegrationTestCommand(t *testing.T) *cobra.Command { bitBucketWrapper := wrappers.NewBitbucketWrapper() bflWrapper := wrappers.NewBflHTTPWrapper(bfl) learnMoreWrapper := wrappers.NewHTTPLearnMoreWrapper(learnMore) - prWrapper := wrappers.NewHTTPPRWrapper(prDecorationGithubPath) + prWrapper := wrappers.NewHTTPPRWrapper(prDecorationGithubPath,prDecorationGitlabPath) tenantConfigurationWrapper := wrappers.NewHTTPTenantConfigurationWrapper(tenantConfigurationPath) jwtWrapper := wrappers.NewJwtWrapper() scaRealtimeWrapper := wrappers.NewHTTPScaRealTimeWrapper() From bea07a8f5e7ffe6c3b7c6dc384f6a04bb93a1846 Mon Sep 17 00:00:00 2001 From: tiagobcx Date: Fri, 3 Nov 2023 14:44:27 +0000 Subject: [PATCH 07/11] adding check for integration tests failing --- .github/workflows/ci.yml | 5 +++++ internal/commands/.scripts/integration_up.sh | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b279231dc..6efed06c8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -75,6 +75,11 @@ jobs: PR_GITHUB_NAMESPACE: "checkmarx" PR_GITHUB_REPO_NAME: "ast-cli" PR_GITHUB_NUMBER: 418 + PR_GITLAB_TOKEN : ${{ secrets.PR_GITLAB_TOKEN }} + PR_GITLAB_NAMESPACE: "tiagobcx" + PR_GITLAB_REPO_NAME: "testProject" + PR_GITLAB_PROJECT_ID: 40227565 + PR_GITLAB_IID: 19 AZURE_ORG: ${{ secrets.AZURE_ORG }} AZURE_PROJECT: ${{ secrets.AZURE_PROJECT }} AZURE_REPOS: ${{ secrets.AZURE_REPOS }} diff --git a/internal/commands/.scripts/integration_up.sh b/internal/commands/.scripts/integration_up.sh index 541aea0de..52d76a6c6 100644 --- a/internal/commands/.scripts/integration_up.sh +++ b/internal/commands/.scripts/integration_up.sh @@ -18,4 +18,9 @@ go test \ -coverprofile cover.out \ github.com/checkmarx/ast-cli/test/integration +status=$? +if [ status -ne 0 ]; then + echo "Integration tests failed" + rm cover.out +fi go tool cover -html=cover.out -o coverage.html From 64accf2748440486e65af5a5f620bf41aede3708 Mon Sep 17 00:00:00 2001 From: tiagobcx Date: Fri, 3 Nov 2023 15:41:48 +0000 Subject: [PATCH 08/11] adding check for integration tests failing --- internal/commands/.scripts/integration_up.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/commands/.scripts/integration_up.sh b/internal/commands/.scripts/integration_up.sh index 52d76a6c6..d89c4a209 100644 --- a/internal/commands/.scripts/integration_up.sh +++ b/internal/commands/.scripts/integration_up.sh @@ -19,7 +19,9 @@ go test \ github.com/checkmarx/ast-cli/test/integration status=$? -if [ status -ne 0 ]; then +echo "status value after tests" +echo $status +if [ $status -ne 0 ]; then echo "Integration tests failed" rm cover.out fi From 749c510196538b0c7949a2bf5a7bed7dc3796ace Mon Sep 17 00:00:00 2001 From: tiagobcx Date: Mon, 6 Nov 2023 12:24:53 +0000 Subject: [PATCH 09/11] removing verbose --- internal/commands/.scripts/integration_up.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/commands/.scripts/integration_up.sh b/internal/commands/.scripts/integration_up.sh index d89c4a209..25f793f61 100644 --- a/internal/commands/.scripts/integration_up.sh +++ b/internal/commands/.scripts/integration_up.sh @@ -12,7 +12,6 @@ rm -rf ScaResolver-linux64.tar.gz go test \ -tags integration \ - -v \ -timeout 60m \ -coverpkg github.com/checkmarx/ast-cli/internal/commands,github.com/checkmarx/ast-cli/internal/wrappers \ -coverprofile cover.out \ From 8a47bac45e0e4c483a6193ada4a39aaad33057f4 Mon Sep 17 00:00:00 2001 From: tiagobcx Date: Mon, 6 Nov 2023 13:22:34 +0000 Subject: [PATCH 10/11] removing verbose --- internal/commands/.scripts/integration_up.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/commands/.scripts/integration_up.sh b/internal/commands/.scripts/integration_up.sh index 25f793f61..d89c4a209 100644 --- a/internal/commands/.scripts/integration_up.sh +++ b/internal/commands/.scripts/integration_up.sh @@ -12,6 +12,7 @@ rm -rf ScaResolver-linux64.tar.gz go test \ -tags integration \ + -v \ -timeout 60m \ -coverpkg github.com/checkmarx/ast-cli/internal/commands,github.com/checkmarx/ast-cli/internal/wrappers \ -coverprofile cover.out \ From 481a28159e73aaf3628eaef44add98dfd1ac4287 Mon Sep 17 00:00:00 2001 From: tiagobcx Date: Tue, 7 Nov 2023 10:41:32 +0000 Subject: [PATCH 11/11] removing verbose --- test/integration/util_command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/util_command.go b/test/integration/util_command.go index aafd4e55d..2c36fbf77 100644 --- a/test/integration/util_command.go +++ b/test/integration/util_command.go @@ -155,7 +155,7 @@ Ex.: 1. Create a command 3. Execute command */ func execute(cmd *cobra.Command, args ...string) error { - return executeWithTimeout(cmd, time.Minute, args...) + return executeWithTimeout(cmd, 3*time.Minute, args...) } // Execute a CLI command expecting an error and buffer to execute post assertions