-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Proper webhook installation detection #20240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
34dfbe2
Proper webhook installed detection
filiptronicek d99c1af
fix webhook detection
filiptronicek 5cfebd8
debug: enable webhook on newly enabled prebuilds
filiptronicek c071537
Add a comment about the arbitrary last 50 events
filiptronicek b0e0f5d
Remove unused `GetConfigurationWebhookActivityStatus`
filiptronicek e428a17
update fn comment
filiptronicek e6fe1f3
Delete superflous log
filiptronicek 63e90a8
fix debugging webhooks
filiptronicek f1efc20
remove unused webhook-hooks
filiptronicek f4a78cf
Revert "debug: enable webhook on newly enabled prebuilds"
filiptronicek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
components/server/src/prebuilds/bitbucket-server-service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /** | ||
| * Copyright (c) 2024 Gitpod GmbH. All rights reserved. | ||
| * Licensed under the GNU Affero General Public License (AGPL). | ||
| * See License.AGPL.txt in the project root for license information. | ||
| */ | ||
|
|
||
| import { RepositoryService } from "../repohost/repo-service"; | ||
| import { User } from "@gitpod/gitpod-protocol"; | ||
| import { inject, injectable } from "inversify"; | ||
| import { BitbucketServerApi } from "../bitbucket-server/bitbucket-server-api"; | ||
| import { BitbucketServerContextParser } from "../bitbucket-server/bitbucket-server-context-parser"; | ||
| import { Config } from "../config"; | ||
| import { BitbucketServerApp } from "./bitbucket-server-app"; | ||
|
|
||
| @injectable() | ||
| export class BitbucketServerService extends RepositoryService { | ||
| constructor( | ||
| @inject(BitbucketServerApi) private readonly api: BitbucketServerApi, | ||
| @inject(Config) private readonly config: Config, | ||
| @inject(BitbucketServerContextParser) private readonly contextParser: BitbucketServerContextParser, | ||
| ) { | ||
| super(); | ||
| } | ||
|
|
||
| public async isGitpodWebhookEnabled(user: User, cloneUrl: string): Promise<boolean> { | ||
| try { | ||
| const { owner, repoName, repoKind } = await this.contextParser.parseURL(user, cloneUrl); | ||
| const existing = await this.api.getWebhooks(user, { | ||
| repoKind, | ||
| repositorySlug: repoName, | ||
| owner, | ||
| }); | ||
| if (!existing.values) { | ||
| return false; | ||
| } | ||
| const hookUrl = this.getHookUrl(); | ||
|
|
||
| return existing.values.some((hook) => hook.url && hook.url.includes(hookUrl)); | ||
| } catch (error) { | ||
| console.error("Failed to check if Gitpod webhook is enabled.", error, { cloneUrl }); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| protected getHookUrl() { | ||
| return this.config.hostUrl | ||
| .asPublicServices() | ||
| .with({ | ||
| pathname: BitbucketServerApp.path, | ||
| }) | ||
| .toString(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * Copyright (c) 2024 Gitpod GmbH. All rights reserved. | ||
| * Licensed under the GNU Affero General Public License (AGPL). | ||
| * See License.AGPL.txt in the project root for license information. | ||
| */ | ||
|
|
||
| import { RepositoryService } from "../repohost/repo-service"; | ||
| import { User } from "@gitpod/gitpod-protocol"; | ||
| import { inject, injectable } from "inversify"; | ||
| import { BitbucketApiFactory } from "../bitbucket/bitbucket-api-factory"; | ||
| import { BitbucketApp } from "./bitbucket-app"; | ||
| import { Config } from "../config"; | ||
| import { BitbucketContextParser } from "../bitbucket/bitbucket-context-parser"; | ||
|
|
||
| @injectable() | ||
| export class BitbucketService extends RepositoryService { | ||
| constructor( | ||
| @inject(BitbucketApiFactory) private readonly api: BitbucketApiFactory, | ||
| @inject(Config) private readonly config: Config, | ||
| @inject(BitbucketContextParser) private readonly bitbucketContextParser: BitbucketContextParser, | ||
| ) { | ||
| super(); | ||
| } | ||
|
|
||
| public async isGitpodWebhookEnabled(user: User, cloneUrl: string): Promise<boolean> { | ||
| try { | ||
| const api = await this.api.create(user); | ||
| const { owner, repoName } = await this.bitbucketContextParser.parseURL(user, cloneUrl); | ||
| const hooks = await api.repositories.listWebhooks({ | ||
| repo_slug: repoName, | ||
| workspace: owner, | ||
| }); | ||
| if (!hooks.data.values) { | ||
| return false; | ||
| } | ||
| return hooks.data.values.some((hook) => hook.url === this.getHookUrl()); | ||
| } catch (error) { | ||
| console.error("Failed to check if Gitpod webhook is enabled.", error, { cloneUrl }); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| protected getHookUrl() { | ||
| return this.config.hostUrl | ||
| .asPublicServices() | ||
| .with({ | ||
| pathname: BitbucketApp.path, | ||
| }) | ||
| .toString(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /** | ||
| * Copyright (c) 2024 Gitpod GmbH. All rights reserved. | ||
| * Licensed under the GNU Affero General Public License (AGPL). | ||
| * See License.AGPL.txt in the project root for license information. | ||
| */ | ||
|
|
||
| import { RepositoryService } from "../repohost/repo-service"; | ||
| import { inject, injectable } from "inversify"; | ||
| import { GitHubApiError, GitHubRestApi } from "../github/api"; | ||
| import { GitHubEnterpriseApp } from "./github-enterprise-app"; | ||
| import { GithubContextParser } from "../github/github-context-parser"; | ||
| import { User } from "@gitpod/gitpod-protocol"; | ||
| import { Config } from "../config"; | ||
| import { RepoURL } from "../repohost"; | ||
| import { UnauthorizedError } from "../errors"; | ||
| import { GitHubOAuthScopes } from "@gitpod/public-api-common/lib/auth-providers"; | ||
| import { containsScopes } from "./token-scopes-inclusion"; | ||
| import { TokenService } from "../user/token-service"; | ||
| import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error"; | ||
|
|
||
| @injectable() | ||
| export class GitHubService extends RepositoryService { | ||
| constructor( | ||
| @inject(GitHubRestApi) protected readonly githubApi: GitHubRestApi, | ||
| @inject(Config) private readonly config: Config, | ||
| @inject(TokenService) private readonly tokenService: TokenService, | ||
| @inject(GithubContextParser) private readonly githubContextParser: GithubContextParser, | ||
| ) { | ||
| super(); | ||
| } | ||
|
|
||
| async isGitpodWebhookEnabled(user: User, cloneUrl: string): Promise<boolean> { | ||
| try { | ||
| const { owner, repoName: repo } = await this.githubContextParser.parseURL(user, cloneUrl); | ||
| const webhooks = (await this.githubApi.run(user, (gh) => gh.repos.listWebhooks({ owner, repo }))).data; | ||
| return webhooks.some((webhook) => webhook.config.url === this.getHookUrl()); | ||
| } catch (error) { | ||
| if (GitHubApiError.is(error)) { | ||
| throw UnauthorizedError.create({ | ||
| host: RepoURL.parseRepoUrl(cloneUrl)!.host, | ||
| providerType: "GitHub", | ||
| repoName: RepoURL.parseRepoUrl(cloneUrl)!.repo, | ||
| requiredScopes: GitHubOAuthScopes.Requirements.DEFAULT, | ||
| providerIsConnected: true, | ||
| }); | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| async installAutomatedPrebuilds(user: User, cloneUrl: string): Promise<void> { | ||
| const parsedRepoUrl = RepoURL.parseRepoUrl(cloneUrl); | ||
| if (!parsedRepoUrl) { | ||
| throw new ApplicationError(ErrorCodes.BAD_REQUEST, `Clone URL not parseable.`); | ||
| } | ||
| let tokenEntry; | ||
| try { | ||
| const { owner, repoName: repo } = await this.githubContextParser.parseURL(user, cloneUrl); | ||
| const webhooks = (await this.githubApi.run(user, (gh) => gh.repos.listWebhooks({ owner, repo }))).data; | ||
| for (const webhook of webhooks) { | ||
| if (webhook.config.url === this.getHookUrl()) { | ||
| await this.githubApi.run(user, (gh) => | ||
| gh.repos.deleteWebhook({ owner, repo, hook_id: webhook.id }), | ||
| ); | ||
| } | ||
| } | ||
| tokenEntry = await this.tokenService.createGitpodToken(user, "prebuild", cloneUrl); | ||
| const config = { | ||
| url: this.getHookUrl(), | ||
| content_type: "json", | ||
| secret: user.id + "|" + tokenEntry.token.value, | ||
| }; | ||
| await this.githubApi.run(user, (gh) => gh.repos.createWebhook({ owner, repo, config })); | ||
| } catch (error) { | ||
| // Hint: here we catch all GH API errors to forward them as Unauthorized to FE, | ||
| // eventually that should be done depending on the error code. | ||
| // Also, if user is not connected at all, then the GH API wrapper is throwing | ||
| // the same error type, but with `providerIsConnected: false`. | ||
|
|
||
| if (GitHubApiError.is(error)) { | ||
| // TODO check for `error.code` | ||
| throw UnauthorizedError.create({ | ||
| host: parsedRepoUrl.host, | ||
| providerType: "GitHub", | ||
| repoName: parsedRepoUrl.repo, | ||
| requiredScopes: GitHubOAuthScopes.Requirements.PRIVATE_REPO, | ||
| providerIsConnected: true, | ||
| isMissingScopes: containsScopes( | ||
| tokenEntry?.token.scopes, | ||
| GitHubOAuthScopes.Requirements.PRIVATE_REPO, | ||
| ), | ||
| }); | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| protected getHookUrl() { | ||
| return this.config.hostUrl | ||
| .asPublicServices() | ||
| .with({ | ||
| pathname: GitHubEnterpriseApp.path, | ||
| }) | ||
| .toString(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /** | ||
| * Copyright (c) 2024 Gitpod GmbH. All rights reserved. | ||
| * Licensed under the GNU Affero General Public License (AGPL). | ||
| * See License.AGPL.txt in the project root for license information. | ||
| */ | ||
|
|
||
| import { RepositoryService } from "../repohost/repo-service"; | ||
| import { User } from "@gitpod/gitpod-protocol"; | ||
| import { inject, injectable } from "inversify"; | ||
| import { GitLabApi, GitLab } from "../gitlab/api"; | ||
| import { GitLabApp } from "./gitlab-app"; | ||
| import { Config } from "../config"; | ||
| import { GitlabContextParser } from "../gitlab/gitlab-context-parser"; | ||
| import { RepoURL } from "../repohost"; | ||
| import { UnauthorizedError } from "../errors"; | ||
| import { GitLabOAuthScopes } from "@gitpod/public-api-common/lib/auth-providers"; | ||
|
|
||
| @injectable() | ||
| export class GitlabService extends RepositoryService { | ||
| constructor( | ||
| @inject(GitLabApi) protected api: GitLabApi, | ||
| @inject(Config) private readonly config: Config, | ||
| @inject(GitlabContextParser) private readonly gitlabContextParser: GitlabContextParser, | ||
| ) { | ||
| super(); | ||
| } | ||
|
|
||
| public async isGitpodWebhookEnabled(user: User, cloneUrl: string): Promise<boolean> { | ||
| try { | ||
| const { owner, repoName } = await this.gitlabContextParser.parseURL(user, cloneUrl); | ||
| const hooks = (await this.api.run(user, (g) => | ||
| g.ProjectHooks.all(`${owner}/${repoName}`), | ||
| )) as unknown as GitLab.ProjectHook[]; | ||
| return hooks.some((hook) => hook.url === this.getHookUrl()); | ||
| } catch (error) { | ||
| if (GitLab.ApiError.is(error)) { | ||
| throw UnauthorizedError.create({ | ||
| host: RepoURL.parseRepoUrl(cloneUrl)!.host, | ||
| providerType: "GitLab", | ||
| repoName: RepoURL.parseRepoUrl(cloneUrl)!.repo, | ||
| requiredScopes: GitLabOAuthScopes.Requirements.REPO, | ||
| providerIsConnected: true, | ||
| }); | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| private getHookUrl() { | ||
| return this.config.hostUrl | ||
| .asPublicServices() | ||
| .with({ | ||
| pathname: GitLabApp.path, | ||
| }) | ||
| .toString(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.