Groovy to generate dynamic parameters based on JSON of job values? - json

I just found that Jenkins jobs via a JSON URL each job has using this format: https://jenkinsurl.net/job/_test/lastBuild/api/json provides info like below.
{"_class":"hudson.model.FreeStyleBuild","actions":[{"_class":"hudson.model.ParametersAction","parameters":[{"_class":"hudson.model.StringParameterValue","name":"build_num","value":""}]},{"_class":"hudson.model.CauseAction","causes":[{"_class":"hudson.model.Cause$UserIdCause","shortDescription":"Started by user anonymous","userId":"anonymous","userName":"anonymous"}]},{},{},{},{},{},{"_class":"org.jenkinsci.plugins.buildenvironment.actions.BuildEnvironmentBuildAction"},{}],"artifacts":[],"building":false,"description":null,"displayName":"#1","duration":1850,"estimatedDuration":1850,"executor":null,"fullDisplayName":"_test #1","id":"1","keepLog":false,"number":1,"queueId":5753,"result":"SUCCESS","timestamp":1479700053274,"url":"http://jenkinsserver.net/job/_test/1/","builtOn":"node_name","changeSet":{"_class":"hudson.scm.EmptyChangeLogSet","items":[],"kind":null},"culprits":[]}
I'd like to know how I can query those fields with a Groovy script to help me build Dynamic parameters when selecting "Build with Parameters". I already have the Groovy and Dynamic parameter plugins and use them for some simple queries I've been using to generate some parameters, like timestamps not overwritten by Jenkins jobs downstream.
How do I query for these and return their values? For example, if I wanted to return the value of the user that started the build from this segment:
[{"_class":"hudson.model.Cause$UserIdCause","shortDescription":"Started by user anonymous","userId":"anonymous","userName":"anonymous"}]
This isn't a question about getting the person that triggered the build. Sorry if that's not such a great example, but this one appears to be a bit nested, so presumably a good one to learn with.
I know how to reference the value of parameters and get some info to return to generate them, but not how to use that URL and extract specific info from it to help create a parameter's value before the build begins. Is this possible? If not, what other mechanism could I use?

You can do something similar to this one:
How do I discover the additional causes of my Jenkins build?
job = hudson.model.Hudson.instance.getItem("demo-job")
build = job.getLastBuild()
// get action first
def action = build.getAction(hudson.model.CauseAction.class)
cause = action.findCause(hudson.model.Cause.UserIdCause)
println cause.userId
http://javadoc.jenkins-ci.org/hudson/model/Cause.UserIdCause.html
The question is abit confusing so not sure if this helps at all. Anyway, you should be able to extract all the information you need from build returned by getLastBuild().
It not possible to extra parameters before the build begins. If that is what you mean.

Related

Jenkins API xpath like functionality for JSON

I am trying to use the jenkins API to retrieve a list of running jobs buildURLs and this works with this the query
https://jenkins.server.com/computer/api/xml?tree=computer[executors[currentExecutable[url]]]&depth=1&xpath=//url&wrapper=buildUrls
By searching for all executors on given jenkins server and then grabbing the urls and wrapping them in a xml buildUrls object
(what I actually want is a count of total running jobs but I can filter in api then get a .size of this once client side)
However the application I am uses only accepts json, although I could convert to json I am wondering if there is a way I can have this return a json object which contains just the buildUrls. Currently if I change return to json the xpath modification is removed (since its xml only)
And I instead get returned the list of all executors and their status
I think my answer lies within Tree but i cant seem to get the result I want
To return a JSON object you can modify the URL as below
http://jenkins.server.com/computer/api/json?tree=computer[executors[currentExecutable[url]]]&depth=1&pretty=true
It will not be possible to get only the build urls you need to iterate through all the executables to get the total running jobs. The reason is that build urls alone cannot give you information where it is running. Hence, unfortunately we need to iterate through each executor.
The output you will get is something like below.
Another way of doing this same thing is parsing via jobs
http://jenkins.server.com/api/json?tree=jobs[name,url,lastBuild[building,timestamp]]&pretty=true
You can check for building parameter. But here too you will not be able to filter output from url diretly in jenkins. Iteration would be required through each and every job.

How to make Simple-JSON variable template query in grafana?

I am trying to create a dynamic variable for a graph using a kdb+ database, which requires the Simple-JSON plug in. However, I am struggling to figure out how to write the query in the Query search bar under Variables on my Dashboard.
I have looked into the documentation found here but there is only a few mentions of the templating feature for variables in grafana, but nothing on how to do it.
https://github.com/grafana/simple-json-datasource/blob/master/README.md
You can write arbitrary text in the timeserie query, it doesn't need to be one of the hardcoded metrics that /search returns.
There is also the simpod json datasource plugin.
https://grafana.com/grafana/plugins/simpod-json-datasource
built on top of the Simple JSON Datasource. It has refactored code, additional features and active development.
One of the "additional features" is an "Additional JSON Data" tab where you can use variables. {"key": $variableValue}
It actually turns out that the adaptor for kdb+/grafana needs it's search function overloaded to be able to accept variables and return a string (or something that grafana can understand).

