Skip to content

Commit d3ee3a7

Browse files
cid-3582: Change methods names, and add logs
1 parent 79a28db commit d3ee3a7

File tree

5 files changed

+20
-49
lines changed

5 files changed

+20
-49
lines changed

src/main/kotlin/net/leanix/githubagent/client/GitHubClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ interface GitHubClient {
7070
): GitHubSearchResponse
7171

7272
@GetMapping("/api/v3/repos/{owner}/{repo}/actions/runs/{runId}/artifacts")
73-
fun listRunArtifacts(
73+
fun getRunArtifacts(
7474
@PathVariable("owner") owner: String,
7575
@PathVariable("repo") repo: String,
7676
@PathVariable("runId") runId: Long,

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class WebhookEventService(
5555
val owner = pushEventPayload.repository.owner.name
5656
val defaultBranch = pushEventPayload.repository.defaultBranch
5757

58-
val installationToken = getInstallationToken(pushEventPayload.installation.id)
58+
val installationToken = gitHubAuthenticationService.getInstallationToken(pushEventPayload.installation.id)
5959

6060
if (headCommit != null && pushEventPayload.ref == "refs/heads/$defaultBranch") {
6161
handleManifestFileChanges(
@@ -146,16 +146,6 @@ class WebhookEventService(
146146
}
147147
}
148148

149-
private fun getInstallationToken(installationId: Int): String {
150-
var installationToken = cachingService.get("installationToken:$installationId")?.toString()
151-
if (installationToken == null) {
152-
gitHubAuthenticationService.refreshTokens()
153-
installationToken = cachingService.get("installationToken:$installationId")?.toString()
154-
require(installationToken != null) { "Installation token not found/ expired" }
155-
}
156-
return installationToken
157-
}
158-
159149
@SuppressWarnings("LongParameterList")
160150
private fun handleAddedOrModifiedManifestFile(
161151
repositoryFullName: String,

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,22 @@ class WorkflowRunService(
2323
private val sbomConfig = SbomConfig()
2424

2525
fun consumeWebhookPayload(payload: String) {
26-
val event = parseEvent(payload) ?: return
27-
if (!event.isCompleted()) return
26+
runCatching {
27+
val event = parseEvent(payload) ?: return
28+
if (!event.isCompleted()) return
2829

29-
logger.info("Detected workflow event that successfully completed on default branch")
30-
val installationToken = "Bearer ${gitHubAuthenticationService.getInstallationToken(event.installation.id)}"
30+
logger.info("Detected workflow event that successfully completed")
31+
val installationToken = "Bearer ${gitHubAuthenticationService.getInstallationToken(event.installation.id)}"
3132

32-
val artifacts = getValidArtifacts(event, installationToken)
33-
if (artifacts.isEmpty()) return
34-
logger.info("Found ${artifacts.size} artifact(s).")
35-
processArtifacts(artifacts, event, installationToken)
33+
getValidArtifacts(event, installationToken)
34+
.takeIf { it.isNotEmpty() }
35+
?.let { artifacts ->
36+
logger.info("Found ${artifacts.size} artifact(s).")
37+
fetchAndProcessArtifacts(artifacts, event, installationToken)
38+
} ?: logger.info("No artifacts found for the event")
39+
}.onFailure {
40+
logger.error("Failed to consume workflow webhook", it)
41+
}
3642
}
3743
private fun parseEvent(payload: String): WorkflowRunEventDto? {
3844
return try {
@@ -47,12 +53,12 @@ class WorkflowRunService(
4753
val repo = event.repository.name
4854
val runId = event.workflowRun.id
4955

50-
return gitHubClient.listRunArtifacts(owner, repo, runId, token)
56+
return gitHubClient.getRunArtifacts(owner, repo, runId, token)
5157
.artifacts
5258
.filter { sbomConfig.isFileNameValid(it.name, event.workflowRun.headBranch ?: "") }
5359
}
5460

55-
private fun processArtifacts(
61+
private fun fetchAndProcessArtifacts(
5662
artifacts: List<Artifact>,
5763
event: WorkflowRunEventDto,
5864
installationToken: String

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class WebhookEventServiceTest {
5656
fun setUp() {
5757
val installation = Installation(1, Account("testInstallation"), permissions, events)
5858
every { gitHubAuthenticationService.refreshTokens() } returns Unit
59+
every { gitHubAuthenticationService.getInstallationToken(any()) } returns "token"
5960
every { webSocketService.sendMessage(any(), any()) } returns Unit
6061
every { cachingService.get(any()) } returns "token"
6162
every { gitHubGraphQLService.getManifestFileContent(any(), any(), any(), any()) } returns "content"
@@ -64,31 +65,6 @@ class WebhookEventServiceTest {
6465
every { workflowRunService.consumeWebhookPayload(any()) } returns Unit
6566
}
6667

67-
@Test
68-
fun `should refresh tokens if expired`() {
69-
val payload = """{
70-
"repository": {
71-
"name": "repo",
72-
"full_name": "owner/repo",
73-
"owner": {"name": "owner"},
74-
"default_branch": "main"
75-
},
76-
"head_commit": {
77-
"added": [],
78-
"modified": [],
79-
"removed": []
80-
},
81-
"installation": {"id": 1},
82-
"ref": "refs/heads/main"
83-
}"""
84-
85-
every { cachingService.get("installationToken:1") } returns null andThen "token"
86-
87-
webhookEventService.consumeWebhookEvent("PUSH", payload)
88-
89-
verify(exactly = 1) { gitHubAuthenticationService.refreshTokens() }
90-
}
91-
9268
@Test
9369
fun `should process push event`() {
9470
val payload = """{

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import net.leanix.githubagent.dto.ArtifactsListResponse
1111
import org.junit.jupiter.api.Test
1212
import java.nio.file.Files
1313
import java.nio.file.Paths
14-
import java.util.*
1514

1615
class WorkflowRunServiceTest {
1716

@@ -50,7 +49,7 @@ class WorkflowRunServiceTest {
5049
}
5150
}"""
5251
every { gitHubAuthenticationService.getInstallationToken(any()) } returns "token"
53-
every { gitHubClient.listRunArtifacts(any(), any(), any(), any()) } returns ArtifactsListResponse(
52+
every { gitHubClient.getRunArtifacts(any(), any(), any(), any()) } returns ArtifactsListResponse(
5453
totalCount = 2,
5554
artifacts = listOf(
5655
Artifact(

0 commit comments

Comments
 (0)