-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: introduce
ForgeRepo
as a parameter object
`forgeType` & `repoUrl` are two parameters that are passed together in several method calls, suggesting that they belong together in a single parameter object - a `ForgeRepo` where the type of forge indicated by the repoUrl has been established:: https://refactoring.com/catalog/introduceParameterObject.html https://refactoring.guru/introduce-parameter-object https://docondev.com/blog/2020/6/2/refactoring-introduce-parameter-object Those two pieces of information - `forgeType` & `repoUrl` - are all that are required to generate urls for diffs or file display, meaning that actually `ForgeRepo` can be more than a simple parameter object, and provide convenient methods to generate those urls. New tests for generated file & diff urls ======================================== This change also adds `ForgeRepoTest`, which gives clear examples of what diff and file urls should be for different forge types. The urls included in the test correspond to real live repos as much as possible, so it's possible to click through on them and see that they do indeed correspond to real live urls genuinely served by those different forge hosts.
- Loading branch information
Showing
5 changed files
with
156 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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
37 changes: 37 additions & 0 deletions
37
modules/core/src/main/scala/org/scalasteward/core/forge/ForgeRepo.scala
This file contains 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,37 @@ | ||
/* | ||
* Copyright 2018-2023 Scala Steward contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.scalasteward.core.forge | ||
|
||
import org.http4s.Uri | ||
import org.scalasteward.core.application.Config.ForgeCfg | ||
|
||
/** ForgeRepo encapsulates two concepts that are commonly considered together - the URI of a repo, | ||
* and the 'type' of forge that url represents. Given a URI, once we know it's a GitHub or GitLab | ||
* forge, etc, then we can know how to construct many of the urls for common resources existing at | ||
* that repo host- for instance, the url to view a particular file, or to diff two commits. | ||
*/ | ||
case class ForgeRepo(forgeType: ForgeType, repoUrl: Uri) { | ||
def diffUrlFor(from: String, to: String): Uri = forgeType.diffs.forDiff(from, to)(repoUrl) | ||
|
||
def fileUrlFor(fileName: String): Uri = forgeType.files.forFile(fileName)(repoUrl) | ||
} | ||
|
||
object ForgeRepo { | ||
def fromRepoUrl(repoUrl: Uri)(implicit config: ForgeCfg): Option[ForgeRepo] = for { | ||
repoForgeType <- ForgeType.fromRepoUrl(repoUrl) | ||
} yield ForgeRepo(repoForgeType, repoUrl) | ||
} |
This file contains 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
81 changes: 81 additions & 0 deletions
81
modules/core/src/test/scala/org/scalasteward/core/forge/ForgeRepoTest.scala
This file contains 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,81 @@ | ||
package org.scalasteward.core.forge | ||
|
||
import munit.FunSuite | ||
import org.http4s.Uri | ||
import org.http4s.implicits._ | ||
import org.scalasteward.core.forge.ForgeType._ | ||
|
||
/** As much as possible, uris in this test suite should aim to be real, clickable, uris that | ||
* actually go to real pages, allowing developers working against this test suite to verify that | ||
* the url patterns constructed here actually _are_ valid, and match what forges are currently | ||
* using in the real world. | ||
*/ | ||
class ForgeRepoTest extends FunSuite { | ||
|
||
def check( | ||
repo: ForgeRepo, | ||
expectedReadme: Uri, | ||
diffTags: (String, String), | ||
expectedDiff: Uri | ||
): Unit = { | ||
assertEquals(repo.fileUrlFor("README.md"), expectedReadme) | ||
assertEquals(repo.diffUrlFor(diffTags._1, diffTags._2), expectedDiff) | ||
} | ||
|
||
test("GitHub url patterns") { | ||
check( | ||
ForgeRepo(GitHub, uri"https://github.com/scala-steward-org/scala-steward-action"), | ||
uri"https://github.com/scala-steward-org/scala-steward-action/blob/master/README.md", | ||
"v2.55.0" -> "v2.56.0", | ||
uri"https://github.com/scala-steward-org/scala-steward-action/compare/v2.55.0...v2.56.0" | ||
) | ||
} | ||
|
||
test("GitLab url patterns") { | ||
check( | ||
ForgeRepo(GitLab, uri"https://gitlab.com/gitlab-org/gitlab"), | ||
uri"https://gitlab.com/gitlab-org/gitlab/blob/master/README.md", | ||
"v15.11.8-ee" -> "v15.11.9-ee", | ||
uri"https://gitlab.com/gitlab-org/gitlab/compare/v15.11.8-ee...v15.11.9-ee" | ||
) | ||
} | ||
|
||
test("Gitea url patterns") { | ||
check( | ||
ForgeRepo(Gitea, uri"https://gitea.com/lunny/levelqueue"), | ||
uri"https://gitea.com/lunny/levelqueue/src/branch/master/README.md", | ||
"v0.1.0" -> "v0.2.0", | ||
uri"https://gitea.com/lunny/levelqueue/compare/v0.1.0...v0.2.0" | ||
) | ||
} | ||
|
||
test("Azure url patterns") { | ||
check( | ||
ForgeRepo( | ||
AzureRepos, | ||
uri"https://dev.azure.com/rtyley/scala-steward-testing/_git/scala-steward-testing" | ||
), | ||
uri"https://dev.azure.com/rtyley/scala-steward-testing/_git/scala-steward-testing?path=README.md", | ||
"v1.0.0" -> "v1.0.1", | ||
uri"https://dev.azure.com/rtyley/scala-steward-testing/_git/scala-steward-testing/branchCompare?baseVersion=GTv1.0.0&targetVersion=GTv1.0.1" | ||
) | ||
} | ||
|
||
test("BitBucket url patterns") { | ||
check( | ||
ForgeRepo(Bitbucket, uri"https://bitbucket.org/rtyley/scala-steward-test-repo"), | ||
uri"https://bitbucket.org/rtyley/scala-steward-test-repo/src/master/README.md", | ||
"v1.0.0" -> "v1.0.1", | ||
uri"https://bitbucket.org/rtyley/scala-steward-test-repo/compare/v1.0.1..v1.0.0#diff" | ||
) | ||
} | ||
|
||
test("BitBucket Server url patterns") { | ||
check( | ||
ForgeRepo(BitbucketServer, uri"https://bitbucket-server.on-prem.com/foo/bar"), | ||
uri"https://bitbucket-server.on-prem.com/foo/bar/browse/README.md", | ||
"v1.0.0" -> "v1.0.1", | ||
uri"https://bitbucket-server.on-prem.com/foo/bar/compare/v1.0.1..v1.0.0#diff" | ||
) | ||
} | ||
} |
This file contains 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