2

我正在编写一个脚本,它将查询 github graphql API 以获取打开的拉取请求,并将有关它们的计数和摘要信息发布到松弛通道。

我能够查询 github-graphql API 并返回结果。我能够遍历结果以读取属性。

我原以为键值是唯一的,但我发现它们不是(例如 - 有多个键的值为“名称”)。

我现在被卡住了,因为我无法弄清楚如何从 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.
]
4

0 回答 0