Skip to content

PART 2: Generate events using jsonschema2pojo and mustache template #60

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

Merged
merged 51 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
8b200ae
Changes to generate class file using template
Jun 26, 2023
f40e395
update CDEventsGenerator file
Jun 26, 2023
12fa8c2
update CDEventsGenerator file
Jun 26, 2023
a2673d1
Update phase for CDEventsGenerator main class
Jun 26, 2023
1aa585b
adding missing file to repo
Jun 26, 2023
a57c616
Update src/main/java/dev/cdevents/CDEventsGenerator.java
rjalander Jun 27, 2023
a9e7c90
Update src/main/java/dev/cdevents/CDEventsGenerator.java
rjalander Jun 27, 2023
2a66889
update with review comments and some linter issues
Jun 27, 2023
487705a
fix linter issues
Jun 27, 2023
30f3e7d
updating linter.yml
Jun 27, 2023
678e197
fix linter issues with MagicNumber
Jun 27, 2023
a8c7831
fix linter issues with MagicNumber
Jun 27, 2023
90f2e2a
lint exclude list in array path
Jun 27, 2023
20cd637
update FILTER_REGEX_EXCLUDE pattern
Jun 27, 2023
759d00c
update MaginNumners with constants
Jun 27, 2023
a5bb503
ignoreFieldDeclaration for MagicNumber check-style
Jun 27, 2023
e865d59
cleanup existing/manual events to keep only gererated events
Jun 29, 2023
3369ae9
Generate models and events from spec/schema repo
Jul 3, 2023
9865d62
Changes to generate class file using template
Jun 26, 2023
886070a
update CDEventsGenerator file
Jun 26, 2023
3653b32
update CDEventsGenerator file
Jun 26, 2023
7366ba2
Update phase for CDEventsGenerator main class
Jun 26, 2023
fcb0514
adding missing file to repo
Jun 26, 2023
8dc440a
Update src/main/java/dev/cdevents/CDEventsGenerator.java
rjalander Jun 27, 2023
8bf6783
Update src/main/java/dev/cdevents/CDEventsGenerator.java
rjalander Jun 27, 2023
8715628
update with review comments and some linter issues
Jun 27, 2023
8e837e6
fix linter issues
Jun 27, 2023
fb70e74
updating linter.yml
Jun 27, 2023
a594c13
fix linter issues with MagicNumber
Jun 27, 2023
04151e7
fix linter issues with MagicNumber
Jun 27, 2023
4e1c5ed
lint exclude list in array path
Jun 27, 2023
68f666b
update FILTER_REGEX_EXCLUDE pattern
Jun 27, 2023
30c6654
update MaginNumners with constants
Jun 27, 2023
05e0dd3
ignoreFieldDeclaration for MagicNumber check-style
Jun 27, 2023
d78dfeb
cleanup existing/manual events to keep only gererated events
Jun 29, 2023
a2e9beb
Refacoring the code to use maven submodules for generator and sdk
Jul 12, 2023
81a7864
fix minor lint issue
Jul 12, 2023
ea383aa
updating release files as per comment
Jul 13, 2023
77f84e4
Changes to generate class file using template
Jun 26, 2023
52992b5
Merge branch 'gen_sdk_v0_1_2' of github.com:Nordix/sdk-java into gen_…
Jul 13, 2023
9c10633
removing plugin definition from sdk/pom
Jul 14, 2023
8b4ec23
removing test dependencies
Jul 14, 2023
6571579
Merge branch 'main' into gen_sdk_v0_1_2
rjalander Jul 28, 2023
4080307
Update parent pom artifactId
Aug 8, 2023
7aa2e7e
Update sdk and generator pom artifact version
Aug 8, 2023
c12ab5a
merge from gen_sdk_v0_1_2 branch for the latest changes
Aug 30, 2023
65de572
adding test file to sdk
Sep 27, 2023
29feacf
removing old event files
Sep 28, 2023
fca78fb
merge from master branch with events generator changes
Oct 3, 2023
7eda387
fix linter issue
Oct 6, 2023
a9631b5
adding submodule in actions to checkout
Oct 6, 2023
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
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
uses: actions/checkout@v3
with:
fetch-depth: 0
submodules: 'true'

