1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-02-05 13:25:19 +02:00

fix(mailSendNotification): do not send recovery notifications on first build (#3978)

* fix(mailSendNotification): do not send recovery notifications on first build

* added unit tests
This commit is contained in:
Pavel Busko 2022-12-12 10:26:16 +01:00 committed by GitHub
parent f5b6580e4c
commit 50ac1a3ab5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 1 deletions

View File

@ -244,4 +244,57 @@ user3@domain.com noreply+github@domain.com'''
assertThat(credentials, hasItem(''))
assertJobStatusSuccess()
}
@Test
void testSendNotificationMailOnFirstBuild() throws Exception {
def emailExtCalls = []
def buildMock = [
fullProjectName: 'testProjectName',
displayName: 'testDisplayName',
result: 'SUCCESS',
getPreviousBuild: {
return null
}
]
nullScript.currentBuild = buildMock
helper.registerAllowedMethod('emailext', [Map.class], { map ->
emailExtCalls.add(map)
return ''
})
stepRule.step.mailSendNotification(
script: nullScript,
notifyCulprits: false,
gitUrl: 'git@github.domain.com:IndustryCloudFoundation/pipeline-test-node.git'
)
assertThat(emailExtCalls, hasSize(0))
}
@Test
void testSendNotificationMailOnRecovery() throws Exception {
def emailExtCalls = []
def buildMock = [
fullProjectName: 'testProjectName',
displayName: 'testDisplayName',
result: 'SUCCESS',
getPreviousBuild: {
return [result: 'FAILURE']
}
]
nullScript.currentBuild = buildMock
helper.registerAllowedMethod('emailext', [Map.class], { map ->
emailExtCalls.add(map)
return ''
})
stepRule.step.mailSendNotification(
script: nullScript,
notifyCulprits: false,
gitUrl: 'git@github.domain.com:IndustryCloudFoundation/pipeline-test-node.git'
)
assertThat(emailExtCalls, hasSize(1))
assertThat(emailExtCalls[0].subject, is("SUCCESS: Build testProjectName testDisplayName is back to normal"))
}
}

View File

@ -252,5 +252,11 @@ def getDistinctRecipients(recipients){
}
def hasRecovered(buildResult, currentBuild){
return buildResult == 'SUCCESS' && currentBuild.getPreviousBuild()?.result != 'SUCCESS'
def previousBuild = currentBuild.getPreviousBuild()
if (previousBuild) {
return buildResult == 'SUCCESS' && previousBuild.result != 'SUCCESS'
}
return false
}