-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[server] Extract ScmService to be used by both APIs (#19098)
* [server] add ScmService to be used by ScmServiceAPI (and WS API) * add simple test for `getToken` * refactor `ScmService.getToken` to return token of undefined * fix duplicata validation * add api converter tests * just some docs
- Loading branch information
1 parent
0fba169
commit 612b919
Showing
23 changed files
with
590 additions
and
272 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,93 @@ | ||
/** | ||
* Copyright (c) 2023 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 { HandlerContext, ServiceImpl } from "@connectrpc/connect"; | ||
import { SCMService as ScmServiceInterface } from "@gitpod/public-api/lib/gitpod/v1/scm_connect"; | ||
import { inject, injectable } from "inversify"; | ||
import { PublicAPIConverter } from "@gitpod/gitpod-protocol/lib/public-api-converter"; | ||
import { ScmService } from "../scm/scm-service"; | ||
import { | ||
GuessTokenScopesRequest, | ||
GuessTokenScopesResponse, | ||
SearchRepositoriesRequest, | ||
SearchRepositoriesResponse, | ||
ListSuggestedRepositoriesRequest, | ||
ListSuggestedRepositoriesResponse, | ||
SearchSCMTokensRequest, | ||
SearchSCMTokensResponse, | ||
} from "@gitpod/public-api/lib/gitpod/v1/scm_pb"; | ||
import { ctxUserId } from "../util/request-context"; | ||
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error"; | ||
import { validate as uuidValidate } from "uuid"; | ||
import { ProjectsService } from "../projects/projects-service"; | ||
import { WorkspaceService } from "../workspace/workspace-service"; | ||
import { PaginationResponse } from "@gitpod/public-api/lib/gitpod/v1/pagination_pb"; | ||
|
||
@injectable() | ||
export class ScmServiceAPI implements ServiceImpl<typeof ScmServiceInterface> { | ||
constructor( | ||
@inject(PublicAPIConverter) private readonly apiConverter: PublicAPIConverter, | ||
@inject(ScmService) private readonly scmService: ScmService, | ||
@inject(ProjectsService) private readonly projectService: ProjectsService, | ||
@inject(WorkspaceService) private readonly workspaceService: WorkspaceService, | ||
) {} | ||
|
||
async searchSCMTokens(request: SearchSCMTokensRequest, _: HandlerContext): Promise<SearchSCMTokensResponse> { | ||
const userId = ctxUserId(); | ||
const response = new SearchSCMTokensResponse(); | ||
const token = await this.scmService.getToken(userId, request); | ||
if (token) { | ||
response.tokens.push(this.apiConverter.toSCMToken(token)); | ||
} | ||
return response; | ||
} | ||
|
||
async guessTokenScopes(request: GuessTokenScopesRequest, _: HandlerContext): Promise<GuessTokenScopesResponse> { | ||
const userId = ctxUserId(); | ||
const { scopes, message } = await this.scmService.guessTokenScopes(userId, request); | ||
return new GuessTokenScopesResponse({ | ||
scopes, | ||
message, | ||
}); | ||
} | ||
|
||
async searchRepositories( | ||
request: SearchRepositoriesRequest, | ||
_: HandlerContext, | ||
): Promise<SearchRepositoriesResponse> { | ||
const userId = ctxUserId(); | ||
const repos = await this.scmService.searchRepositories(userId, { | ||
searchString: request.searchString, | ||
limit: request.limit, | ||
}); | ||
return new SearchRepositoriesResponse({ | ||
repositories: repos.map((r) => this.apiConverter.toSuggestedRepository(r)), | ||
}); | ||
} | ||
|
||
async listSuggestedRepositories( | ||
request: ListSuggestedRepositoriesRequest, | ||
_: HandlerContext, | ||
): Promise<ListSuggestedRepositoriesResponse> { | ||
const userId = ctxUserId(); | ||
const { organizationId } = request; | ||
|
||
if (!uuidValidate(organizationId)) { | ||
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "organizationId must be a valid UUID"); | ||
} | ||
|
||
const projectsPromise = this.projectService.getProjects(userId, organizationId); | ||
const workspacesPromise = this.workspaceService.getWorkspaces(userId, { organizationId }); | ||
const repos = await this.scmService.listSuggestedRepositories(userId, { projectsPromise, workspacesPromise }); | ||
return new ListSuggestedRepositoriesResponse({ | ||
repositories: repos.map((r) => this.apiConverter.toSuggestedRepository(r)), | ||
pagination: new PaginationResponse({ | ||
nextToken: "", | ||
total: repos.length, | ||
}), | ||
}); | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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
Oops, something went wrong.