【问题标题】:Jenkins REST API - using tree to reference specific item in JSON arrayJenkins REST API - 使用树来引用 JSON 数组中的特定项目
【发布时间】:2013-06-21 13:38:44
【问题描述】:

我可以使用 Jenkins API 通过 url 获取有关我的构建的信息

http://localhost:8080/job/myjob/149/api/json

我希望能够使用树查询字符串参数来查询 changeSet 节点。我可以通过

成功查询“持续时间”等非索引节点
http://localhost:8080/job/myjob/149/api/json?tree=duration

如何查询像 changeSet 这样的索引节点?我似乎在任何地方都找不到任何文档。

{
    "actions": [
        {
            "causes": [
                {
                    "shortDescription": "Started by an SCM change"
                }
            ]
        },
        {},
        {},
        {}
    ],
    "artifacts": [],
    "building": false,
    "description": null,
    "duration": 80326,
    "estimatedDuration": 68013,
    "executor": null,
    "fullDisplayName": "my project #149",
    "id": "2013-06-14_14-31-06",
    "keepLog": false,
    "number": 149,
    "result": "SUCCESS",
    "timestamp": 1371234666000,
    "url": "http://localhost:8080/job/my project/149/",
    "builtOn": "",
    "changeSet": {
        "items": [
            {
                "affectedPaths": [
                    "SearchViewController.m",
                    "Sample.strings"
                ],
                "author": {
                    "absoluteUrl": "http://localhost:8080/user/my user",
                    "fullName": "My User"
                },
                "commitId": "9032",
                "timestamp": 1371234304048,
                "date": "2013-06-14T18:25:04.048031Z",
                "msg": "Author:my_author Description: changes Id: B-186199 Reviewer:reviewer_name",
                "paths": [
                    {
                        "editType": "edit",
                        "file": "/branches/project_name/iOS/_MainLine/project_name/SearchViewController.m"
                    },
                                       ],
                "revision": 9032,
                "user": "user_name"
            }
        ],
        "kind": "svn",
        "revisions": [
            {
                "module": "repo_url",
                "revision": 8953
            },
            {
                "module": "repo_url",
                "revision": 9032
            }
        ]
    },
    "culprits": [
        {
            "absoluteUrl": "http://localhost:8080/user/username",
            "fullName": "username"
        }
    ]
}

【问题讨论】:

    标签: json rest jenkins


    【解决方案1】:

    API 文档有提示:

    一种较新的替代方法是树查询参数。 [snip] 你只需要知道你在寻找什么元素,而不是你不寻找什么(当插件可以贡献 API 元素时,这无论如何都是一个开放式列表)。该值应该是要包含的属性名称列表,方括号内包含子属性。

    对于一个简单的列表,获取整个子树:

    http://jenkins/job/myjob/../api/json?tree=artifacts[*]
    

    或在大括号内列出特定属性。

    对于changeSet,使用

    http://jenkins/job/myjob/../api/json?tree=changeSet[*[*]]
    

    检索所有内容。

    对特定的子子属性使用嵌套的方括号,例如:

    http://jenkins/job/myjob/../api/json?tree=changeSet[items[revision]]
    

    树文档说它适用于调用者不知道要检索哪些属性的情况。

    【讨论】:

    • 谢谢戴夫...我摆弄了一下,最终到达了那里...尽管 [*] 对我来说非常有用并且是全新的。你加载了哪个文档来找到这个?我一直在从 Jenkins 仪表板引用的文档中搜索,但似乎找不到它……但显然它很容易被忽略。
    • 请注意,curl 需要在双引号内使用反斜杠转义方括号。示例:$ curl -s -k "https://$jenkins/pluginManager/api/json?pretty=1&tree=plugins\[shortName,longName,version\]"
    猜你喜欢
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2021-08-12
    相关资源
    最近更新 更多