From 0b6fe9f5e3d8c07b481a6a6ea845171e12ba8190 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sun, 28 Jul 2019 17:32:50 +0200 Subject: [PATCH] Add GitLab support --- frontend/src/Data.elm | 9 +++++++- frontend/src/Parse.elm | 15 ++++++++------ frontend/tests/ParseTest.elm | 40 ++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/frontend/src/Data.elm b/frontend/src/Data.elm index cdb4958..f7ade75 100644 --- a/frontend/src/Data.elm +++ b/frontend/src/Data.elm @@ -9,6 +9,7 @@ hostname = type Provider = GitHub | Bitbucket + | GitLab type alias Url = @@ -29,6 +30,9 @@ toHost prov = Bitbucket -> "bitbucket/" + GitLab -> + "gitlab/" + pathSeparator : Provider -> String pathSeparator prov = @@ -39,7 +43,10 @@ pathSeparator prov = Bitbucket -> "src" + GitLab -> + "blob" + toUrl : Url -> String toUrl { prov, user, repo, gitref, file } = - hostname ++ toHost prov ++ String.join "/" [ user, repo, pathSeparator prov, gitref, file ] + hostname ++ toHost prov ++ String.join "/" [ user, repo, gitref, file ] diff --git a/frontend/src/Parse.elm b/frontend/src/Parse.elm index 886cf65..83ecf5a 100644 --- a/frontend/src/Parse.elm +++ b/frontend/src/Parse.elm @@ -7,10 +7,10 @@ parseUrl : String -> Maybe Url parseUrl url = stripProtocol url |> splitProvider - |> Maybe.andThen splitOfHead - |> Maybe.andThen splitOfHead - |> Maybe.andThen splitOfHead - |> Maybe.andThen splitOfHead + |> Maybe.andThen splitOffHead + |> Maybe.andThen splitOffHead + |> Maybe.andThen splitOffHead + |> Maybe.andThen splitOffHead |> Maybe.andThen (\( ( ( ( ( prov, user ), repo ), separator ), gitref ), file ) -> if List.isEmpty file || (separator /= pathSeparator prov) then @@ -27,8 +27,8 @@ parseUrl url = ) -splitOfHead : ( a, List b ) -> Maybe ( ( a, b ), List b ) -splitOfHead ( head, tail ) = +splitOffHead : ( a, List b ) -> Maybe ( ( a, b ), List b ) +splitOffHead ( head, tail ) = splitPart tail |> Maybe.map (\( h, t ) -> ( ( head, h ), t )) @@ -53,6 +53,9 @@ parseProvider prov = "bitbucket.org" -> Just Bitbucket + "gitlab.com" -> + Just GitLab + _ -> Nothing diff --git a/frontend/tests/ParseTest.elm b/frontend/tests/ParseTest.elm index 807f0c8..a47901d 100644 --- a/frontend/tests/ParseTest.elm +++ b/frontend/tests/ParseTest.elm @@ -1,14 +1,19 @@ module ParseTest exposing ( invalidHostInUrlBitbucket , invalidHostInUrlGitHub + , invalidHostInUrlGitLab , invalidUrlBitbucket , invalidUrlGitHub + , invalidUrlGitLab , validHttpUrlBitbucket , validHttpUrlGitHub + , validHttpUrlGitLab , validHttpsUrlBitbucket , validHttpsUrlGitHub + , validHttpsUrlGitLab , validUrlWithoutProtocolBitbucket , validUrlWithoutProtocolGitHub + , validUrlWithoutProtocolGitLab ) import Data exposing (Provider(..), Url) @@ -32,6 +37,11 @@ expectedBitbucketUrl = expectedUrl Bitbucket +expectedGitLabUrl : Url +expectedGitLabUrl = + expectedUrl GitLab + + validHttpsUrlGitHub : Test validHttpsUrlGitHub = test "Parsing Valid HTTPS URL for GitHub" @@ -90,3 +100,33 @@ invalidHostInUrlBitbucket : Test invalidHostInUrlBitbucket = test "Parsing Invalid Host in URL for Bitbucket" (\_ -> Expect.equal Nothing (parseUrl "https://example.com/user/repo/blob/src/README.md")) + + +validHttpsUrlGitLab : Test +validHttpsUrlGitLab = + test "Parsing Valid HTTPS URL for GitLab" + (\_ -> Expect.equal (Just expectedGitLabUrl) (parseUrl "https://GITLAB.CoM/user/repo/blob/master/README.md")) + + +validHttpUrlGitLab : Test +validHttpUrlGitLab = + test "Parsing Valid HTTP URL for GitLab" + (\_ -> Expect.equal (Just expectedGitLabUrl) (parseUrl "http://gItLaB.CoM/user/repo/blob/master/README.md")) + + +validUrlWithoutProtocolGitLab : Test +validUrlWithoutProtocolGitLab = + test "Parsing Valid URL without Protocol for GitLab" + (\_ -> Expect.equal (Just expectedGitLabUrl) (parseUrl "GiTlAb.CoM/user/repo/blob/master/README.md")) + + +invalidUrlGitLab : Test +invalidUrlGitLab = + test "Parsing Invalid URL for GitLab" + (\_ -> Expect.equal Nothing (parseUrl "https://gitLab.CoM/user")) + + +invalidHostInUrlGitLab : Test +invalidHostInUrlGitLab = + test "Parsing Invalid Host in URL for GitLab" + (\_ -> Expect.equal Nothing (parseUrl "https://example.com/user/repo/blob/master/README.md"))