Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/main/kotlin/com/wire/github/EventsHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
43 changes: 33 additions & 10 deletions src/main/kotlin/com/wire/github/github/GitHubWebhookManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<QualifiedId> =
Expand Down