I am writing a script that will query the github graphql API for open pull requests, and post the count and summary information about them to a slack channel.
I am able to query the github-graphql API and return the results. I am able to iterate over the results to read the properties.
I had thought that the key values would be unique, however I have found they are not (for example - there are multiple keys with a value of 'name').
I am stuck now, as I cannot work out how to extract the information I want to from the graphql api response. In a REST API I would be calling multiple APIs multiple times for the data, however I want to use the graphql API this time.
The graphql query is:
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 } }
}
}
}
}
}
}
}
}
}
}
The response is along the lines of:
{
"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
}
}
]
}
}
]
}
},
And the code I'd written thinking I would be able to use to get the data I wanted:
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]);
}
}
}
}
}
But this isn't going to work, and I cannot think of how to do this. Any pointers really appreciated.
I would like to create an array along the lines of:
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.
]