Skip to content

Commit

Permalink
chore(uniquet): add uniquet job to add the single ET file 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mathias-vandaele committed Nov 8, 2024
1 parent 6002d2c commit 5d4a267
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/RUN_UNIQUET.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ jobs:
run-uniquet-and-push:
runs-on: ubuntu-latest
steps:
- name: Set up JDK
uses: actions/setup-java@v2
with:
java-version: '21'
distribution: 'temurin'

- name: Checkout repository
uses: actions/checkout@v2
with:
Expand Down
6 changes: 6 additions & 0 deletions element-template-generator/uniquet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<version>${version.picocli}</version>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.9.9</version>
</dependency>

<dependency>
<groupId>io.camunda.connector</groupId>
<artifactId>jackson-datatype-feel</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,32 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.camunda.connector.uniquet.dto.ElementTemplate;
import io.camunda.connector.uniquet.dto.ElementTemplateFile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Objects;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ElementTemplateIterator implements Iterator<ElementTemplateFile> {

private static final Logger log = LoggerFactory.getLogger(ElementTemplateIterator.class);
private final RevCommit commit;
private final ObjectMapper objectMapper;
private final Repository repository;
private final TreeWalk initialWalk;
private final String currentConnectorRuntime;
private ElementTemplateFile elementTemplate;
private TreeWalk currentWalk;
private String currentFolderBeingAnalyzed;
Expand All @@ -47,15 +57,39 @@ public ElementTemplateIterator(Repository repository, RevCommit commit) {
TreeWalk treeWalk = new TreeWalk(repository);
treeWalk.addTree(this.commit.getTree());
treeWalk.setRecursive(false);
treeWalk.setFilter(PathFilter.create("connectors"));
// treeWalk.setFilter(PathFilter.create("connectors"));
this.initialWalk = treeWalk;
this.initialWalk.next();
} catch (IOException e) {
throw new RuntimeException(e);
}
this.currentConnectorRuntime = findCurrentConnectorRuntime(repository, commit);
this.elementTemplate = this.prepareNext();
}

private static String findCurrentConnectorRuntime(Repository repository, RevCommit commit) {
TreeWalk treeWalk = new TreeWalk(repository);
try {
treeWalk.addTree(commit.getTree());
treeWalk.setRecursive(false);
treeWalk.setFilter(PathFilter.create("pom.xml"));
treeWalk.next();
ObjectId objectId = treeWalk.getObjectId(0);
ObjectLoader loader = repository.open(objectId);
ByteArrayOutputStream out = new ByteArrayOutputStream();
loader.copyTo(out);
String pomContent = out.toString(StandardCharsets.UTF_8);
try (StringReader reader = new StringReader(pomContent)) {
MavenXpp3Reader mavenReader = new MavenXpp3Reader();
Model model = mavenReader.read(reader);
return model.getParent().getVersion();
}
} catch (IOException | XmlPullParserException | NullPointerException e) {
log.error("Commit: " + commit.getName() + ". No connector runtime found");
return "";
}
}

