@@ -11,73 +11,20 @@ import { GitHubEnterpriseApp } from "./github-enterprise-app";
1111import { GithubContextParser } from "../github/github-context-parser" ;
1212import { User } from "@gitpod/gitpod-protocol" ;
1313import { Config } from "../config" ;
14- import { TokenService } from "../user/token-service" ;
1514import { RepoURL } from "../repohost" ;
16- import { ApplicationError , ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error" ;
1715import { UnauthorizedError } from "../errors" ;
18- import { containsScopes } from "./token-scopes-inclusion" ;
1916import { GitHubOAuthScopes } from "@gitpod/public-api-common/lib/auth-providers" ;
2017
2118@injectable ( )
2219export class GitHubService extends RepositoryService {
23- static PREBUILD_TOKEN_SCOPE = "prebuilds" ;
24-
2520 constructor (
2621 @inject ( GitHubRestApi ) protected readonly githubApi : GitHubRestApi ,
2722 @inject ( Config ) private readonly config : Config ,
28- @inject ( TokenService ) private readonly tokenService : TokenService ,
2923 @inject ( GithubContextParser ) private readonly githubContextParser : GithubContextParser ,
3024 ) {
3125 super ( ) ;
3226 }
3327
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-
8128 async isGitpodWebhookEnabled ( user : User , cloneUrl : string ) : Promise < boolean > {
8229 try {
8330 const { owner, repoName : repo } = await this . githubContextParser . parseURL ( user , cloneUrl ) ;
0 commit comments