Skip to content

Commit

Permalink
Add GitHub action to export rules
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn committed Jul 18, 2024
1 parent 4af3932 commit db7fb02
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 12 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/export_rules.yml
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions tools/rule-exporter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<groupId>io.ecocode</groupId>
<artifactId>ecocode-parent</artifactId>
<version>1.5.4-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>rule-exporter</artifactId>
Expand Down Expand Up @@ -65,6 +66,37 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>io.ecocode.tools.exporter.RuleExporter</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>false</shadedArtifactAttached>
<minimizeJar>true</minimizeJar>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,8 +60,9 @@ public Collection<RuleMetadata> readRules() throws IOException {

private Map<String, JsonObject> readAllMetadataFromZip() throws IOException {
Map<String, JsonObject> 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;
Expand All @@ -77,13 +79,6 @@ private Map<String, JsonObject> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<RuleMetadata> rules = ruleReader.readRules();

// check rules count
Expand All @@ -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");
Expand All @@ -61,4 +62,8 @@ void invalidRules() throws IOException {
}
}

private String getFilePath(String fileName) {
return Objects.requireNonNull(getClass().getResource("/" + fileName)).getPath();
}

}

0 comments on commit db7fb02

Please sign in to comment.