How to get the repository URL from the StageStatusRequest in the notification plugin?

24 views
Skip to first unread message

Alexey Savchkov

unread,
Feb 19, 2023, 3:33:59 AM2/19/23
to go-cd
I am trying to write a custom notification plugin based on https://github.com/gocd-contrib/notification-skeleton-plugin and I can't figure out how to get the repository URL from the StageStatusRequest request body. May I ask for a quick help?

The URL is located here:

{"pipeline":{"name":"notif-test","counter":"15","group":"My-group","build-cause":[{"material":{"git-configuration":{"shallow-clone":false,"branch":"main","url":"g...@my.git.server:mygroup/dummy.git"} ...

The type of request.pipeline.buildCause.get(0).material.get("git-configuration") is com.google.gson.internal.LinkedTreeMap and when I output its contents in the log  it's

{shallow-clone=false, branch=main, url=g...@my.git.server:mygroup/dummy.git}

but when I further try to get the URL string like this
request.pipeline.buildCause.get(0).material.get("git-configuration").get("url")
the code doesn't compile with:

/plugin/src/main/java/com/example/notification/executors/StageStatusRequestExecutor.java:71: error: cannot find symbol
LOG.info(String.format("DEBUG in StageStatusRequestExecutor sendNotification url=%s", request.pipeline.buildCause.get(0).material.get("git-configuration").get("url")));

  symbol:   method get(String)
  location: class Object

(the error indicator ^ points to the dot next to get("url")). How could I get the URL value from the map? Do I have to pass something else as the key to get() ? Not a Java man so may be missiong something very basic here.

Thanks a lot.

Chad Wilson

unread,
Feb 19, 2023, 4:01:39 AM2/19/23
to go...@googlegroups.com
At least with the notification skeleton plugin the buildCause.material map is not typed, so Java at compile time doesn't know what types of things are in it (will assume Object --> Object), which is a problem as a statically typed language.

You'll need to cast to the types that exist at runtime - or use the JSON-unmarshalling to try and do that, to turn a compile time problem into a (potential) runtime problem. You could either try

Might be good to keep in mind that if the trigger is not directly from a git material, but is triggered by an upstream pipeline, the content of the material will be different, and might be a material with "pipeline-configuration" map or a different material type.

-Chad

--
You received this message because you are subscribed to the Google Groups "go-cd" group.
To unsubscribe from this group and stop receiving emails from it, send an email to go-cd+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/go-cd/9ee4f486-ba2b-4da1-b20a-418e3d7bd1een%40googlegroups.com.

Alexey Savchkov

unread,
Mar 11, 2023, 8:49:31 AM3/11/23
to go-cd
Many thanks for the exact explanation. I've chosen the solution #2 (casting buildCause..material to Map<String, Object>) which subjectively seems more natural to me. With this the plugin is now working successfully for me,
воскресенье, 19 февраля 2023 г. в 16:01:39 UTC+7, Chad Wilson:
Reply all
Reply to author
Forward
0 new messages