From 1cf7a3a21218b4c80f48470cd821a9018f065c9f Mon Sep 17 00:00:00 2001 From: Peter Streef Date: Wed, 4 Sep 2024 16:58:02 +0200 Subject: [PATCH] Add static constructor for `GitRemote.Service` from any previously used name formats (#4469) --- .../main/java/org/openrewrite/GitRemote.java | 19 ++++++++++++++++++- .../java/org/openrewrite/GitRemoteTest.java | 17 +++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/rewrite-core/src/main/java/org/openrewrite/GitRemote.java b/rewrite-core/src/main/java/org/openrewrite/GitRemote.java index 22bc3c63743..1da3335207d 100644 --- a/rewrite-core/src/main/java/org/openrewrite/GitRemote.java +++ b/rewrite-core/src/main/java/org/openrewrite/GitRemote.java @@ -44,7 +44,24 @@ public enum Service { Bitbucket, BitbucketCloud, AzureDevOps, - Unknown + Unknown; + + public static Service forName(String serviceName) { + switch (serviceName.toLowerCase(Locale.ENGLISH).replaceAll("[-_ ]", "")) { + case "github": + return GitHub; + case "gitlab": + return GitLab; + case "bitbucket": + return Bitbucket; + case "bitbucketcloud": + return BitbucketCloud; + case "azuredevops": + return AzureDevOps; + default: + return Unknown; + } + } } public static class Parser { diff --git a/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java b/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java index 8247c3d89a9..c6d86ea15af 100644 --- a/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java +++ b/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java @@ -257,4 +257,21 @@ void parseOriginCaseInsensitive(String cloneUrl, String expectedOrigin, String e assertThat(remote.getOrganization()).isEqualTo(expectedOrganization); assertThat(remote.getRepositoryName()).isEqualTo(expectedRepositoryName); } + + @ParameterizedTest + @CsvSource(textBlock = """ + GitHub, GitHub + GITLAB, GitLab + bitbucket, Bitbucket + BitbucketCloud, BitbucketCloud + Bitbucket Cloud, BitbucketCloud + BITBUCKET_CLOUD, BitbucketCloud + AzureDevOps, AzureDevOps + AZURE_DEVOPS, AzureDevOps + Azure DevOps, AzureDevOps + idontknow, Unknown + """) + void findServiceForName(String name, GitRemote.Service service){ + assertThat(GitRemote.Service.forName(name)).isEqualTo(service); + } }