Skip to content
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

Merged
merged 5 commits into from Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/kotlin/com/springernature/newversion/Main.kt
Expand Up @@ -20,5 +20,7 @@ fun main() {
is SuccessfulChecks -> Unit
}

SummaryWriter(System.getenv("GITHUB_STEP_SUMMARY")).write(results)

exitProcess(results.exitStatus())
}
46 changes: 46 additions & 0 deletions src/main/kotlin/com/springernature/newversion/SummaryWriter.kt
@@ -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()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")
Copy link
Collaborator

Choose a reason for hiding this comment

The 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") }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this monstrosity of a line? 🙈

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be tempted to move the if to a chained appendText, e.g.

updates.forEach {
  file.appendText("* currentBuildpack ${it.currentBuildpack} ")
  file.appendText(if (it.hasUpdate())"has an update to " + it.latestUpdate else "has no update")
  file.appendText("\n")
}

}

private fun writeFailures(errors: Map<BuildpackUpdate, Exception>) {
file.appendText("\n## failures\n" +
Copy link
Collaborator

Choose a reason for hiding this comment

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

}
65 changes: 65 additions & 0 deletions src/test/kotlin/com/springernature/newversion/SummaryWriterTest.kt
@@ -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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can template the string

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean raw strings? 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The 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
}

}

}