100 Days of Code – Day 4 – Groovy Pipelines and the Left Shift Operator <<

Much like yesterday’s post about injections, it took me a little bit to figure out what was going on with pipelines and the left shift << operator in Groovy.

In the end, it was simplest for me to say “X << Y basically means feed the data in Y into the function of X”.  Pipelines are the closure functions that X would represent.

 

Let’s look at a Jira-related example:

import com.atlassian.jira.component.ComponentAccessor

// Get the Jira issue manager
def issueManager = ComponentAccessor.getIssueManager()

// Get all the issues in a target project
def issues = issueManager.getIssueObjects(issueManager.getIssueIdsForProject(10100))

log.warn("This is the full list of issues: " + issues)
//Assume one of those issues, ZT-4, has a priority of "lowest"

//Define a new pipeline into which we'll feed the list of issue objects
def pipeline = {
 it.findAll { !(it.priority.name == "Lowest") }
}

// Apply the pipeline to the list of issues
def filteredAndSortedIssues = pipeline << issues

return filteredAndSortedIssues

 

So what are we doing?  First we’re grabbing a list of issues from a target project.  Then we’re defining a pipeline, which is simply a closure that performs an operation on an as-yet undefined set of data.

Finally, we’re invoking the pipeline and feeding it the list of issues using that left shift << operator.

The result should be a list of all of the issues that do not have a priority of “Lowest”.

 

It’s easy to see how this would reduce code reuse.  You’ve got a declared function, the pipeline, and you could feed it different arrays of issues. 

Of course, there are depths to this functionality that I’ve not yet begun to explore. But as far as a basic idea of what a pipeline and a left shift operator are up to,  this is the idea.

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

One response

  1. Matt Doar Avatar
    Matt Doar

    Interesting and clear. Would it be worth adding a link to the Groovy doc where pipelines are specified?

    I think this comment could be removed:
    //Assume one of those issues, ZT-4, has a priority of “lowest”