Skip to content

Commit

Permalink
Adding method for setitng build description and various detekt fixes (#…
Browse files Browse the repository at this point in the history
…28)

Co-authored-by: Caleb Reder <[email protected]>
  • Loading branch information
reder9 and Caleb Reder authored Oct 7, 2021
1 parent a588903 commit 707c1fa
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.code42.jenkins.pipelinekt.examples

import com.code42.jenkins.pipelinekt.core.vars.Var
import com.code42.jenkins.pipelinekt.core.vars.ext.environmentVar
import com.code42.jenkins.pipelinekt.core.vars.ext.parameter
import com.code42.jenkins.pipelinekt.dsl.PipelineDsl
import com.code42.jenkins.pipelinekt.dsl.`when`.expression
Expand All @@ -18,7 +16,6 @@ fun PipelineDsl.conditionalPipeline() = pipeline {
`when` { expression { "DO_BUILD".parameter().statement() }}
steps {
sh("./build.sh")
val myPath: Var.Variable = def("PATH".environmentVar())
val isRelease = def { sh(script = "./isRelease.sh", returnStdout = true) }
`if`((isRelease `==` "true") `||` "DO_RELEASE".parameter(),
`then` = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ import com.code42.jenkins.pipelinekt.core.writer.GroovyWriter
* @param mimeType Email body MIME type. Defaults to text/plain.
* @param charset Email body character encoding. Defaults to UTF-8.
*/
data class Mail(val to: Var.Literal.Str = "".strSingle(), val subject: Var.Literal.Str = "".strSingle(), val body: Var.Literal.Str = "".strSingle(), val cc: Var.Literal.Str = "".strSingle(), val bcc: Var.Literal.Str = "".strSingle(), val from: Var.Literal.Str = "".strSingle(), val replyTo: Var.Literal.Str = "".strSingle(), val mimeType: Var.Literal.Str = "".strSingle(), val charset: Var.Literal.Str = "".strSingle()) : DeclarativeStep, SingletonStep {
data class Mail(
val to: Var.Literal.Str = "".strSingle(),
val subject: Var.Literal.Str = "".strSingle(),
val body: Var.Literal.Str = "".strSingle(),
val cc: Var.Literal.Str = "".strSingle(),
val bcc: Var.Literal.Str = "".strSingle(),
val from: Var.Literal.Str = "".strSingle(),
val replyTo: Var.Literal.Str = "".strSingle(),
val mimeType: Var.Literal.Str = "".strSingle(),
val charset: Var.Literal.Str = "".strSingle()
) : DeclarativeStep, SingletonStep {
override fun toGroovy(writer: GroovyWriter) {
writer.writeln("mail to: ${to.toGroovy()}, subject: ${subject.toGroovy()}, body: ${body.toGroovy()}, from: ${from.toGroovy()}, cc: ${cc.toGroovy()}, bcc: ${bcc.toGroovy()}, replyTo: ${replyTo.toGroovy()}, mimeType: ${mimeType.toGroovy()}, charset: ${charset.toGroovy()}")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.code42.jenkins.pipelinekt.internal.step.scripted

import com.code42.jenkins.pipelinekt.core.step.ScriptedStep
import com.code42.jenkins.pipelinekt.core.step.SingletonStep
import com.code42.jenkins.pipelinekt.core.writer.GroovyWriter

/**
* Set the description of a build.
*
* @param description The description of this build.
*/
data class SetBuildDescription(val description: String) : ScriptedStep, SingletonStep {
override fun scriptedGroovy(writer: GroovyWriter) {
writer.writeln("currentBuild.description = \"${description}\"")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.code42.jenkins.pipelinekt.core.issues.RecordIssuesTool
import com.code42.jenkins.pipelinekt.core.vars.ext.boolVar
import com.code42.jenkins.pipelinekt.core.vars.ext.strSingle
import com.code42.jenkins.pipelinekt.internal.step.declarative.RecordIssues
import kotlin.test.assertEquals
import org.junit.Test

class RecordIssuesTest : GroovyScriptTest() {
Expand All @@ -13,16 +14,27 @@ class RecordIssuesTest : GroovyScriptTest() {
val id = "my-id".strSingle()
val pattern = "my/includes,and/more".strSingle()

val expected = "recordIssues enabledForFailure: true, aggregateResults: true, tool: spotbugs(pattern: ${pattern.toGroovy()}, id: ${id.toGroovy()}, name: ${name.toGroovy()})"
val expected = "recordIssues enabledForFailure: true," +
" aggregatingResults: true," +
" tool: spotBugs(pattern: ${pattern.toGroovy()}," +
" id: ${id.toGroovy()}," +
" name: ${name.toGroovy()})\n"
RecordIssues(true.boolVar(), RecordIssuesTool.Spotbugs(pattern, id, name)).toGroovy(writer)
assertEquals(expected, out.toString())
}

@Test fun recordCheckstyleIssue() {
val name = "my name".strSingle()
val id = "my-id".strSingle()
val pattern = "my/includes,and/more".strSingle()

val expected = "recordIssues enabledForFailure: true, aggregateResults: true, tool: checkStyle(pattern: ${pattern.toGroovy()}, id: ${id.toGroovy()}, name: ${name.toGroovy()})"
RecordIssues(true.boolVar(), RecordIssuesTool.Spotbugs(pattern, id, name)).toGroovy(writer)
val expected = "recordIssues enabledForFailure: true," +
" aggregatingResults: true," +
" tool: checkStyle(pattern: ${pattern.toGroovy()}," +
" id: ${id.toGroovy()}," +
" name: ${name.toGroovy()})\n"

RecordIssues(true.boolVar(), RecordIssuesTool.CheckStyle(pattern, id, name)).toGroovy(writer)
assertEquals(expected, out.toString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,56 @@ import org.junit.Test

class MailTest : GroovyScriptTest() {
@Test fun empty_email() {
val expected = "mail to: '', subject: '', body: '', from: '', cc: '', bcc: '', replyTo: '', mimeType: '', charset: ''\n"
val expected = "mail to: ''," +
" subject: ''," +
" body: ''," +
" from: ''," +
" cc: ''," +
" bcc: ''," +
" replyTo: ''," +
" mimeType: ''," +
" charset: ''\n"

Mail().toGroovy(writer)
assertEquals(expected, out.toString())
}

@Test fun basic_email() {
val expected = "mail to: '[email protected]', subject: 'Job Failure', body: 'Jenkins job build failure!', from: '', cc: '', bcc: '', replyTo: '', mimeType: '', charset: ''\n"
Mail("[email protected]".strSingle(), "Job Failure".strSingle(), "Jenkins job build failure!".strSingle()).toGroovy(writer)
val expected = "mail to: '[email protected]'," +
" subject: 'Job Failure'," +
" body: 'Jenkins job build failure!'," +
" from: ''," +
" cc: ''," +
" bcc: ''," +
" replyTo: ''," +
" mimeType: ''," +
" charset: ''\n"

Mail("[email protected]".strSingle(),
"Job Failure".strSingle(),
"Jenkins job build failure!".strSingle()).toGroovy(writer)
assertEquals(expected, out.toString())
}

@Test fun all_parameters_email() {
val expected = "mail to: '[email protected]', subject: 'Job Failure', body: 'Jenkins job build failure!', from: '[email protected]', cc: '[email protected]', bcc: '[email protected]', replyTo: '[email protected]', mimeType: 'text/plain', charset: 'UTF-8'\n"
Mail("[email protected]".strSingle(), "Job Failure".strSingle(), "Jenkins job build failure!".strSingle(), "[email protected]".strSingle(), "[email protected]".strSingle(), "[email protected]".strSingle(), "[email protected]".strSingle(), "text/plain".strSingle(), "UTF-8".strSingle()).toGroovy(writer)
val expected = "mail to: '[email protected]'," +
" subject: 'Job Failure'," +
" body: 'Jenkins job build failure!'," +
" from: '[email protected]'," +
" cc: '[email protected]'," +
" bcc: '[email protected]'," +
" replyTo: '[email protected]'," +
" mimeType: 'text/plain', charset: 'UTF-8'\n"

Mail("[email protected]".strSingle(),
"Job Failure".strSingle(),
"Jenkins job build failure!".strSingle(),
"[email protected]".strSingle(),
"[email protected]".strSingle(),
"[email protected]".strSingle(),
"[email protected]".strSingle(),
"text/plain".strSingle(),
"UTF-8".strSingle()).toGroovy(writer)
assertEquals(expected, out.toString())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.code42.jenkins.pipelinekt.internal.step.scripted

import com.code42.jenkins.pipelinekt.GroovyScriptTest
import kotlin.test.assertEquals
import org.junit.Test

class SetBuildDescriptionTest : GroovyScriptTest() {
@Test fun setBuildDescription() {
val description = "Special Build Description Here!"
SetBuildDescription(description).toGroovy(writer)
val expected = "script {\n" +
"\tcurrentBuild.description = \"Special Build Description Here!\"\n" +
"}\n"
assertEquals(expected, out.toString())
}
}

0 comments on commit 707c1fa

Please sign in to comment.