Skip to content

Commit

Permalink
merge with main
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagobcx committed Nov 14, 2024
1 parent 6015683 commit 603e594
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 91 deletions.
73 changes: 16 additions & 57 deletions internal/commands/util/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@ import (
)

const (
failedCreatingGithubPrDecoration = "Failed creating github PR Decoration"
failedCreatingGitlabPrDecoration = "Failed creating gitlab MR Decoration"
failedCreatingGithubPrDecoration = "Failed creating github PR Decoration"
failedCreatingGitlabPrDecoration = "Failed creating gitlab MR Decoration"
failedCreatingBitbucketPrDecoration = "Failed creating bitbucket PR Decoration"
failedCreatingAzurePrDecoration = "Failed creating azure PR Decoration"
failedCreatingGitlabPrDecoration = "Failed creating gitlab MR Decoration"
errorCodeFormat = "%s: CODE: %d, %s\n"
policyErrorFormat = "%s: Failed to get scanID policy information"
waitDelayDefault = 5
resultPolicyDefaultTimeout = 1
failedGettingScanError = "Failed showing a scan"
noPRDecorationCreated = "A PR couldn't be created for this scan because it is still in progress."
githubOnPremURLSuffix = "/api/v3/repos/"
gitlabOnPremURLSuffix = "/api/v4/"
githubCloudURL = "https://api.github.com/repos/"
gitlabCloudURL = "https://gitlab.com" + gitlabOnPremURLSuffix
azureCloudURL = "https://dev.azure.com/"
failedCreatingAzurePrDecoration = "Failed creating azure PR Decoration"
errorCodeFormat = "%s: CODE: %d, %s\n"
policyErrorFormat = "%s: Failed to get scanID policy information"
waitDelayDefault = 5
resultPolicyDefaultTimeout = 1
failedGettingScanError = "Failed showing a scan"
noPRDecorationCreated = "A PR couldn't be created for this scan because it is still in progress."
githubOnPremURLSuffix = "/api/v3/repos/"
gitlabOnPremURLSuffix = "/api/v4/"
githubCloudURL = "https://api.github.com/repos/"
gitlabCloudURL = "https://gitlab.com" + gitlabOnPremURLSuffix
azureCloudURL = "https://dev.azure.com/"
bitbucketCloudURL = "bitbucket.org"
errorAzureOnPremParams = "code-repository-url must be set when code-repository-username is set"

errorAzureOnPremParams = "code-repository-url must be set when code-repository-username is set"
)

func NewPRDecorationCommand(prWrapper wrappers.PRWrapper, policyWrapper wrappers.PolicyWrapper, scansWrapper wrappers.ScansWrapper) *cobra.Command {
Expand Down Expand Up @@ -304,45 +302,6 @@ func runPRDecoration(prWrapper wrappers.PRWrapper, policyWrapper wrappers.Policy
return nil
}
}
func PRDecorationBitbucket(prWrapper wrappers.PRWrapper, policyWrapper wrappers.PolicyWrapper, scansWrapper wrappers.ScansWrapper) *cobra.Command {
prDecorationBitbucket := &cobra.Command{
Use: "bitbucket ",
Short: "Decorate bitbucket PR with vulnerabilities",
Long: "Decorate bitbucket PR with vulnerabilities",
Example: heredoc.Doc(
`
$ cx utils pr bitbucket --scan-id <scan-id> --token <PAT> --namespace <username (required for cloud services)> --repo-name <repository-slug>
--pr-id <pr number> --code-repository-url <bitbucket-server-url (required for self-hosted)>
`,
),
Annotations: map[string]string{
"command:doc": heredoc.Doc(
`
`,
),
},
RunE: runPRDecorationBitbucket(prWrapper, policyWrapper, scansWrapper),
}

prDecorationBitbucket.Flags().String(params.ScanIDFlag, "", "Scan ID to retrieve results from")
prDecorationBitbucket.Flags().String(params.SCMTokenFlag, "", params.BitbucketTokenUsage)
prDecorationBitbucket.Flags().String(params.NamespaceFlag, "", fmt.Sprintf(params.NamespaceFlagUsage, "Bitbucket"))
prDecorationBitbucket.Flags().String(params.RepoNameFlag, "", fmt.Sprintf(params.RepoNameFlagUsage, "Bitbucket"))
prDecorationBitbucket.Flags().Int(params.PRBBIDFlag, 0, params.PRBBIDFlagUsage)
prDecorationBitbucket.Flags().String(params.ProjectKeyFlag, "", params.ProjectKeyFlagUsage)
prDecorationBitbucket.Flags().String(params.CodeRepositoryFlag, "", params.CodeRepositoryFlagUsage)

// Set the value for token to mask the scm token
_ = viper.BindPFlag(params.SCMTokenFlag, prDecorationBitbucket.Flags().Lookup(params.SCMTokenFlag))

// mark all fields as required\
_ = prDecorationBitbucket.MarkFlagRequired(params.ScanIDFlag)
_ = prDecorationBitbucket.MarkFlagRequired(params.SCMTokenFlag)
_ = prDecorationBitbucket.MarkFlagRequired(params.RepoNameFlag)
_ = prDecorationBitbucket.MarkFlagRequired(params.PRBBIDFlag)

return prDecorationBitbucket
}

func updateAPIURLForGithubOnPrem(apiURL string) string {
if apiURL != "" {
Expand Down Expand Up @@ -514,7 +473,7 @@ func runPRDecorationAzure(prWrapper wrappers.PRWrapper, policyWrapper wrappers.P
Policies: policies,
APIURL: updatedAPIURL,
}
prResponse, errorModel, err := prWrapper.PostAzurePRDecoration(prModel)
prResponse, errorModel, err := prWrapper.PostPRDecoration(prModel)
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions internal/wrappers/mock/pr-mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ func (pr *PRMockWrapper) PostPRDecoration(model interface{}) (
return "Bitbucket Cloud PR comment created successfully.", nil, nil
case *wrappers.BitbucketServerPRModel:
return "Bitbucket Server PR comment created successfully.", nil, nil
case *wrappers.AzurePRModel:
return "PR comment created successfully.", nil, nil

default:
return "", nil, errors.New("unsupported model type")
}
}

func (pr *PRMockWrapper) PostAzurePRDecoration(model *wrappers.AzurePRModel) (string, *wrappers.WebError, error) {
return "PR comment created successfully.", nil, nil
}
36 changes: 8 additions & 28 deletions internal/wrappers/pr-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ const (
)

type PRHTTPWrapper struct {
githubPath string
gitlabPath string
azurePath string
githubPath string
gitlabPath string
azurePath string
bitbucketCloudPath string
bitbucketServerPath string
}

func NewHTTPPRWrapper(githubPath, gitlabPath, bitbucketCloudPath, bitbucketServerPath, azurePath string) PRWrapper {
return &PRHTTPWrapper{
githubPath: githubPath,
gitlabPath: gitlabPath,
azurePath: azurePath,
githubPath: githubPath,
gitlabPath: gitlabPath,
azurePath: azurePath,
bitbucketCloudPath: bitbucketCloudPath,
bitbucketServerPath: bitbucketServerPath,
}
Expand Down Expand Up @@ -56,28 +56,6 @@ func (r *PRHTTPWrapper) PostPRDecoration(model interface{}) (string, *WebError,
return handlePRResponseWithBody(resp, err)
}

func (r *PRHTTPWrapper) PostAzurePRDecoration(model *AzurePRModel) (
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.azurePath, bytes.NewBuffer(jsonBytes), true, clientTimeout)
if err != nil {
return "", nil, err
}
defer func() {
if err == nil {
_ = resp.Body.Close()
}
}()
return handlePRResponseWithBody(resp, err)
}

func (r *PRHTTPWrapper) getPRDecorationURL(model interface{}) (string, error) {
switch model.(type) {
case *PRModel:
Expand All @@ -88,6 +66,8 @@ func (r *PRHTTPWrapper) getPRDecorationURL(model interface{}) (string, error) {
return r.bitbucketCloudPath, nil
case *BitbucketServerPRModel:
return r.bitbucketServerPath, nil
case *AzurePRModel:
return r.azurePath, nil
default:
return "", errors.New("unsupported model type")
}
Expand Down
4 changes: 2 additions & 2 deletions test/integration/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,10 @@ func TestPRAzureDecoration_WhenUseCodeRepositoryFlag_ShouldSuccess(t *testing.T)
"https://azure.example.com",
}

monkey.Patch((*wrappers.PRHTTPWrapper).PostAzurePRDecoration, func(*wrappers.PRHTTPWrapper, *wrappers.AzurePRModel) (string, *wrappers.WebError, error) {
monkey.Patch((*wrappers.PRHTTPWrapper).PostPRDecoration, func(*wrappers.PRHTTPWrapper, interface{}) (string, *wrappers.WebError, error) {
return azurePRCommentCreated, nil, nil
})
defer monkey.Unpatch((*wrappers.PRHTTPWrapper).PostAzurePRDecoration)
defer monkey.Unpatch((*wrappers.PRHTTPWrapper).PostPRDecoration)

file := createOutputFile(t, outputFileName)
defer deleteOutputFile(t, file)
Expand Down

0 comments on commit 603e594

Please sign in to comment.