-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d855db
commit 7662caa
Showing
16 changed files
with
546 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...n/kotlin/net/leanix/vsm/gitlab/broker/connector/application/WebhookConsumerServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package net.leanix.vsm.gitlab.broker.connector.application | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import net.leanix.vsm.gitlab.broker.connector.domain.MergeRequest | ||
import net.leanix.vsm.gitlab.broker.connector.domain.ProjectCreated | ||
import net.leanix.vsm.gitlab.broker.connector.domain.WebhookConsumerService | ||
import net.leanix.vsm.gitlab.broker.connector.domain.WebhookEventType | ||
import net.leanix.vsm.gitlab.broker.connector.json.computeWebhookEventType | ||
import net.leanix.vsm.gitlab.broker.shared.exception.GitlabTokenException | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class WebhookConsumerServiceImpl( | ||
@Value("\${leanix.gitlab.leanix-id}") private val leanixId: String | ||
) : WebhookConsumerService { | ||
|
||
val mapper: ObjectMapper = jacksonObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | ||
override fun consumeWebhookEvent(payloadToken: String?, payload: String) { | ||
|
||
// validate token | ||
if (payloadToken?.equals(leanixId) == true) { | ||
throw GitlabTokenException(payloadToken) | ||
} | ||
|
||
// figure out event type | ||
when (computeWebhookEventType(payload)) { | ||
WebhookEventType.REPOSITORY -> processProjectCreated(payload) | ||
WebhookEventType.MERGE_REQUEST -> println() | ||
} | ||
} | ||
|
||
private fun processProjectCreated(payload: String) { | ||
val project = mapper.readValue<ProjectCreated>(payload) | ||
println(project) | ||
} | ||
|
||
private fun processMergeRequest(payload: String) { | ||
val mergeRequest = mapper.readValue<MergeRequest>(payload) | ||
println(mergeRequest) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/net/leanix/vsm/gitlab/broker/connector/domain/WebhookConsumerService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package net.leanix.vsm.gitlab.broker.connector.domain | ||
|
||
interface WebhookConsumerService { | ||
fun consumeWebhookEvent(payloadToken: String?, payload: String) | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/kotlin/net/leanix/vsm/gitlab/broker/connector/domain/WebhookData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package net.leanix.vsm.gitlab.broker.connector.domain | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import java.util.Date | ||
|
||
data class ProjectCreated( | ||
@JsonProperty("project_id") | ||
val id: Int, | ||
val eventName: String, | ||
val name: String, | ||
val path: String, | ||
@JsonProperty("path_with_namespace") | ||
val pathWithNamespace: String, | ||
@JsonProperty("project_visibility") | ||
val projectVisibility: String, | ||
) | ||
|
||
data class MergeRequest( | ||
val project: Project, | ||
@JsonProperty("object_attributes") | ||
val objectAttributes: ObjectAttributes, | ||
) | ||
|
||
data class Project( | ||
val id: Int, | ||
val name: String, | ||
@JsonProperty("web_url") | ||
val webURL: String, | ||
val namespace: String, | ||
@JsonProperty("path_with_namespace") | ||
val pathWithNamespace: String, | ||
@JsonProperty("default_branch") | ||
val defaultBranch: String, | ||
) | ||
|
||
data class ObjectAttributes( | ||
val description: String, | ||
val created_at: Date, | ||
val source_branch: String, | ||
val target_branch: String, | ||
val title: String, | ||
val updated_at: String, | ||
val last_commit: GitlabCommit, | ||
val state: String, | ||
val action: String, | ||
) | ||
|
||
data class GitlabCommit( | ||
val id: String, | ||
val message: String, | ||
val title: String, | ||
) |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/net/leanix/vsm/gitlab/broker/connector/domain/WebhookEventType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package net.leanix.vsm.gitlab.broker.connector.domain | ||
|
||
enum class WebhookEventType { | ||
REPOSITORY, | ||
MERGE_REQUEST | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/net/leanix/vsm/gitlab/broker/connector/json/GitlabParseUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package net.leanix.vsm.gitlab.broker.connector.json | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import net.leanix.vsm.gitlab.broker.connector.domain.WebhookEventType | ||
import net.leanix.vsm.gitlab.broker.shared.exception.GitlabPayloadNotSupportedException | ||
|
||
val mapper: ObjectMapper = jacksonObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | ||
|
||
fun computeWebhookEventType(payload: String): WebhookEventType { | ||
val node = mapper.readTree(payload) | ||
|
||
return if (node.at("/event_name").asText() == "project_create") WebhookEventType.REPOSITORY | ||
else if ( | ||
node.at("/event_type").asText() == "merge_request" | ||
&& node.at("/object_attributes/action").asText() == "merge" | ||
&& node.path("/project/default_branch").asText() | ||
== node.path("/object_attributes/target_branch").asText() | ||
) WebhookEventType.MERGE_REQUEST | ||
else throw GitlabPayloadNotSupportedException() | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/kotlin/net/leanix/vsm/gitlab/broker/connector/rest/GitlabWebhookController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package net.leanix.vsm.gitlab.broker.connector.rest | ||
|
||
import net.leanix.vsm.gitlab.broker.connector.domain.WebhookConsumerService | ||
import net.leanix.vsm.gitlab.broker.webhook.adapter.feign.LEANIX_WEBHOOK_PATH | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestHeader | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.ResponseStatus | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequestMapping(LEANIX_WEBHOOK_PATH) | ||
class GitlabWebhookController( | ||
private val webhookService: WebhookConsumerService | ||
) { | ||
|
||
private val logger = LoggerFactory.getLogger(this::class.java) | ||
|
||
@PostMapping | ||
@ResponseStatus(HttpStatus.ACCEPTED) | ||
fun webhook( | ||
@RequestHeader("X-Gitlab-Instance", required = false) instance: String?, | ||
@RequestHeader("X-Gitlab-Webhook-UUID", required = false) webhookUUID: String?, | ||
@RequestHeader("X-Gitlab-Event", required = false) event: String?, | ||
@RequestHeader("X-Gitlab-Event-UUID", required = false) eventUUID: String?, | ||
@RequestHeader("X-Gitlab-Token", required = false) payloadToken: String?, | ||
@RequestBody payload: String | ||
) { | ||
logger.info("instance: $instance") | ||
logger.info("webhookUUID: $webhookUUID") | ||
logger.info("event: $event") | ||
logger.info("eventUUID: $eventUUID") | ||
logger.info("payloadToken: $payloadToken") | ||
logger.info("payload: $payload") | ||
|
||
runCatching { | ||
webhookService.consumeWebhookEvent(payloadToken, payload) | ||
}.onFailure { | ||
logger.error("Error consuming github event", it) | ||
} | ||
} | ||
|
||
@GetMapping | ||
fun health() = "OK" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/net/leanix/vsm/gitlab/broker/shared/exception/Exceptions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package net.leanix.vsm.gitlab.broker.shared.exception | ||
|
||
sealed class VsmException(message: String? = null) : RuntimeException(message) | ||
|
||
class NoRepositoriesFound : VsmException() | ||
|
||
class GraphqlException(message: String?) : VsmException(message) | ||
class GitlabTokenException(token: String?) : VsmException(message = "Invalid gitlab payload token: $token") | ||
class GitlabPayloadNotSupportedException() : | ||
VsmException(message = "Payload is neither for project creation nor for merge request being merged") |
8 changes: 0 additions & 8 deletions
8
src/main/kotlin/net/leanix/vsm/gitlab/broker/shared/exception/VsmException.kt
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
src/test/kotlin/net/leanix/vsm/gitlab/broker/connector/json/GitlabParseUtilTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package net.leanix.vsm.gitlab.broker.connector.json | ||
|
||
import net.leanix.vsm.gitlab.broker.connector.domain.WebhookEventType | ||
import net.leanix.vsm.gitlab.broker.shared.exception.GitlabPayloadNotSupportedException | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
|
||
class GitlabParseUtilTest { | ||
|
||
@Test | ||
fun `should return webhook event type REPOSITORY when event_name=project_create in payload`() { | ||
val payload = this::class.java.getResource("/webhook_calls/project_created.json")!!.readText() | ||
|
||
val result = computeWebhookEventType(payload) | ||
|
||
assertEquals(WebhookEventType.REPOSITORY, result) | ||
} | ||
|
||
@Test | ||
fun `should return webhook event type MERGE_REQUEST when object_kind=merge_request and action=merged in payload`() { | ||
val payload = this::class.java.getResource("/webhook_calls/merge_request_merged.json")!!.readText() | ||
|
||
val result = computeWebhookEventType(payload) | ||
|
||
assertEquals(WebhookEventType.MERGE_REQUEST, result) | ||
} | ||
|
||
@Test | ||
fun `should throw GitlabPayloadNotSupportedException when object_kind=merge_request and action!=merged in payload`() { | ||
val payload = this::class.java.getResource("/webhook_calls/merge_request_opened.json")!!.readText() | ||
|
||
assertThrows<GitlabPayloadNotSupportedException> { computeWebhookEventType(payload) } | ||
} | ||
|
||
@Test | ||
fun `should throw GitlabPayloadNotSupportedException when payload has no supported fields`() { | ||
|
||
assertThrows<GitlabPayloadNotSupportedException> { | ||
computeWebhookEventType("{ \"dummy_key\": \"dummy value\" }") | ||
} | ||
} | ||
} |
Oops, something went wrong.