Skip to content

Commit

Permalink
Merge pull request #37 from leanix/feature/CID-3144/persist-default-b…
Browse files Browse the repository at this point in the history
…ranch-name

CID-3144 Persist default branch name
  • Loading branch information
geoandri authored Nov 14, 2024
2 parents 8f6acd6 + 57079ca commit c9cb396
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ build/
**/application-local.yaml
**/application-local.yml
**/bom.json
**/bom.xml
**/bom.xml
.env
2 changes: 1 addition & 1 deletion config/detekt/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ complexity:
active: true
excludes: [ '**/test/**' ]
LongParameterList:
active: true
active: false
excludes: [ '**/test/**' ]

naming:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ data class RepositoryDto(
val organizationName: String,
val description: String?,
val url: String,
val defaultBranch: String?,
val archived: Boolean,
val visibility: RepositoryVisibility,
val updatedAt: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class GitHubGraphQLService(
organizationName = it.owner.login,
description = it.description,
url = it.url,
defaultBranch = it.defaultBranchRef?.name,
archived = it.isArchived,
visibility = it.visibility,
updatedAt = it.updatedAt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import net.leanix.githubagent.exceptions.JwtTokenNotFound
import net.leanix.githubagent.exceptions.ManifestFileNotFoundException
import net.leanix.githubagent.shared.MANIFEST_FILE_NAME
import net.leanix.githubagent.shared.fileNameMatchRegex
import net.leanix.githubagent.shared.generateFullPath
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.util.UUID
Expand Down Expand Up @@ -125,7 +126,12 @@ class GitHubScanningService(

private fun fetchManifestFilesAndSend(installation: Installation, repository: RepositoryDto) {
val manifestFiles = fetchManifestFiles(installation, repository.name).getOrThrow().items
val manifestFilesContents = fetchManifestContents(installation, manifestFiles, repository.name).getOrThrow()
val manifestFilesContents = fetchManifestContents(
installation,
manifestFiles,
repository.name,
repository.defaultBranch
).getOrThrow()

webSocketService.sendMessage(
"${cachingService.get("runId")}/manifestFiles",
Expand All @@ -148,7 +154,8 @@ class GitHubScanningService(
private fun fetchManifestContents(
installation: Installation,
items: List<ItemResponse>,
repositoryName: String
repositoryName: String,
defaultBranch: String?
) = runCatching {
val installationToken = cachingService.get("installationToken:${installation.id}").toString()
syncLogService.sendInfoLog("Scanning repository $repositoryName for manifest files")
Expand All @@ -164,7 +171,7 @@ class GitHubScanningService(
numOfManifestFilesFound++
syncLogService.sendInfoLog("Fetched manifest file ${manifestFile.path} from repository $repositoryName")
ManifestFileDTO(
path = fileNameMatchRegex.replace(manifestFile.path, ""),
path = generateFullPath(defaultBranch, fileNameMatchRegex.replace(manifestFile.path, "")),
content = content
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import net.leanix.githubagent.dto.PushEventCommit
import net.leanix.githubagent.dto.PushEventPayload
import net.leanix.githubagent.shared.MANIFEST_FILE_NAME
import net.leanix.githubagent.shared.fileNameMatchRegex
import net.leanix.githubagent.shared.generateFullPath
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

Expand Down Expand Up @@ -38,11 +39,13 @@ class WebhookEventService(
val repositoryFullName = pushEventPayload.repository.fullName
val headCommit = pushEventPayload.headCommit
val owner = pushEventPayload.repository.owner.name
val defaultBranch = pushEventPayload.repository.defaultBranch

val installationToken = getInstallationToken(pushEventPayload.installation.id)

if (pushEventPayload.ref == "refs/heads/${pushEventPayload.repository.defaultBranch}") {
if (pushEventPayload.ref == "refs/heads/$defaultBranch") {
handleManifestFileChanges(
defaultBranch,
headCommit,
repositoryFullName,
owner,
Expand All @@ -53,6 +56,7 @@ class WebhookEventService(
}

private fun handleManifestFileChanges(
defaultBranch: String,
headCommit: PushEventCommit,
repositoryFullName: String,
owner: String,
Expand All @@ -70,7 +74,8 @@ class WebhookEventService(
repositoryName,
installationToken,
filePath,
ManifestFileAction.ADDED
ManifestFileAction.ADDED,
defaultBranch
)
}

Expand All @@ -81,12 +86,13 @@ class WebhookEventService(
repositoryName,
installationToken,
filePath,
ManifestFileAction.MODIFIED
ManifestFileAction.MODIFIED,
defaultBranch
)
}

removedManifestFiles.forEach { filePath ->
handleRemovedManifestFile(repositoryFullName, filePath)
handleRemovedManifestFile(repositoryFullName, filePath, defaultBranch)
}
}

Expand All @@ -107,7 +113,8 @@ class WebhookEventService(
repositoryName: String,
installationToken: String,
manifestFilePath: String,
action: ManifestFileAction
action: ManifestFileAction,
defaultBranch: String?
) {
val location = getManifestFileLocation(manifestFilePath)

Expand All @@ -125,12 +132,16 @@ class WebhookEventService(
repositoryFullName,
action,
fileContent,
fileNameMatchRegex.replace(manifestFilePath, "")
generateFullPath(defaultBranch, fileNameMatchRegex.replace(manifestFilePath, ""))
)
)
}

private fun handleRemovedManifestFile(repositoryFullName: String, manifestFilePath: String) {
private fun handleRemovedManifestFile(
repositoryFullName: String,
manifestFilePath: String,
defaultBranch: String?
) {
val location = getManifestFileLocation(manifestFilePath)
logger.info("Manifest file ${ManifestFileAction.REMOVED} from repository $repositoryFullName under $location")
webSocketService.sendMessage(
Expand All @@ -139,7 +150,7 @@ class WebhookEventService(
repositoryFullName,
ManifestFileAction.REMOVED,
null,
fileNameMatchRegex.replace(manifestFilePath, "")
generateFullPath(defaultBranch, fileNameMatchRegex.replace(manifestFilePath, ""))
)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.leanix.githubagent.shared

fun generateFullPath(defaultBranch: String?, filePath: String?): String {
if (filePath.isNullOrEmpty()) {
return ""
}
val branch = defaultBranch?.takeIf { it.isNotEmpty() } ?: "-"

return "tree/$branch/$filePath"
}
3 changes: 3 additions & 0 deletions src/main/resources/graphql/GetRepositories.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ query GetRepositories($pageCount: Int!, $cursor: String) {
name
description
url
defaultBranchRef{
name
}
isArchived
visibility
updatedAt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class GitHubScanningServiceTest {
organizationName = "testOrg",
description = "A test repository",
url = "https://github.com/testRepo",
defaultBranch = "main",
archived = false,
visibility = RepositoryVisibility.PUBLIC,
updatedAt = "2024-01-01T00:00:00Z",
Expand Down Expand Up @@ -130,6 +131,7 @@ class GitHubScanningServiceTest {
organizationName = "testOrg",
description = "A test repository",
url = "https://github.com/testRepo",
defaultBranch = "main",
archived = false,
visibility = RepositoryVisibility.PUBLIC,
updatedAt = "2024-01-01T00:00:00Z",
Expand Down Expand Up @@ -180,6 +182,7 @@ class GitHubScanningServiceTest {
organizationName = "testOrg",
description = "A test repository",
url = "https://github.com/testRepo",
defaultBranch = "main",
archived = false,
visibility = RepositoryVisibility.PUBLIC,
updatedAt = "2024-01-01T00:00:00Z",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class WebhookEventServiceTest {
"owner/repo",
ManifestFileAction.REMOVED,
null,
"a/b/c"
"tree/main/a/b/c"
)
)
}
Expand Down Expand Up @@ -212,7 +212,7 @@ class WebhookEventServiceTest {
"owner/repo",
ManifestFileAction.ADDED,
"content",
"a/b/c"
"tree/main/a/b/c"
)
)
}
Expand Down Expand Up @@ -245,7 +245,7 @@ class WebhookEventServiceTest {
"owner/repo",
ManifestFileAction.ADDED,
"content",
"custom/path/added1"
"tree/main/custom/path/added1"
)
)
webSocketService.sendMessage(
Expand All @@ -254,7 +254,7 @@ class WebhookEventServiceTest {
"owner/repo",
ManifestFileAction.ADDED,
"content",
"custom/path/added2"
"tree/main/custom/path/added2"
)
)
webSocketService.sendMessage(
Expand All @@ -263,7 +263,7 @@ class WebhookEventServiceTest {
"owner/repo",
ManifestFileAction.MODIFIED,
"content",
"custom/path/modified"
"tree/main/custom/path/modified"
)
)
}
Expand Down Expand Up @@ -296,7 +296,7 @@ class WebhookEventServiceTest {
"owner/repo",
ManifestFileAction.ADDED,
"content",
"custom/path/added2"
"tree/main/custom/path/added2"
)
)
}
Expand All @@ -308,7 +308,7 @@ class WebhookEventServiceTest {
"owner/repo",
ManifestFileAction.ADDED,
"content",
"custom/path/added1/$UNSUPPORTED_MANIFEST_EXTENSION"
"tree/main/custom/path/added1/$UNSUPPORTED_MANIFEST_EXTENSION"
)
)
}
Expand Down

0 comments on commit c9cb396

Please sign in to comment.