Add GitLab support

This commit is contained in:
Valentin Brandl 2019-07-28 17:32:50 +02:00
parent ec0254ec75
commit 0b6fe9f5e3
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D
3 changed files with 57 additions and 7 deletions

View File

@ -9,6 +9,7 @@ hostname =
type Provider type Provider
= GitHub = GitHub
| Bitbucket | Bitbucket
| GitLab
type alias Url = type alias Url =
@ -29,6 +30,9 @@ toHost prov =
Bitbucket -> Bitbucket ->
"bitbucket/" "bitbucket/"
GitLab ->
"gitlab/"
pathSeparator : Provider -> String pathSeparator : Provider -> String
pathSeparator prov = pathSeparator prov =
@ -39,7 +43,10 @@ pathSeparator prov =
Bitbucket -> Bitbucket ->
"src" "src"
GitLab ->
"blob"
toUrl : Url -> String toUrl : Url -> String
toUrl { prov, user, repo, gitref, file } = 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 ]

View File

@ -7,10 +7,10 @@ parseUrl : String -> Maybe Url
parseUrl url = parseUrl url =
stripProtocol url stripProtocol url
|> splitProvider |> splitProvider
|> Maybe.andThen splitOfHead |> Maybe.andThen splitOffHead
|> Maybe.andThen splitOfHead |> Maybe.andThen splitOffHead
|> Maybe.andThen splitOfHead |> Maybe.andThen splitOffHead
|> Maybe.andThen splitOfHead |> Maybe.andThen splitOffHead
|> Maybe.andThen |> Maybe.andThen
(\( ( ( ( ( prov, user ), repo ), separator ), gitref ), file ) -> (\( ( ( ( ( prov, user ), repo ), separator ), gitref ), file ) ->
if List.isEmpty file || (separator /= pathSeparator prov) then if List.isEmpty file || (separator /= pathSeparator prov) then
@ -27,8 +27,8 @@ parseUrl url =
) )
splitOfHead : ( a, List b ) -> Maybe ( ( a, b ), List b ) splitOffHead : ( a, List b ) -> Maybe ( ( a, b ), List b )
splitOfHead ( head, tail ) = splitOffHead ( head, tail ) =
splitPart tail splitPart tail
|> Maybe.map (\( h, t ) -> ( ( head, h ), t )) |> Maybe.map (\( h, t ) -> ( ( head, h ), t ))
@ -53,6 +53,9 @@ parseProvider prov =
"bitbucket.org" -> "bitbucket.org" ->
Just Bitbucket Just Bitbucket
"gitlab.com" ->
Just GitLab
_ -> _ ->
Nothing Nothing

View File

@ -1,14 +1,19 @@
module ParseTest exposing module ParseTest exposing
( invalidHostInUrlBitbucket ( invalidHostInUrlBitbucket
, invalidHostInUrlGitHub , invalidHostInUrlGitHub
, invalidHostInUrlGitLab
, invalidUrlBitbucket , invalidUrlBitbucket
, invalidUrlGitHub , invalidUrlGitHub
, invalidUrlGitLab
, validHttpUrlBitbucket , validHttpUrlBitbucket
, validHttpUrlGitHub , validHttpUrlGitHub
, validHttpUrlGitLab
, validHttpsUrlBitbucket , validHttpsUrlBitbucket
, validHttpsUrlGitHub , validHttpsUrlGitHub
, validHttpsUrlGitLab
, validUrlWithoutProtocolBitbucket , validUrlWithoutProtocolBitbucket
, validUrlWithoutProtocolGitHub , validUrlWithoutProtocolGitHub
, validUrlWithoutProtocolGitLab
) )
import Data exposing (Provider(..), Url) import Data exposing (Provider(..), Url)
@ -32,6 +37,11 @@ expectedBitbucketUrl =
expectedUrl Bitbucket expectedUrl Bitbucket
expectedGitLabUrl : Url
expectedGitLabUrl =
expectedUrl GitLab
validHttpsUrlGitHub : Test validHttpsUrlGitHub : Test
validHttpsUrlGitHub = validHttpsUrlGitHub =
test "Parsing Valid HTTPS URL for GitHub" test "Parsing Valid HTTPS URL for GitHub"
@ -90,3 +100,33 @@ invalidHostInUrlBitbucket : Test
invalidHostInUrlBitbucket = invalidHostInUrlBitbucket =
test "Parsing Invalid Host in URL for Bitbucket" test "Parsing Invalid Host in URL for Bitbucket"
(\_ -> Expect.equal Nothing (parseUrl "https://example.com/user/repo/blob/src/README.md")) (\_ -> 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"))