Skip to content

Instantly share code, notes, and snippets.

@andreadellacorte
Created June 5, 2020 16:23
Show Gist options
  • Save andreadellacorte/8252b061338075bb285994d0deb8f71c to your computer and use it in GitHub Desktop.
Save andreadellacorte/8252b061338075bb285994d0deb8f71c to your computer and use it in GitHub Desktop.
Paginated JIRA query in Ruby
require 'json'
require 'csv'
### This is a function definition; functions are the same kind of thing you
### use in excel, like VLOOKUP - you give them a variable and they do something
### or return some variable; this one is simple, you give it a URL and it
### returns a command to be run in a shell
def make_query(url)
### This makes the request to the url you've requested; think about it as a
### browser window that does things for you and returns you data / html / etc
### Note: the parameters of this curl request are missing; the easiest way
### to get these is to use the chrome "Copy as CURL" feature
return %{curl '#{url}'}
end
### This is another function definition; Jira only returns you stuff in chunks
### of 50; so you have to do a series of requests, one after another, and
### continually accumulate the result into one variable; this function does just
### that
def paginated_query(uri, params="", accumulator=[])
### This line makes a request to the JIRA Rest API and puts it in a data
### structure that can be access later, called "results"
results = JSON.parse(`#{make_query(uri + params)}`)
### Accumulator is the variable I mentioned above where we keep shoving items
### into; here we do just that, we add whatever the rest API returned to it
accumulator += results["issues"]
### Below is a branch; it tells us whether we do what's on the next line or
### not; in this particular case, we're checking if the accumulator includes
### all items in the filter provided or not
if(accumulator.length < results["total"])
### This is a bit tricky; this makes another request to the same function
### we're in, but passes some extra variables so that JIRA returns us items
### that are further along in the stack, and keeps accumulating them
accumulator = paginated_query(uri, "&startAt=#{results["startAt"] + results["maxResults"]}", accumulator)
end
### This sends the accumulator we've been creating out of the function we've
### called
return accumulator
end
### The part above is just preparation; this is where we actually call the
### function and take all the items returned from JIRA and put them in a
### variable called results
results = paginated_query("https://jira-website.here/rest/api/3/search?jql=filter=32434")
### Here it's actually quite easy, we use the CSV library to start editing
### a CSV file called "output.csv"
CSV.open("./output.csv", "wb") do |csv|
### We put in the "output.csv file" a summary and story points header
csv << ["summary", "story points"]
### We then go through all the issues return from JIRA and put their summary
### and story points into the csv one at the time
results.each_with_index do |issue, index|
csv << [issue["fields"]["summary"], issue["fields"]["customfield_10002"]]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment