Skip to content

Commit bcc0b83

Browse files
authored
Log query if repo is not found (#19989)
1 parent 6ab7fea commit bcc0b83

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

components/server/src/github/github-context-parser.ts

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
CommitContext,
1616
RefType,
1717
} from "@gitpod/gitpod-protocol";
18-
import { GitHubGraphQlEndpoint } from "./api";
18+
import { GitHubGraphQlEndpoint, QueryResult } from "./api";
1919
import { NotFoundError, UnauthorizedError } from "../errors";
2020
import { log, LogContext, LogPayload } from "@gitpod/gitpod-protocol/lib/util/logging";
2121
import { IContextParser, IssueContexts, AbstractContextParser } from "../workspace/context-parser";
@@ -24,6 +24,7 @@ import { GitHubTokenHelper } from "./github-token-helper";
2424
import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing";
2525
import { RepoURL } from "../repohost";
2626
import { containsScopes } from "../prebuilds/token-scopes-inclusion";
27+
import { TrustedValue } from "@gitpod/gitpod-protocol/lib/util/scrubbing";
2728

2829
@injectable()
2930
export class GithubContextParser extends AbstractContextParser implements IContextParser {
@@ -116,9 +117,7 @@ export class GithubContextParser extends AbstractContextParser implements IConte
116117
const span = TraceContext.startSpan("GithubContextParser.handleDefaultContext", ctx);
117118

118119
try {
119-
const result: any = await this.githubQueryApi.runQuery(
120-
user,
121-
`
120+
const query = `
122121
query {
123122
repository(name: "${repoName}", owner: "${owner}") {
124123
${this.repoProperties()}
@@ -130,11 +129,16 @@ export class GithubContextParser extends AbstractContextParser implements IConte
130129
},
131130
}
132131
}
133-
`,
134-
);
132+
`;
133+
const result: QueryResult<any> = await this.githubQueryApi.runQuery(user, query);
135134
span.log({ "request.finished": "" });
136135

136+
if (result.errors) {
137+
log.error("Errors executing query.", { query, errors: new TrustedValue(result.errors) });
138+
}
139+
137140
if (result.data.repository === null) {
141+
log.warn(`Repository not found.`, { query });
138142
throw await NotFoundError.create(
139143
await this.tokenHelper.getCurrentToken(user),
140144
user,
@@ -184,9 +188,7 @@ export class GithubContextParser extends AbstractContextParser implements IConte
184188
const path = decodeURIComponent(segments.slice(i).join("/"));
185189
// Sanitize path expression to prevent GraphQL injections (e.g. escape any `"` or `\n`).
186190
const pathExpression = JSON.stringify(`${branchNameOrCommitHash}:${path}`);
187-
const result: any = await this.githubQueryApi.runQuery(
188-
user,
189-
`
191+
const query = `
190192
query {
191193
repository(name: "${repoName}", owner: "${owner}") {
192194
${this.repoProperties()}
@@ -213,12 +215,16 @@ export class GithubContextParser extends AbstractContextParser implements IConte
213215
}
214216
}
215217
}
216-
`,
217-
);
218+
`;
219+
const result: QueryResult<any> = await this.githubQueryApi.runQuery(user, query);
218220
span.log({ "request.finished": "" });
221+
if (result.errors) {
222+
log.error("Errors executing query.", { query, errors: new TrustedValue(result.errors) });
223+
}
219224

220225
const repo = result.data.repository;
221226
if (repo === null) {
227+
log.warn(`Repository not found.`, { query });
222228
throw await NotFoundError.create(
223229
await this.tokenHelper.getCurrentToken(user),
224230
user,
@@ -293,9 +299,7 @@ export class GithubContextParser extends AbstractContextParser implements IConte
293299
}
294300

295301
try {
296-
const result: any = await this.githubQueryApi.runQuery(
297-
user,
298-
`
302+
const query = `
299303
query {
300304
repository(name: "${repoName}", owner: "${owner}") {
301305
object(oid: "${sha}") {
@@ -313,11 +317,17 @@ export class GithubContextParser extends AbstractContextParser implements IConte
313317
},
314318
}
315319
}
316-
`,
317-
);
320+
`;
321+
const result: QueryResult<any> = await this.githubQueryApi.runQuery(user, query);
318322
span.log({ "request.finished": "" });
319323

324+
if (result.errors) {
325+
log.error("Errors executing query.", { query, errors: new TrustedValue(result.errors) });
326+
}
327+
320328
if (result.data.repository === null) {
329+
log.warn(`Repository not found.`, { query });
330+
321331
throw await NotFoundError.create(
322332
await this.tokenHelper.getCurrentToken(user),
323333
user,
@@ -362,9 +372,7 @@ export class GithubContextParser extends AbstractContextParser implements IConte
362372
const span = TraceContext.startSpan("handlePullRequestContext", ctx);
363373

364374
try {
365-
const result: any = await this.githubQueryApi.runQuery(
366-
user,
367-
`
375+
const query = `
368376
query {
369377
repository(name: "${repoName}", owner: "${owner}") {
370378
pullRequest(number: ${pullRequestNr}) {
@@ -402,11 +410,17 @@ export class GithubContextParser extends AbstractContextParser implements IConte
402410
}
403411
}
404412
}
405-
`,
406-
);
413+
`;
414+
const result: QueryResult<any> = await this.githubQueryApi.runQuery(user, query);
407415
span.log({ "request.finished": "" });
408416

417+
if (result.errors) {
418+
log.error("Errors executing query.", { query, errors: new TrustedValue(result.errors) });
419+
}
420+
409421
if (result.data.repository === null) {
422+
log.warn(`Repository not found.`, { query });
423+
410424
throw await NotFoundError.create(
411425
await this.tokenHelper.getCurrentToken(user),
412426
user,
@@ -464,9 +478,7 @@ export class GithubContextParser extends AbstractContextParser implements IConte
464478
const span = TraceContext.startSpan("handleIssueContext", ctx);
465479

466480
try {
467-
const result: any = await this.githubQueryApi.runQuery(
468-
user,
469-
`
481+
const query = `
470482
query {
471483
repository(name: "${repoName}", owner: "${owner}") {
472484
issue(number: ${issueNr}) {
@@ -481,11 +493,17 @@ export class GithubContextParser extends AbstractContextParser implements IConte
481493
},
482494
}
483495
}
484-
`,
485-
);
496+
`;
497+
const result: QueryResult<any> = await this.githubQueryApi.runQuery(user, query);
486498
span.log({ "request.finished": "" });
487499

500+
if (result.errors) {
501+
log.error("Errors executing query.", { query, errors: new TrustedValue(result.errors) });
502+
}
503+
488504
if (result.data.repository === null) {
505+
log.warn(`Repository not found.`, { query });
506+
489507
throw await NotFoundError.create(
490508
await this.tokenHelper.getCurrentToken(user),
491509
user,

0 commit comments

Comments
 (0)