Skip to content

Commit

Permalink
feat(gitlab): simplify diff scan config (#1447)
Browse files Browse the repository at this point in the history
  • Loading branch information
gotbadger committed Jan 5, 2024
1 parent f493379 commit df02552
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 37 deletions.
5 changes: 2 additions & 3 deletions api/fetch_ignores.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"encoding/json"
"os"

ignoretypes "github.com/bearer/bearer/internal/util/ignore/types"
)
Expand All @@ -20,7 +19,7 @@ type CloudIgnorePayload struct {
PullRequestNumber string `json:"pull_request_number,omitempty"`
}

func (api *API) FetchIgnores(fullname string, localIgnores []string) (*CloudIgnoreData, error) {
func (api *API) FetchIgnores(fullname string, pullRequestNumber string, localIgnores []string) (*CloudIgnoreData, error) {
endpoint := Endpoints.FetchIgnores

bytes, err := api.makeRequest(endpoint.Route, endpoint.HttpMethod,
Expand All @@ -29,7 +28,7 @@ func (api *API) FetchIgnores(fullname string, localIgnores []string) (*CloudIgno
Data: CloudIgnorePayload{
Project: fullname,
LocalIgnores: localIgnores,
PullRequestNumber: os.Getenv("PR_NUMBER"),
PullRequestNumber: pullRequestNumber,
},
})
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion internal/commands/artifact/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (r *runner) scanBaseBranch(
return result, nil
}

func getIgnoredFingerprints(client *api.API, settings settings.Config, gitContext *gitrepository.Context) (
func getIgnoredFingerprints(client *api.API, settings settings.Config, gitContext *gitrepository.Context, pullRequestNumber string) (
useCloudIgnores bool,
ignoredFingerprints map[string]ignoretypes.IgnoredFingerprint,
staleIgnoredFingerprintIds []string,
Expand All @@ -246,6 +246,7 @@ func getIgnoredFingerprints(client *api.API, settings settings.Config, gitContex
useCloudIgnores, ignoredFingerprints, staleIgnoredFingerprintIds, err = ignore.GetIgnoredFingerprintsFromCloud(
client,
gitContext.FullName,
pullRequestNumber,
localIgnoredFingerprints,
)
if err != nil {
Expand Down Expand Up @@ -309,6 +310,7 @@ func Run(ctx context.Context, opts flagtypes.Options) (err error) {
opts.GeneralOptions.Client,
scanSettings,
gitContext,
opts.PullRequestNumber,
)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions internal/commands/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ $ bearer ignore pull /path/to/your_project --api-key=XXXXX`,

if fileExists {
overwriteApproved := requestConfirmation("Warning: this action will overwrite your current ignore file. Continue?")
cmd.Printf("\n")
if !overwriteApproved {
cmd.Printf("Okay, pull cancelled!\n")
return nil
Expand All @@ -363,7 +362,7 @@ $ bearer ignore pull /path/to/your_project --api-key=XXXXX`,
return fmt.Errorf("failed to get git context: %w", err)
}

data, err := options.GeneralOptions.Client.FetchIgnores(gitContext.FullName, []string{})
data, err := options.GeneralOptions.Client.FetchIgnores(gitContext.FullName, "", []string{})
if err != nil {
return fmt.Errorf("cloud error: %s", err)
}
Expand Down
43 changes: 22 additions & 21 deletions internal/flag/repository_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,31 +112,32 @@ var (
DisableInConfig: true,
Hide: true,
})
PullRequestNumberFlag = RepositoryFlagGroup.add(flagtypes.Flag{
Name: "pull-request-number",
ConfigName: "repository.pull-request-number",
Value: "",
Usage: "Used when fetching branch level ignores for a PR/MR",
EnvironmentVariables: []string{
"PR_NUMBER", // github
"CI_MERGE_REQUEST_ID", //gitlab
},
DisableInConfig: true,
Hide: true,
})
)

type RepositoryOptions struct {
OriginURL string
Branch string
Commit string
DefaultBranch string
DiffBaseBranch string
DiffBaseCommit string
GithubToken string
GithubRepository string
GithubAPIURL string
}

func (repositoryFlagGroup) SetOptions(options *flagtypes.Options, args []string) error {
options.RepositoryOptions = flagtypes.RepositoryOptions{
OriginURL: getString(RepositoryURLFlag),
Branch: getString(BranchFlag),
Commit: getString(CommitFlag),
DefaultBranch: getString(DefaultBranchFlag),
DiffBaseBranch: getString(DiffBaseBranchFlag),
DiffBaseCommit: getString(DiffBaseCommitFlag),
GithubToken: getString(GithubTokenFlag),
GithubRepository: getString(GithubRepositoryFlag),
GithubAPIURL: getString(GithubAPIURLFlag),
OriginURL: getString(RepositoryURLFlag),
Branch: getString(BranchFlag),
Commit: getString(CommitFlag),
DefaultBranch: getString(DefaultBranchFlag),
DiffBaseBranch: getString(DiffBaseBranchFlag),
DiffBaseCommit: getString(DiffBaseCommitFlag),
GithubToken: getString(GithubTokenFlag),
GithubRepository: getString(GithubRepositoryFlag),
GithubAPIURL: getString(GithubAPIURLFlag),
PullRequestNumber: getString(PullRequestNumberFlag),
}

return nil
Expand Down
35 changes: 35 additions & 0 deletions internal/flag/repository_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,38 @@ func Test_getRepositoryGithubAPIURLFlag(t *testing.T) {

RunFlagTests(testCases, t)
}

func Test_getPullRequestNumberFlag(t *testing.T) {
testCases := []TestCase{
{
name: "Repository PullRequestNumber. Default",
flag: PullRequestNumberFlag,
flagValue: "",
want: nil,
},
{
name: "Repository PullRequestNumber. PR_NUMBER env",
flag: PullRequestNumberFlag,
env: Env{
key: "PR_NUMBER",
value: "42",
},
want: []string{
string("42"),
},
},
{
name: "Repository PullRequestNumber. CI_MERGE_REQUEST_ID env",
flag: PullRequestNumberFlag,
env: Env{
key: "CI_MERGE_REQUEST_ID",
value: "24",
},
want: []string{
string("24"),
},
},
}

RunFlagTests(testCases, t)
}
19 changes: 10 additions & 9 deletions internal/flag/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,16 @@ type ReportOptions struct {
}

type RepositoryOptions struct {
OriginURL string
Branch string
Commit string
DefaultBranch string
DiffBaseBranch string
DiffBaseCommit string
GithubToken string
GithubRepository string
GithubAPIURL string
OriginURL string
Branch string
Commit string
DefaultBranch string
DiffBaseBranch string
DiffBaseCommit string
GithubToken string
GithubRepository string
GithubAPIURL string
PullRequestNumber string
}

// GlobalOptions defines flags and other configuration parameters for all the subcommands
Expand Down
4 changes: 3 additions & 1 deletion internal/util/ignore/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ func GetIgnoredFingerprints(filePath string, target *string) (ignoredFingerprint
func GetIgnoredFingerprintsFromCloud(
client *api.API,
fullname string,
pullRequestNumber string,
localIgnores map[string]types.IgnoredFingerprint,
) (
useCloudIgnores bool,
ignoredFingerprints map[string]types.IgnoredFingerprint,
staleIgnoredFingerprintIds []string,
err error,
) {
data, err := client.FetchIgnores(fullname, maps.Keys(localIgnores))

data, err := client.FetchIgnores(fullname, pullRequestNumber, maps.Keys(localIgnores))
if err != nil {
return useCloudIgnores, ignoredFingerprints, staleIgnoredFingerprintIds, err
}
Expand Down

0 comments on commit df02552

Please sign in to comment.