我正在编写一个脚本,该脚本将查询github graphql API的打开请求请求,并将有关请求的计数和摘要信息发布到一个松弛通道。
我能够查询github-graphql API并返回结果。我可以遍历结果以读取属性。
我曾经以为键值是唯一的,但是我发现它们不是唯一的(例如-有多个值为'name'的键)。
我现在很困,因为我无法解决如何从graphql api响应中提取想要的信息。在REST API中,我将多次调用多个API来获取数据,但是这次我想使用graphql API。
graphql查询为:
query { organization(login: "MyOrganisation") {
repositories(first: 20, orderBy: {field: PUSHED_AT, direction: DESC}) {
nodes {
name
pullRequests(first: 10, states: OPEN, orderBy: {field: UPDATED_AT, direction: DESC}) {
nodes {
headRepository { nameWithOwner }
url
author {
... on User {
login name
}
}
mergeable
createdAt
baseRefName
headRefName
title
... on PullRequest {
pullRequestcommits: commits(last: 1) {
totalCount
nodes {
commit {
url
status { state contexts { context description createdAt targetUrl } }
}
}
}
}
}
}
}
}
}
}
响应如下:
{
"data": {
"organization": {
"repositories": {
"nodes": [
{
"name": "my-service-1",
"pullRequests": {
"nodes": []
}
},
{
"name": "my-service-2",
"pullRequests": {
"nodes": []
}
},
{
"name": "my-service-3",
"pullRequests": {
"nodes": []
}
},
{
"name": "my-service-4",
"pullRequests": {
"nodes": [
{
"headRepository": {
"nameWithOwner": "MyOrganisation/my-service-4"
},
"url": "https://www.githhub.com/MyOrganisation/my-service-4/pull/21",
"author": {
"login": "Bob1",
"name": "Bob"
},
"mergeable": "MERGEABLE",
"createdAt": "2019-08-20T15:24:52Z",
"baseRefName": "develop",
"headRefName": "feature/name-tags",
"title": "Added tags",
"pullRequestcommits": {
"totalCount": 1,
"nodes": [
{
"commit": {
"url": "https://www.githhub.com/MyOrganisation/my-service-4/commit/6bdda3a4adbc9bc7ea621556be1097bf0e618b3a",
"status": null
}
}
]
}
}
]
}
},
{
"name": "my-service-5",
"pullRequests": {
"nodes": [
{
"headRepository": {
"nameWithOwner": "MyOrganisation/my-service-5"
},
"url": "https://www.github.com/MyOrganisation/my-service-5",
"author": {
"login": "Anne2",
"name": "Anne"
},
"mergeable": "MERGEABLE",
"createdAt": "2019-08-27T08:18:52Z",
"baseRefName": "develop",
"headRefName": "feature/new-nodejs-upgrade",
"title": "Changed nodejs version to the latet version of 10 supported b…",
"pullRequestcommits": {
"totalCount": 1,
"nodes": [
{
"commit": {
"url": "https://www.github.com/MyOrganisation/my-service-5/commit/6a0694cfa86864c7bad509273536ef0506ad40ff",
"status": null
}
}
]
}
},
{
"headRepository": {
"nameWithOwner": "MyOrganisation/my-service-5r"
},
"url": "https://www.github.com/MyOrganisation/my-service-5/pull/72",
"author": {
"login": "Peter3",
"name": "Peter"
},
"mergeable": "MERGEABLE",
"createdAt": "2019-08-20T13:13:39Z",
"baseRefName": "develop",
"headRefName": "feature/Tagging-cloudformation-stacks",
"title": "Added tags to the change set, this will add tags to all objec…",
"pullRequestcommits": {
"totalCount": 2,
"nodes": [
{
"commit": {
"url": "https://www.github.com/MyOrganisation/my-service-5/commit/6f14a2d8157ee2efc5a869741df6683da1d6789f",
"status": null
}
}
]
}
}
]
}
},
我编写的代码认为可以用来获取所需数据:
function iterate(obj: any, stack: any) {
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
if (typeof obj[property] == "object") {
iterate(obj[property], stack + '.' + property);
} else {
if (property === 'name') {
console.log(property + " " + obj[property]);
}
}
}
}
}
但是这行不通,而且我想不起来该怎么做。任何指针都非常感激。
我想根据以下内容创建一个数组:
pullrequests [
[repo: my-service-1, openprs: 0],
[repo: my-service-2, openprs: 0],
[repo: my-service-3, openprs: 0],
[repo: my-service-4, openprs: 1, [createdAt: 2019-08-20T15:24:52Z, url: https://www.githhub.com/MyOrganisation/my-service-4/pull/21, author: Bob, headRefName: feature/name-tags, title: Added tags]]
etc.
]