Skip to content

Commit a0c4087

Browse files
committed
Fix graphql query for ghsa
1 parent 488b222 commit a0c4087

1 file changed

Lines changed: 37 additions & 44 deletions

File tree

src/commands/fix/pull-request.mts

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -168,57 +168,49 @@ export async function fetchGhsaDetails(
168168
if (!ids.length) {
169169
return results
170170
}
171+
171172
const octokitGraphql = getOctokitGraphql()
172173
try {
173174
const gqlCacheKey = `${ids.join('-')}-graphql-snapshot`
174-
const gqlResp = await cacheFetch(gqlCacheKey, () =>
175-
octokitGraphql(
176-
`
177-
query($identifiers: [SecurityAdvisoryIdentifierFilter!]!) {
178-
securityAdvisories(first: ${ids.length}, identifiers: $identifiers) {
179-
nodes {
180-
ghsaId
181-
cveId
182-
summary
183-
severity
184-
publishedAt
185-
withdrawnAt
186-
references {
187-
url
188-
}
189-
vulnerabilities(first: 10) {
190-
nodes {
191-
package {
192-
ecosystem
193-
name
194-
}
195-
vulnerableVersionRange
196-
}
197-
}
175+
176+
const aliases = ids
177+
.map(
178+
(id, index) =>
179+
`advisory${index}: securityAdvisory(ghsaId: "${id}") {
180+
ghsaId
181+
summary
182+
severity
183+
publishedAt
184+
withdrawnAt
185+
vulnerabilities(first: 10) {
186+
nodes {
187+
package {
188+
ecosystem
189+
name
198190
}
191+
vulnerableVersionRange
199192
}
200-
}`,
201-
{
202-
identifiers: ids.map(id => ({
203-
type: 'GHSA',
204-
value: id,
205-
})),
206-
},
207-
),
208-
)
193+
}
194+
}`,
195+
)
196+
.join('\n')
209197

210-
const advisories: GhsaDetails[] =
211-
(gqlResp as any)?.securityAdvisories?.nodes || []
212-
for (const advisory of advisories) {
213-
if (advisory.ghsaId) {
214-
results.set(advisory.ghsaId, advisory)
215-
}
216-
}
198+
const gqlResp = await cacheFetch(gqlCacheKey, () =>
199+
octokitGraphql(`
200+
query {
201+
${aliases}
202+
}
203+
`),
204+
)
217205

218-
// Log any missing advisories
219-
for (const id of ids) {
220-
if (!results.has(id)) {
221-
debugFn('notice', `No advisory found for ${id}`)
206+
for (let i = 0, { length } = ids; i < length; i += 1) {
207+
const id = ids[i]!
208+
const advisoryKey = `advisory${i}`
209+
const advisory = (gqlResp as any)?.[advisoryKey]
210+
if (advisory && advisory.ghsaId) {
211+
results.set(id, advisory as GhsaDetails)
212+
} else {
213+
debugFn('notice', `miss: no advisory found for ${id}`)
222214
}
223215
}
224216
} catch (e) {
@@ -227,6 +219,7 @@ export async function fetchGhsaDetails(
227219
`Failed to fetch GHSA details: ${(e as Error)?.message || 'Unknown error'}`,
228220
)
229221
}
222+
230223
return results
231224
}
232225

0 commit comments

Comments
 (0)