4

I'm trying to piece together a Release Gate / ServerGate build task calling a webservice, grabbing the Json and then deciding whether to proceed.

The Json returned by the service looks like this:

{
    "publisher": {
        "publisherId": "c68591c6-8fbd-413b-b7fb-b921737f4f9f",
        "publisherName": "jessehouwing",
        "displayName": "Jesse Houwing",
        "flags": "verified"
    },
    "extensionId": "252ad2b4-a2c5-43fc-bba5-17e631896091",
    "extensionName": "vsts-snyk",
    "displayName": "Snyk Task",
    "flags": "validated, public",
    "lastUpdated": "2018-03-16T18:58:36.327Z",
    "publishedDate": "2016-11-16T21:35:28.387Z",
    "releaseDate": "2016-11-16T21:35:28.387Z",
    "shortDescription": "Snyk continuously finds and fixes vulnerabilities in your dependencies.",
    "versions": [
        {
            "version": "1.1.12",
            "flags": "validated",
            "lastUpdated": "2018-03-16T18:58:53.133Z"
        },
        {
            "version": "1.1.11",
            "flags": "validated",
            "lastUpdated": "2018-02-18T13:53:47.83Z"
        }
    ],
    "deploymentType": 0
}

I'm calling the service based on the HttpHandler execution handler in the task.json:

  "execution": {
    "HttpRequest": {
      "Execute": {
        "EndpointId": "$(connectedServiceName)",
        "EndpointUrl": "$(endpoint.url)_apis/gallery/publishers/$(publisherId)/extensions/$(extensionId)$(extensionTag)?flags=1",
        "Method": "GET",
        "Body": "",
        "Headers": "{\"Content-Type\":\"application/json\"}",
        "WaitForCompletion": "false",
        "Expression": "eq(jsonpath('$.versions[?(@.version==$(extensionVersion))].flags'), 'validated')"
      }
    }
  }

There is very little documentation on the Expression syntax and on the supported jsonpath syntax. I've stolen most of the examples from:

I tried a number of different Expressions, none of them have worked so far:

eq(jsonpath('$.versions[?(@.version==$(extensionVersion))].flags'), 'validated')

eq(count(jsonpath('$.versions[?(@.version==$(extensionVersion)), ?(@.flags==validated)]')), 1)

eq(count(jsonpath('$.versions[?(@.version==$(extensionVersion)) && ?(@.flags==validated)]')), 1)

I've tried a number of different things with double escaping ' or mixing " and '.

Unfortunately, the server isn't giving me a lot of information other than Expressions failed, thus rather useless.

The log of execution looks like:

