正在解析 Release Gate 或 ServerGate 任务的 Json 个成功条件

Parsing Json success condition on Release Gate or ServerGate task

我正在尝试拼凑一个调用 Web 服务的 Release Gate / ServerGate 构建任务,获取 Json 然后决定是否继续。

服务返回的 Json 如下所示:

{
    "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
}

我正在调用基于 task.json 中的 HttpHandler 执行处理程序的服务:

  "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')"
      }
    }
  }

关于表达式语法和支持的 jsonpath 语法的文档非常少。我从以下位置窃取了大部分示例:

我尝试了多种不同的表达方式,none 目前为止有效:

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)

我尝试了很多不同的东西,包括双重转义 ' 或混合 "'

不幸的是,除了表达式失败之外,服务器没有给我很多信息,因此毫无用处。

执行日志如下所示:

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.

表达式中的显式因果错误会给我一条错误消息,因此表达式至少看起来解析得很好。

为了在不必先创建整个任务的情况下进行测试,您可以创建一个 Invoke REST API Release Gate,如下所示:

并使用以下通用端点:

我不得不下定决心构建自己的表达式测试工具(稍后将在我的博客上发布),以便快速迭代不同的表达式并获得更好的错误消息。

以下表达式有效:

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

您还可以从表达式中提取值,然后使用以下方法进行比较:

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

注意标记到 jsonpath 结果的 [0]

单引号'需要双转义''表达式组合方式在?(a && b)块内

同时,我发现表达式使用NewtonSoft.Json库来解析表达式,所以他们的表达式语法就是这样。 Unfortunately, their docs aren't any better。我尝试了几个在线 jsonpath 测试工具,但这些工具依赖于 Javascript 库,它不像 Newtonsoft 实现那样严格。

本地验证工具的

Sneak-peek:

您可以在这里找到测试工具。由于依赖于 TFS 服务器对象模型,它仅在安装 TFS 2018 update 2 或更高版本时有效(不必配置或在您的系统上 运行):