jenkinsci / groovy-events-listener-plugin

A Jenkins plugin, which executes groovy code when an event occurs.
https://plugins.jenkins.io/groovy-events-listener-plugin/
MIT License
36 stars 34 forks source link

Please Support GraphListener for FlowExecution Events #41

Closed solvingj closed 5 years ago

solvingj commented 5 years ago

I believe these are the events that execute during the various stages/steps of pipelines.

https://javadoc.jenkins.io/plugin/workflow-api/org/jenkinsci/plugins/workflow/flow/GraphListener.html

We're trying to experiment with them using this plugin and it's not natively supported. Seems like it might be easy to implement.

markjacksonfishing commented 5 years ago

Sorry for the delay here, post-grad studies have been keeping me busy. Will get this out tomorrow @solvingj

markjacksonfishing commented 5 years ago

@solvingj I tried to implement this and it really was not ideal. I tried:

package org.jenkinsci.plugins.globalEventsPlugin;

import jenkins.model.Jenkins;
import hudson.model.Run;
import org.jenkinsci.plugins.workflow.flow.GraphListener;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import java.util.HashMap;
import java.util.logging.Logger;

/**
 * Warning: This MUST stay a Java class, Groovy cannot compile (for some reason??).
 */
public class GlobalWorkflowListener implements GraphListener {

    protected static Logger log = Logger.getLogger(GlobalWorkflowListener.class.getName());
    private final Run run;

    /**
     * This class is lazy loaded (as required).
     */
    public GlobalWorkflowListener(Run r) {
        this.run = r;
        log.fine(">>> Initialised");
    }

    GlobalEventsPlugin.DescriptorImpl parentPluginDescriptorOverride = null;

    GlobalEventsPlugin.DescriptorImpl getParentPluginDescriptor() {
        if (parentPluginDescriptorOverride != null){
            return parentPluginDescriptorOverride;
        } else {
            return Jenkins.getInstance().getPlugin(GlobalEventsPlugin.class).getDescriptor();
        }
    }

    @Override
    public void onNewHead(final FlowNode node) {
       this.getParentPluginDescriptor().processEvent(Event.WORKFLOW_NEW_HEAD, log, new HashMap<Object, Object>() {{
            put("flowNode", node);
            put("run", run);
        }});
    }
}

Basically, Groovy Event Listener supports pipeline jobs. The problem is that each job will be considered as one big chunk :) What are you looking for? I do not think adding this is going to get you what you need

solvingj commented 5 years ago

I think you are right, so don't worry about it too much.

Over the past three months, I dug much more deeply into other plugin code and then the Jenkins API's. The situation surrounding API's that apply to declarative pipelines is extremely challenging right now because the new model is so vastly different from the old model. I've found some tickets in various places where the issue is known and well understood, and a few people who have gone to extreme lengths to do something that used to be trivial, and even then it wasn't ideal. Sadly, it doesn't look like there's any Jenkins core resources able to work on adding API's on top of the current declarative pipeline API's, in an effort to make it feasible to port many of these old plugins to support declarative pipelines. The particular area of difficulty/impass relevant to me at the moment is the class of events related to upstream/downstream job graph.

Anyway, thanks for looking. You can close if you think it's too complex to support.