2018-04-01T13:34:51.2938460Z GET https://marketplace.visualstudio.com/_apis/gallery/publishers/jessehouwing/extensions/vsts-snyk?flags=1
                Response Code: OK
                Response: {"publisher":{"publisherId":"c68591c6-8fbd-413b-b7fb-b921737f4f9f","publisherName":"jessehouwing","displayName":"Jesse Houwing","flags":"verified"},"extensionId":"252ad2b4-a2c5-43fc-bba5-17e631896091","extensionName":"vsts-snyk","displayName":"Snyk Task","flags":"validated, public","lastUpdated":"2018-03-16T18:58:36.327Z","publishedDate":"2016-11-16T21:35:28.387Z","releaseDate":"2016-11-16T21:35:28.387Z","shortDescription":"Snyk continuously finds and fixes vulnerabilities in your dependencies.","versions":[{"version":"1.1.12","flags":"validated","lastUpdated":"2018-03-16T18:58:53.133Z"},{"version":"1.1.11","flags":"validated","lastUpdated":"2018-02-18T13:53:47.83Z"},{"version":"1.1.9","flags":"validated","lastUpdated":"2017-03-14T09:40:40.75Z"},{"version":"1.1.8","flags":"validated","lastUpdated":"2017-03-13T19:50:57.603Z"},{"version":"1.1.7","flags":"validated","lastUpdated":"2017-03-13T19:45:29.647Z"},{"version":"1.1.6","flags":"validated","lastUpdated":"2017-02-21T10:58:40.617Z"},{"version":"1.1.3","flags":"validated","lastUpdated":"2017-02-21T10:46:02.86Z"},{"version":"1.1.2","flags":"validated","lastUpdated":"2017-02-21T10:42:05.503Z"},{"version":"1.0.21","flags":"validated","lastUpdated":"2017-01-12T20:54:40.587Z"},{"version":"1.0.20","flags":"validated","lastUpdated":"2017-01-12T20:19:58.767Z"},{"version":"1.0.19","flags":"validated","lastUpdated":"2017-01-12T20:14:43.677Z"},{"version":"1.0.17","flags":"validated","lastUpdated":"2017-01-04T18:49:51.233Z"},{"version":"1.0.16","flags":"validated","lastUpdated":"2017-01-04T10:27:29.48Z"},{"version":"1.0.5","flags":"validated","lastUpdated":"2016-12-15T12:00:16.027Z"},{"version":"1.0.4","flags":"validated","lastUpdated":"2016-12-02T20:03:02.753Z"},{"version":"1.0.3","flags":"validated","lastUpdated":"2016-12-01T18:56:42.703Z"},{"version":"1.0.1","flags":"validated","lastUpdated":"2016-11-29T20:25:24.887Z"},{"version":"0.9.17","flags":"validated","lastUpdated":"2016-11-29T18:41:15.72Z"},{"version":"0.9.16","flags":"validated","lastUpdated":"2016-11-29T16:07:54.787Z"},{"version":"0.9.13","flags":"validated","lastUpdated":"2016-11-29T15:22:14.327Z"},{"version":"0.9.12","flags":"validated","lastUpdated":"2016-11-26T13:38:01.453Z"},{"version":"0.9.9","flags":"validated","lastUpdated":"2016-11-21T23:03:30.797Z"},{"version":"0.9.7","flags":"validated","lastUpdated":"2016-11-20T13:20:51.503Z"},{"version":"0.9.3","flags":"validated","lastUpdated":"2016-11-19T13:38:40.32Z"},{"version":"0.9.2","flags":"validated","lastUpdated":"2016-11-16T21:35:36.59Z"}],"deploymentType":0}
                Evaluation of expression 'eq(count(jsonpath('$.versions[?(@.version==''1.1.12''),?(@.flags==''validated'')]')), 1)' failed.

Explicitly causign errors in the expression gives me an error message, so the expression at least seems to parse just fine.

In order to test without having to first create a whole task, you can create an Invoke REST API Release Gate as follows:

enter image description here

And use the following Generic Endpoint:

1
  • It seems that it can't compare the string value, to compare the number, you don't need to specify quotes. This sample works fine (github.com/Microsoft/vsts-tasks/blob/…) {"Items":[{"name":"item1","price":100},{"name":"item2","price":40},{"name":"item3","price":70}]} the express is eq(count(jsonpath('Items[?(@.price<50)]')), 1) Commented Apr 2, 2018 at 8:20

1 Answer 1

9

I had to resolve to building my own Expression test Tool (will publish later on my blog) in order to quickly iterate on the different expressions and get better error messages.

The following expression works:

eq(count(jsonpath('$.versions[?(@.version==''1.1.12'' && @.flags==''validated'')]')), 1)

You can also extract the value from an expression and then compare it using:

eq(jsonpath('$.versions[?(@.version==''1.1.12'')].flags')[0], 'validated')

Note the [0] that's tagged onto the result of the jsonpath.

Single Quotes ' need to be double escaped '' and the way to combine expressions is within the ?(a && b) block.

At the same time, I found that the expressions use the NewtonSoft.Json library to parse expressions, so their expression syntax is the one to use. Unfortunately, their docs aren't any better. I tried a couple of online jsonpath test tools, but those rely on a Javascript library which isn't as strict as the Newtonsoft implementation.

Sneak-peek of the local validation tool: enter image description here

You can find the test tool here. Due to dependencies on the TFS server object model, it only works when TFS 2018 update 2 or later is installed (doesn't have to be configured or to be running on your system):

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.