fixup! Add support for PATs in GitHub Enterprise server

pull/603/head
ivinokur 2023-11-15 14:46:14 +02:00
parent 1e5c0a760f
commit 6ad5704a8c
6 changed files with 42 additions and 12 deletions

View File

@ -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<Pair<Boolean, String>> valid =
isValid(

View File

@ -132,10 +132,14 @@ public abstract class AbstractGithubURLParser {
}
private Optional<String> getServerUrl(String repositoryUrl) {
// If the given repository url is an SSH url, generate the base url from the pattern:
// https://<hostname extracted from the SSH url>.
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(

View File

@ -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");

View File

@ -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"));

View File

@ -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()

View File

@ -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");