Skip to content

Commit

Permalink
fix: resolution of git remotes
Browse files Browse the repository at this point in the history
Once the first git remote is found, we iterate over all the values and overwrite the values.

For example, having:

```
[remote "origin"]
	url = [email protected]:Sgitario/dekorate.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[remote "upstream"]
	url = [email protected]:dekorateio/dekorate.git
	fetch = +refs/heads/*:refs/remotes/upstream/*
[remote "auri"]
	url = https://github.com/aureamunoz/dekorate
	fetch = +refs/heads/*:refs/remotes/auri/*
```

Will produce a map of only an entry with:

```
'remote "origin"': https://github.com/aureamunoz/dekorate
```

Because https://github.com/aureamunoz/dekorate, it's the last value.

Also, the logic about remotes need to be aligned with this in the TektonManifestGenerator.
  • Loading branch information
Sgitario committed Jun 14, 2023
1 parent 67b6a28 commit 172d27e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
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

0 comments on commit 172d27e

Please sign in to comment.