How to write a Jenkins Plugin – Part 3

In this part we will find a way to count the number of failed jobs in a view and pass it back to our Jelly UI fragment.

Counting Failed Jobs In a View

First we’re going to modify our viewTabs.jelly to ask our plugin for the failed job count for each view. We’ll have to pass in the current view so that our plugin knows what we’re looking at and has something to work with. After the tab label text let’s add a call to a new method on our plugin that takes the current view and returns the failed job count, so we’ll have something that starts off like this:

    <j:forEach var="v" items="${views}">
        name="${v.viewName} - ${it.getTabLabel()}${it.getFailureCount(v)}"
        href="${rootURL}/${v.url}" />

OK, let’s add the corresponding method on our plugin:

public int getFailureCount(View v){
    return 0;

Redeploy and you’ll see the “0” at the end of the tab label.

Now for counting the failed jobs. This is where you’ll need to start looking through the Jenkins JavaDoc to see how it all hangs together and what you can get hold of. It’s hard to know where to start, but as before, the source code of existing plugins is a good bet.

Find plugin source code by choosing a plugin from the Jenkins wiki

and on the plugin page you’ll usually see a link to the source code, usually in GitHub, or just go straight to GitHub at

Javadoc can be found at

For example if we look at then we can see that a View has the method

abstract Collection getItems()
    Gets all the items in this collection in a read-only view.

This is our starting point. Next we can see that an item has a getAllJobs() method, and a job has a getIconColor(). Aha! That will do for now. Let’s just rattle through the list of items, and the list of jobs in each item, and count the number of jobs with a red icon colour:

public int getFailureCount(View v) {

    int failed = 0;

    for (TopLevelItem item : v.getItems()) {
        for (Job j : item.getAllJobs()) {
          switch (j.getIconColor()) {
            case RED:
            case BLUE:
            case YELLOW:
            case DISABLED:

    return failed;

Now, I’m not suggesting that you should write code like this, it’s just an example. You can see how you might extend this to count the other statuses.

Fire it up and you should see the failure count on the view tab:

with failure count

We’re now pulling information out of the Jenkins model and passing it out to our custom Jelly UI component for display. Easy as that.

However, it’s not so easy to see how to get hold of the last build status for a job without having to work backwards from the “icon colour”. There must be a more direct way, but I couldn’t see it. You also have to deal with things like nested views, and different types of “TopLevelItem” and so on. There’s a lot going on.

This example plugin isn’t doing anything very useful, but what we’ve done so far should give you a bit of a leg-up when it comes to getting your project off the ground and finding your way around the Jenkins model.

In the next segment we’ll look at getting your plugin hosted on the Jenkins GitHub, released for general consumption, and documented in the Jenkins wiki.

Posted in Jenkins, Jenkins Plugins

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: