-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[server] Extract ScmService to be used by both APIs #19098
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b169754
[server] add ScmService to be used by ScmServiceAPI (and WS API)
AlexTugarev 59096d5
add simple test for `getToken`
AlexTugarev baaadc3
refactor `ScmService.getToken` to return token of undefined
AlexTugarev 408f997
fix duplicata validation
AlexTugarev 8eed8a7
add api converter tests
AlexTugarev 4ce5044
just some docs
AlexTugarev 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 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing this on purpose. 'Should avoid sending sensitive information if not needed.
Here we'd be in the position to obtain the token from DB to perform the write permissions check if required.