diff --git a/cmd/fortifyExecuteScan.go b/cmd/fortifyExecuteScan.go index c04cae339..ef41a1b25 100644 --- a/cmd/fortifyExecuteScan.go +++ b/cmd/fortifyExecuteScan.go @@ -159,7 +159,7 @@ func runFortifyScan(config fortifyExecuteScanOptions, sys fortify.System, utils log.Entry().Debugf("Looked up / created project version with ID %v for PR %v", projectVersion.ID, fortifyProjectVersion) } else { prID, prAuthor := determinePullRequestMerge(config) - if len(prID) > 0 { + if prID != "0" { log.Entry().Debugf("Determined PR ID '%v' for merge check", prID) if len(prAuthor) > 0 && !piperutils.ContainsString(config.Assignees, prAuthor) { log.Entry().Debugf("Determined PR Author '%v' for result assignment", prAuthor) @@ -957,16 +957,22 @@ func determinePullRequestMerge(config fortifyExecuteScanOptions) (string, string if matches != nil && len(matches) > 1 { return string(matches[config.PullRequestMessageRegexGroup]), author } - return "", "" + return "0", "" } func determinePullRequestMergeGithub(ctx context.Context, config fortifyExecuteScanOptions, pullRequestServiceInstance pullRequestService) (string, string, error) { + number := "0" + email := "" options := github.PullRequestListOptions{State: "closed", Sort: "updated", Direction: "desc"} prList, _, err := pullRequestServiceInstance.ListPullRequestsWithCommit(ctx, config.Owner, config.Repository, config.CommitID, &options) if err == nil && len(prList) > 0 { - return fmt.Sprintf("%v", prList[0].GetNumber()), *prList[0].User.Email, nil + number = fmt.Sprintf("%v", prList[0].GetNumber()) + if nil != prList[0].User { + email = *(prList[0].User.Email) + } + return number, email, nil } - return "", "", err + return number, email, err } func appendToOptions(config *fortifyExecuteScanOptions, options []string, t map[string]string) []string { diff --git a/cmd/fortifyExecuteScan_test.go b/cmd/fortifyExecuteScan_test.go index 9be1dfae2..b330b923f 100644 --- a/cmd/fortifyExecuteScan_test.go +++ b/cmd/fortifyExecuteScan_test.go @@ -287,6 +287,8 @@ func (prService pullRequestServiceMock) ListPullRequestsWithCommit(ctx context.C return []*github.PullRequest{{Number: &result, User: &user}}, &github.Response{}, nil } else if owner == "C" { return []*github.PullRequest{{User: &user}}, &github.Response{}, errors.New("Test error") + } else if owner == "E" { + return []*github.PullRequest{{User: nil}}, &github.Response{}, errors.New("Test error") } return []*github.PullRequest{}, &github.Response{}, nil } @@ -751,7 +753,7 @@ func TestDeterminePullRequestMerge(t *testing.T) { t.Run("no match", func(t *testing.T) { config.CommitMessage = "Some test commit" match, authorString := determinePullRequestMerge(config) - assert.Equal(t, "", match, "Expected different result") + assert.Equal(t, "0", match, "Expected different result") assert.Equal(t, "", authorString, "Expected different result") }) } @@ -769,14 +771,14 @@ func TestDeterminePullRequestMergeGithub(t *testing.T) { t.Run("no match", func(t *testing.T) { match, authorString, err := determinePullRequestMergeGithub(nil, fortifyExecuteScanOptions{Owner: "B"}, prServiceMock) assert.NoError(t, err) - assert.Equal(t, "", match, "Expected different result") + assert.Equal(t, "0", match, "Expected different result") assert.Equal(t, "", authorString, "Expected different result") }) t.Run("error", func(t *testing.T) { - match, authorString, err := determinePullRequestMergeGithub(nil, fortifyExecuteScanOptions{Owner: "C"}, prServiceMock) + match, authorString, err := determinePullRequestMergeGithub(nil, fortifyExecuteScanOptions{Owner: "E"}, prServiceMock) assert.EqualError(t, err, "Test error") - assert.Equal(t, "", match, "Expected different result") + assert.Equal(t, "0", match, "Expected different result") assert.Equal(t, "", authorString, "Expected different result") }) }