Skip to content

Commit

Permalink
fix(artifacts): Add support for github enterprise
Browse files Browse the repository at this point in the history
Signed-off-by: Jesse Sanford <[email protected]>
  • Loading branch information
jessesanford authored and nimakaviani committed Apr 6, 2022
1 parent 1bf35f0 commit 69f70a9
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ class ArtifactVersionLinks(private val scmInfo: ScmInfo, private val cacheFactor
//Generating a SCM compare link between source (new version) and target (old version) versions (the order matter!)
private fun generateCompareLink(newerGitMetadata: GitMetadata?, olderGitMetadata: GitMetadata?): String? {
val commitLink = newerGitMetadata?.commitInfo?.link ?: return null
val normScmType = getNormalizedScmType(commitLink)
val baseScmUrl = getScmBaseLink(commitLink)
return if (baseScmUrl != null && olderGitMetadata != null && !(olderGitMetadata.commitInfo?.sha.isNullOrEmpty())) {
return if (normScmType != null && baseScmUrl != null && olderGitMetadata != null && !(olderGitMetadata.commitInfo?.sha.isNullOrEmpty())) {
when {
"stash" in commitLink -> {
"stash" in normScmType -> {
"$baseScmUrl/projects/${newerGitMetadata.project}/repos/${newerGitMetadata.repo?.name}/compare/commits?" +
"targetBranch=${olderGitMetadata.commitInfo?.sha}&sourceBranch=${newerGitMetadata.commitInfo?.sha}"
}
"github" in commitLink -> {
"github" in normScmType -> {
"$baseScmUrl/${newerGitMetadata.project}/${newerGitMetadata.repo?.name}/compare/" +
"${olderGitMetadata.commitInfo?.sha}...${newerGitMetadata.commitInfo?.sha}"
}
Expand All @@ -51,22 +52,46 @@ class ArtifactVersionLinks(private val scmInfo: ScmInfo, private val cacheFactor
} else null
}

fun getNormalizedScmType(commitLink: String): String? {
val scmType = getScmType(commitLink)
return scmType?.toLowerCase()
}

fun getScmType(commitLink: String): String? {
val commitURL = URL(commitLink)

val scmBaseURLs = runBlocking {
val cachedValue = cache[cacheName].get()
cachedValue ?: scmInfo.getScmInfo()
}

val base = scmBaseURLs.filter { (_,baseUrl) ->
commitURL.host == URL(baseUrl).host
}

return base.keys.toList().firstOrNull()
}

//Calling igor to fetch all base urls by SCM type, and returning the right one based on current commit link
fun getScmBaseLink(commitLink: String): String? {

val normScmType = getNormalizedScmType(commitLink)
val scmType = getScmType(commitLink)
val scmBaseURLs = runBlocking {
val cachedValue = cache[cacheName].get()
cachedValue ?: scmInfo.getScmInfo()
}
return when {
"stash" in commitLink ->
scmBaseURLs["stash"]
"github" in commitLink -> {
val url = URL(scmBaseURLs["gitHub"])
"${url.protocol}://${url.host}"

return if (normScmType != null) {
when {
"stash" in normScmType ->
scmBaseURLs["stash"]
"github" in normScmType -> {
val url = URL(scmBaseURLs[scmType])
"${url.protocol}://${url.host}"
}
else ->
throw UnsupportedScmType(message = "Stash and GitHub are currently the only supported SCM types.")
}
else ->
throw UnsupportedScmType(message = "Stash and GitHub are currently the only supported SCM types.")
}
} else null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,171 @@ class ComparableLinksTests : JUnit5Minutests {
}
}

context("github enterprise") {
before {
every { repository.artifactVersions(releaseArtifact, limit) } returns versions.toArtifactVersions(
releaseArtifact,
"https://git.foo.com"
)
}

test("compare links for previous-->current are generated as expected, in the correct env") {
// current is compared against what it replaced
val summaries = applicationService.getArtifactSummariesFor(application1, limit)
expectThat(summaries.first())
.withVersionInEnvironment(version2, "test") {
state.isEqualTo(CURRENT.name.toLowerCase())
compareLink
.isEqualTo("https://git.foo.com/spkr/keel/compare/${version1}...${version2}")
}
.withVersionInEnvironment(version1, "staging") {
state.isEqualTo(CURRENT.name.toLowerCase())
compareLink.isEqualTo("https://git.foo.com/spkr/keel/compare/${version0}...${version1}")
}
}

test("compare links for current --> deploying are generated as expected, in the correct env") {
// deploying is compared against the current version
val summaries = applicationService.getArtifactSummariesFor(application1, limit)
expectThat(summaries.first())
.withVersionInEnvironment(version3, "test") {
state.isEqualTo(DEPLOYING.name.toLowerCase())
compareLink
.isEqualTo("https://git.foo.com/spkr/keel/compare/${version2}...${version3}")
}
}

test("compare links for previous --> current are generated as expected, in the correct env") {
// previous is compared to what replaced it, which we know.
val summaries = applicationService.getArtifactSummariesFor(application1, limit)
expectThat(summaries.first())
.withVersionInEnvironment(version0, "staging") {
state.isEqualTo(PREVIOUS.name.toLowerCase())
compareLink.isEqualTo("https://git.foo.com/spkr/keel/compare/${version0}...${version1}")
}
}

test("compare links for pending --> current are generated as expected, in the correct env") {
// pending is compared to current
val summaries = applicationService.getArtifactSummariesFor(application1, limit)
expectThat(summaries.first())
.withVersionInEnvironment(version2, "staging") {
state.isEqualTo(PENDING.name.toLowerCase())
compareLink.isEqualTo("https://git.foo.com/spkr/keel/compare/${version1}...${version2}")
}
}

context("pinned") {
before {
every {
repository.getVersionInfoInEnvironment(any(), "test", any())
} returns listOf(
StatusInfoForArtifactInEnvironment(version0, CURRENT, null, clock.instant().minus(Duration.ofHours(2))),
)

every {
repository.getVersionInfoInEnvironment(any(), "staging", any())
} returns listOf(
StatusInfoForArtifactInEnvironment(
version0,
PREVIOUS,
version1,
clock.instant().minus(Duration.ofHours(4))
),
StatusInfoForArtifactInEnvironment(version1, CURRENT, null, clock.instant()),
)

// for statuses other than PENDING, we go look for the artifact summary in environment
every { repository.getArtifactSummariesInEnvironment(
singleArtifactDeliveryConfig,
"test",
any(),
versions
) } returns testSummaryInEnv.map { versionSummary ->
if (versionSummary.version == version0) {
// version 0 is pinned
ArtifactSummaryInEnvironment("test", version0, "previous", replacedBy = version1, pinned = ActionMetadata(clock.instant(), "me", "because I said so"))
} else {
versionSummary
}
}

every { repository.getArtifactSummariesInEnvironment(
singleArtifactDeliveryConfig,
"staging",
any(),
versions
) } returns stagingSummaryInEnv.map { versionSummary ->
if (versionSummary.version == version3) {
// version 3 is pinned
ArtifactSummaryInEnvironment("staging", version3, "pending", pinned = ActionMetadata(clock.instant(), "me", "because I said so"))
} else {
versionSummary
}
}
}

test("get the correct compare link when pinning forward (current)") {
// pinning to version 3 here
val summaries = applicationService.getArtifactSummariesFor(application1, limit)
expectThat(summaries.first())
.withVersionInEnvironment(version1, "staging") {
compareLink.isEqualTo("https://git.foo.com/spkr/keel/compare/${version1}...${version3}")
}
}

test("get the correct compare link when pinning forward (pending)") {
val summaries = applicationService.getArtifactSummariesFor(application1, limit)
expectThat(summaries.first())
.withVersionInEnvironment(version4, "test") {
compareLink.isEqualTo("https://git.foo.com/spkr/keel/compare/${version0}...${version4}")
}
}

test("get the correct compare link when pinning forward (prev)") {
val summaries = applicationService.getArtifactSummariesFor(application1, limit)
expectThat(summaries.first())
.withVersionInEnvironment(version1, "test") {
compareLink.isEqualTo("https://git.foo.com/spkr/keel/compare/${version1}...${version2}")
}
}

test("get the correct compare link when pinning backwards") {
val summaries = applicationService.getArtifactSummariesFor(application1, limit)
expectThat(summaries.first())
.withVersionInEnvironment(version2, "test") {
compareLink.isEqualTo("https://git.foo.com/spkr/keel/compare/${version0}...${version2}")
}
}

context("pin version == current version") {
before {
every { repository.getArtifactSummariesInEnvironment(
singleArtifactDeliveryConfig,
"staging",
any(),
versions
) } returns stagingSummaryInEnv.map { versionSummary ->
if (versionSummary.version == version1) {
// version 1 is pinned
ArtifactSummaryInEnvironment("staging", version1, "current", pinned = ActionMetadata(clock.instant(), "me", "because I said so"))
} else {
versionSummary
}
}
}

test("generate the right compare link") {
val summaries = applicationService.getArtifactSummariesFor(application1, limit)
expectThat(summaries.first())
.withVersionInEnvironment(version1, "staging") {
compareLink.isEqualTo("https://git.foo.com/spkr/keel/compare/${version0}...${version1}")
}
}
}
}
}

context("github") {
before {
every { repository.artifactVersions(releaseArtifact, limit) } returns versions.toArtifactVersions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ internal class ManualJudgementNotifierTests : JUnit5Minutests {
project = "myproj",
branch = "master",
repo = Repo("myapp"),
commitInfo = Commit(message = "A test commit #2", link = "stash", sha = "v2.0.0")
commitInfo = Commit(message = "A test commit #2", link = "https://stash", sha = "v2.0.0")
)
)
}
Expand Down Expand Up @@ -312,7 +312,7 @@ internal class ManualJudgementNotifierTests : JUnit5Minutests {
project = "myproj",
branch = "master",
repo = Repo("myapp"),
commitInfo = Commit(message = "A test commit", link = "stash", sha = "v1.0.0")
commitInfo = Commit(message = "A test commit", link = "https://stash", sha = "v1.0.0")
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ class GitDataGeneratorTests : JUnit5Minutests {
project = "spkr",
repo = Repo("keel", null),
branch = "main",
pullRequest = PullRequest("1", "stash/pr/1"),
pullRequest = PullRequest("1", "https://stash/pr/1"),
commitInfo = Commit(
link = "stash",
link = "https://stash",
sha = "676fea96a33cbc774685ff8b511092d9a3809f90",
message = null
)
Expand All @@ -87,8 +87,8 @@ class GitDataGeneratorTests : JUnit5Minutests {
expect {
that(text.toString().contains("text=<https://stash/projects/spkr/repos/keel|spkr/keel>")).isTrue()
that(text.toString().contains("<https://stash/projects/spkr/repos/keel/branches|main>")).isTrue()
that(text.toString().contains("<stash/pr/1|PR#1>")).isTrue()
that(text.toString().contains("<stash|676fea9>")).isTrue()
that(text.toString().contains("<https://stash/pr/1|PR#1>")).isTrue()
that(text.toString().contains("<https://stash|676fea9>")).isTrue()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import java.util.function.Function

val scmData = mapOf(
"stash" to "https://stash",
"gitHub" to "https://github.com")
"gitHub" to "https://github.com",
"gitHubEnterprise" to "https://git.foo.com")

fun mockScmInfo(): ScmInfo {
return mockk() {
Expand Down

0 comments on commit 69f70a9

Please sign in to comment.