Skip to content
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

fix: resolution of git remotes #1214

Merged
merged 1 commit into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -16,13 +16,16 @@
package io.dekorate.tekton.manifest;

import static io.dekorate.tekton.util.TektonUtils.getContextPath;
import static io.dekorate.utils.Git.REMOTE_PATTERN;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import io.dekorate.BuildImage;
Expand Down Expand Up @@ -411,6 +414,31 @@ public PipelineResource createGitResource(TektonConfig config) {
ScmInfo scm = Optional.ofNullable(config.getProject().getScmInfo())
.orElseThrow(() -> new IllegalStateException("No scm info found!"));

String repoUrl = null;
if (scm.getRemote() != null) {
// Try to find the Origin remote
Pattern remotePattern = Pattern.compile(REMOTE_PATTERN);
for (Map.Entry<String, String> remote : scm.getRemote().entrySet()) {
Matcher m = remotePattern.matcher(remote.getKey());
if (m.matches()) {
String remoteValue = m.group(1);
if (Git.ORIGIN.equals(remoteValue)) {
repoUrl = remote.getValue();
break;
}
}
}

// if not found, let's pick up the first remote
if (repoUrl == null) {
repoUrl = scm.getRemote().values().iterator().next();
}
}

if (repoUrl == null) {
throw new IllegalStateException("Could not find the repository URL from the scm info!");
}

return new PipelineResourceBuilder()
.withNewMetadata()
.withName(gitResourceName(config))
Expand All @@ -419,7 +447,7 @@ public PipelineResource createGitResource(TektonConfig config) {
.withType(GIT)
.addNewParam()
.withName(URL)
.withValue(scm.getRemote().get(Git.ORIGIN))
.withValue(repoUrl)
.endParam()
.addNewParam()
.withName(REVISION)
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/io/dekorate/utils/Git.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public static Map<String, String> getRemotes(Path path) {
String remoteLine = linesIter.next();
if (remoteLine.startsWith(URL) && remoteLine.contains(EQUALS)) {
result.put(remote, remoteLine.split(EQUALS)[1].trim());
break;
}
}
});
Expand Down