Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 44 additions & 26 deletions components/server/src/github/github-context-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
CommitContext,
RefType,
} from "@gitpod/gitpod-protocol";
import { GitHubGraphQlEndpoint } from "./api";
import { GitHubGraphQlEndpoint, QueryResult } from "./api";
import { NotFoundError, UnauthorizedError } from "../errors";
import { log, LogContext, LogPayload } from "@gitpod/gitpod-protocol/lib/util/logging";
import { IContextParser, IssueContexts, AbstractContextParser } from "../workspace/context-parser";
Expand All @@ -24,6 +24,7 @@ import { GitHubTokenHelper } from "./github-token-helper";
import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing";
import { RepoURL } from "../repohost";
import { containsScopes } from "../prebuilds/token-scopes-inclusion";
import { TrustedValue } from "@gitpod/gitpod-protocol/lib/util/scrubbing";

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

try {
const result: any = await this.githubQueryApi.runQuery(
user,
`
const query = `
query {
repository(name: "${repoName}", owner: "${owner}") {
${this.repoProperties()}
Expand All @@ -130,11 +129,16 @@ export class GithubContextParser extends AbstractContextParser implements IConte
},
}
}
`,
);
`;
const result: QueryResult<any> = await this.githubQueryApi.runQuery(user, query);
span.log({ "request.finished": "" });

if (result.errors) {
log.error("Errors executing query.", { query, errors: new TrustedValue(result.errors) });
}

if (result.data.repository === null) {
log.warn(`Repository not found.`, { query });
throw await NotFoundError.create(
await this.tokenHelper.getCurrentToken(user),
user,
Expand Down Expand Up @@ -184,9 +188,7 @@ export class GithubContextParser extends AbstractContextParser implements IConte
const path = decodeURIComponent(segments.slice(i).join("/"));
// Sanitize path expression to prevent GraphQL injections (e.g. escape any `"` or `\n`).
const pathExpression = JSON.stringify(`${branchNameOrCommitHash}:${path}`);
const result: any = await this.githubQueryApi.runQuery(
user,
`
const query = `
query {
repository(name: "${repoName}", owner: "${owner}") {
${this.repoProperties()}
Expand All @@ -213,12 +215,16 @@ export class GithubContextParser extends AbstractContextParser implements IConte
}
}
}
`,
);
`;
const result: QueryResult<any> = await this.githubQueryApi.runQuery(user, query);
span.log({ "request.finished": "" });
if (result.errors) {
log.error("Errors executing query.", { query, errors: new TrustedValue(result.errors) });
}

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

try {
const result: any = await this.githubQueryApi.runQuery(
user,
`
const query = `
query {
repository(name: "${repoName}", owner: "${owner}") {
object(oid: "${sha}") {
Expand All @@ -313,11 +317,17 @@ export class GithubContextParser extends AbstractContextParser implements IConte
},
}
}
`,
);
`;
const result: QueryResult<any> = await this.githubQueryApi.runQuery(user, query);
span.log({ "request.finished": "" });

if (result.errors) {
log.error("Errors executing query.", { query, errors: new TrustedValue(result.errors) });
}

if (result.data.repository === null) {
log.warn(`Repository not found.`, { query });

throw await NotFoundError.create(
await this.tokenHelper.getCurrentToken(user),
user,
Expand Down Expand Up @@ -362,9 +372,7 @@ export class GithubContextParser extends AbstractContextParser implements IConte
const span = TraceContext.startSpan("handlePullRequestContext", ctx);

try {
const result: any = await this.githubQueryApi.runQuery(
user,
`
const query = `
query {
repository(name: "${repoName}", owner: "${owner}") {
pullRequest(number: ${pullRequestNr}) {
Expand Down Expand Up @@ -402,11 +410,17 @@ export class GithubContextParser extends AbstractContextParser implements IConte
}
}
}
`,
);
`;
const result: QueryResult<any> = await this.githubQueryApi.runQuery(user, query);
span.log({ "request.finished": "" });

if (result.errors) {
log.error("Errors executing query.", { query, errors: new TrustedValue(result.errors) });
}

if (result.data.repository === null) {
log.warn(`Repository not found.`, { query });

throw await NotFoundError.create(
await this.tokenHelper.getCurrentToken(user),
user,
Expand Down Expand Up @@ -464,9 +478,7 @@ export class GithubContextParser extends AbstractContextParser implements IConte
const span = TraceContext.startSpan("handleIssueContext", ctx);

try {
const result: any = await this.githubQueryApi.runQuery(
user,
`
const query = `
query {
repository(name: "${repoName}", owner: "${owner}") {
issue(number: ${issueNr}) {
Expand All @@ -481,11 +493,17 @@ export class GithubContextParser extends AbstractContextParser implements IConte
},
}
}
`,
);
`;
const result: QueryResult<any> = await this.githubQueryApi.runQuery(user, query);
span.log({ "request.finished": "" });

if (result.errors) {
log.error("Errors executing query.", { query, errors: new TrustedValue(result.errors) });
}

if (result.data.repository === null) {
log.warn(`Repository not found.`, { query });

throw await NotFoundError.create(
await this.tokenHelper.getCurrentToken(user),
user,
Expand Down