Skip to content

Commit c74f7f7

Browse files
authored
Merge pull request #1563 from eugeniyk/support-on-prem-github-release-notes
Support on prem GitHub release notes / changelog
2 parents 6525ca6 + 68393ed commit c74f7f7

File tree

4 files changed

+167
-39
lines changed

4 files changed

+167
-39
lines changed

modules/core/src/main/scala/org/scalasteward/core/vcs/VCSExtraAlg.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package org.scalasteward.core.vcs
1919
import cats.Monad
2020
import cats.syntax.all._
2121
import org.http4s.Uri
22+
import org.scalasteward.core.application.Config
2223
import org.scalasteward.core.data.{ReleaseRelatedUrl, Update}
2324
import org.scalasteward.core.util.HttpExistenceClient
2425
import org.scalasteward.core.vcs
@@ -30,13 +31,13 @@ trait VCSExtraAlg[F[_]] {
3031
object VCSExtraAlg {
3132
def create[F[_]](implicit
3233
existenceClient: HttpExistenceClient[F],
34+
config: Config,
3335
F: Monad[F]
3436
): VCSExtraAlg[F] =
3537
new VCSExtraAlg[F] {
3638
override def getReleaseRelatedUrls(repoUrl: Uri, update: Update): F[List[ReleaseRelatedUrl]] =
3739
vcs
38-
.possibleReleaseRelatedUrls(repoUrl, update)
40+
.possibleReleaseRelatedUrls(config.vcsType, config.vcsApiHost, repoUrl, update)
3941
.filterA(releaseRelatedUrl => existenceClient.exists(releaseRelatedUrl.url))
40-
4142
}
4243
}

modules/core/src/main/scala/org/scalasteward/core/vcs/package.scala

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,50 +73,88 @@ package object vcs {
7373
possibleFilenames(baseNames)
7474
}
7575

76-
def possibleCompareUrls(repoUrl: Uri, update: Update): List[VersionDiff] = {
76+
private[this] def extractRepoVCSType(
77+
vcsType: SupportedVCS,
78+
vcsUri: Uri,
79+
repoUrl: Uri
80+
): Option[SupportedVCS] = {
7781
val host = repoUrl.host.map(_.value)
82+
if (vcsUri.host.map(_.value).contains(host.getOrElse("")))
83+
Option(vcsType)
84+
else
85+
host
86+
.collect {
87+
case "github.com" => GitHub
88+
case "gitlab.com" => Gitlab
89+
}
90+
.orElse {
91+
if (host.contains_("bitbucket.org"))
92+
Some(Bitbucket)
93+
else None
94+
}
95+
}
96+
97+
def possibleCompareUrls(
98+
vcsType: SupportedVCS,
99+
vcsUri: Uri,
100+
repoUrl: Uri,
101+
update: Update
102+
): List[VersionDiff] = {
78103
val from = update.currentVersion
79104
val to = update.nextVersion
80105

81-
if (host.exists(Set("github.com", "gitlab.com")))
82-
possibleTags(from).zip(possibleTags(to)).map { case (from1, to1) =>
83-
VersionDiff(repoUrl / "compare" / s"$from1...$to1")
84-
}
85-
else if (host.contains_("bitbucket.org"))
86-
possibleTags(from).zip(possibleTags(to)).map { case (from1, to1) =>
87-
VersionDiff((repoUrl / "compare" / s"$to1..$from1").withFragment("diff"))
106+
extractRepoVCSType(vcsType, vcsUri, repoUrl)
107+
.map {
108+
case GitHub | Gitlab =>
109+
possibleTags(from).zip(possibleTags(to)).map { case (from1, to1) =>
110+
VersionDiff(repoUrl / "compare" / s"$from1...$to1")
111+
}
112+
case Bitbucket | BitbucketServer =>
113+
possibleTags(from).zip(possibleTags(to)).map { case (from1, to1) =>
114+
VersionDiff((repoUrl / "compare" / s"$to1..$from1").withFragment("diff"))
115+
}
88116
}
89-
else
90-
List.empty
117+
.getOrElse(List.empty)
91118
}
92119

93-
def possibleReleaseRelatedUrls(repoUrl: Uri, update: Update): List[ReleaseRelatedUrl] = {
94-
val host = repoUrl.host.map(_.value)
95-
val github =
96-
if (host.contains_("github.com"))
120+
def possibleReleaseRelatedUrls(
121+
vcsType: SupportedVCS,
122+
vcsUri: Uri,
123+
repoUrl: Uri,
124+
update: Update
125+
): List[ReleaseRelatedUrl] = {
126+
val repoVCSType = extractRepoVCSType(vcsType, vcsUri, repoUrl)
127+
128+
val github = repoVCSType
129+
.collect { case GitHub =>
97130
possibleTags(update.nextVersion).map(tag =>
98131
ReleaseRelatedUrl.GitHubReleaseNotes(repoUrl / "releases" / "tag" / tag)
99132
)
100-
else
101-
List.empty
133+
}
134+
.getOrElse(List.empty)
135+
102136
def files(fileNames: List[String]): List[Uri] = {
103-
val maybeSegments =
104-
if (host.exists(Set("github.com", "gitlab.com")))
105-
Some(List("blob", "master"))
106-
else if (host.contains_("bitbucket.org"))
107-
Some(List("master"))
108-
else
109-
None
137+
val maybeSegments = repoVCSType.map {
138+
case SupportedVCS.GitHub | SupportedVCS.Gitlab => List("blob", "master")
139+
case SupportedVCS.Bitbucket | SupportedVCS.BitbucketServer => List("master")
140+
}
141+
110142
maybeSegments.toList.flatMap { segments =>
111143
val base = segments.foldLeft(repoUrl)(_ / _)
112144
fileNames.map(name => base / name)
113145
}
114146
}
147+
115148
val customChangelog = files(possibleChangelogFilenames).map(ReleaseRelatedUrl.CustomChangelog)
116149
val customReleaseNotes =
117150
files(possibleReleaseNotesFilenames).map(ReleaseRelatedUrl.CustomReleaseNotes)
118151

119-
github ++ customReleaseNotes ++ customChangelog ++ possibleCompareUrls(repoUrl, update)
152+
github ++ customReleaseNotes ++ customChangelog ++ possibleCompareUrls(
153+
vcsType,
154+
vcsUri,
155+
repoUrl,
156+
update
157+
)
120158
}
121159

122160
private def possibleFilenames(baseNames: List[String]): List[String] = {

modules/core/src/test/scala/org/scalasteward/core/vcs/VCSExtraAlgTest.scala

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import org.http4s.dsl.io._
77
import org.http4s.implicits._
88
import org.scalasteward.core.TestInstances.ioLogger
99
import org.scalasteward.core.TestSyntax._
10+
import org.scalasteward.core.application.{Config, SupportedVCS}
1011
import org.scalasteward.core.data.{ReleaseRelatedUrl, Update}
11-
import org.scalasteward.core.mock.MockContext.config
12+
import org.scalasteward.core.mock.MockContext
1213
import org.scalasteward.core.util.{HttpExistenceClient, Nel}
1314
import org.scalatest.funsuite.AnyFunSuite
1415
import org.scalatest.matchers.should.Matchers
@@ -21,26 +22,52 @@ class VCSExtraAlgTest extends AnyFunSuite with Matchers {
2122
case _ => NotFound()
2223
}
2324

25+
implicit val cfg = MockContext.config
2426
implicit val client = Client.fromHttpApp[IO](routes.orNotFound)
2527
implicit val httpExistenceClient =
2628
HttpExistenceClient.create[IO].allocated.map(_._1).unsafeRunSync()
2729

28-
val vcsExtraAlg = VCSExtraAlg.create[IO]
2930
val updateFoo = Update.Single("com.example" % "foo" % "0.1.0", Nel.of("0.2.0"))
3031
val updateBar = Update.Single("com.example" % "bar" % "0.1.0", Nel.of("0.2.0"))
3132
val updateBuz = Update.Single("com.example" % "buz" % "0.1.0", Nel.of("0.2.0"))
3233

33-
test("getBranchCompareUrl") {
34+
test("getBranchCompareUrl: std vsc") {
35+
val vcsExtraAlg = VCSExtraAlg.create[IO]
36+
3437
vcsExtraAlg
3538
.getReleaseRelatedUrls(uri"https://github.com/foo/foo", updateFoo)
3639
.unsafeRunSync() shouldBe List.empty
40+
3741
vcsExtraAlg
3842
.getReleaseRelatedUrls(uri"https://github.com/foo/bar", updateBar)
3943
.unsafeRunSync() shouldBe List(
4044
ReleaseRelatedUrl.VersionDiff(uri"https://github.com/foo/bar/compare/v0.1.0...v0.2.0")
4145
)
46+
4247
vcsExtraAlg
4348
.getReleaseRelatedUrls(uri"https://github.com/foo/buz", updateBuz)
4449
.unsafeRunSync() shouldBe List.empty
4550
}
51+
52+
test("getBranchCompareUrl: github on prem") {
53+
implicit val cfg: Config = MockContext.config.copy(
54+
vcsType = SupportedVCS.GitHub,
55+
vcsApiHost = uri"https://github.on-prem.com/"
56+
)
57+
val githubOnPremVcsExtraAlg = VCSExtraAlg.create[IO]
58+
59+
githubOnPremVcsExtraAlg
60+
.getReleaseRelatedUrls(uri"https://github.on-prem.com/foo/foo", updateFoo)
61+
.unsafeRunSync() shouldBe List.empty
62+
63+
githubOnPremVcsExtraAlg
64+
.getReleaseRelatedUrls(uri"https://github.on-prem.com/foo/bar", updateBar)
65+
.unsafeRunSync() shouldBe List(
66+
ReleaseRelatedUrl.VersionDiff(uri"https://github.on-prem.com/foo/bar/compare/v0.1.0...v0.2.0")
67+
)
68+
69+
githubOnPremVcsExtraAlg
70+
.getReleaseRelatedUrls(uri"https://github.on-prem.com/foo/buz", updateFoo)
71+
.unsafeRunSync() shouldBe List.empty
72+
}
4673
}

modules/core/src/test/scala/org/scalasteward/core/vcs/VCSPackageTest.scala

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.scalasteward.core.vcs
22

33
import org.http4s.syntax.literals._
44
import org.scalasteward.core.TestSyntax._
5+
import org.scalasteward.core.application.SupportedVCS
56
import org.scalasteward.core.application.SupportedVCS.{GitHub, Gitlab}
67
import org.scalasteward.core.data.Update
78
import org.scalasteward.core.util.Nel
@@ -25,38 +26,63 @@ class VCSPackageTest extends AnyFunSuite with Matchers {
2526
}
2627

2728
test("possibleCompareUrls") {
28-
possibleCompareUrls(uri"https://github.com/foo/bar", update)
29+
val onPremVCS = "https://github.onprem.io/"
30+
val onPremVCSUri = uri"https://github.onprem.io/"
31+
32+
possibleCompareUrls(SupportedVCS.GitHub, onPremVCSUri, uri"https://github.com/foo/bar", update)
2933
.map(_.url.renderString) shouldBe List(
3034
"https://github.com/foo/bar/compare/v1.2.0...v1.2.3",
3135
"https://github.com/foo/bar/compare/1.2.0...1.2.3",
3236
"https://github.com/foo/bar/compare/release-1.2.0...release-1.2.3"
3337
)
3438
// should canonicalize (drop last slash)
35-
possibleCompareUrls(uri"https://github.com/foo/bar/", update)
39+
possibleCompareUrls(SupportedVCS.GitHub, onPremVCSUri, uri"https://github.com/foo/bar/", update)
3640
.map(_.url.renderString) shouldBe List(
3741
"https://github.com/foo/bar/compare/v1.2.0...v1.2.3",
3842
"https://github.com/foo/bar/compare/1.2.0...1.2.3",
3943
"https://github.com/foo/bar/compare/release-1.2.0...release-1.2.3"
4044
)
4145

42-
possibleCompareUrls(uri"https://gitlab.com/foo/bar", update)
46+
possibleCompareUrls(SupportedVCS.GitHub, onPremVCSUri, uri"https://gitlab.com/foo/bar", update)
4347
.map(_.url.renderString) shouldBe List(
4448
"https://gitlab.com/foo/bar/compare/v1.2.0...v1.2.3",
4549
"https://gitlab.com/foo/bar/compare/1.2.0...1.2.3",
4650
"https://gitlab.com/foo/bar/compare/release-1.2.0...release-1.2.3"
4751
)
48-
possibleCompareUrls(uri"https://bitbucket.org/foo/bar", update)
52+
possibleCompareUrls(
53+
SupportedVCS.GitHub,
54+
onPremVCSUri,
55+
uri"https://bitbucket.org/foo/bar",
56+
update
57+
)
4958
.map(_.url.renderString) shouldBe List(
5059
"https://bitbucket.org/foo/bar/compare/v1.2.3..v1.2.0#diff",
5160
"https://bitbucket.org/foo/bar/compare/1.2.3..1.2.0#diff",
5261
"https://bitbucket.org/foo/bar/compare/release-1.2.3..release-1.2.0#diff"
5362
)
5463

55-
possibleCompareUrls(uri"https://scalacenter.github.io/scalafix/", update) shouldBe List()
64+
possibleCompareUrls(
65+
SupportedVCS.GitHub,
66+
onPremVCSUri,
67+
uri"https://scalacenter.github.io/scalafix/",
68+
update
69+
) shouldBe List.empty
70+
71+
possibleCompareUrls(SupportedVCS.GitHub, onPremVCSUri, onPremVCSUri.addPath("/foo/bar"), update)
72+
.map(_.url.renderString) shouldBe List(
73+
s"${onPremVCS}foo/bar/compare/v1.2.0...v1.2.3",
74+
s"${onPremVCS}foo/bar/compare/1.2.0...1.2.3",
75+
s"${onPremVCS}foo/bar/compare/release-1.2.0...release-1.2.3"
76+
)
5677
}
5778

5879
test("possibleChangelogUrls: github.com") {
59-
possibleReleaseRelatedUrls(uri"https://github.com/foo/bar", update)
80+
possibleReleaseRelatedUrls(
81+
SupportedVCS.GitHub,
82+
uri"https://github.com",
83+
uri"https://github.com/foo/bar",
84+
update
85+
)
6086
.map(_.url.renderString) shouldBe List(
6187
"https://github.com/foo/bar/releases/tag/v1.2.3",
6288
"https://github.com/foo/bar/releases/tag/1.2.3",
@@ -92,7 +118,12 @@ class VCSPackageTest extends AnyFunSuite with Matchers {
92118
}
93119

94120
test("possibleChangelogUrls: gitlab.com") {
95-
possibleReleaseRelatedUrls(uri"https://gitlab.com/foo/bar", update)
121+
possibleReleaseRelatedUrls(
122+
SupportedVCS.GitHub,
123+
uri"https://github.com",
124+
uri"https://gitlab.com/foo/bar",
125+
update
126+
)
96127
.map(_.url.renderString) shouldBe
97128
possibleReleaseNotesFilenames.map(name => s"https://gitlab.com/foo/bar/blob/master/$name") ++
98129
possibleChangelogFilenames.map(name => s"https://gitlab.com/foo/bar/blob/master/$name") ++
@@ -103,8 +134,34 @@ class VCSPackageTest extends AnyFunSuite with Matchers {
103134
)
104135
}
105136

137+
test("possibleChangelogUrls: on-prem gitlab") {
138+
possibleReleaseRelatedUrls(
139+
SupportedVCS.Gitlab,
140+
uri"https://gitlab.on-prem.net",
141+
uri"https://gitlab.on-prem.net/foo/bar",
142+
update
143+
)
144+
.map(_.url.renderString) shouldBe
145+
possibleReleaseNotesFilenames.map(name =>
146+
s"https://gitlab.on-prem.net/foo/bar/blob/master/$name"
147+
) ++
148+
possibleChangelogFilenames.map(name =>
149+
s"https://gitlab.on-prem.net/foo/bar/blob/master/$name"
150+
) ++
151+
List(
152+
"https://gitlab.on-prem.net/foo/bar/compare/v1.2.0...v1.2.3",
153+
"https://gitlab.on-prem.net/foo/bar/compare/1.2.0...1.2.3",
154+
"https://gitlab.on-prem.net/foo/bar/compare/release-1.2.0...release-1.2.3"
155+
)
156+
}
157+
106158
test("possibleChangelogUrls: bitbucket.org") {
107-
possibleReleaseRelatedUrls(uri"https://bitbucket.org/foo/bar", update)
159+
possibleReleaseRelatedUrls(
160+
SupportedVCS.GitHub,
161+
uri"https://github.com",
162+
uri"https://bitbucket.org/foo/bar",
163+
update
164+
)
108165
.map(_.url.renderString) shouldBe
109166
possibleReleaseNotesFilenames.map(name => s"https://bitbucket.org/foo/bar/master/$name") ++
110167
possibleChangelogFilenames.map(name => s"https://bitbucket.org/foo/bar/master/$name") ++
@@ -116,7 +173,12 @@ class VCSPackageTest extends AnyFunSuite with Matchers {
116173
}
117174

118175
test("possibleChangelogUrls: homepage") {
119-
possibleReleaseRelatedUrls(uri"https://scalacenter.github.io/scalafix/", update)
120-
.map(_.url.renderString) shouldBe List()
176+
possibleReleaseRelatedUrls(
177+
SupportedVCS.GitHub,
178+
uri"https://github.com",
179+
uri"https://scalacenter.github.io/scalafix/",
180+
update
181+
)
182+
.map(_.url.renderString) shouldBe List.empty
121183
}
122184
}

0 commit comments

Comments
 (0)