JBPM rest calls with JSON

We want to start a process in JBPM6 using the rest API. We need to pass an object as a process variable.
We know how to do it JAXB and the execute call. But we want to do it with JSON and /runtime/{deploymentId}/process/{processDefId}/start
Is it possible? we try and have no success.
I am not sure whether my answer exactly addresses the question. But for someones use in future I place couple of lines here.
If you want to set a process variable when starting a process using the RESTful API, you can do it like this.
If your variable name is myVar just add the value as a URL parameter by appending the phrase "map_" to the parameter name. It means the parameter name should now be map_myVar. For an example see below request.
http://<host>:<port>/jbpm-console/rest/runtime/{deploymentId}/process/{processDefId}/start?map_myVar=myValue
You can confirm whether the value is set by writing below code in a script task.
Object var = kcontext.getVariable("myVar");
System.out.println("myVar : " + var);
See the 17.1.1.3. Map query parameters section of JBPM6 documentation.
After talking to the dev that is responsible for the REST API. I was able to confirm how it works.
The
/runtime/{deploymentId}/process/{processDefId}/start
Is a POST request where all the contents in the payload are ignored. The variables are written as key=value in the GET string.
With deployment id: com.web:work:1.0
With processDefId: work.worload
2 variables: var1 and var2
For example:
/runtime/com.web:work:1.0/process/work.worload/start?var1=a&var2=b
I'm still after trying to understand how to define objects with the remote API.
Meanwhile, I also confirmed that it is impossible to define objects using this way. The only way to define objects is only by using the JaxB. This uses the "/execute" path

Export entire Logical Data Model from GoodData

In this https://developer.gooddata.com/article/data-modeling-api there is a logical data model and its corresponding JSON. However, I can't seem to find out how to extract JSON from a logical data model via the REST API. Is there a way to do this other than using the single load interface (which would be very inefficient)?
For the record, my end goal is to make a tool that extracts that JSON (which would be in dev), then post that to the ldm manager2, and then apply the suggested changes through the returned MaQL to production. Any help is greatly appreciated
Currently this works only for Getting or Updating the entire Project. Anyway you can GET all model definition by simple API call. See the documentation:
http://docs.gooddatadrafts.apiary.io/
There is a GET request which is asynchronous. You can build some logic on the top of that on your end. You can get all models, store per datasets information, but at the end you need to POST the "final version" and all updates will be applied.
Let me know if I can help you with anything!
Regards,
JT

Filter entries using form

I have an ExpressionEngine site that I'm building with Bootstrap. It's a site for volunteers to find projects to help with. On the home page I have a modal with a form for them to select when they're available and what categories of jobs they're looking for. Then they can click submit and it'll go to a new page with filtered entries.
I don't know if this is possible using the GET method or POST method on the form. I've figured out how to use the GET method and get a query string into my URL but I don't know how to use that data to filter my entries on the entries page. Or would using POST and JSON be a better option? I don't know really how to implement either so any help would be great.
Thanks a lot!
It depends on how the information you would like to show is stored.
If you are using MySQL (a common RDMS), or any other form of SQL Database for that matter, the most common way is to send your GET query string (for example) to your server, have a sever-side language (such as PHP) handle that request by accessing your database, and then echo the result. This can be done synchronously, or with AJAX.
For example, the flow of everything might look like this:
User selects an option (say, "Gardening Projects").
JavaScript converts the value of that input option to a query string and sends an HTTP request using the GET method.
The destination of this request is "filter.php" (for example).
"filter.php" access your database using an SQL query, which searches for any entries in your database, say, having a tag of "gardening".
"filter.php" echos a statement with those entries (or, better yet, returns a JSON object)
JavaScript then parses the resultant JSON object into the DOM, which displays as a bunch of links in a result area that your user can click on.
The question you have about how to handle this is very broad, so I would recommend simply doing some Google searches or looking around this site for resources that show you how to set up databases, access those databases with possibly PHP/SQL, and maybe even use AJAX to return those results, etc.
To get you started (these are in no particular order):
AJAX Tutorial
PHP - JSON encode
SQL tutorial
jQuery AJAX
I got it figured out with some help from #JoshBeam. It turns out that I was trying to make it way more complicated than it actually is. Rookie mistake. In the end I just ended up using method=get in my form and the setting the action as the page with the filtered entries. I then used php to echo the inputs into my EE channel:entries tag.
One thing I still haven't figured out is how to make it so that my query string will combine values for matching names. Currently I have checkboxes for days of the week, each with name="day" and different values for each day. If there are multiple checked, my query string becomes example.com/?day=sun&day=mon when I'd rather have it as example.com/?day=sun&mon. So if anyone has any tips on that, I'd welcome them! I also wonder if there's a way to add pipes between each value when I echo them in my EE tag so that I can have multiples - e.g. {exp:channel:entries category="1|2|3"}. But I have really yet to Google either of these questions so I'll do that.
Thanks!