· 6 years ago · Sep 02, 2019, 06:22 AM
1
2
3I 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.
4
5I am able to query the github-graphql API and return the results. I am able to iterate over the results to read the properties.
6
7I 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').
8
9I 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.
10
11The graphql query is:
12
13query { organization(login: "MyOrganisation") {
14 repositories(first: 20, orderBy: {field: PUSHED_AT, direction: DESC}) {
15 nodes {
16 name
17 pullRequests(first: 10, states: OPEN, orderBy: {field: UPDATED_AT, direction: DESC}) {
18 nodes {
19 headRepository { nameWithOwner }
20 url
21 author {
22 ... on User {
23 login name
24 }
25 }
26 mergeable
27 createdAt
28 baseRefName
29 headRefName
30 title
31 ... on PullRequest {
32 pullRequestcommits: commits(last: 1) {
33 totalCount
34 nodes {
35 commit {
36 url
37 status { state contexts { context description createdAt targetUrl } }
38 }
39 }
40 }
41 }
42 }
43 }
44 }
45 }
46 }
47 }
48
49The response is along the lines of:
50
51{
52 "data": {
53 "organization": {
54 "repositories": {
55 "nodes": [
56 {
57 "name": "my-service-1",
58 "pullRequests": {
59 "nodes": []
60 }
61 },
62 {
63 "name": "my-service-2",
64 "pullRequests": {
65 "nodes": []
66 }
67 },
68 {
69 "name": "my-service-3",
70 "pullRequests": {
71 "nodes": []
72 }
73 },
74 {
75 "name": "my-service-4",
76 "pullRequests": {
77 "nodes": [
78 {
79 "headRepository": {
80 "nameWithOwner": "MyOrganisation/my-service-4"
81 },
82 "url": "https://www.githhub.com/MyOrganisation/my-service-4/pull/21",
83 "author": {
84 "login": "Bob1",
85 "name": "Bob"
86 },
87 "mergeable": "MERGEABLE",
88 "createdAt": "2019-08-20T15:24:52Z",
89 "baseRefName": "develop",
90 "headRefName": "feature/name-tags",
91 "title": "Added tags",
92 "pullRequestcommits": {
93 "totalCount": 1,
94 "nodes": [
95 {
96 "commit": {
97 "url": "https://www.githhub.com/MyOrganisation/my-service-4/commit/6bdda3a4adbc9bc7ea621556be1097bf0e618b3a",
98 "status": null
99 }
100 }
101 ]
102 }
103 }
104 ]
105 }
106 },
107 {
108 "name": "my-service-5",
109 "pullRequests": {
110 "nodes": [
111 {
112 "headRepository": {
113 "nameWithOwner": "MyOrganisation/my-service-5"
114 },
115 "url": "https://www.github.com/MyOrganisation/my-service-5",
116 "author": {
117 "login": "Anne2",
118 "name": "Anne"
119 },
120 "mergeable": "MERGEABLE",
121 "createdAt": "2019-08-27T08:18:52Z",
122 "baseRefName": "develop",
123 "headRefName": "feature/new-nodejs-upgrade",
124 "title": "Changed nodejs version to the latet version of 10 supported b…",
125 "pullRequestcommits": {
126 "totalCount": 1,
127 "nodes": [
128 {
129 "commit": {
130 "url": "https://www.github.com/MyOrganisation/my-service-5/commit/6a0694cfa86864c7bad509273536ef0506ad40ff",
131 "status": null
132 }
133 }
134 ]
135 }
136 },
137 {
138 "headRepository": {
139 "nameWithOwner": "MyOrganisation/my-service-5r"
140 },
141 "url": "https://www.github.com/MyOrganisation/my-service-5/pull/72",
142 "author": {
143 "login": "Peter3",
144 "name": "Peter"
145 },
146 "mergeable": "MERGEABLE",
147 "createdAt": "2019-08-20T13:13:39Z",
148 "baseRefName": "develop",
149 "headRefName": "feature/Tagging-cloudformation-stacks",
150 "title": "Added tags to the change set, this will add tags to all objec…",
151 "pullRequestcommits": {
152 "totalCount": 2,
153 "nodes": [
154 {
155 "commit": {
156 "url": "https://www.github.com/MyOrganisation/my-service-5/commit/6f14a2d8157ee2efc5a869741df6683da1d6789f",
157 "status": null
158 }
159 }
160 ]
161 }
162 }
163 ]
164 }
165 },
166
167And the code I'd written thinking I would be able to use to get the data I wanted:
168
169function iterate(obj: any, stack: any) {
170 for (var property in obj) {
171 if (obj.hasOwnProperty(property)) {
172 if (typeof obj[property] == "object") {
173 iterate(obj[property], stack + '.' + property);
174 } else {
175 if (property === 'name') {
176 console.log(property + " " + obj[property]);
177 }
178 }
179 }
180 }
181}
182
183But this isn't going to work, and I cannot think of how to do this. Any pointers really appreciated.
184
185I would like to create an array along the lines of:
186
187pullrequests [
188 [repo: my-service-1, openprs: 0],
189 [repo: my-service-2, openprs: 0],
190 [repo: my-service-3, openprs: 0],
191 [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]]
192 etc.
193]