Skip to content

Commit

Permalink
#1357 Adding the source promotion run in the events
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoraboeuf committed Oct 14, 2024
1 parent 6615674 commit b8014a9
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object AutoVersioningEvents {
context = eventContext(
eventProject("Target project"),
eventBranch("Target branch"),
eventXProject("Dependency/source project"),
eventXPromotionRun("Source promotion run"),
eventValue("VERSION", "Version being set"),
eventValue("MESSAGE", "Auto versioning message"),
eventValue("PR_NAME", "Title of the PR having been created"),
Expand All @@ -38,7 +38,7 @@ object AutoVersioningEvents {
context = eventContext(
eventProject("Target project"),
eventBranch("Target branch"),
eventXProject("Dependency/source project"),
eventXPromotionRun("Source promotion run"),
eventValue("VERSION", "Version being set"),
eventValue("MESSAGE", "Auto versioning message"),
eventValue("ERROR", "Error message"),
Expand All @@ -56,7 +56,7 @@ object AutoVersioningEvents {
context = eventContext(
eventProject("Target project"),
eventBranch("Target branch"),
eventXProject("Dependency/source project"),
eventXPromotionRun("Source promotion run"),
eventValue("VERSION", "Version being set"),
eventValue("MESSAGE", "Auto versioning message"),
eventValue("LINK", "Link to the post processing process"),
Expand All @@ -76,7 +76,7 @@ object AutoVersioningEvents {
context = eventContext(
eventProject("Target project"),
eventBranch("Target branch"),
eventXProject("Dependency/source project"),
eventXPromotionRun("Source promotion run"),
eventValue("VERSION", "Version being set"),
eventValue("PR_NAME", "Title of the PR having been created"),
eventValue("PR_LINK", "Link to the PR having been created"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import net.nemerosa.ontrack.extension.av.dispatcher.AutoVersioningOrder
import net.nemerosa.ontrack.extension.av.postprocessing.PostProcessingFailureException
import net.nemerosa.ontrack.extension.scm.service.SCMPullRequest
import net.nemerosa.ontrack.model.events.Event
import net.nemerosa.ontrack.model.exceptions.ProjectNotFoundException
import net.nemerosa.ontrack.model.structure.ID
import net.nemerosa.ontrack.model.structure.StructureService
import org.springframework.stereotype.Service
import kotlin.jvm.optionals.getOrNull

@Service
class AutoVersioningEventsFactoryImpl(
Expand All @@ -21,7 +20,7 @@ class AutoVersioningEventsFactoryImpl(
): Event =
Event.of(AutoVersioningEvents.AUTO_VERSIONING_SUCCESS)
.withBranch(order.branch)
.withExtraProject(sourceProject(order))
.withSourcePromotionRun(order)
.with("VERSION", order.targetVersion)
.with("PROMOTION", order.sourcePromotion)
.with("MESSAGE", close(message))
Expand All @@ -36,7 +35,7 @@ class AutoVersioningEventsFactoryImpl(
): Event = if (error is PostProcessingFailureException) {
Event.of(AutoVersioningEvents.AUTO_VERSIONING_POST_PROCESSING_ERROR)
.withBranch(order.branch)
.withExtraProject(sourceProject(order))
.withSourcePromotionRun(order)
.with("VERSION", order.targetVersion)
.with("PROMOTION", order.sourcePromotion)
.with("MESSAGE", close(message))
Expand All @@ -45,7 +44,7 @@ class AutoVersioningEventsFactoryImpl(
} else {
Event.of(AutoVersioningEvents.AUTO_VERSIONING_ERROR)
.withBranch(order.branch)
.withExtraProject(sourceProject(order))
.withSourcePromotionRun(order)
.with("VERSION", order.targetVersion)
.with("PROMOTION", order.sourcePromotion)
.with("MESSAGE", close(message))
Expand All @@ -59,17 +58,22 @@ class AutoVersioningEventsFactoryImpl(
): Event =
Event.of(AutoVersioningEvents.AUTO_VERSIONING_PR_MERGE_TIMEOUT_ERROR)
.withBranch(order.branch)
.withExtraProject(sourceProject(order))
.withSourcePromotionRun(order)
.with("VERSION", order.targetVersion)
.with("PROMOTION", order.sourcePromotion)
.with("PR_NAME", pr.name)
.with("PR_LINK", pr.link)
.build()

private fun sourceProject(order: AutoVersioningOrder) =
structureService.findProjectByName(order.sourceProject)
.getOrNull()
?: throw ProjectNotFoundException(order.sourceProject)
private fun Event.EventBuilder.withSourcePromotionRun(order: AutoVersioningOrder) =
sourcePromotionRun(order)?.let {
withExtra(it)
} ?: this

private fun sourcePromotionRun(order: AutoVersioningOrder) =
order.sourcePromotionRunId?.let {
structureService.findPromotionRunByID(ID.of(it))
}

private fun close(message: String) =
if (message.endsWith(".")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ object AutoVersioningTestFixtures {
fun Build.createOrder(
targetBranch: Branch,
targetVersion: String = "2.0.0",
sourcePromotionRunId: Int? = null,
) = AutoVersioningOrder(
uuid = UUID.randomUUID().toString(),
sourceProject = project.name,
sourceBuildId = id(),
sourcePromotionRunId = null,
sourcePromotionRunId = sourcePromotionRunId,
sourcePromotion = "GOLD",
sourceBackValidation = null,
branch = targetBranch,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package net.nemerosa.ontrack.extension.av.event

import net.nemerosa.ontrack.extension.av.AutoVersioningTestFixtures.createOrder
import net.nemerosa.ontrack.extension.av.dispatcher.AutoVersioningOrder
import net.nemerosa.ontrack.extension.scm.service.SCMPullRequest
import net.nemerosa.ontrack.it.AbstractDSLTestSupport
import net.nemerosa.ontrack.model.events.EventTemplatingService
import net.nemerosa.ontrack.model.events.HtmlNotificationEventRenderer
import net.nemerosa.ontrack.model.structure.Branch
import net.nemerosa.ontrack.model.structure.PromotionRun
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import kotlin.test.assertEquals
Expand All @@ -22,149 +25,133 @@ internal class AutoVersioningEventsIT : AbstractDSLTestSupport() {

@Test
fun `Rendering the post processing error event`() {
val target = doCreateBranch()
project {
branch {
build {
val order = createOrder(
targetBranch = target,
targetVersion = "1.1.0",
)
val event = autoVersioningEventsFactory.error(
order = order,
message = "Post processing error",
error = MockPostProcessingFailureException(
message = "Remote job failed",
link = "https://job.link",
)
)
val text = eventTemplatingService.renderEvent(
event,
context = emptyMap(),
renderer = htmlNotificationEventRenderer
)
assertEquals(
"""
Auto versioning post-processing of <a href="http://localhost:8080/#/project/${target.project.id}">${target.project.name}</a>/<a href="http://localhost:8080/#/branch/${target.id}">${target.name}</a> for dependency <a href="http://localhost:8080/#/project/${project.id}">${project.name}</a> version "1.1.0" has failed.
<a href="https://job.link">Post processing error.</a>
""".trimIndent(),
text
)
}
}
withOrder { order, run, target ->
val event = autoVersioningEventsFactory.error(
order = order,
message = "Post processing error",
error = MockPostProcessingFailureException(
message = "Remote job failed",
link = "https://job.link",
)
)
val text = eventTemplatingService.renderEvent(
event,
context = emptyMap(),
renderer = htmlNotificationEventRenderer
)
assertEquals(
"""
Auto versioning post-processing of <a href="http://localhost:8080/#/project/${target.project.id}">${target.project.name}</a>/<a href="http://localhost:8080/#/branch/${target.id}">${target.name}</a> for dependency <a href="http://localhost:8080/#/project/${run.project.id}">${run.project.name}</a> version "1.1.0" has failed.
<a href="https://job.link">Post processing error.</a>
""".trimIndent(),
text
)
}
}

@Test
fun `Rendering the success event`() {
val target = doCreateBranch()
project {
branch {
build {
val order = createOrder(
targetBranch = target,
targetVersion = "1.1.0",
)
val event = autoVersioningEventsFactory.success(
order = order,
message = "Created, approved and merged.",
pr = SCMPullRequest(
id = "42",
name = "PR-42",
link = "https://scm/pr/42",
merged = true,
)
)
val text = eventTemplatingService.renderEvent(
event,
context = emptyMap(),
renderer = htmlNotificationEventRenderer
)
assertEquals(
"""
Auto versioning of <a href="http://localhost:8080/#/project/${target.project.id}">${target.project.name}</a>/<a href="http://localhost:8080/#/branch/${target.id}">${target.name}</a> for dependency <a href="http://localhost:8080/#/project/${project.id}">${project.name}</a> version "1.1.0" has been done.
withOrder { order, run, target ->
val event = autoVersioningEventsFactory.success(
order = order,
message = "Created, approved and merged.",
pr = SCMPullRequest(
id = "42",
name = "PR-42",
link = "https://scm/pr/42",
merged = true,
)
)
val text = eventTemplatingService.renderEvent(
event,
context = emptyMap(),
renderer = htmlNotificationEventRenderer
)
assertEquals(
"""
Auto versioning of <a href="http://localhost:8080/#/project/${target.project.id}">${target.project.name}</a>/<a href="http://localhost:8080/#/branch/${target.id}">${target.name}</a> for dependency <a href="http://localhost:8080/#/project/${run.project.id}">${run.project.name}</a> version "1.1.0" has been done.
Created, approved and merged.
Pull request <a href="https://scm/pr/42">PR-42</a>
""".trimIndent(),
text
)
}
}
text
)
}
}

@Test
fun `Rendering the processing error event`() {
val target = doCreateBranch()
project {
branch {
build {
val order = createOrder(
targetBranch = target,
targetVersion = "1.1.0",
)
val event = autoVersioningEventsFactory.error(
order = order,
message = "Processing failed.",
error = RuntimeException("Processing failed because of this error.")
)
val text = eventTemplatingService.renderEvent(
event,
context = emptyMap(),
renderer = htmlNotificationEventRenderer
)
assertEquals(
"""
Auto versioning of <a href="http://localhost:8080/#/project/${target.project.id}">${target.project.name}</a>/<a href="http://localhost:8080/#/branch/${target.id}">${target.name}</a> for dependency <a href="http://localhost:8080/#/project/${project.id}">${project.name}</a> version "1.1.0" has failed.
withOrder { order, run, target ->
val event = autoVersioningEventsFactory.error(
order = order,
message = "Processing failed.",
error = RuntimeException("Processing failed because of this error.")
)
val text = eventTemplatingService.renderEvent(
event,
context = emptyMap(),
renderer = htmlNotificationEventRenderer
)
assertEquals(
"""
Auto versioning of <a href="http://localhost:8080/#/project/${target.project.id}">${target.project.name}</a>/<a href="http://localhost:8080/#/branch/${target.id}">${target.name}</a> for dependency <a href="http://localhost:8080/#/project/${run.project.id}">${run.project.name}</a> version "1.1.0" has failed.
Processing failed.
Error: Processing failed because of this error.
""".trimIndent(),
text
)
}
}
text
)
}
}

@Test
fun `Rendering the PR timeout event`() {
withOrder { order, run, target ->
val event = autoVersioningEventsFactory.prMergeTimeoutError(
order = order,
pr = SCMPullRequest(
id = "42",
name = "PR-42",
link = "https://scm/pr/42",
merged = true,
)
)
val text = eventTemplatingService.renderEvent(
event,
context = emptyMap(),
renderer = htmlNotificationEventRenderer
)
assertEquals(
"""
Auto versioning of <a href="http://localhost:8080/#/project/${target.project.id}">${target.project.name}</a>/<a href="http://localhost:8080/#/branch/${target.id}">${target.name}</a> for dependency <a href="http://localhost:8080/#/project/${run.project.id}">${run.project.name}</a> version "1.1.0" has failed.
Timeout while waiting for the PR to be ready to be merged.
Pull request <a href="https://scm/pr/42">PR-42</a>
""".trimIndent(),
text
)
}
}

private fun withOrder(
code: (order: AutoVersioningOrder, run: PromotionRun, target: Branch) -> Unit
) {
val target = doCreateBranch()
project {
branch {
val pl = promotionLevel()
build {
val run = promote(pl)
val order = createOrder(
targetBranch = target,
targetVersion = "1.1.0",
sourcePromotionRunId = run.id(),
)
val event = autoVersioningEventsFactory.prMergeTimeoutError(
order = order,
pr = SCMPullRequest(
id = "42",
name = "PR-42",
link = "https://scm/pr/42",
merged = true,
)
)
val text = eventTemplatingService.renderEvent(
event,
context = emptyMap(),
renderer = htmlNotificationEventRenderer
)
assertEquals(
"""
Auto versioning of <a href="http://localhost:8080/#/project/${target.project.id}">${target.project.name}</a>/<a href="http://localhost:8080/#/branch/${target.id}">${target.name}</a> for dependency <a href="http://localhost:8080/#/project/${project.id}">${project.name}</a> version "1.1.0" has failed.
Timeout while waiting for the PR to be ready to be merged.
Pull request <a href="https://scm/pr/42">PR-42</a>
""".trimIndent(),
text
)
code(order, run, target)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ class Event(
return with(project)
}

fun withExtraProject(project: Project): EventBuilder {
return withExtra(project)
}

fun withRef(entity: ProjectEntity): EventBuilder {
ref = entity.projectEntityType
return withProject(entity.project).with(entity)
Expand Down
Loading

0 comments on commit b8014a9

Please sign in to comment.