fix: fix resolving Gitlab raw url (#371)

Partially reverts 5aa763a in order to fix the Gitlab OAuth flow.
The rawFileUrl method uses the API request now. If the request is passed without token it works for public repos, unlike the previous raw file request which fails for private projects, even with the Authorisation header.
pull/372/head
Igor Vinokur 2022-10-19 17:59:17 +03:00 committed by GitHub
parent acc57c7788
commit a6faefa953
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 15 deletions

View File

@ -11,7 +11,6 @@
*/
package org.eclipse.che.api.factory.server.gitlab;
import static com.google.common.base.MoreObjects.firstNonNull;
import static java.net.URLEncoder.encode;
import com.google.common.base.Charsets;
@ -200,14 +199,22 @@ public class GitlabUrl implements RemoteFactoryUrl {
* @return location of specified file in a repository
*/
public String rawFileLocation(String fileName) {
return new StringJoiner("/")
.add(hostName)
.add(username)
.add(project)
.add("-/raw")
.add(firstNonNull(branch, "HEAD"))
.add(fileName)
.toString();
String resultUrl =
new StringJoiner("/")
.add(hostName)
.add("api/v4/projects")
// use URL-encoded path to the project as a selector instead of id
.add(geProjectIdentifier())
.add("repository")
.add("files")
.add(encode(fileName, Charsets.UTF_8))
.add("raw")
.toString();
if (branch != null) {
resultUrl = resultUrl + "?ref=" + branch;
}
return resultUrl;
}
private String geProjectIdentifier() {

View File

@ -43,7 +43,10 @@ public class GitlabAuthorizingFileContentProviderTest {
when(personalAccessTokenManager.getAndStore(anyString())).thenReturn(personalAccessToken);
fileContentProvider.fetchContent("devfile.yaml");
verify(urlFetcher)
.fetch(eq("https://gitlab.net/eclipse/che/-/raw/HEAD/devfile.yaml"), eq("Bearer my-token"));
.fetch(
eq(
"https://gitlab.net/api/v4/projects/eclipse%2Fche/repository/files/devfile.yaml/raw"),
eq("Bearer my-token"));
}
@Test

View File

@ -69,22 +69,25 @@ public class GitlabUrlTest {
@DataProvider
public static Object[][] urlsProvider() {
return new Object[][] {
{"https://gitlab.net/eclipse/che.git", "https://gitlab.net/eclipse/che/-/raw/HEAD/%s"},
{
"https://gitlab.net/eclipse/che.git",
"https://gitlab.net/api/v4/projects/eclipse%%2Fche/repository/files/%s/raw"
},
{
"https://gitlab.net/eclipse/fooproj/che.git",
"https://gitlab.net/eclipse/fooproj/-/raw/HEAD/%s"
"https://gitlab.net/api/v4/projects/eclipse%%2Ffooproj%%2Fche/repository/files/%s/raw"
},
{
"https://gitlab.net/eclipse/fooproj/-/tree/master/",
"https://gitlab.net/eclipse/fooproj/-/raw/master/%s"
"https://gitlab.net/api/v4/projects/eclipse%%2Ffooproj/repository/files/%s/raw?ref=master"
},
{
"https://gitlab.net/eclipse/fooproj/che/-/tree/foobranch/",
"https://gitlab.net/eclipse/fooproj/-/raw/foobranch/%s"
"https://gitlab.net/api/v4/projects/eclipse%%2Ffooproj%%2Fche/repository/files/%s/raw?ref=foobranch"
},
{
"https://gitlab.net/eclipse/fooproj/che/-/tree/foobranch/subfolder",
"https://gitlab.net/eclipse/fooproj/-/raw/foobranch/%s"
"https://gitlab.net/api/v4/projects/eclipse%%2Ffooproj%%2Fche/repository/files/%s/raw?ref=foobranch"
},
};
}