Skip to content

Support GitHub repository devfile url #793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public abstract class AbstractGithubURLParser {
private final Pattern githubPattern;

private final String githubPatternTemplate =
"^%s/(?<repoUser>[^/]+)/(?<repoName>[^/]++)((/)|(?:/tree/(?<branchName>.++))|(/pull/(?<pullRequestId>\\d++)))?$";
"%s/(?<repoUser>[^/]+)/(?<repoName>[^/]++)((/)|(?:/((tree)|(blob))/(?<path>.++))|(/pull/(?<pullRequestId>\\d++)))?$";

private final Pattern githubSSHPattern;

Expand Down Expand Up @@ -207,7 +207,7 @@ private GithubUrl parse(String url, boolean authenticationRequired) throws ApiEx
String branchName = null;
String pullRequestId = null;
if (isHTTPSUrl) {
branchName = matcher.group("branchName");
branchName = getBranch(matcher.group("path"));
pullRequestId = matcher.group("pullRequestId");
}

Expand Down Expand Up @@ -254,6 +254,21 @@ private GithubUrl parse(String url, boolean authenticationRequired) throws ApiEx
.withUrl(url);
}

/*
* Get branch from the file path by extracting the segment before the devfile name.
* Example of a file path: "branch/devfile.yaml" => "branch"
*/
private String getBranch(String path) {
if (path != null && path.contains("/")) {
Optional<String> devfileNameOptional =
devfileFilenamesProvider.getConfiguredDevfileFilenames().stream()
.filter(devfileName -> path.endsWith("/" + devfileName))
.findAny();
return devfileNameOptional.map(s -> path.substring(0, path.indexOf(s) - 1)).orElse(path);
}
return path;
}

private GithubPullRequest getPullRequest(
String githubEndpoint,
String pullRequestId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
import static java.util.Arrays.asList;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
Expand All @@ -38,6 +39,7 @@
import org.eclipse.che.api.factory.server.scm.PersonalAccessTokenManager;
import org.eclipse.che.api.factory.server.urlfactory.DevfileFilenamesProvider;
import org.eclipse.che.commons.subject.Subject;
import org.mockito.Mock;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
Expand All @@ -55,6 +57,7 @@ public class GithubURLParserTest {
private GithubApiClient githubApiClient;

private PersonalAccessTokenManager personalAccessTokenManager;
@Mock private DevfileFilenamesProvider devfileFilenamesProvider;

/** Instance of component that will be tested. */
private GithubURLParser githubUrlParser;
Expand All @@ -75,11 +78,7 @@ protected void start() throws ApiException {

githubUrlParser =
new GithubURLParser(
personalAccessTokenManager,
mock(DevfileFilenamesProvider.class),
githubApiClient,
null,
false);
personalAccessTokenManager, devfileFilenamesProvider, githubApiClient, null, false);
}

/** Check invalid url (not a GitHub one) */
Expand All @@ -96,13 +95,16 @@ public void checkRegexp(String url) {

/** Compare parsing */
@Test(dataProvider = "parsing")
public void checkParsing(String url, String username, String repository, String branch)
public void checkParsing(String url, String username, String repository, String path)
throws ApiException {
when(devfileFilenamesProvider.getConfiguredDevfileFilenames())
.thenReturn(asList("devfile.yaml", ".devfile.yaml"));

GithubUrl githubUrl = githubUrlParser.parse(url);

assertEquals(githubUrl.getUsername(), username);
assertEquals(githubUrl.getRepository(), repository);
assertEquals(githubUrl.getBranch(), branch);
assertEquals(githubUrl.getBranch(), path);
}

/** Compare parsing */
Expand Down Expand Up @@ -160,12 +162,26 @@ public Object[][] expectedParsing() {
{"https://github.com/eclipse/repositorygit", "eclipse", "repositorygit", null},
{"https://github.com/eclipse/che/tree/4.2.x", "eclipse", "che", "4.2.x"},
{"https://github.com/eclipse/che/tree/master", "eclipse", "che", "master"},
{"https://github.com/eclipse/che/tree/master/", "eclipse", "che", "master"},
{
"https://github.com/eclipse/che/tree/branch/with/slash",
"eclipse",
"che",
"branch/with/slash"
},
{"https://github.com/eclipse/che/blob/branch/path/to/", "eclipse", "che", "branch/path/to"},
{
"https://github.com/eclipse/che/blob/branch/path/to/devfile.yaml",
"eclipse",
"che",
"branch/path/to"
},
{
"https://github.com/eclipse/che/blob/branch/path/to/.devfile.yaml",
"eclipse",
"che",
"branch/path/to"
},
{"[email protected]:eclipse/che", "eclipse", "che", null},
{"[email protected]:eclipse/che123", "eclipse", "che123", null},
{"[email protected]:eclipse/che.git", "eclipse", "che", null},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2024 Red Hat, Inc.
* Copyright (c) 2012-2025 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand All @@ -13,7 +13,7 @@

import static com.google.common.base.Strings.isNullOrEmpty;
import static java.lang.String.format;
import static org.eclipse.che.api.factory.server.FactoryResolverPriority.HIGHEST;
import static org.eclipse.che.api.factory.server.FactoryResolverPriority.LOWEST;
import static org.eclipse.che.api.factory.shared.Constants.URL_PARAMETER_NAME;

import com.fasterxml.jackson.databind.JsonNode;
Expand Down Expand Up @@ -128,6 +128,6 @@ public RemoteFactoryUrl parseFactoryUrl(String factoryUrl) throws ApiException {

@Override
public FactoryResolverPriority priority() {
return HIGHEST;
return LOWEST;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vinokurig hmm, why raw Devfile has the lowest priority now?

Copy link
Contributor Author

@vinokurig vinokurig Apr 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the repository devfile url may matc the RAW devfile url pattern, so we need to check the provider resolvers first.

}
}
Loading