@Override
public boolean hasNext() {
return this.elementTemplate != null;
Expand Down Expand Up @@ -91,7 +125,8 @@ private ElementTemplateFile prepareNext() {
try {
return new ElementTemplateFile(
objectMapper.readValue(bytes, ElementTemplate.class),
this.currentWalk.getPathString());
this.currentWalk.getPathString(),
this.currentConnectorRuntime);
} catch (IOException e) {
System.err.println(
"Error while reading element-template: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import io.camunda.connector.uniquet.dto.OutputElementTemplate;
import io.camunda.connector.uniquet.dto.VersionValue;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
Expand All @@ -35,7 +36,7 @@ public class GitCrawler {

private static final String RAW_GITHUB_LINK =
"https://raw.githubusercontent.com/camunda/connectors/%s/%s";
private final Map<String, Map<Integer, String>> result = new HashMap<>();
private final Map<String, Map<Integer, VersionValue>> result = new HashMap<>();
private final Repository repository;

public GitCrawler(Repository repository) {
Expand All @@ -54,7 +55,7 @@ public static GitCrawler create(String gitDirectory) {
}
}

public Map<String, Map<Integer, String>> getResult() {
public Map<String, Map<Integer, VersionValue>> getResult() {
return result;
}

Expand All @@ -81,12 +82,16 @@ private void analyzeCommit(RevCommit commit) {
.get(elementTemplateFile.elementTemplate().id())
.putIfAbsent(
elementTemplateFile.elementTemplate().version(),
RAW_GITHUB_LINK.formatted(commit.getName(), elementTemplateFile.path()));
new VersionValue(
RAW_GITHUB_LINK.formatted(commit.getName(), elementTemplateFile.path()),
elementTemplateFile.connectorRuntime()));
} else {
Map<Integer, String> version = new HashMap<>();
Map<Integer, VersionValue> version = new HashMap<>();
version.put(
elementTemplateFile.elementTemplate().version(),
RAW_GITHUB_LINK.formatted(commit.getName(), elementTemplateFile.path()));
new VersionValue(
RAW_GITHUB_LINK.formatted(commit.getName(), elementTemplateFile.path()),
elementTemplateFile.connectorRuntime()));
result.put(elementTemplateFile.elementTemplate().id(), version);
}
});
Expand All @@ -103,17 +108,19 @@ public GitCrawler persist(String location) {
}

private Map<String, List<OutputElementTemplate>> fromMap(
Map<String, Map<Integer, String>> result) {
Map<String, Map<Integer, VersionValue>> result) {
return result.entrySet().stream()
.map(
stringMapEntry ->
Map.entry(
stringMapEntry.getKey(),
stringMapEntry.getValue().entrySet().stream()
.map(
integerStringEntry ->
integerVersionValueEntry ->
new OutputElementTemplate(
integerStringEntry.getKey(), integerStringEntry.getValue()))
integerVersionValueEntry.getKey(),
integerVersionValueEntry.getValue().link(),
integerVersionValueEntry.getValue().connectorRuntime()))
.sorted((o1, o2) -> o2.version() - o1.version())
.toList()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
*/
package io.camunda.connector.uniquet.dto;

public record ElementTemplateFile(ElementTemplate elementTemplate, String path) {}
public record ElementTemplateFile(
ElementTemplate elementTemplate, String path, String connectorRuntime) {}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
*/
package io.camunda.connector.uniquet.dto;

public record OutputElementTemplate(Integer version, String ref) {}
public record OutputElementTemplate(Integer version, String ref, String connectorRuntime) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.camunda.connector.uniquet.dto;

public record VersionValue(String link, String connectorRuntime) {}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.camunda.connector.core;

import io.camunda.connector.uniquet.core.GitCrawler;
import io.camunda.connector.uniquet.dto.VersionValue;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -35,10 +36,12 @@ class GitCrawlerTest {

private static Git git;
private static Path newFilePath;
private static Path pomPath;

@BeforeAll
public static void setUp(@TempDir Path tempDir) throws GitAPIException, IOException {
git = Git.init().setDirectory(tempDir.toFile()).call();
pomPath = tempDir.resolve("pom.xml");
newFilePath = tempDir.resolve("element-templates/test.json");
Files.createDirectories(newFilePath.getParent());
}
Expand All @@ -52,6 +55,8 @@ public static void tearDown() {
void crawl() throws IOException, GitAPIException {

// commit 1
String pomFile = Files.readString(Path.of("src/test/resources/fakepom.xml"));
Files.write(pomPath, pomFile.getBytes(), StandardOpenOption.CREATE);
String content1 = Files.readString(Path.of("src/test/resources/commit1.json"));
Files.write(newFilePath, content1.getBytes(), StandardOpenOption.CREATE);
git.add().addFilepattern(".").call();
Expand All @@ -70,15 +75,16 @@ void crawl() throws IOException, GitAPIException {
git.add().addFilepattern(".").call();
RevCommit revCommit3 = git.commit().setSign(false).setMessage("commit 3").call();

Map<String, Map<Integer, String>> map =
Map<String, Map<Integer, VersionValue>> map =
GitCrawler.create(git.getRepository().getDirectory().getParentFile().getPath())
.crawl("master")
.getResult();

// Verification that the version 2 is the last commit
Assertions.assertTrue(map.get("test").get(2).contains(revCommit3.getName()));
Assertions.assertTrue(map.get("test").get(2).link().contains(revCommit3.getName()));
Assertions.assertTrue(map.get("test").get(2).connectorRuntime().equals("9.9"));
// Verification that the version 1 is the last commit containing version 1
Assertions.assertTrue(map.get("test").get(1).contains(revCommit2.getName()));
Assertions.assertFalse(map.get("test").get(1).contains(revCommit1.getName()));
Assertions.assertTrue(map.get("test").get(1).link().contains(revCommit2.getName()));
Assertions.assertFalse(map.get("test").get(1).link().contains(revCommit1.getName()));
}
}
15 changes: 15 additions & 0 deletions element-template-generator/uniquet/src/test/resources/fakepom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>test</groupId>
<artifactId>test-parent</artifactId>
<version>9.9</version>
</parent>

<artifactId>test-test</artifactId>
<packaging>pom</packaging>

<name>test project</name>
<description>Root POM</description>
</project>

0 comments on commit 5d4a267

Please sign in to comment.