From 5ecea6eee617184decfab0c38545059b53d0254b Mon Sep 17 00:00:00 2001 From: Regis David Souza Mesquita Date: Sat, 2 Dec 2023 15:07:30 +0000 Subject: [PATCH] Adds support to sourcehut (#319) --- .../ben_gibson/git/link/platform/Platform.kt | 7 +++ .../git/link/platform/PlatformRepository.kt | 1 + .../uk/co/ben_gibson/git/link/ui/Icons.kt | 1 + .../factory/TemplatedUrlFactoryProvider.kt | 1 + .../git/link/url/template/UrlTemplates.kt | 9 +++ src/main/resources/icons/sourcehut.svg | 36 ++++++++++++ .../resources/messages/MyBundle.properties | 1 + .../uk/co/ben_gibson/git/link/url/SrhtTest.kt | 57 +++++++++++++++++++ 8 files changed, 113 insertions(+) create mode 100644 src/main/resources/icons/sourcehut.svg create mode 100644 src/test/kotlin/uk/co/ben_gibson/git/link/url/SrhtTest.kt diff --git a/src/main/kotlin/uk/co/ben_gibson/git/link/platform/Platform.kt b/src/main/kotlin/uk/co/ben_gibson/git/link/platform/Platform.kt index 6ecfce3..3ed2111 100644 --- a/src/main/kotlin/uk/co/ben_gibson/git/link/platform/Platform.kt +++ b/src/main/kotlin/uk/co/ben_gibson/git/link/platform/Platform.kt @@ -61,6 +61,13 @@ class Gogs : Platform( setOf(Host("gogs.io")) ) +class Srht : Platform( + UUID.fromString("aa358239-5c11-4b53-8b97-723181c48f4f"), + message("platform.srht.name"), + Icons.SOURCEHUT, + setOf(Host("git.sr.ht")) +) + class Gitea : Platform( UUID.fromString("e0f86390-1091-4871-8aeb-f534fbc99cf0"), message("platform.gitea.name"), diff --git a/src/main/kotlin/uk/co/ben_gibson/git/link/platform/PlatformRepository.kt b/src/main/kotlin/uk/co/ben_gibson/git/link/platform/PlatformRepository.kt index 76932c5..bc70c42 100644 --- a/src/main/kotlin/uk/co/ben_gibson/git/link/platform/PlatformRepository.kt +++ b/src/main/kotlin/uk/co/ben_gibson/git/link/platform/PlatformRepository.kt @@ -15,6 +15,7 @@ private val EXISTING_PLATFORMS = setOf( Gitee(), Gitea(), Gogs(), + Srht(), Azure(), Chromium(), Gerrit() diff --git a/src/main/kotlin/uk/co/ben_gibson/git/link/ui/Icons.kt b/src/main/kotlin/uk/co/ben_gibson/git/link/ui/Icons.kt index a105169..ba80662 100644 --- a/src/main/kotlin/uk/co/ben_gibson/git/link/ui/Icons.kt +++ b/src/main/kotlin/uk/co/ben_gibson/git/link/ui/Icons.kt @@ -6,6 +6,7 @@ object Icons { val GITLAB = IconLoader.getIcon("/icons/gitlab.svg", javaClass) val BITBUCKET = IconLoader.getIcon("/icons/bitbucket.svg", javaClass) val GOGS = IconLoader.getIcon("/icons/gogs.svg", javaClass) + val SOURCEHUT = IconLoader.getIcon("/icons/sourcehut.svg", javaClass) val AZURE = IconLoader.getIcon("/icons/azure.svg", javaClass) val GITEA = IconLoader.getIcon("/icons/gitea.svg", javaClass) val GIT = IconLoader.getIcon("/icons/git.svg", javaClass) diff --git a/src/main/kotlin/uk/co/ben_gibson/git/link/url/factory/TemplatedUrlFactoryProvider.kt b/src/main/kotlin/uk/co/ben_gibson/git/link/url/factory/TemplatedUrlFactoryProvider.kt index 0204859..7781d28 100644 --- a/src/main/kotlin/uk/co/ben_gibson/git/link/url/factory/TemplatedUrlFactoryProvider.kt +++ b/src/main/kotlin/uk/co/ben_gibson/git/link/url/factory/TemplatedUrlFactoryProvider.kt @@ -13,6 +13,7 @@ val PLATFORM_MAP = mapOf( Gitee::class.java to UrlTemplates.gitee(), Gitea::class.java to UrlTemplates.gitea(), Gogs::class.java to UrlTemplates.gogs(), + Srht::class.java to UrlTemplates.srht(), BitbucketServer::class.java to UrlTemplates.bitbucketServer(), BitbucketCloud::class.java to UrlTemplates.bitbucketCloud(), Gerrit::class.java to UrlTemplates.gerrit(), diff --git a/src/main/kotlin/uk/co/ben_gibson/git/link/url/template/UrlTemplates.kt b/src/main/kotlin/uk/co/ben_gibson/git/link/url/template/UrlTemplates.kt index 4f24b36..294b363 100644 --- a/src/main/kotlin/uk/co/ben_gibson/git/link/url/template/UrlTemplates.kt +++ b/src/main/kotlin/uk/co/ben_gibson/git/link/url/template/UrlTemplates.kt @@ -50,6 +50,15 @@ data class UrlTemplates(val fileAtBranch: String, val fileAtCommit : String, val ) } + fun srht(): UrlTemplates { + return UrlTemplates( + "{remote:url}/tree/{branch}/item/{file:path}/{file:name}{line-block:start}#L{line:start}{line-block:end}", + "{remote:url}/tree/{commit}/item/{file:path}/{file:name}{line-block:start}#L{line:start}{line-block:end}", + "{remote:url}/tree/{commit}" + + ) + } + fun gitee() = gitHub() fun gerrit(): UrlTemplates { diff --git a/src/main/resources/icons/sourcehut.svg b/src/main/resources/icons/sourcehut.svg new file mode 100644 index 0000000..45826a1 --- /dev/null +++ b/src/main/resources/icons/sourcehut.svg @@ -0,0 +1,36 @@ + + + + + + diff --git a/src/main/resources/messages/MyBundle.properties b/src/main/resources/messages/MyBundle.properties index a65c98e..6281b9f 100644 --- a/src/main/resources/messages/MyBundle.properties +++ b/src/main/resources/messages/MyBundle.properties @@ -76,6 +76,7 @@ platform.bitbucket.cloud.name=Bitbucket Cloud platform.bitbucket.server.name=Bitbucket Server platform.gitea.name=Gitea platform.gogs.name=Gogs +platform.srht.name=SourceHut platform.azure.name=Azure platform.gitee.name=Gitee platform.chromium.name=Chromium (Experimental) diff --git a/src/test/kotlin/uk/co/ben_gibson/git/link/url/SrhtTest.kt b/src/test/kotlin/uk/co/ben_gibson/git/link/url/SrhtTest.kt new file mode 100644 index 0000000..c7894b9 --- /dev/null +++ b/src/test/kotlin/uk/co/ben_gibson/git/link/url/SrhtTest.kt @@ -0,0 +1,57 @@ +package uk.co.ben_gibson.git.link.url + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.Arguments +import org.junit.jupiter.params.provider.MethodSource +import uk.co.ben_gibson.git.link.git.* +import uk.co.ben_gibson.git.link.ui.LineSelection +import java.util.stream.Stream +import uk.co.ben_gibson.git.link.url.factory.TemplatedUrlFactory +import uk.co.ben_gibson.git.link.url.template.UrlTemplates +import uk.co.ben_gibson.url.URL + +class SourceHutTest { + + companion object { + + private val REMOTE_BASE_URL = URL.fromString("https://git.sr.ht/~myuser/myproject") + private const val BRANCH = "main" + private val COMMIT = Commit("23471005d2d874bb7ab400d45a2360f988c0be33") + private val FILE = File("main.rs", false, "src", false) + private val LINE_SELECTION = LineSelection(1, 2) + + @JvmStatic + fun urlExpectationsProvider(): Stream = Stream.of( + Arguments.of( + UrlOptionsFileAtBranch(REMOTE_BASE_URL, FILE, BRANCH, LINE_SELECTION), + "https://git.sr.ht/~myuser/myproject/tree/main/item/src/main.rs#L1" + ), + Arguments.of( + UrlOptionsFileAtBranch(REMOTE_BASE_URL, FILE, BRANCH), + "https://git.sr.ht/~myuser/myproject/tree/main/item/src/main.rs" + ), + Arguments.of( + UrlOptionsFileAtCommit(REMOTE_BASE_URL, FILE, COMMIT, LINE_SELECTION), + "https://git.sr.ht/~myuser/myproject/tree/23471005d2d874bb7ab400d45a2360f988c0be33/item/src/main.rs#L1" + ), + Arguments.of( + UrlOptionsFileAtCommit(REMOTE_BASE_URL, FILE, COMMIT), + "https://git.sr.ht/~myuser/myproject/tree/23471005d2d874bb7ab400d45a2360f988c0be33/item/src/main.rs" + ), + Arguments.of( + UrlOptionsCommit(REMOTE_BASE_URL, COMMIT), + "https://git.sr.ht/~myuser/myproject/tree/23471005d2d874bb7ab400d45a2360f988c0be33" + ) + ) + } + + @ParameterizedTest + @MethodSource("urlExpectationsProvider") + fun canGenerateUrl(options: UrlOptions, expectedUrl: String) { + val factory = TemplatedUrlFactory(UrlTemplates.srht()) + val url = factory.createUrl(options) + + assertEquals(expectedUrl, url.toString()) + } +} \ No newline at end of file