At least with the notification skeleton plugin the buildCause.material map
is not typed
<https://github.com/gocd-contrib/notification-skeleton-plugin/blob/master/src/main/java/com/example/notification/requests/StageStatusRequest.java#L69>,
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

   - updating your copy of
   
https://github.com/gocd-contrib/notification-skeleton-plugin/blob/master/src/main/java/com/example/notification/requests/StageStatusRequest.java#L69
   to public Map<String, Map<String, Object>> material (and see if the
   unmarshalling from JSON to objects work fine - it may not though)
   - if that doesn't work, and without going into Gson unmarshalling, as
   two steps like
   Map<String, Object> gitConfig = (Map<String, Object>)
   request.pipeline.buildCause.get(0).material.get("git-configuration");
   String gitUrl = (String) gitConfig.get("url");


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

On Sun, Feb 19, 2023 at 4:34 PM 'Alexey Savchkov' via go-cd <
[email protected]> wrote:

> 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":"[email protected]: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, [email protected]
> :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.
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/go-cd/9ee4f486-ba2b-4da1-b20a-418e3d7bd1een%40googlegroups.com
> <https://groups.google.com/d/msgid/go-cd/9ee4f486-ba2b-4da1-b20a-418e3d7bd1een%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/go-cd/CAA1RwH9xM4Tuqpuzi1ZmojXva2mSL2V34H1471KB-xBSygv7zQ%40mail.gmail.com.

Reply via email to