将存储/位桶数据导出为 XML 或 JSON



有没有方便的方法可以从 Java API 中将数据从 stash/bitbucket 导出为 XML 或 JSON? 我专门查看拉取请求,并希望将它们流式传输到 stdout 上,以便由外部脚本处理。

不确定您需要什么,但有几个选项。

Bitbucket REST API

从Java中,您可以轻松地使用Stash/Bitbucket REST API来检索信息。这通常会输出 json。

例如,您可以使用此资源搜索拉取请求:

/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/pull-requests

它返回如下输出:

{
"size": 1,
"limit": 25,
"isLastPage": true,
"values": [
    {
        "id": 101,
        "version": 1,
        "title": "Talking Nerdy",
        "description": "It’s a kludge, but put the tuple from the database in the cache.",
        "state": "OPEN",
        "open": true,
        "closed": false,
        "createdDate": 1359075920,
        "updatedDate": 1359085920,
        "fromRef": {
            "id": "refs/heads/feature-ABC-123",
            "repository": {
                "slug": "my-repo",
                "name": null,
                "project": {
                    "key": "PRJ"
                }
            }
        },
        "toRef": {
            "id": "refs/heads/master",
            "repository": {
                "slug": "my-repo",
                "name": null,
                "project": {
                    "key": "PRJ"
                }
            }
        },
        "locked": false,
        "author": {
            "user": {
                "name": "tom",
                "emailAddress": "tom@example.com",
                "id": 115026,
                "displayName": "Tom",
                "active": true,
                "slug": "tom",
                "type": "NORMAL"
            },
            "role": "AUTHOR",
            "approved": true,
            "status": "APPROVED"
        },
        "reviewers": [
            {
                "user": {
                    "name": "jcitizen",
                    "emailAddress": "jane@example.com",
                    "id": 101,
                    "displayName": "Jane Citizen",
                    "active": true,
                    "slug": "jcitizen",
                    "type": "NORMAL"
                },
                "role": "REVIEWER",
                "approved": true,
                "status": "APPROVED"
            }
        ],
        "participants": [
            {
                "user": {
                    "name": "dick",
                    "emailAddress": "dick@example.com",
                    "id": 3083181,
                    "displayName": "Dick",
                    "active": true,
                    "slug": "dick",
                    "type": "NORMAL"
                },
                "role": "PARTICIPANT",
                "approved": false,
                "status": "UNAPPROVED"
            },
            {
                "user": {
                    "name": "harry",
                    "emailAddress": "harry@example.com",
                    "id": 99049120,
                    "displayName": "Harry",
                    "active": true,
                    "slug": "harry",
                    "type": "NORMAL"
                },
                "role": "PARTICIPANT",
                "approved": true,
                "status": "APPROVED"
            }
        ],
        "links": {
            "self": [
                {
                    "href": "http://link/to/pullrequest"
                }
            ]
        }
    }
],
"start": 0
}

有关可用参数和其他资源的信息,请参阅文档。

位桶的脚本运行程序

如果您运行 Bitbucket

Server,您还可以查看"Bitbucket 的脚本运行器"附加组件。它提供了许多功能来使用Groovy脚本自定义Bitbucket,并且带有许多开箱即用的脚本。您还可以轻松添加脚本来响应"已创建拉取请求"等事件。有关详细信息,请参阅其文档。

您还可以从 Groovy 脚本中执行 REST 调用。

Groovy在处理json方面非常灵活,所以如果你想完全控制json的生成,你可以检查JsonSlurper和JsonOutput。

自定义爪哇

如果你有一个独立的Java应用程序,并且想要完全控制返回的json/xml,你可以使用GSON,JAXB和/或Jackson等库来完成所有这些操作。

希望这有帮助。

最新更新