From dd46dc6ee4f8fcd3c45ac225dcf48fabac6fc178 Mon Sep 17 00:00:00 2001 From: Felix Werner Date: Thu, 18 Jun 2026 14:45:34 +0200 Subject: [PATCH] fix: only notify registration when actually adding a new webhook --- .../kotlin/com/wire/github/EventsHandler.kt | 22 ++++++---- .../github/github/GitHubWebhookManager.kt | 43 ++++++++++++++----- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/com/wire/github/EventsHandler.kt b/src/main/kotlin/com/wire/github/EventsHandler.kt index a2d94fd..8f19a9e 100644 --- a/src/main/kotlin/com/wire/github/EventsHandler.kt +++ b/src/main/kotlin/com/wire/github/EventsHandler.kt @@ -32,20 +32,20 @@ class EventsHandler : WireEventsHandlerSuspending() { ) return } - val repositories = GitHubPullRequestLinkParser.parse(wireMessage.text) if (repositories.isEmpty()) { return } - val message = registerRepositories( + registerRepositories( repositories = repositories, conversationId = wireMessage.conversationId - ) - sendMessage( - conversationId = wireMessage.conversationId, - text = message - ) + )?.let { message -> + sendMessage( + conversationId = wireMessage.conversationId, + text = message + ) + } } override suspend fun onAppAddedToConversation( @@ -84,8 +84,12 @@ class EventsHandler : WireEventsHandlerSuspending() { conversationId = conversationId ) }.fold( - onSuccess = { - "Receiving pull request notifications for `${repository.fullName}`." + onSuccess = { result -> + if (result.newlySubscribed) { + "Receiving pull request notifications for `${repository.fullName}`." + } else { + null + } }, onFailure = { exception -> logger.warn( diff --git a/src/main/kotlin/com/wire/github/github/GitHubWebhookManager.kt b/src/main/kotlin/com/wire/github/github/GitHubWebhookManager.kt index 2679c37..9ded774 100644 --- a/src/main/kotlin/com/wire/github/github/GitHubWebhookManager.kt +++ b/src/main/kotlin/com/wire/github/github/GitHubWebhookManager.kt @@ -34,22 +34,45 @@ class GitHubWebhookManager( ) } + data class RegistrationResult( + val newlySubscribed: Boolean, + val webhookId: Long + ) + fun ensureWebhookForConversation( repository: GitHubRepository, conversationId: QualifiedId - ): Long { + ): RegistrationResult { storage.sadd(KNOWN_REPOSITORIES_KEY, repository.fullName) - storage.sadd(conversationsKey(repository.fullName), conversationId.toStorageKey()) + + val newlySubscribed = + storage.sadd( + conversationsKey(repository.fullName), + conversationId.toStorageKey() + ) > 0 + markRepositoryActive(repository.fullName) - return gitHubAppClient - .ensureRepositoryWebhook( - repository = repository, - webhookUrl = webhookUrl(), - webhookSecret = githubWebhookSecret() - ).also { webhookId -> - storage.set(webhookIdKey(repository.fullName), webhookId.toString()) - } + val existingWebhookId = storage.get(webhookIdKey(repository.fullName))?.toLongOrNull() + if (existingWebhookId != null) { + return RegistrationResult( + newlySubscribed = newlySubscribed, + webhookId = existingWebhookId + ) + } + + val webhookId = gitHubAppClient.ensureRepositoryWebhook( + repository = repository, + webhookUrl = webhookUrl(), + webhookSecret = githubWebhookSecret() + ) + + storage.set(webhookIdKey(repository.fullName), webhookId.toString()) + + return RegistrationResult( + newlySubscribed = newlySubscribed, + webhookId = webhookId + ) } fun conversationsForRepository(fullName: String): List =