- name: Setup Java
uses: actions/setup-java@v3
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "spec"]
path = spec
url = git@github.com:cdevents/spec.git
503 changes: 495 additions & 8 deletions generator/pom.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public static void main(String[] args) {
}
String generatorBaseDir = args[0];
String sdkBaseDir = args[1];
String parentBaseDir = args[2];
String targetPackageDir = sdkBaseDir + File.separator + "src/main/java/dev/cdevents/events";
File folder = new File(sdkBaseDir + File.separator + RESOURCES_DIR + "schema");
System.out.println(folder.toPath().toAbsolutePath());
File folder = new File(parentBaseDir + File.separator + "spec" + File.separator + "schemas");
if (folder.isDirectory()) {
File[] files = folder.listFiles((dir, name) -> name.toLowerCase().endsWith(".json"));
if (files != null) {
Expand All @@ -68,7 +68,11 @@ public static void main(String[] args) {
SchemaData schemaData = buildCDEventDataFromJsonSchema(file);
generateClassFileFromSchemaData(mustache, schemaData, targetPackageDir);
}
} else {
log.error("No schema files found in the specified directory {}", folder.getAbsolutePath());
}
} else {
log.error("No schema directory found in the specified directory {}", folder.getAbsolutePath());
Comment on lines +71 to +75
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For automation purposes, it would be nice if the tool accumulated the various errors and failed at the end, rather than only logging errors.

}
}

