generated from leanix/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Feature/cid 3582 process workflow run events #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
henriq-amaral-leanix
merged 6 commits into
main
from
feature/cid-3582-process-workflow-run-events
Mar 14, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
389ad94
cid-3582: Add Workflow run event support
henriq-amaral-leanix 0aa0253
cid-3582: Fix sbom validation and sending
henriq-amaral-leanix 8e78d44
cid-3582: Add tests for sbom ingestion
henriq-amaral-leanix 79a28db
cid-3582: Change api calls
henriq-amaral-leanix d3ee3a7
cid-3582: Change methods names, and add logs
henriq-amaral-leanix 57641a8
Merge branch 'main' into feature/cid-3582-process-workflow-run-events
henriq-amaral-leanix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/net/leanix/githubagent/dto/ArtifactsDto.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package net.leanix.githubagent.dto | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonIgnoreProperties | ||
| import com.fasterxml.jackson.annotation.JsonProperty | ||
|
|
||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| data class ArtifactsListResponse( | ||
| @JsonProperty("total_count") | ||
| val totalCount: Int, | ||
| val artifacts: List<Artifact> | ||
| ) | ||
|
|
||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| data class Artifact( | ||
| val id: Long, | ||
| val name: String, | ||
| val url: String, | ||
| @JsonProperty("archive_download_url") | ||
| val archiveDownloadUrl: String, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package net.leanix.githubagent.dto | ||
|
|
||
| data class SbomConfig( | ||
| val source: GitHubSbomSource = GitHubSbomSource.GITHUB_ARTIFACT, | ||
| val namingConventions: String = "leanix-serviceName-sbom", | ||
| val defaultBranch: String = "main", | ||
| ) { | ||
| fun isFileNameValid(fileName: String, branchName: String): Boolean { | ||
| return fileName == generateExpectedFileName(fileName) && branchName == defaultBranch | ||
| } | ||
| fun extractFactSheetName(fileName: String): String { | ||
| val parts = fileName.split("-") | ||
| return parts.drop(1).dropLast(1).joinToString("-") | ||
| } | ||
|
|
||
| private fun generateExpectedFileName(fileName: String): String { | ||
| val parts = fileName.split("-") | ||
| if (parts.size < 3) return "" | ||
|
|
||
| val serviceName = parts.drop(1).dropLast(1).joinToString("-") | ||
|
|
||
| val conventionParts = namingConventions.split("-") | ||
| if (conventionParts.size < 3) return "" | ||
|
|
||
| val orgPrefix = conventionParts.first() | ||
| val sbomSuffix = conventionParts.last() | ||
|
|
||
| return "$orgPrefix-$serviceName-$sbomSuffix" | ||
| } | ||
| } | ||
|
|
||
| enum class GitHubSbomSource { | ||
| GITHUB_ARTIFACT, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package net.leanix.githubagent.dto | ||
|
|
||
| data class SbomEventDTO( | ||
| val repositoryName: String, | ||
| val factSheetName: String, | ||
| val sbomFileName: String, | ||
| val sbomFileContent: String, | ||
| ) |
53 changes: 53 additions & 0 deletions
53
src/main/kotlin/net/leanix/githubagent/dto/WorkflowRunEventDto.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package net.leanix.githubagent.dto | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonIgnoreProperties | ||
| import com.fasterxml.jackson.annotation.JsonProperty | ||
|
|
||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| data class WorkflowRunEventDto( | ||
| val action: String, | ||
| @JsonProperty("workflow_run") val workflowRun: WorkflowRun, | ||
| val repository: WorkflowRepository, | ||
| val installation: InstallationDTO | ||
| ) { | ||
| fun isCompleted() = ( | ||
| action == "completed" && workflowRun.conclusion == "success" | ||
| ) | ||
| } | ||
|
|
||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| data class WorkflowRun( | ||
| val id: Long, | ||
| @JsonProperty("head_branch") val headBranch: String?, | ||
| @JsonProperty("url") val runUrl: String?, | ||
| @JsonProperty("run_attempt") val runAttempt: Int?, | ||
| @JsonProperty("node_id") val nodeId: String, | ||
| @JsonProperty("head_sha") val headSha: String?, | ||
| val status: String, | ||
| val conclusion: String?, | ||
| @JsonProperty("created_at") val createdAt: String?, | ||
| @JsonProperty("run_started_at") val startedAt: String?, | ||
| val name: String? | ||
| ) | ||
|
|
||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| data class WorkflowRepository( | ||
| val id: Long, | ||
| @JsonProperty("node_id") val nodeId: String, | ||
| val name: String, | ||
| @JsonProperty("full_name") val fullName: String, | ||
| val private: Boolean, | ||
| @JsonProperty("html_url") val htmlUrl: String?, | ||
| @JsonProperty("default_branch") val defaultBranch: String?, | ||
| val owner: RepositoryOwner | ||
| ) | ||
|
|
||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| data class RepositoryOwner( | ||
| val login: String | ||
| ) | ||
|
|
||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| data class InstallationDTO( | ||
| val id: Int | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/main/kotlin/net/leanix/githubagent/services/WorkflowRunService.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package net.leanix.githubagent.services | ||
|
|
||
| import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
| import com.fasterxml.jackson.module.kotlin.readValue | ||
| import net.leanix.githubagent.client.GitHubClient | ||
| import net.leanix.githubagent.dto.Artifact | ||
| import net.leanix.githubagent.dto.SbomConfig | ||
| import net.leanix.githubagent.dto.SbomEventDTO | ||
| import net.leanix.githubagent.dto.WorkflowRunEventDto | ||
| import org.slf4j.LoggerFactory | ||
| import org.springframework.stereotype.Service | ||
| import java.util.* | ||
|
|
||
| @Service | ||
| class WorkflowRunService( | ||
| private val gitHubAuthenticationService: GitHubAuthenticationService, | ||
| private val webSocketService: WebSocketService, | ||
| private val gitHubClient: GitHubClient, | ||
| ) { | ||
|
|
||
| private val logger = LoggerFactory.getLogger(WorkflowRunService::class.java) | ||
| private val objectMapper = jacksonObjectMapper() | ||
| private val sbomConfig = SbomConfig() | ||
|
|
||
| fun consumeWebhookPayload(payload: String) { | ||
| runCatching { | ||
| val event = parseEvent(payload) ?: return | ||
| if (!event.isCompleted()) return | ||
|
|
||
| logger.info("Detected workflow event that successfully completed") | ||
| val installationToken = "Bearer ${gitHubAuthenticationService.getInstallationToken(event.installation.id)}" | ||
|
|
||
| getValidArtifacts(event, installationToken) | ||
| .takeIf { it.isNotEmpty() } | ||
| ?.let { artifacts -> | ||
| logger.info("Found ${artifacts.size} artifact(s).") | ||
| fetchAndProcessArtifacts(artifacts, event, installationToken) | ||
| } ?: logger.info("No artifacts found for the event") | ||
| }.onFailure { | ||
| logger.error("Failed to consume workflow webhook", it) | ||
| } | ||
| } | ||
| private fun parseEvent(payload: String): WorkflowRunEventDto? { | ||
| return try { | ||
| objectMapper.readValue(payload) | ||
| } catch (e: Exception) { | ||
| logger.error("Failed to parse webhook payload", e) | ||
| null | ||
| } | ||
| } | ||
| private fun getValidArtifacts(event: WorkflowRunEventDto, token: String): List<Artifact> { | ||
| val owner = event.repository.owner.login | ||
| val repo = event.repository.name | ||
| val runId = event.workflowRun.id | ||
|
|
||
| return gitHubClient.getRunArtifacts(owner, repo, runId, token) | ||
| .artifacts | ||
| .filter { sbomConfig.isFileNameValid(it.name, event.workflowRun.headBranch ?: "") } | ||
| } | ||
|
|
||
| private fun fetchAndProcessArtifacts( | ||
| artifacts: List<Artifact>, | ||
| event: WorkflowRunEventDto, | ||
| installationToken: String | ||
| ) { | ||
| val owner = event.repository.owner.login | ||
| val repo = event.repository.name | ||
|
|
||
| artifacts.forEach { artifact -> | ||
| logger.info("Processing artifact: ${artifact.name}") | ||
| downloadAndSendArtifact(owner, repo, artifact, installationToken) | ||
| } | ||
| } | ||
|
|
||
| private fun downloadAndSendArtifact(owner: String, repo: String, artifact: Artifact, token: String) = runCatching { | ||
| gitHubClient.downloadArtifact(owner, repo, artifact.id, token).body()?.use { body -> | ||
| val sbomContent = Base64.getEncoder().encodeToString(body.asInputStream().readAllBytes()) | ||
| sendSbomEvent(repo, artifact.name, sbomContent) | ||
| } ?: logger.error("Failed to download artifact: ${artifact.name}") | ||
mohamedlajmileanix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }.onFailure { | ||
| logger.error("Error processing artifact: ${artifact.name}", it) | ||
| } | ||
|
|
||
| private fun sendSbomEvent(repo: String, artifactName: String, sbomContent: String) { | ||
| logger.info("Sending sbom file: $repo - $artifactName") | ||
| webSocketService.sendMessage( | ||
| "/events/sbom", | ||
| SbomEventDTO( | ||
| repositoryName = repo, | ||
| factSheetName = sbomConfig.extractFactSheetName(artifactName), | ||
| sbomFileName = artifactName, | ||
| sbomFileContent = sbomContent | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.