From 4734004bfa2855c5d1629344ed7715761a8d6cb5 Mon Sep 17 00:00:00 2001 From: mohamedlajmileanix Date: Thu, 19 Dec 2024 08:00:57 +0100 Subject: [PATCH] CID-3362: ignore push events without a head commit --- .../githubagent/dto/PushEventPayload.kt | 2 +- .../services/WebhookEventService.kt | 20 ++++++++++--------- .../services/WebhookEventServiceTest.kt | 20 +++++++++++++++++++ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/net/leanix/githubagent/dto/PushEventPayload.kt b/src/main/kotlin/net/leanix/githubagent/dto/PushEventPayload.kt index 1710784..0d51731 100644 --- a/src/main/kotlin/net/leanix/githubagent/dto/PushEventPayload.kt +++ b/src/main/kotlin/net/leanix/githubagent/dto/PushEventPayload.kt @@ -9,7 +9,7 @@ data class PushEventPayload( val repository: PushEventRepository, val installation: PushEventInstallation, @JsonProperty("head_commit") - val headCommit: PushEventCommit + val headCommit: PushEventCommit? ) @JsonIgnoreProperties(ignoreUnknown = true) diff --git a/src/main/kotlin/net/leanix/githubagent/services/WebhookEventService.kt b/src/main/kotlin/net/leanix/githubagent/services/WebhookEventService.kt index 60f1377..a3a56dc 100644 --- a/src/main/kotlin/net/leanix/githubagent/services/WebhookEventService.kt +++ b/src/main/kotlin/net/leanix/githubagent/services/WebhookEventService.kt @@ -51,15 +51,17 @@ class WebhookEventService( val installationToken = getInstallationToken(pushEventPayload.installation.id) - if (pushEventPayload.ref == "refs/heads/$defaultBranch") { - handleManifestFileChanges( - defaultBranch, - headCommit, - repositoryFullName, - owner, - repositoryName, - installationToken - ) + headCommit?.let { + if (pushEventPayload.ref == "refs/heads/$defaultBranch") { + handleManifestFileChanges( + defaultBranch, + headCommit, + repositoryFullName, + owner, + repositoryName, + installationToken + ) + } } } diff --git a/src/test/kotlin/net/leanix/githubagent/services/WebhookEventServiceTest.kt b/src/test/kotlin/net/leanix/githubagent/services/WebhookEventServiceTest.kt index 39672c7..73e35d7 100644 --- a/src/test/kotlin/net/leanix/githubagent/services/WebhookEventServiceTest.kt +++ b/src/test/kotlin/net/leanix/githubagent/services/WebhookEventServiceTest.kt @@ -338,4 +338,24 @@ class WebhookEventServiceTest { verify(exactly = 6) { cachingService.get("runId") } } + + @Test + fun `should ignore push events without a head commit`() { + val payload = """{ + "repository": { + "name": "repo", + "full_name": "owner/repo", + "owner": {"name": "owner"}, + "default_branch": "main" + }, + "installation": {"id": 1}, + "ref": "refs/heads/main" + }""" + + webhookEventService.consumeWebhookEvent("PUSH", payload) + + verify(exactly = 0) { + webSocketService.sendMessage(any(), any()) + } + } }