New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial implementation of job summary #16
Changes from 1 commit
6ad68c9
86e2e79
06d4985
d6e3d29
a86fc58
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.springernature.newversion | ||
|
||
import java.io.File | ||
|
||
class SummaryWriter(val file: File) { | ||
|
||
constructor(filename: String?) : this( | ||
if (filename == null || filename.isBlank()) { | ||
System.err.println("no filename for report provided, will not create a report") | ||
File("/dev/null") | ||
} else { | ||
File(filename) | ||
}) | ||
|
||
fun write(results: ChecksResult) { | ||
writeHeader(file) | ||
when (results) { | ||
is FailedChecks -> { | ||
writeFailures(results.errors) | ||
writeSuccessfulUpdates(results.updates) | ||
} | ||
is SuccessfulChecks -> writeSuccessfulUpdates(results.updates) | ||
} | ||
writeFooter(file) | ||
} | ||
|
||
private fun writeHeader(file: File) { | ||
file.writeText("# CF buildpack update action results\n") | ||
} | ||
|
||
private fun writeSuccessfulUpdates(updates: List<BuildpackUpdate>) { | ||
file.appendText("\n## success\n\n") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might need to be conditionally written, only when updates.length > 0? Otherwise you have a heading without content |
||
updates.forEach { file.appendText("* currentBuildpack ${it.currentBuildpack} ${if (it.hasUpdate()) "has an update to " + it.latestUpdate else "has no update"}\n") } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this monstrosity of a line? 🙈 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be tempted to move the
|
||
} | ||
|
||
private fun writeFailures(errors: Map<BuildpackUpdate, Exception>) { | ||
file.appendText("\n## failures\n" + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might need to be conditionally written, only when updates.length > 0? Otherwise you have a heading without content? Also: newline-ception |
||
"\n") | ||
errors.forEach { file.appendText("* ${it.key.currentBuildpack} could not be updated: ${it.value}\n") } | ||
} | ||
|
||
private fun writeFooter(file: File) { | ||
file.appendText("\nThanks for watching!\n") | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.springernature.newversion | ||
|
||
import org.amshove.kluent.fail | ||
import org.amshove.kluent.shouldBeEqualTo | ||
import org.junit.jupiter.api.Test | ||
import java.io.File | ||
|
||
class SummaryWriterTest { | ||
|
||
@Test | ||
fun `successful checks report`() { | ||
val tempFile = getTempReportFile() | ||
|
||
SummaryWriter(tempFile).write(successfulChecksResults) | ||
|
||
tempFile.readText() shouldBeEqualTo "# CF buildpack update action results\n" + "\n" + "## success\n" + "\n" + "* currentBuildpack VersionedBuildpack(name=test/buildpack1, url=https://a.host/path, version=1.3, tag=GitTag(value=v1.3)) has no update\n" + "\n" + "Thanks for watching!\n" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can template the string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean raw strings? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That works better tbh, much more readable |
||
} | ||
|
||
@Test | ||
fun `failed checks report`() { | ||
val updates: List<BuildpackUpdate> = listOf(buildpackUpdate) | ||
val errors = mapOf(Pair(buildpackUpdate, RuntimeException("Successful failure!"))) | ||
val results = FailedChecks(updates, errors) | ||
|
||
val tempFile = getTempReportFile() | ||
|
||
SummaryWriter(tempFile).write(results) | ||
|
||
tempFile.readText() shouldBeEqualTo "# CF buildpack update action results\n" + "\n" + "## failures\n" + "\n" + "* VersionedBuildpack(name=test/buildpack1, url=https://a.host/path, version=1.3, tag=GitTag(value=v1.3)) could not be updated: java.lang.RuntimeException: Successful failure!\n" + "\n" + "## success\n" + "\n" + "* currentBuildpack VersionedBuildpack(name=test/buildpack1, url=https://a.host/path, version=1.3, tag=GitTag(value=v1.3)) has no update\n" + "\n" + "Thanks for watching!\n" | ||
} | ||
|
||
@Test | ||
fun `handle blank filename`() { | ||
try { | ||
SummaryWriter("").write(successfulChecksResults) | ||
} catch (e: Exception) { | ||
fail("Unexpected exception: $e") | ||
} | ||
} | ||
|
||
@Test | ||
fun `handle null filename`() { | ||
try { | ||
SummaryWriter(null).write(successfulChecksResults) | ||
} catch (e: Exception) { | ||
fail("Unexpected exception: $e") | ||
} | ||
} | ||
|
||
companion object { | ||
val buildpackUpdate = BuildpackUpdate(listOf(File("a/path")), | ||
VersionedBuildpack("test/buildpack1", "https://a.host/path", SemanticVersion("1.3"), GitTag("v1.3")), | ||
BuildpackVersion(SemanticVersion("1.2.4"), GitTag("v1.2.4"))) | ||
|
||
val successfulChecksResults = SuccessfulChecks(listOf(buildpackUpdate)) | ||
|
||
private fun getTempReportFile(): File { | ||
val tempFile = kotlin.io.path.createTempFile(suffix = ".md").toFile() | ||
tempFile.deleteOnExit() | ||
return tempFile | ||
} | ||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/is-null-or-blank.html