From db7fb0275d9ac32539346db96380388bf7bc8515 Mon Sep 17 00:00:00 2001 From: utarwyn Date: Sun, 19 May 2024 19:59:17 +0200 Subject: [PATCH] Add GitHub action to export rules --- .github/workflows/export_rules.yml | 60 +++++++++++++++++++ tools/rule-exporter/pom.xml | 32 ++++++++++ .../tools/exporter/infra/RuleReader.java | 15 ++--- .../tools/exporter/infra/RuleReaderTest.java | 9 ++- 4 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/export_rules.yml diff --git a/.github/workflows/export_rules.yml b/.github/workflows/export_rules.yml new file mode 100644 index 00000000..f9c530ab --- /dev/null +++ b/.github/workflows/export_rules.yml @@ -0,0 +1,60 @@ +name: Export rules + +on: + push: + branches: + - main + paths-ignore: + - '*.md' + - '.github/**/*.yml' + tags: + - '[0-9]+.[0-9]+.[0-9]+' + +jobs: + export: + name: Export + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up JDK 11 + uses: actions/setup-java@v4 + with: + distribution: 'temurin' + java-version: 11 + cache: 'maven' + + - name: Build + run: mvn -e -B install + + - name: Retrieve current version + id: version + run: | + echo "version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_OUTPUT + + - name: Export + run: | + mkdir -p dist && \ + java -jar ./tools/rule-exporter/target/rule-exporter-${{ steps.version.outputs.version }}.jar \ + ./ecocode-rules-specifications/target/ecocode-rules-specifications-${{ steps.version.outputs.version }}.jar \ + ./dist/rules.json + + - name: Archive artifacts + uses: actions/upload-pages-artifact@v3 + with: + path: ./dist + + deploy: + needs: export + permissions: + pages: write # to deploy to Pages + id-token: write # to verify the deployment originates from an appropriate source + environment: + name: rules + url: ${{ steps.deployment.outputs.page_url }} + runs-on: ubuntu-latest + steps: + - name: Deploy to GitHub Pages + id: deployment + uses: actions/deploy-pages@v4 diff --git a/tools/rule-exporter/pom.xml b/tools/rule-exporter/pom.xml index 72fdfc6c..1a73fac5 100644 --- a/tools/rule-exporter/pom.xml +++ b/tools/rule-exporter/pom.xml @@ -7,6 +7,7 @@ io.ecocode ecocode-parent 1.5.4-SNAPSHOT + ../../pom.xml rule-exporter @@ -65,6 +66,37 @@ maven-compiler-plugin 3.11.0 + + org.apache.maven.plugins + maven-jar-plugin + 3.3.0 + + + + true + io.ecocode.tools.exporter.RuleExporter + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.5.0 + + + package + + shade + + + false + true + false + + + + maven-surefire-plugin 3.1.2 diff --git a/tools/rule-exporter/src/main/java/io/ecocode/tools/exporter/infra/RuleReader.java b/tools/rule-exporter/src/main/java/io/ecocode/tools/exporter/infra/RuleReader.java index c87642d4..baa416b6 100644 --- a/tools/rule-exporter/src/main/java/io/ecocode/tools/exporter/infra/RuleReader.java +++ b/tools/rule-exporter/src/main/java/io/ecocode/tools/exporter/infra/RuleReader.java @@ -17,19 +17,20 @@ */ package io.ecocode.tools.exporter.infra; -import io.ecocode.tools.exporter.RuleExporter; import io.ecocode.tools.exporter.domain.RuleMetadata; import jakarta.json.Json; import jakarta.json.JsonObject; import jakarta.json.JsonReader; +import java.io.File; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collection; import java.util.HashMap; import java.util.Map; -import java.util.Objects; import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -59,8 +60,9 @@ public Collection readRules() throws IOException { private Map readAllMetadataFromZip() throws IOException { Map metadataMap = new HashMap<>(); + Path path = new File(zipFilename).toPath(); try ( - InputStream inputStream = getZipInputStream(); + InputStream inputStream = Files.newInputStream(path); ZipInputStream zipIn = new ZipInputStream(inputStream) ) { ZipEntry entry; @@ -77,13 +79,6 @@ private Map readAllMetadataFromZip() throws IOException { return metadataMap; } - private InputStream getZipInputStream() { - return Objects.requireNonNull( - RuleExporter.class.getResourceAsStream("/" + this.zipFilename), - this.zipFilename + " not found" - ); - } - private JsonObject readContents(InputStream contentsIn) { try (JsonReader reader = Json.createReader(contentsIn)) { return reader.readObject(); diff --git a/tools/rule-exporter/src/test/java/io/ecocode/tools/exporter/infra/RuleReaderTest.java b/tools/rule-exporter/src/test/java/io/ecocode/tools/exporter/infra/RuleReaderTest.java index bc8ee304..9c29e9c5 100644 --- a/tools/rule-exporter/src/test/java/io/ecocode/tools/exporter/infra/RuleReaderTest.java +++ b/tools/rule-exporter/src/test/java/io/ecocode/tools/exporter/infra/RuleReaderTest.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.util.Collection; +import java.util.Objects; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; @@ -33,7 +34,7 @@ public class RuleReaderTest { @Test void validRules() throws IOException { - RuleReader ruleReader = new RuleReader("rules.jar"); + RuleReader ruleReader = new RuleReader(getFilePath("rules.jar")); Collection rules = ruleReader.readRules(); // check rules count @@ -52,7 +53,7 @@ void validRules() throws IOException { @Test void invalidRules() throws IOException { - RuleReader ruleReader = new RuleReader("invalid-rules.jar"); + RuleReader ruleReader = new RuleReader(getFilePath("invalid-rules.jar")); try { ruleReader.readRules(); fail("Exception not thrown"); @@ -61,4 +62,8 @@ void invalidRules() throws IOException { } } + private String getFilePath(String fileName) { + return Objects.requireNonNull(getClass().getResource("/" + fileName)).getPath(); + } + }