Expand All @@ -94,21 +98,19 @@ private static SchemaData buildCDEventDataFromJsonSchema(File file) {
try {
JsonNode rootNode = objectMapper.readTree(file);
JsonNode contextNode = rootNode.get("properties").get("context").get("properties");
String schemaURL = rootNode.get("$id").asText();

String eventType = contextNode.get("type").get("enum").get(0).asText();
log.info("eventType: {}", eventType);
String[] type = eventType.split("\\.");
String subject = type[SUBJECT_INDEX];
String predicate = type[PREDICATE_INDEX];
String capitalizedSubject = StringUtils.capitalize(subject);
if (subject.equals("pipelinerun")) {
capitalizedSubject = capitalizedSubject.substring(0, SUBSTRING_PIPELINE_INDEX)
+ StringUtils.capitalize(subject.substring(SUBSTRING_PIPELINE_INDEX));
}
String capitalizedPredicate = StringUtils.capitalize(predicate);
String version = type[VERSION_INDEX];

//set the Schema JsonNode required values to schemaData
schemaData.setSchemaURL(schemaURL);
schemaData.setSubject(subject);
schemaData.setPredicate(predicate);
schemaData.setCapitalizedSubject(capitalizedSubject);
Expand Down
16 changes: 16 additions & 0 deletions generator/src/main/java/dev/cdevents/generator/SchemaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class SchemaData {
private String upperCaseSubject;
private String schemaFileName;

private String schemaURL;

private List<ContentField> contentFields;

private List<ContentObjectField> contentObjectFields;
Expand Down Expand Up @@ -153,6 +155,20 @@ public void setSchemaFileName(String schemaFileName) {
this.schemaFileName = schemaFileName;
}

/**
* @return schemaURL
*/
public String getSchemaURL() {
return schemaURL;
}

/**
* @param schemaURL
*/
public void setSchemaURL(String schemaURL) {
this.schemaURL = schemaURL;
}

/**
* @return the Content fields of an event
*/
Expand Down
5 changes: 2 additions & 3 deletions generator/src/main/resources/template/event-template.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import java.nio.file.Files;
import java.nio.file.Paths;


public class {{capitalizedSubject}}{{capitalizedPredicate}}CDEvent extends {{capitalizedSubject}}{{capitalizedPredicate}}Event implements CDEvent {
public class {{capitalizedSubject}}{{capitalizedPredicate}}CDEvent extends {{capitalizedSubject}}{{predicate}} implements CDEvent {


/**
Expand Down Expand Up @@ -93,8 +93,7 @@ public class {{capitalizedSubject}}{{capitalizedPredicate}}CDEvent extends {{cap

@Override
public String schemaURL() {
String eventSchemaName = "{{schemaFileName}}".substring(0, "{{schemaFileName}}".lastIndexOf(".json"));
return String.format("https://cdevents.dev/%s/schema/%s", CDEventConstants.CDEVENTS_SPEC_VERSION, eventSchemaName);
return "{{schemaURL}}";
}


Expand Down
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>publication</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ private CDEventConstants() {
/**
* Event JsonSchema files location.
*/
public static final String SCHEMA_FOLDER = "src/main/resources/schema";
public static final String SCHEMA_FOLDER = "../spec/schemas";

/**
* Mustache generic event template file.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.nio.file.Paths;


public class ArtifactPackagedCDEvent extends ArtifactPackagedEvent implements CDEvent {
public class ArtifactPackagedCDEvent extends Artifactpackaged implements CDEvent {


/**
Expand Down Expand Up @@ -86,24 +86,23 @@ public String currentCDEventType() {


/**
* @return the artifact-packaged-event.json schema URL
* @return the artifactpackaged.json schema URL
*/

@Override
public String schemaURL() {
String eventSchemaName = "artifact-packaged-event.json".substring(0, "artifact-packaged-event.json".lastIndexOf(".json"));
return String.format("https://cdevents.dev/%s/schema/%s", CDEventConstants.CDEVENTS_SPEC_VERSION, eventSchemaName);
return "https://cdevents.dev/0.1.2/schema/artifact-packaged-event";
}


/**
* @return the artifact-packaged-event.json schema Json
* @return the artifactpackaged.json schema Json
*/

@Override
public String eventSchema() {
try {
return Files.readString(Paths.get(CDEventConstants.SCHEMA_FOLDER + "/artifact-packaged-event.json"));
return Files.readString(Paths.get(CDEventConstants.SCHEMA_FOLDER + "/artifactpackaged.json"));
} catch (IOException e) {
throw new CDEventsException("Exception while reading Event JsonSchema file ", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.nio.file.Paths;


public class ArtifactPublishedCDEvent extends ArtifactPublishedEvent implements CDEvent {
public class ArtifactPublishedCDEvent extends Artifactpublished implements CDEvent {


/**
Expand Down Expand Up @@ -85,24 +85,23 @@ public String currentCDEventType() {


/**
* @return the artifact-published-event.json schema URL
* @return the artifactpublished.json schema URL
*/

@Override
public String schemaURL() {
String eventSchemaName = "artifact-published-event.json".substring(0, "artifact-published-event.json".lastIndexOf(".json"));
return String.format("https://cdevents.dev/%s/schema/%s", CDEventConstants.CDEVENTS_SPEC_VERSION, eventSchemaName);
return "https://cdevents.dev/0.1.2/schema/artifact-published-event";
}


/**
* @return the artifact-published-event.json schema Json
* @return the artifactpublished.json schema Json
*/

@Override
public String eventSchema() {
try {
return Files.readString(Paths.get(CDEventConstants.SCHEMA_FOLDER + "/artifact-published-event.json"));
return Files.readString(Paths.get(CDEventConstants.SCHEMA_FOLDER + "/artifactpublished.json"));
} catch (IOException e) {
throw new CDEventsException("Exception while reading Event JsonSchema file ", e);
}
Expand Down
156 changes: 156 additions & 0 deletions sdk/src/main/java/dev/cdevents/events/BranchCreatedCDEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Code generated by dev.cdevents.generator.CDEventsGenerator. DO NOT EDIT.

/*
Copyright 2023 The CDEvents Authors

Licensed under the Apache License, Version 2.0 (the "License");
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.

SPDX-License-Identifier: Apache-2.0
*/

package dev.cdevents.events;


import dev.cdevents.constants.CDEventConstants;
import dev.cdevents.exception.CDEventsException;
import dev.cdevents.models.CDEvent;
import dev.cdevents.models.branch.created.*;

import java.net.URI;
import java.util.Date;
import java.util.UUID;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;


public class BranchCreatedCDEvent extends Branchcreated implements CDEvent {


/**
* Constructor to init CDEvent and set the Subject for {@link BranchCreatedCDEvent}.
*/

public BranchCreatedCDEvent() {
initCDEvent();
}


/**
* Initialize the CDEvent with the context values.
*/

@Override
public void initCDEvent() {
setContext(new Context());
setSubject(new Subject());
setCustomData(new Object());
setCustomDataContentType("application/json");
Context context = getContext();
context.setId(UUID.randomUUID().toString());
context.setTimestamp(new Date());
context.setVersion(CDEventConstants.CDEVENTS_SPEC_VERSION);
getSubject().setContent(new Content());
getSubject().getContent().setRepository(new Repository());
getSubject().setType(CDEventConstants.SubjectType.BRANCH.getSubjectType());
}

/**
* @return the event source
*/

@Override
public String eventSource() {
return getContext().getSource();
}


/**
* @return the current CDEvent type
*/

@Override
public String currentCDEventType() {
return getContext().getType().value();
}


/**
* @return the branchcreated.json schema URL
*/

@Override
public String schemaURL() {
return "https://cdevents.dev/0.1.2/schema/branch-created-event";
}


/**
* @return the branchcreated.json schema Json
*/

@Override
public String eventSchema() {
try {
return Files.readString(Paths.get(CDEventConstants.SCHEMA_FOLDER + "/branchcreated.json"));
} catch (IOException e) {
throw new CDEventsException("Exception while reading Event JsonSchema file ", e);
}
}


/**
* @param source
* Sets the {@link Context} source value
*/

public void setSource(URI source) {
getContext().setSource(source.toString());
}


/**
* @param subjectId
* sets the subject Id
*/

public void setSubjectId(String subjectId) {
getSubject().setId(subjectId);
}


/**
* @param subjectSource
* sets the subject source
*/

public void setSubjectSource(URI subjectSource) {
getSubject().setSource(subjectSource.toString());
}



/**
* @param id
*/
public void setSubjectRepositoryId(String id) {
getSubject().getContent().getRepository().setId(id);
}
/**
* @param source
*/
public void setSubjectRepositorySource(String source) {
getSubject().getContent().getRepository().setSource(source);
}

}
Loading