-
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.
[CID-1867] Registering webhooks on boot (Create new, then delete old …
…ones) (#7) * Registering webhooks on boot. (Create new then delete old ones) * fixing formatting issues * changing gitlab webhook url from to /webhook * addressing PR comments * deleting unused CreateGitlabWebhookRequest * reading enableSSLVerification from props * adding enable-ssl-verification to test application.yaml * adding enable-ssl-verification to test application.yaml * moving interfaces to the domain package * moving interfaces to the domain package * adding LEANIX_WEBHOOK_PATH assertion to test
- Loading branch information
1 parent
503e874
commit 9d75e99
Showing
15 changed files
with
254 additions
and
6 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...onnector/applicaiton/AssignmentService.kt → ...onnector/application/AssignmentService.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
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
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/net/leanix/vsm/gitlab/broker/connector/scheduler/HeartbeatScheduler.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
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/net/leanix/vsm/gitlab/broker/webhook/adapter/feign/GitlabWebhookClient.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,35 @@ | ||
package net.leanix.vsm.gitlab.broker.webhook.adapter.feign | ||
|
||
import net.leanix.vsm.gitlab.broker.webhook.domain.GitlabWebhook | ||
import org.springframework.cloud.openfeign.FeignClient | ||
import org.springframework.web.bind.annotation.DeleteMapping | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestParam | ||
|
||
@FeignClient( | ||
name = "gitlabWebhookClient", | ||
url = "\${leanix.gitlab.base-url}", | ||
configuration = [GitlabWebhookFeignClientConfiguration::class] | ||
) | ||
interface GitlabWebhookClient { | ||
|
||
@GetMapping("/hooks") | ||
fun getAllWebhooks(): List<GitlabWebhook> | ||
|
||
@DeleteMapping("/hooks/{webhookId}") | ||
fun deleteWebhook(@PathVariable("webhookId") webhookId: Int) | ||
|
||
@Suppress("LongParameterList") | ||
@PostMapping("/hooks") | ||
fun createWebhook( | ||
@RequestParam("url") url: String, | ||
@RequestParam("token") token: String, | ||
@RequestParam("push_events") receivePushEvents: Boolean, | ||
@RequestParam("tag_push_events") receiveTagPushEvents: Boolean, | ||
@RequestParam("merge_requests_events") receiveMergeRequestEvents: Boolean, | ||
@RequestParam("repository_update_events") receiveRepositoryUpdateEvents: Boolean, | ||
@RequestParam("enable_ssl_verification") enableSSLVerification: Boolean, | ||
): GitlabWebhook | ||
} |
19 changes: 19 additions & 0 deletions
19
...t/leanix/vsm/gitlab/broker/webhook/adapter/feign/GitlabWebhookFeignClientConfiguration.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,19 @@ | ||
package net.leanix.vsm.gitlab.broker.webhook.adapter.feign | ||
|
||
import feign.RequestInterceptor | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
class GitlabWebhookFeignClientConfiguration( | ||
|
||
@Value("\${leanix.gitlab.access-token}") private val gitlabAccessToken: String | ||
) { | ||
@Bean | ||
fun requestInterceptor(): RequestInterceptor { | ||
return RequestInterceptor { | ||
it.header("PRIVATE-TOKEN", gitlabAccessToken) | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/kotlin/net/leanix/vsm/gitlab/broker/webhook/adapter/feign/GitlabWebhookProvider.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,59 @@ | ||
package net.leanix.vsm.gitlab.broker.webhook.adapter.feign | ||
|
||
import net.leanix.vsm.gitlab.broker.connector.application.AssignmentService | ||
import net.leanix.vsm.gitlab.broker.webhook.domain.GitlabWebhook | ||
import net.leanix.vsm.gitlab.broker.webhook.domain.WebhookProvider | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Component | ||
|
||
const val LEANIX_WEBHOOK_PATH = "/leanix-vsm/webhook" | ||
|
||
@Component | ||
class GitlabWebhookProvider( | ||
private val webhookClient: GitlabWebhookClient, | ||
@Value("\${leanix.gitlab.webhook-url}") private val gitlabWebhookUrl: String, | ||
@Value("\${leanix.gitlab.leanix-id}") private val leanixId: String, | ||
@Value("\${leanix.gitlab.enable-ssl-verification}") private val enableSSLVerification: Boolean, | ||
) : WebhookProvider { | ||
|
||
private val logger = LoggerFactory.getLogger(AssignmentService::class.java) | ||
|
||
override fun getAllWebhooks(): List<GitlabWebhook> { | ||
return kotlin.runCatching { | ||
webhookClient.getAllWebhooks() | ||
}.onSuccess { | ||
logger.info("Webhooks fetched. size: ${it.size}") | ||
}.onFailure { | ||
logger.error("Error while fetching webhooks: ${it.message}") | ||
}.getOrThrow() | ||
} | ||
|
||
override fun deleteWebhook(webhookId: Int) { | ||
return kotlin.runCatching { | ||
webhookClient.deleteWebhook(webhookId) | ||
}.onSuccess { | ||
logger.info("Webhooks deleted for id: $webhookId") | ||
}.onFailure { | ||
logger.error("Error while deleting webhook with id $webhookId: ${it.message}") | ||
}.getOrThrow() | ||
} | ||
|
||
override fun createWebhook(): GitlabWebhook { | ||
return kotlin.runCatching { | ||
webhookClient.createWebhook( | ||
url = "$gitlabWebhookUrl$LEANIX_WEBHOOK_PATH", | ||
token = leanixId, | ||
receivePushEvents = true, | ||
receiveTagPushEvents = false, | ||
receiveMergeRequestEvents = true, | ||
receiveRepositoryUpdateEvents = true, | ||
enableSSLVerification = enableSSLVerification | ||
) | ||
}.onSuccess { | ||
logger.info("Webhook created with id ${it.id}") | ||
}.onFailure { | ||
logger.error("Error creating webhook: ${it.message}") | ||
}.getOrThrow() | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/net/leanix/vsm/gitlab/broker/webhook/application/GitlabWebhookServiceImpl.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,23 @@ | ||
package net.leanix.vsm.gitlab.broker.webhook.application | ||
|
||
import net.leanix.vsm.gitlab.broker.webhook.adapter.feign.LEANIX_WEBHOOK_PATH | ||
import net.leanix.vsm.gitlab.broker.webhook.domain.GitlabWebhook | ||
import net.leanix.vsm.gitlab.broker.webhook.domain.WebhookProvider | ||
import net.leanix.vsm.gitlab.broker.webhook.domain.WebhookService | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class GitlabWebhookServiceImpl( | ||
private val webhookProvider: WebhookProvider | ||
) : WebhookService { | ||
|
||
override fun registerWebhook(): GitlabWebhook { | ||
val webhook = webhookProvider.createWebhook() | ||
|
||
webhookProvider.getAllWebhooks() | ||
.filter { it.url.contains(LEANIX_WEBHOOK_PATH) && it.id != webhook.id } | ||
.forEach { webhookProvider.deleteWebhook(it.id) } | ||
|
||
return webhook | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/net/leanix/vsm/gitlab/broker/webhook/domain/GitlabWebhook.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,21 @@ | ||
package net.leanix.vsm.gitlab.broker.webhook.domain | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import java.util.Date | ||
|
||
data class GitlabWebhook( | ||
val id: Int, | ||
val url: String, | ||
@JsonProperty("created_at") | ||
val createdAt: Date, | ||
@JsonProperty("push_events") | ||
val pushEvents: Boolean, | ||
@JsonProperty("tag_push_events") | ||
val tagPushEvents: Boolean, | ||
@JsonProperty("merge_requests_events") | ||
val mergeRequestsEvents: Boolean, | ||
@JsonProperty("repository_update_events") | ||
val repositoryUpdateEvents: Boolean, | ||
@JsonProperty("enable_ssl_verification") | ||
val enableSSLVerification: Boolean, | ||
) |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/net/leanix/vsm/gitlab/broker/webhook/domain/WebhookProvider.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,8 @@ | ||
package net.leanix.vsm.gitlab.broker.webhook.domain | ||
|
||
interface WebhookProvider { | ||
|
||
fun getAllWebhooks(): List<GitlabWebhook> | ||
fun deleteWebhook(webhookId: Int) | ||
fun createWebhook(): GitlabWebhook | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/net/leanix/vsm/gitlab/broker/webhook/domain/WebhookService.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.webhook.domain | ||
|
||
interface WebhookService { | ||
fun registerWebhook(): GitlabWebhook | ||
} |
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
2 changes: 1 addition & 1 deletion
2
src/test/kotlin/net/leanix/vsm/gitlab/broker/connector/scheduler/HeartbeatSchedulerTest.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
47 changes: 47 additions & 0 deletions
47
...t/kotlin/net/leanix/vsm/gitlab/broker/webhook/application/GitlabWebhookServiceImplTest.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,47 @@ | ||
package net.leanix.vsm.gitlab.broker.webhook.application | ||
|
||
import net.leanix.vsm.gitlab.broker.webhook.adapter.feign.LEANIX_WEBHOOK_PATH | ||
import net.leanix.vsm.gitlab.broker.webhook.domain.GitlabWebhook | ||
import net.leanix.vsm.gitlab.broker.webhook.domain.WebhookProvider | ||
import org.junit.jupiter.api.Test | ||
import org.mockito.ArgumentMatchers.eq | ||
import org.mockito.Mockito.mock | ||
import org.mockito.Mockito.times | ||
import org.mockito.Mockito.verify | ||
import org.mockito.Mockito.`when` | ||
import java.util.Date | ||
|
||
class GitlabWebhookServiceImplTest { | ||
|
||
private val webhookProvider = mock(WebhookProvider::class.java) | ||
private val subject = GitlabWebhookServiceImpl(webhookProvider) | ||
|
||
@Test | ||
fun `given a new one webhook is created successfully when registerWebhook then delete all other leanix webhooks`() { | ||
`when`(webhookProvider.createWebhook()).thenReturn(dummyGitlabWebhookDto(id = 1)) | ||
`when`(webhookProvider.getAllWebhooks()).thenReturn( | ||
listOf( | ||
dummyGitlabWebhookDto(id = 1), | ||
dummyGitlabWebhookDto(id = 2), | ||
dummyGitlabWebhookDto(id = 3, url = "https://gitlab.example.com/webhook"), | ||
) | ||
) | ||
|
||
subject.registerWebhook() | ||
|
||
verify(webhookProvider, times(0)).deleteWebhook(eq(1)) | ||
verify(webhookProvider, times(0)).deleteWebhook(eq(3)) | ||
verify(webhookProvider).deleteWebhook(eq(2)) | ||
} | ||
} | ||
|
||
fun dummyGitlabWebhookDto(id: Int, url: String = "https://gitlab.example.com$LEANIX_WEBHOOK_PATH") = GitlabWebhook( | ||
id = id, | ||
url = url, | ||
createdAt = Date(), | ||
pushEvents = true, | ||
tagPushEvents = false, | ||
mergeRequestsEvents = true, | ||
repositoryUpdateEvents = true, | ||
enableSSLVerification = false | ||
) |
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