Skip to content

Commit f2b4b82

Browse files
Merge pull request #121 from leanix/feature/CID-3913/ignore-webhook-events-if-a-scan-is-running
CID-3913: Ignore webhook events if a scan is running
2 parents 912c834 + 1169bed commit f2b4b82

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/main/kotlin/net/leanix/githubagent/services/GitHubWebhookService.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@ import org.springframework.stereotype.Service
1313
@Service
1414
class GitHubWebhookService(
1515
private val webhookEventService: WebhookEventService,
16-
private val gitHubEnterpriseProperties: GitHubEnterpriseProperties
16+
private val gitHubEnterpriseProperties: GitHubEnterpriseProperties,
17+
private val cachingService: CachingService,
1718
) {
1819

1920
private val logger = LoggerFactory.getLogger(GitHubWebhookService::class.java)
2021

2122
@Async
2223
fun handleWebhookEvent(eventType: String, host: String, signature256: String?, payload: String) {
24+
val runId = cachingService.get("runId")
25+
if (runId != null && eventType.uppercase() != "INSTALLATION") {
26+
logger.info("Received a webhook event while a full sync is in progress, ignoring the event.")
27+
return
28+
}
2329
if (SUPPORTED_EVENT_TYPES.contains(eventType.uppercase())) {
2430
if (!gitHubEnterpriseProperties.baseUrl.contains(host)) {
2531
logger.error("Received a webhook event from an unknown host: $host")

src/test/kotlin/net/leanix/githubagent/services/GitHubWebhookServiceTest.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ class GitHubWebhookServiceTest {
1414

1515
private val webhookEventService = mockk<WebhookEventService>()
1616
private val gitHubEnterpriseProperties = mockk<GitHubEnterpriseProperties>()
17-
private val gitHubWebhookService = GitHubWebhookService(webhookEventService, gitHubEnterpriseProperties)
17+
private val cachingService = mockk<CachingService>()
18+
private val gitHubWebhookService = GitHubWebhookService(
19+
webhookEventService,
20+
gitHubEnterpriseProperties,
21+
cachingService,
22+
)
1823

1924
@BeforeEach
2025
fun setUp() {
26+
every { cachingService.get("runId") } returns null
2127
}
2228

2329
@Test
@@ -69,4 +75,16 @@ class GitHubWebhookServiceTest {
6975

7076
verify { webhookEventService.consumeWebhookEvent("PUSH", "{}") }
7177
}
78+
79+
@Test
80+
fun `should ignore event when runId is present and event type is not INSTALLATION`() {
81+
every { gitHubEnterpriseProperties.baseUrl } returns "host"
82+
every { gitHubEnterpriseProperties.webhookSecret } returns ""
83+
every { webhookEventService.consumeWebhookEvent(any(), any()) } returns Unit
84+
every { cachingService.get("runId") } returns "someRunId"
85+
86+
gitHubWebhookService.handleWebhookEvent("PUSH", "host", null, "{}")
87+
88+
verify(exactly = 0) { webhookEventService.consumeWebhookEvent(any(), any()) }
89+
}
7290
}

0 commit comments

Comments
 (0)