|
| 1 | +/** |
| 2 | + * Copyright (c) 2024 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License.AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import { RepositoryService } from "../repohost/repo-service"; |
| 8 | +import { inject, injectable } from "inversify"; |
| 9 | +import { GitHubApiError, GitHubRestApi } from "../github/api"; |
| 10 | +import { GitHubEnterpriseApp } from "./github-enterprise-app"; |
| 11 | +import { GithubContextParser } from "../github/github-context-parser"; |
| 12 | +import { User } from "@gitpod/gitpod-protocol"; |
| 13 | +import { Config } from "../config"; |
| 14 | +import { TokenService } from "../user/token-service"; |
| 15 | +import { RepoURL } from "../repohost"; |
| 16 | +import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error"; |
| 17 | +import { UnauthorizedError } from "../errors"; |
| 18 | +import { containsScopes } from "./token-scopes-inclusion"; |
| 19 | +import { GitHubOAuthScopes } from "@gitpod/public-api-common/lib/auth-providers"; |
| 20 | + |
| 21 | +@injectable() |
| 22 | +export class GitHubService extends RepositoryService { |
| 23 | + static PREBUILD_TOKEN_SCOPE = "prebuilds"; |
| 24 | + |
| 25 | + constructor( |
| 26 | + @inject(GitHubRestApi) protected readonly githubApi: GitHubRestApi, |
| 27 | + @inject(Config) private readonly config: Config, |
| 28 | + @inject(TokenService) private readonly tokenService: TokenService, |
| 29 | + @inject(GithubContextParser) private readonly githubContextParser: GithubContextParser, |
| 30 | + ) { |
| 31 | + super(); |
| 32 | + } |
| 33 | + |
| 34 | + async installAutomatedPrebuilds(user: User, cloneUrl: string): Promise<void> { |
| 35 | + const parsedRepoUrl = RepoURL.parseRepoUrl(cloneUrl); |
| 36 | + if (!parsedRepoUrl) { |
| 37 | + throw new ApplicationError(ErrorCodes.BAD_REQUEST, `Clone URL not parseable.`); |
| 38 | + } |
| 39 | + let tokenEntry; |
| 40 | + try { |
| 41 | + const { owner, repoName: repo } = await this.githubContextParser.parseURL(user, cloneUrl); |
| 42 | + const webhooks = (await this.githubApi.run(user, (gh) => gh.repos.listWebhooks({ owner, repo }))).data; |
| 43 | + for (const webhook of webhooks) { |
| 44 | + if (webhook.config.url === this.getHookUrl()) { |
| 45 | + await this.githubApi.run(user, (gh) => |
| 46 | + gh.repos.deleteWebhook({ owner, repo, hook_id: webhook.id }), |
| 47 | + ); |
| 48 | + } |
| 49 | + } |
| 50 | + tokenEntry = await this.tokenService.createGitpodToken(user, GitHubService.PREBUILD_TOKEN_SCOPE, cloneUrl); |
| 51 | + const config = { |
| 52 | + url: this.getHookUrl(), |
| 53 | + content_type: "json", |
| 54 | + secret: user.id + "|" + tokenEntry.token.value, |
| 55 | + }; |
| 56 | + await this.githubApi.run(user, (gh) => gh.repos.createWebhook({ owner, repo, config })); |
| 57 | + } catch (error) { |
| 58 | + // Hint: here we catch all GH API errors to forward them as Unauthorized to FE, |
| 59 | + // eventually that should be done depending on the error code. |
| 60 | + // Also, if user is not connected at all, then the GH API wrapper is throwing |
| 61 | + // the same error type, but with `providerIsConnected: false`. |
| 62 | + |
| 63 | + if (GitHubApiError.is(error)) { |
| 64 | + // TODO check for `error.code` |
| 65 | + throw UnauthorizedError.create({ |
| 66 | + host: parsedRepoUrl.host, |
| 67 | + providerType: "GitHub", |
| 68 | + repoName: parsedRepoUrl.repo, |
| 69 | + requiredScopes: GitHubOAuthScopes.Requirements.DEFAULT, |
| 70 | + providerIsConnected: true, |
| 71 | + isMissingScopes: containsScopes( |
| 72 | + tokenEntry?.token.scopes, |
| 73 | + GitHubOAuthScopes.Requirements.PRIVATE_REPO, |
| 74 | + ), |
| 75 | + }); |
| 76 | + } |
| 77 | + throw error; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + async isGitpodWebhookEnabled(user: User, cloneUrl: string): Promise<boolean> { |
| 82 | + try { |
| 83 | + const { owner, repoName: repo } = await this.githubContextParser.parseURL(user, cloneUrl); |
| 84 | + const webhooks = (await this.githubApi.run(user, (gh) => gh.repos.listWebhooks({ owner, repo }))).data; |
| 85 | + return webhooks.some((webhook) => webhook.config.url === this.getHookUrl()); |
| 86 | + } catch (error) { |
| 87 | + if (GitHubApiError.is(error)) { |
| 88 | + throw UnauthorizedError.create({ |
| 89 | + host: RepoURL.parseRepoUrl(cloneUrl)!.host, |
| 90 | + providerType: "GitHub", |
| 91 | + repoName: RepoURL.parseRepoUrl(cloneUrl)!.repo, |
| 92 | + requiredScopes: GitHubOAuthScopes.Requirements.DEFAULT, |
| 93 | + providerIsConnected: true, |
| 94 | + }); |
| 95 | + } |
| 96 | + throw error; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + protected getHookUrl() { |
| 101 | + return this.config.hostUrl |
| 102 | + .asPublicServices() |
| 103 | + .with({ |
| 104 | + pathname: GitHubEnterpriseApp.path, |
| 105 | + }) |
| 106 | + .toString(); |
| 107 | + } |
| 108 | +} |
0 commit comments