From 6ad5704a8ceefcee35e0f47312d8ed61135ac1cb Mon Sep 17 00:00:00 2001 From: ivinokur Date: Wed, 15 Nov 2023 14:46:14 +0200 Subject: [PATCH] fixup! Add support for PATs in GitHub Enterprise server --- .../BitbucketPersonalAccessTokenFetcher.java | 1 - .../github/AbstractGithubURLParser.java | 4 ++++ ...hubAuthorizingFileContentProviderTest.java | 20 ++++++++++++++++--- .../GithubFactoryParametersResolverTest.java | 13 ++++++------ .../github/GithubScmFileResolverTest.java | 1 + .../factory/server/github/GithubUrlTest.java | 15 ++++++++++++-- 6 files changed, 42 insertions(+), 12 deletions(-) diff --git a/wsmaster/che-core-api-factory-bitbucket/src/main/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketPersonalAccessTokenFetcher.java b/wsmaster/che-core-api-factory-bitbucket/src/main/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketPersonalAccessTokenFetcher.java index 0381d4444a..259d1431cb 100644 --- a/wsmaster/che-core-api-factory-bitbucket/src/main/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketPersonalAccessTokenFetcher.java +++ b/wsmaster/che-core-api-factory-bitbucket/src/main/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketPersonalAccessTokenFetcher.java @@ -93,7 +93,6 @@ public class BitbucketPersonalAccessTokenFetcher implements PersonalAccessTokenF try { oAuthToken = oAuthAPI.getToken(OAUTH_PROVIDER_NAME); String tokenName = NameGenerator.generate(OAUTH_PROVIDER_NAME, 5) + "_" + OAUTH_PROVIDER_NAME; - ; String tokenId = NameGenerator.generate("id-", 5); Optional> valid = isValid( diff --git a/wsmaster/che-core-api-factory-github-common/src/main/java/org/eclipse/che/api/factory/server/github/AbstractGithubURLParser.java b/wsmaster/che-core-api-factory-github-common/src/main/java/org/eclipse/che/api/factory/server/github/AbstractGithubURLParser.java index d29b4ff074..24b3a47736 100644 --- a/wsmaster/che-core-api-factory-github-common/src/main/java/org/eclipse/che/api/factory/server/github/AbstractGithubURLParser.java +++ b/wsmaster/che-core-api-factory-github-common/src/main/java/org/eclipse/che/api/factory/server/github/AbstractGithubURLParser.java @@ -132,10 +132,14 @@ public abstract class AbstractGithubURLParser { } private Optional getServerUrl(String repositoryUrl) { + // If the given repository url is an SSH url, generate the base url from the pattern: + // https://. if (repositoryUrl.startsWith("git@")) { String substring = repositoryUrl.substring(4); return Optional.of("https://" + substring.substring(0, substring.indexOf(":"))); } + // Otherwise, extract the base url from the given repository url by cutting the url after the + // first slash. Matcher serverUrlMatcher = compile("[^/|:]/").matcher(repositoryUrl); if (serverUrlMatcher.find()) { return Optional.of( diff --git a/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubAuthorizingFileContentProviderTest.java b/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubAuthorizingFileContentProviderTest.java index 9fd553183b..a530b220ae 100644 --- a/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubAuthorizingFileContentProviderTest.java +++ b/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubAuthorizingFileContentProviderTest.java @@ -49,6 +49,7 @@ public class GithubAuthorizingFileContentProviderTest { .withUsername("eclipse") .withRepository("che") .withBranch("main") + .withServerUrl("https://github.com") .withLatestCommit("d74923ebf968454cf13251f17df69dcd87d3b932"); FileContentProvider fileContentProvider = @@ -74,6 +75,7 @@ public class GithubAuthorizingFileContentProviderTest { new GithubUrl("github") .withUsername("eclipse") .withRepository("che") + .withServerUrl("https://github.com") .withBranch("main") .withLatestCommit("9ac2f42ed62944d164f189afd57f14a2793a7e4b"); @@ -92,7 +94,11 @@ public class GithubAuthorizingFileContentProviderTest { public void shouldThrowNotFoundForPublicRepos() throws Exception { String url = "https://raw.githubusercontent.com/foo/bar/branch-name/devfile.yaml"; - GithubUrl githubUrl = new GithubUrl("github").withUsername("eclipse").withRepository("che"); + GithubUrl githubUrl = + new GithubUrl("github") + .withUsername("eclipse") + .withRepository("che") + .withServerUrl("https://github.com"); URLFetcher urlFetcher = Mockito.mock(URLFetcher.class); FileContentProvider fileContentProvider = @@ -109,7 +115,11 @@ public class GithubAuthorizingFileContentProviderTest { @Test(expectedExceptions = DevfileException.class) public void shouldThrowDevfileException() throws Exception { String url = "https://raw.githubusercontent.com/foo/bar/branch-name/devfile.yaml"; - GithubUrl githubUrl = new GithubUrl("github").withUsername("eclipse").withRepository("che"); + GithubUrl githubUrl = + new GithubUrl("github") + .withUsername("eclipse") + .withRepository("che") + .withServerUrl("https://github.com"); URLFetcher urlFetcher = Mockito.mock(URLFetcher.class); FileContentProvider fileContentProvider = @@ -128,7 +138,11 @@ public class GithubAuthorizingFileContentProviderTest { String raw_url = "https://ghserver.com/foo/bar/branch-name/devfile.yaml"; URLFetcher urlFetcher = Mockito.mock(URLFetcher.class); - GithubUrl githubUrl = new GithubUrl("github").withUsername("eclipse").withRepository("che"); + GithubUrl githubUrl = + new GithubUrl("github") + .withUsername("eclipse") + .withRepository("che") + .withServerUrl("https://github.com"); FileContentProvider fileContentProvider = new GithubAuthorizingFileContentProvider(githubUrl, urlFetcher, personalAccessTokenManager); var personalAccessToken = new PersonalAccessToken(raw_url, "che", "my-token"); diff --git a/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubFactoryParametersResolverTest.java b/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubFactoryParametersResolverTest.java index 387acb6cfb..439881e530 100644 --- a/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubFactoryParametersResolverTest.java +++ b/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubFactoryParametersResolverTest.java @@ -163,6 +163,7 @@ public class GithubFactoryParametersResolverTest { any(RemoteFactoryUrl.class), any(), anyMap(), anyBoolean())) .thenReturn(Optional.empty()); + when(githubApiClient.isConnected(eq("https://github.com"))).thenReturn(true); when(githubApiClient.getLatestCommit(anyString(), anyString(), anyString(), any())) .thenReturn(new GithubCommit().withSha("test-sha")); @@ -182,7 +183,7 @@ public class GithubFactoryParametersResolverTest { // given when(urlFactoryBuilder.buildDefaultDevfile(any())) .thenReturn(generateDevfileFactory().getDevfile()); - + when(githubApiClient.isConnected(eq("https://github.com"))).thenReturn(true); when(githubApiClient.getLatestCommit(anyString(), anyString(), anyString(), any())) .thenReturn(new GithubCommit().withSha("test-sha")); @@ -205,7 +206,7 @@ public class GithubFactoryParametersResolverTest { // given when(urlFactoryBuilder.buildDefaultDevfile(any())) .thenReturn(generateDevfileFactory().getDevfile()); - + when(githubApiClient.isConnected(eq("https://github.com"))).thenReturn(true); when(githubApiClient.getLatestCommit(anyString(), anyString(), anyString(), any())) .thenReturn(new GithubCommit().withSha("test-sha")); @@ -231,7 +232,7 @@ public class GithubFactoryParametersResolverTest { when(urlFactoryBuilder.createFactoryFromDevfile( any(RemoteFactoryUrl.class), any(), anyMap(), anyBoolean())) .thenReturn(Optional.of(computedFactory)); - + when(githubApiClient.isConnected(eq("https://github.com"))).thenReturn(true); when(githubApiClient.getLatestCommit(anyString(), anyString(), anyString(), any())) .thenReturn(new GithubCommit().withSha("13bbd0d4605a6ed3350f7b15eb02c4d4e6f8df6e")); @@ -262,7 +263,7 @@ public class GithubFactoryParametersResolverTest { when(urlFactoryBuilder.createFactoryFromDevfile( any(RemoteFactoryUrl.class), any(), anyMap(), anyBoolean())) .thenReturn(Optional.of(computedFactory)); - + when(githubApiClient.isConnected(eq("https://github.com"))).thenReturn(true); when(githubApiClient.getLatestCommit(anyString(), anyString(), anyString(), any())) .thenReturn(new GithubCommit().withSha("test-sha")); @@ -293,7 +294,7 @@ public class GithubFactoryParametersResolverTest { when(urlFactoryBuilder.createFactoryFromDevfile( any(RemoteFactoryUrl.class), any(), anyMap(), anyBoolean())) .thenReturn(Optional.of(computedFactory)); - + when(githubApiClient.isConnected(eq("https://github.com"))).thenReturn(true); when(githubApiClient.getLatestCommit(anyString(), anyString(), anyString(), any())) .thenReturn(new GithubCommit().withSha("test-sha")); @@ -316,7 +317,7 @@ public class GithubFactoryParametersResolverTest { when(urlFactoryBuilder.createFactoryFromDevfile( any(RemoteFactoryUrl.class), any(), anyMap(), anyBoolean())) .thenReturn(Optional.of(computedFactory)); - + when(githubApiClient.isConnected(eq("https://github.com"))).thenReturn(true); when(githubApiClient.getLatestCommit(anyString(), anyString(), anyString(), any())) .thenReturn(new GithubCommit().withSha("test-sha")); diff --git a/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubScmFileResolverTest.java b/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubScmFileResolverTest.java index 641fcd44f6..1108148160 100644 --- a/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubScmFileResolverTest.java +++ b/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubScmFileResolverTest.java @@ -90,6 +90,7 @@ public class GithubScmFileResolverTest { .when(personalAccessTokenManager.getAndStore(anyString())) .thenReturn(new PersonalAccessToken("foo", "che", "my-token")); + when(githubApiClient.isConnected(eq("https://github.com"))).thenReturn(true); when(githubApiClient.getLatestCommit(anyString(), anyString(), anyString(), any())) .thenReturn( new GithubCommit() diff --git a/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubUrlTest.java b/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubUrlTest.java index c63b5181fe..2cacf8796c 100644 --- a/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubUrlTest.java +++ b/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubUrlTest.java @@ -39,6 +39,7 @@ public class GithubUrlTest { @Test public void checkDevfileLocation() throws Exception { DevfileFilenamesProvider devfileFilenamesProvider = mock(DevfileFilenamesProvider.class); + when(githubApiClient.isConnected("https://github.com")).thenReturn(true); /** Parser used to create the url. */ GithubURLParser githubUrlParser = @@ -67,6 +68,7 @@ public class GithubUrlTest { @Test public void shouldReturnDevfileLocationFromSSHUrl() throws Exception { DevfileFilenamesProvider devfileFilenamesProvider = mock(DevfileFilenamesProvider.class); + when(githubApiClient.isConnected("https://github.com")).thenReturn(true); /** Parser used to create the url. */ GithubURLParser githubUrlParser = @@ -139,7 +141,11 @@ public class GithubUrlTest { public void testRawFileLocationWithDefaultBranchName() { String file = ".che/che-theia-plugins.yaml"; - GithubUrl url = new GithubUrl("github").withUsername("eclipse").withRepository("che"); + GithubUrl url = + new GithubUrl("github") + .withUsername("eclipse") + .withRepository("che") + .withServerUrl("https://github.com"); assertEquals( url.rawFileLocation(file), @@ -151,7 +157,11 @@ public class GithubUrlTest { String file = ".che/che-theia-plugins.yaml"; GithubUrl url = - new GithubUrl("github").withUsername("eclipse").withRepository("che").withBranch("main"); + new GithubUrl("github") + .withUsername("eclipse") + .withRepository("che") + .withBranch("main") + .withServerUrl("https://github.com"); assertEquals( url.rawFileLocation(file), @@ -166,6 +176,7 @@ public class GithubUrlTest { new GithubUrl("github") .withUsername("eclipse") .withRepository("che") + .withServerUrl("https://github.com") .withBranch("main") .withLatestCommit("c24fd44e0f7296be2e49a380fb8abe2fe4db9100");