Skip to content

Commit

Permalink
Add support for Nth build failure for Email Extension (#1297)
Browse files Browse the repository at this point in the history
  • Loading branch information
cml37 authored Jun 2, 2023
1 parent 9ea8d1f commit 0bb338e
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ class ExtendedEmailTriggersContext implements Context {
addTrigger('SecondFailure', closure)
}

/**
* Triggers an email when the build fails n times in a row after a successful build.
*/
void xNthFailure(@DslContext(FailureCountExtendedEmailTriggerContext) Closure closure = null) {
addFailureCountTrigger('XNthFailure', closure)
}

/**
* Triggers an email any time the build fails.
*/
Expand Down Expand Up @@ -135,10 +142,20 @@ class ExtendedEmailTriggersContext implements Context {
}

protected void addTrigger(String name, Closure closure) {
ExtendedEmailTriggerContext context = new ExtendedEmailTriggerContext()
buildTrigger(name, closure, new ExtendedEmailTriggerContext())
}

protected void addFailureCountTrigger(String name, Closure closure) {
buildTrigger(name, closure, new FailureCountExtendedEmailTriggerContext())
}

private void buildTrigger(String name, Closure closure, ExtendedEmailTriggerContext context) {
ContextHelper.executeInContext(closure, context)

configuredTriggers << new NodeBuilder()."hudson.plugins.emailext.plugins.trigger.${name}Trigger" {
if (context.hasProperty('failureCount')) {
requiredFailureCount(context.failureCount)
}
email {
recipientList(context.recipientList.join(', '))
subject(context.subject ?: '')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package javaposse.jobdsl.dsl.helpers.publisher

class FailureCountExtendedEmailTriggerContext extends ExtendedEmailTriggerContext {
int failureCount = 3

/**
* Specifies the failure count.
*/
void failureCount(int failureCount) {
this.failureCount = failureCount
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class PublisherContextSpec extends Specification {
value << ['', null, 'default']
}

def 'call extendedEmail with all options'() {
def 'call extendedEmail with all options except failure count'() {
when:
context.extendedEmail {
recipientList('me@example.org', 'you@example.org')
Expand Down Expand Up @@ -234,6 +234,136 @@ class PublisherContextSpec extends Specification {
'fixedUnhealthy' || 'FixedUnhealthyTrigger'
}

def 'call extendedEmail with all options including failure count'() {
when:
context.extendedEmail {
recipientList('me@example.org', 'you@example.org')
recipientList('other@example.org')
contentType('text/plain')
defaultSubject('Important')
defaultContent('read me')
attachmentPatterns('*.log', '**/report.html')
attachmentPatterns('foo.txt')
preSendScript('a script')
additionalGroovyClasspath('foo.jar', 'bar.jar')
additionalGroovyClasspath('my.jar')
attachBuildLog()
compressBuildLog()
replyToList('someone@example.org')
replyToList('test@example.org', 'ci@example.org')
saveToWorkspace()
disabled()
triggers {
"${trigger}" {
sendTo {
culprits()
developers()
recipientList()
requester()
failingTestSuspects()
firstFailingBuildSuspects()
upstreamCommitter()
}
recipientList('test@example.org', 'foo@example.org')
recipientList('lala@example.org')
contentType('text/html')
subject('Lorem')
content('Ipsum')
attachmentPatterns('test*.html', '*.xml')
attachmentPatterns('hello.txt')
attachBuildLog()
compressBuildLog()
replyToList('one@example.org')
replyToList('two@example.org', 'three@example.org')
failureCount(5)
}
}
configure {
it / foo('bar')
}
}

then:
context.publisherNodes.size() == 1
with(context.publisherNodes[0]) {
name() == 'hudson.plugins.emailext.ExtendedEmailPublisher'
children().size() == 14
recipientList[0].value() == 'me@example.org, you@example.org, other@example.org'
contentType[0].value() == 'text/plain'
defaultSubject[0].value() == 'Important'
defaultContent[0].value() == 'read me'
attachmentsPattern[0].value() == '*.log, **/report.html, foo.txt'
presendScript[0].value() == 'a script'
with(classpath[0]) {
children().size() == 3
with(children()[0]) {
name() == 'hudson.plugins.emailext.GroovyScriptPath'
children().size() == 1
path[0].value() == 'foo.jar'
}
with(children()[1]) {
name() == 'hudson.plugins.emailext.GroovyScriptPath'
children().size() == 1
path[0].value() == 'bar.jar'
}
with(children()[2]) {
name() == 'hudson.plugins.emailext.GroovyScriptPath'
children().size() == 1
path[0].value() == 'my.jar'
}
}
attachBuildLog[0].value() == true
compressBuildLog[0].value() == true
replyTo[0].value() == 'someone@example.org, test@example.org, ci@example.org'
saveOutput[0].value() == true
disabled[0].value() == true
with(configuredTriggers[0]) {
children().size() == 1
with(children()[0]) {
name() == "hudson.plugins.emailext.plugins.trigger.${className}"
children().size() == 2
with(children()[0]) {
name() == 'requiredFailureCount'
value() == 5
}
with(email[0]) {
children().size() == 9
recipientList[0].value() == 'test@example.org, foo@example.org, lala@example.org'
subject[0].value() == 'Lorem'
body[0].value() == 'Ipsum'
with(recipientProviders[0]) {
children().size() == 7
children()[0].name() ==
'hudson.plugins.emailext.plugins.recipients.CulpritsRecipientProvider'
children()[1].name() ==
'hudson.plugins.emailext.plugins.recipients.DevelopersRecipientProvider'
children()[2].name() ==
'hudson.plugins.emailext.plugins.recipients.ListRecipientProvider'
children()[3].name() ==
'hudson.plugins.emailext.plugins.recipients.RequesterRecipientProvider'
children()[4].name() ==
'hudson.plugins.emailext.plugins.recipients.FailingTestSuspectsRecipientProvider'
children()[5].name() ==
'hudson.plugins.emailext.plugins.recipients.FirstFailingBuildSuspectsRecipientProvider'
children()[6].name() ==
'hudson.plugins.emailext.plugins.recipients.UpstreamComitterRecipientProvider'
}
attachmentsPattern[0].value() == 'test*.html, *.xml, hello.txt'
attachBuildLog[0].value() == true
compressBuildLog[0].value() == true
replyTo[0].value() == 'one@example.org, two@example.org, three@example.org'
contentType[0].value() == 'text/html'
}
}
}
foo[0].value() == 'bar'
}

where:
trigger || className
'xNthFailure' || 'XNthFailureTrigger'
}

def 'call standard mailer method'() {
when:
context.mailer('recipient')
Expand Down

0 comments on commit 0bb338e

Please sign in to comment.