1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-30 05:59:39 +02:00

Fix potential nil reference (#3460)

This commit is contained in:
Sven Merk 2022-01-24 11:59:33 +01:00 committed by GitHub
parent b0e4599d4d
commit ffa82c383e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 8 deletions

View File

@ -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 {

View File

@ -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")
})
}