Skip to content

Commit

Permalink
CID-2874: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedlajmileanix committed Aug 13, 2024
1 parent a06173e commit 2d4d205
Showing 1 changed file with 39 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ class WebhookEventService(
val headCommit = pushEventPayload.headCommit
val owner = pushEventPayload.repository.owner.name

var installationToken = cachingService.get("installationToken:${pushEventPayload.installation.id}")?.toString()
if (installationToken == null) {
gitHubAuthenticationService.refreshTokens()
installationToken = cachingService.get("installationToken:${pushEventPayload.installation.id}")?.toString()
require(installationToken != null) { "Installation token not found/ expired" }
}
val installationToken = getInstallationToken(pushEventPayload.installation.id)

if (pushEventPayload.ref == "refs/heads/${pushEventPayload.repository.defaultBranch}") {
handleManifestFileChanges(
Expand All @@ -72,7 +67,6 @@ class WebhookEventService(
val isYAMLFileUpdated = isManifestFileUpdated(headCommit, yamlFileName)
val isYMLFileUpdated = isManifestFileUpdated(headCommit, ymlFileName)

// ignore updates on YML file if YAML file exists
if (!isYAMLFileUpdated && isYMLFileUpdated) {
val yamlFileContent = gitHubGraphQLService.getManifestFileContent(
owner,
Expand All @@ -83,29 +77,34 @@ class WebhookEventService(
if (yamlFileContent != null) return
}

val manifestFilePath = if (isYAMLFileUpdated) {
yamlFileName
} else if (isYMLFileUpdated) {
ymlFileName
} else {
return
val manifestFilePath = determineManifestFilePath(isYAMLFileUpdated, isYMLFileUpdated, yamlFileName, ymlFileName)
manifestFilePath?.let {
when (it) {
in headCommit.added, in headCommit.modified -> {
handleAddedOrModifiedManifestFile(
headCommit,
repositoryFullName,
owner,
repositoryName,
installationToken,
it
)
}
in headCommit.removed -> {
handleRemovedManifestFile(repositoryFullName)
}
}
}
}

when (manifestFilePath) {
in headCommit.added, in headCommit.modified -> {
handleAddedOrModifiedManifestFile(
headCommit,
repositoryFullName,
owner,
repositoryName,
installationToken,
manifestFilePath
)
}
in headCommit.removed -> {
handleRemovedManifestFile(repositoryFullName)
}
private fun getInstallationToken(installationId: Int): String {
var installationToken = cachingService.get("installationToken:$installationId")?.toString()
if (installationToken == null) {
gitHubAuthenticationService.refreshTokens()
installationToken = cachingService.get("installationToken:$installationId")?.toString()
require(installationToken != null) { "Installation token not found/ expired" }
}
return installationToken
}

private fun isManifestFileUpdated(headCommit: PushEventCommit, fileName: String): Boolean {
Expand All @@ -114,6 +113,19 @@ class WebhookEventService(
headCommit.removed.any { it == fileName }
}

private fun determineManifestFilePath(
isYAMLFileUpdated: Boolean,
isYMLFileUpdated: Boolean,
yamlFileName: String,
ymlFileName: String
): String? {
return when {
isYAMLFileUpdated -> yamlFileName
isYMLFileUpdated -> ymlFileName
else -> null
}
}

@SuppressWarnings("LongParameterList")
private fun handleAddedOrModifiedManifestFile(
headCommit: PushEventCommit,
Expand Down

0 comments on commit 2d4d205

Please sign in to comment.