Skip to content

SDK-Issues : Refactoring the code to render as CloudEvent and schema validation #35

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 17 commits into from
Apr 27, 2023

Conversation

rjalander
Copy link
Contributor

This PR has fixes for couple of Issues
#26
#29
#30
#31

This PR has the Refactoring code changes to create CDEvents using an object model,
Creating a smaller PR to have this for only one CDEvent - PipelineRunFinishedCDEvent and the similar pattern can be extended to all other events from the specification.

Copy link
Contributor

@afrittoli afrittoli left a comment

Choose a reason for hiding this comment

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

Thanks @rjalander!

I'm still reviewing but I thought I'd share my feedback this far.

Comment on lines 38 to 39
PipelineRunFinishedCDEvent pipelineRunFinishedCDEvent = new PipelineRunFinishedCDEvent();
return pipelineRunFinishedCDEvent;
Copy link
Contributor

Choose a reason for hiding this comment

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

return new PipelineRunFinishedCDEvent(); ?

try {
return objectMapper.writeValueAsString(cdEvent);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the benefit here of catching a JsonProcessingException and re-throwing it a RuntimeException?

Copy link
Contributor Author

@rjalander rjalander Apr 25, 2023

Choose a reason for hiding this comment

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

Apologies, It was autogenerated and in my TODO list to replace with the error log

*/
public static CloudEvent asCloudEvent(CDEvent cdEvent) {

String cdEventJson = cdEventToJson(cdEvent);
Copy link
Contributor

Choose a reason for hiding this comment

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

This may throw exceptions, shouldn't they be caught or declared in the method signature?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now it will be catching in cdEventAsJson() itself, handling empty String error check before proceeding here..

Comment on lines 55 to 87
PipelineRunStartedEvent("dev.cdevents.pipelinerun.started." + VERSION),
PipelineRunStartedEvent("dev.cdevents.pipelinerun.started." + CDEVENTS_SPEC_VERSION),
Copy link
Contributor

Choose a reason for hiding this comment

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

The version here is wrong. Event types must include the event version, not the spec version - see https://cdevents.dev/docs/primer/#versioning

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess the enum here could define all the unversioned even types and the actual event version could be set in the event class instead.

Copy link
Contributor Author

@rjalander rjalander Apr 25, 2023

Choose a reason for hiding this comment

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

yes, looked at the CDEvents versioning. Keeping un-versioned types with the Enum and setting event version in the event class itself here.

one clarification on Deployment Type event versions, in the spec it is mentioned 0.1-draft but the go-sdk uses 0.1.0 which needs to be updated in the v0.1.0 spec ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, having the -draft in the event version was a bug in release v0.1 of the spec - it is fixed in patch release v0.1.2

Copy link
Contributor

Choose a reason for hiding this comment

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

Since then I automated the start and end of releases, so that hopefully we can avoid those kind of bugs in the future.

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps these classes could be generated with something like https://github.com/joelittlejohn/jsonschema2pojo?
It's also fine to define them by hand for now, but it's quite some work, it's error-prone and hard to maintain.

For the go-sdk I initially used a bash script to generate the skeleton for all even types.
Now I switched to using go templates so that the code is generated at dev time using go.

* @return the pipeline-run-finished-event schema Json
*/
@Override
public String eventSchema() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Very nice, thank you!
In future, it would be great to be able to produce this automatically from the schemas in the spec repo.

Copy link
Contributor

@afrittoli afrittoli left a comment

Choose a reason for hiding this comment

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

Thanks @rjalander - this looks good as a first step.

We'll need to implement all other events after this.
It's good that the new test runs validation, that gives us more confidence on the correctness of the SDK.

@afrittoli
Copy link
Contributor

@rjalander it looks like this needs to be rebased

@rjalander
Copy link
Contributor Author

@rjalander it looks like this needs to be rebased

I think If we do Squash and merge, all commits will be treated as single commit and merged right.

@cdevents-bot
Copy link
Collaborator

🎉 This issue has been resolved in v0.1.2 (Release Notes)

@cdevents-bot cdevents-bot added the released Issue has been released label Jul 24, 2023
@cdevents-bot cdevents-bot added this to the v0.1.2 milestone Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released Issue has been released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants