-
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.
Registering webhooks on boot. (Create new then delete old ones)
- Loading branch information
1 parent
d9e4c9c
commit 9439666
Showing
14 changed files
with
250 additions
and
5 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.GitlabWebhookDto | ||
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<GitlabWebhookDto> | ||
|
||
@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, | ||
): GitlabWebhookDto | ||
} |
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) | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
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.GitlabWebhookDto | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Component | ||
|
||
interface WebhookProvider { | ||
fun getAllWebhooks(): List<GitlabWebhookDto> | ||
fun deleteWebhook(webhookId: Int) | ||
fun createWebhook(): GitlabWebhookDto | ||
} | ||
|
||
@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 | ||
) : WebhookProvider { | ||
|
||
private val logger = LoggerFactory.getLogger(AssignmentService::class.java) | ||
override fun getAllWebhooks(): List<GitlabWebhookDto> { | ||
return kotlin.runCatching { | ||
webhookClient.getAllWebhooks() | ||
}.onSuccess { | ||
logger.info("Webhooks fetched. size: ${it.size}") | ||
}.getOrThrow() | ||
} | ||
|
||
override fun deleteWebhook(webhookId: Int) { | ||
return kotlin.runCatching { | ||
webhookClient.deleteWebhook(webhookId) | ||
}.onSuccess { | ||
logger.info("Webhooks deleted for id: $webhookId") | ||
}.getOrThrow() | ||
} | ||
|
||
override fun createWebhook(): GitlabWebhookDto { | ||
return kotlin.runCatching { | ||
webhookClient.createWebhook( | ||
url = gitlabWebhookUrl, | ||
token = leanixId, | ||
receivePushEvents = true, | ||
receiveTagPushEvents = false, | ||
receiveMergeRequestEvents = true, | ||
receiveRepositoryUpdateEvents = true, | ||
enableSSLVerification = false | ||
) | ||
}.onSuccess { | ||
logger.info("Webhook created with id ${it.id}") | ||
}.getOrThrow() | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/net/leanix/vsm/gitlab/broker/webhook/application/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,25 @@ | ||
package net.leanix.vsm.gitlab.broker.webhook.application | ||
|
||
import net.leanix.vsm.gitlab.broker.webhook.adapter.feign.WebhookProvider | ||
import net.leanix.vsm.gitlab.broker.webhook.domain.GitlabWebhookDto | ||
import org.springframework.stereotype.Service | ||
|
||
interface WebhookService { | ||
fun registerWebhook(): GitlabWebhookDto | ||
} | ||
|
||
@Service | ||
class GitlabWebhookServiceImpl( | ||
private val webhookProvider: WebhookProvider | ||
) : WebhookService { | ||
|
||
override fun registerWebhook(): GitlabWebhookDto { | ||
val webhook = webhookProvider.createWebhook() | ||
|
||
webhookProvider.getAllWebhooks() | ||
.filter { it.id != webhook.id } | ||
.forEach { webhookProvider.deleteWebhook(it.id) } | ||
|
||
return webhook | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/net/leanix/vsm/gitlab/broker/webhook/domain/CreateGitlabWebhookRequestDto.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,18 @@ | ||
package net.leanix.vsm.gitlab.broker.webhook.domain | ||
|
||
import com.nimbusds.jose.shaded.gson.annotations.SerializedName | ||
|
||
data class CreateGitlabWebhookRequestDto( | ||
val url: String, | ||
val token: String, | ||
@SerializedName("push_events") | ||
val pushEvents: Boolean, | ||
@SerializedName("tag_push_events") | ||
val tagPushEvents: Boolean, | ||
@SerializedName("merge_requests_events") | ||
val mergeRequestsEvents: Boolean, | ||
@SerializedName("repository_update_events") | ||
val repositoryUpdateEvents: Boolean, | ||
@SerializedName("enable_ssl_verification") | ||
val enableSSLVerification: Boolean, | ||
) |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/net/leanix/vsm/gitlab/broker/webhook/domain/GitlabWebhookDto.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 GitlabWebhookDto( | ||
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, | ||
) |
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
44 changes: 44 additions & 0 deletions
44
...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,44 @@ | ||
package net.leanix.vsm.gitlab.broker.webhook.application | ||
|
||
import net.leanix.vsm.gitlab.broker.webhook.adapter.feign.WebhookProvider | ||
import net.leanix.vsm.gitlab.broker.webhook.domain.GitlabWebhookDto | ||
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 webhooks`() { | ||
`when`(webhookProvider.createWebhook()).thenReturn(dummyGitlabWebhookDto(id = 1)) | ||
`when`(webhookProvider.getAllWebhooks()).thenReturn( | ||
listOf( | ||
dummyGitlabWebhookDto(id = 1), | ||
dummyGitlabWebhookDto(id = 2) | ||
) | ||
) | ||
|
||
subject.registerWebhook() | ||
|
||
verify(webhookProvider, times(0)).deleteWebhook(eq(1)) | ||
verify(webhookProvider).deleteWebhook(eq(2)) | ||
} | ||
} | ||
|
||
fun dummyGitlabWebhookDto(id: Int) = GitlabWebhookDto( | ||
id = id, | ||
url = "https://gitlab.example.com/hook", | ||
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