|
1 | | -import { parse } from 'graphql'; |
2 | | -import combineQuery from 'graphql-combine-query'; |
| 1 | +import axios from 'axios'; |
3 | 2 |
|
4 | 3 | import type { |
5 | 4 | AccountNotifications, |
6 | 5 | GitifyState, |
7 | 6 | GitifySubject, |
| 7 | + Link, |
8 | 8 | SettingsState, |
9 | 9 | } from '../../types'; |
10 | 10 | import type { Notification } from '../../typesGitHub'; |
11 | 11 | import { listNotificationsForAuthenticatedUser } from '../api/client'; |
12 | 12 | import { determineFailureType } from '../api/errors'; |
| 13 | +import { getHeaders } from '../api/request'; |
| 14 | +import { getGitHubGraphQLUrl, getNumberFromUrl } from '../api/utils'; |
13 | 15 | import { rendererLogError, rendererLogWarn } from '../logger'; |
14 | 16 | import { |
15 | 17 | filterBaseNotifications, |
@@ -133,20 +135,93 @@ export async function enrichNotifications( |
133 | 135 | } |
134 | 136 |
|
135 | 137 | // Build combined query for pull requests (builder is immutable) |
136 | | - const buildQuery = combineQuery('PullRequestBatch'); |
| 138 | + let mergedQuery = ''; |
| 139 | + let i = 0; |
| 140 | + const args = []; |
137 | 141 |
|
138 | 142 | for (const notification of notifications) { |
139 | 143 | if (notification.subject.type === 'PullRequest') { |
140 | | - const handler = createNotificationHandler(notification); |
141 | | - const queryData = handler.query(notification); |
| 144 | + const org = notification.repository.owner.login; |
| 145 | + const repo = notification.repository.name; |
| 146 | + const number = getNumberFromUrl(notification.subject.url); |
142 | 147 |
|
143 | | - if (queryData?.query && queryData?.variables) { |
144 | | - buildQuery.addN(parse(queryData.query), queryData.variables); |
145 | | - } |
| 148 | + args.push({ |
| 149 | + org: org, |
| 150 | + repo: repo, |
| 151 | + number: number, |
| 152 | + }); |
| 153 | + |
| 154 | + mergedQuery += `repo${i}: repository(owner: $owner${i}, name: $name${i}) { |
| 155 | + pullRequest(number: $number${i}) { |
| 156 | + title |
| 157 | + } |
| 158 | + }\n`; |
| 159 | + |
| 160 | + i += 1; |
| 161 | + |
| 162 | + // const handler = createNotificationHandler(notification); |
| 163 | + // const queryData = handler.query(notification); |
146 | 164 | } |
147 | 165 | } |
148 | 166 |
|
149 | | - // console.log('ADAM COMBINED QUERY: ', JSON.stringify(buildQuery, null, 2)); |
| 167 | + let queryArgs = ''; |
| 168 | + let queryArgsVariables = {}; |
| 169 | + |
| 170 | + for (let idx = 0; idx < args.length; idx++) { |
| 171 | + const arg = args[idx]; |
| 172 | + if (idx > 0) { |
| 173 | + queryArgs += ', '; |
| 174 | + } |
| 175 | + queryArgs += `$owner${idx}: String!, $name${idx}: String!, $number${idx}: Int!`; |
| 176 | + queryArgsVariables = { |
| 177 | + ...queryArgsVariables, |
| 178 | + [`owner${idx}`]: arg.org, |
| 179 | + [`name${idx}`]: arg.repo, |
| 180 | + [`number${idx}`]: arg.number, |
| 181 | + }; |
| 182 | + } |
| 183 | + |
| 184 | + mergedQuery = `query JumboQuery(${queryArgs}) {\n${mergedQuery}}\n`; |
| 185 | + |
| 186 | + console.log('ADAM COMBINED QUERY ', JSON.stringify(mergedQuery, null, 2)); |
| 187 | + console.log( |
| 188 | + 'ADAM COMBINED ARGS ', |
| 189 | + JSON.stringify(queryArgsVariables, null, 2), |
| 190 | + ); |
| 191 | + |
| 192 | + try { |
| 193 | + const url = getGitHubGraphQLUrl( |
| 194 | + notifications[0].account.hostname, |
| 195 | + ).toString(); |
| 196 | + const token = notifications[0].account.token; |
| 197 | + |
| 198 | + const headers = await getHeaders(url as Link, token); |
| 199 | + |
| 200 | + axios({ |
| 201 | + method: 'POST', |
| 202 | + url, |
| 203 | + data: { |
| 204 | + query: mergedQuery, |
| 205 | + variables: queryArgsVariables, |
| 206 | + }, |
| 207 | + headers: headers, |
| 208 | + }).then((response) => { |
| 209 | + console.log('ADAM RESPONSE ', JSON.stringify(response, null, 2)); |
| 210 | + }); |
| 211 | + } catch (err) { |
| 212 | + console.error('oops'); |
| 213 | + } |
| 214 | + |
| 215 | + // const headers = await getHeaders(url.toString() as Link, token); |
| 216 | + |
| 217 | + // await axios.post(url.toString(), combined, { headers }); |
| 218 | + // } catch (err) { |
| 219 | + // rendererLogError( |
| 220 | + // 'enrichNotifications', |
| 221 | + // 'failed to fetch batch pull request details', |
| 222 | + // err, |
| 223 | + // ); |
| 224 | + // } |
150 | 225 |
|
151 | 226 | const enrichedNotifications = await Promise.all( |
152 | 227 | notifications.map(async (notification: Notification) => { |
|
0 commit comments