Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
easyCZ committed Nov 20, 2023
1 parent ece56eb commit 4484109
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 50 deletions.
21 changes: 0 additions & 21 deletions components/gitpod-protocol/go/gitpod-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ type APIInterface interface {
GetWorkspaceOwner(ctx context.Context, workspaceID string) (res *UserInfo, err error)
GetWorkspaceUsers(ctx context.Context, workspaceID string) (res []*WorkspaceInstanceUser, err error)
GetFeaturedRepositories(ctx context.Context) (res []*WhitelistedRepository, err error)
GetSuggestedContextURLs(ctx context.Context) (res []*string, err error)
GetWorkspace(ctx context.Context, id string) (res *WorkspaceInfo, err error)
GetIDEOptions(ctx context.Context) (res *IDEOptions, err error)
IsWorkspaceOwner(ctx context.Context, workspaceID string) (res bool, err error)
Expand Down Expand Up @@ -147,8 +146,6 @@ const (
FunctionGetWorkspaceUsers FunctionName = "getWorkspaceUsers"
// FunctionGetFeaturedRepositories is the name of the getFeaturedRepositories function
FunctionGetFeaturedRepositories FunctionName = "getFeaturedRepositories"
// FunctionGetSuggestedContextURLs is the name of the getSuggestedContextURLs function
FunctionGetSuggestedContextURLs FunctionName = "getSuggestedContextURLs"
// FunctionGetWorkspace is the name of the getWorkspace function
FunctionGetWorkspace FunctionName = "getWorkspace"
// FunctionGetIDEOptions is the name of the getIDEOptions function
Expand Down Expand Up @@ -1057,24 +1054,6 @@ func (gp *APIoverJSONRPC) ClosePort(ctx context.Context, workspaceID string, por
return
}

// GetSuggestedContextURLs calls getSuggestedContextURLs on the server
func (gp *APIoverJSONRPC) GetSuggestedContextURLs(ctx context.Context) (res []*string, err error) {
if gp == nil {
err = errNotConnected
return
}
var _params []interface{}

var result []*string
err = gp.C.Call(ctx, "getSuggestedContextURLs", _params, &result)
if err != nil {
return
}
res = result

return
}

// UpdateGitStatus calls UpdateGitStatus on the server
func (gp *APIoverJSONRPC) UpdateGitStatus(ctx context.Context, workspaceID string, status *WorkspaceInstanceRepoStatus) (err error) {
if gp == nil {
Expand Down
15 changes: 0 additions & 15 deletions components/gitpod-protocol/go/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions components/gitpod-protocol/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

export const IAnalyticsWriter = Symbol("IAnalyticsWriter");

type Identity =
| { userId: string | number; anonymousId?: string | number }
| { userId?: string | number; anonymousId: string | number };
type Identity = { userId?: string | number; anonymousId?: string | number; subjectId?: string };

interface Message {
messageId?: string;
Expand Down
131 changes: 131 additions & 0 deletions components/gitpod-protocol/src/auth-providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* 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 { AuthProviderType } from "@gitpod/public-api/lib/gitpod/v1/authprovider_pb";

export namespace GitLabScope {
export const READ_USER = "read_user";
export const API = "api";
export const READ_REPO = "read_repository";

export const ALL = [READ_USER, API, READ_REPO];
/**
* Minimal required permission.
* GitLab API usage requires the permission of a user.
*/
export const DEFAULT = [READ_USER, API];
export const REPO = [API, READ_REPO];
}

export namespace GitHubScope {
export const EMAIL = "user:email";
export const READ_USER = "read:user";
export const PUBLIC = "public_repo";
export const PRIVATE = "repo";
export const ORGS = "read:org";
export const WORKFLOW = "workflow";

export const ALL = [EMAIL, READ_USER, PUBLIC, PRIVATE, ORGS, WORKFLOW];
export const DEFAULT = ALL;
export const PUBLIC_REPO = ALL;
export const PRIVATE_REPO = ALL;
}

export namespace BitbucketOAuthScopes {
// https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html

/** Read user info like name, e-mail adresses etc. */
export const ACCOUNT_READ = "account";
/** Access repo info, clone repo over https, read and write issues */
export const REPOSITORY_READ = "repository";
/** Push over https, fork repo */
export const REPOSITORY_WRITE = "repository:write";
/** Lists and read pull requests */
export const PULL_REQUEST_READ = "pullrequest";
/** Create, comment and merge pull requests */
export const PULL_REQUEST_WRITE = "pullrequest:write";
/** Create, list web hooks */
export const WEBHOOK = "webhook";

export const ALL = [
ACCOUNT_READ,
REPOSITORY_READ,
REPOSITORY_WRITE,
PULL_REQUEST_READ,
PULL_REQUEST_WRITE,
WEBHOOK,
];

export const DEFAULT = ALL;
}

export namespace BitbucketServerOAuthScopes {
// https://confluence.atlassian.com/bitbucketserver/bitbucket-oauth-2-0-provider-api-1108483661.html#BitbucketOAuth2.0providerAPI-scopesScopes

/** View projects and repositories that are publicly accessible, including pulling code and cloning repositories. */
export const PUBLIC_REPOS = "PUBLIC_REPOS";
/** View projects and repositories the user account can view, including pulling code, cloning, and forking repositories. Create and comment on pull requests. */
export const REPO_READ = "REPO_READ";
/** Push over https, fork repo */
export const REPO_WRITE = "REPO_WRITE";

export const REPO_ADMIN = "REPO_ADMIN";
export const PROJECT_ADMIN = "PROJECT_ADMIN";

export const ALL = [PUBLIC_REPOS, REPO_READ, REPO_WRITE, REPO_ADMIN, PROJECT_ADMIN];

export const DEFAULT = ALL;
}

export function getScopesForAuthProviderType(type: AuthProviderType | string) {
switch (type) {
case AuthProviderType.GITHUB:
case "GitHub":
return GitHubScope.ALL;
case AuthProviderType.GITLAB:
case "GitLab":
return GitLabScope.ALL;
case AuthProviderType.BITBUCKET:
case "Bitbucket":
return BitbucketOAuthScopes.ALL;
case AuthProviderType.BITBUCKET_SERVER:
case "BitbucketServer":
return BitbucketServerOAuthScopes.ALL;
}
}

export function getRequiredScopes(type: AuthProviderType | string) {
switch (type) {
case AuthProviderType.GITHUB:
case "GitHub":
return {
default: GitHubScope.DEFAULT,
publicRepo: GitHubScope.PUBLIC_REPO,
privateRepo: GitHubScope.PRIVATE_REPO,
};
case AuthProviderType.GITLAB:
case "GitLab":
return {
default: GitLabScope.DEFAULT,
publicRepo: GitLabScope.DEFAULT,
privateRepo: GitLabScope.REPO,
};
case AuthProviderType.BITBUCKET:
case "Bitbucket":
return {
default: BitbucketOAuthScopes.DEFAULT,
publicRepo: BitbucketOAuthScopes.DEFAULT,
privateRepo: BitbucketOAuthScopes.DEFAULT,
};
case AuthProviderType.BITBUCKET_SERVER:
case "BitbucketServer":
return {
default: BitbucketServerOAuthScopes.DEFAULT,
publicRepo: BitbucketServerOAuthScopes.DEFAULT,
privateRepo: BitbucketServerOAuthScopes.DEFAULT,
};
}
}
4 changes: 3 additions & 1 deletion components/gitpod-protocol/src/gitpod-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ export interface GitpodServer extends JsonRpcServer<GitpodClient>, AdminServer,
getWorkspaceOwner(workspaceId: string): Promise<UserInfo | undefined>;
getWorkspaceUsers(workspaceId: string): Promise<WorkspaceInstanceUser[]>;
getFeaturedRepositories(): Promise<WhitelistedRepository[]>;
getSuggestedContextURLs(): Promise<string[]>;
getSuggestedRepositories(organizationId: string): Promise<SuggestedRepository[]>;
searchRepositories(params: SearchRepositoriesParams): Promise<SuggestedRepository[]>;
/**
Expand Down Expand Up @@ -188,6 +187,7 @@ export interface GitpodServer extends JsonRpcServer<GitpodClient>, AdminServer,
getOnboardingState(): Promise<GitpodServer.OnboardingState>;

// Projects
/** @deprecated no-op */
getProviderRepositoriesForUser(
params: GetProviderRepositoriesParams,
cancellationToken?: CancellationToken,
Expand All @@ -212,7 +212,9 @@ export interface GitpodServer extends JsonRpcServer<GitpodClient>, AdminServer,
deleteGitpodToken(tokenHash: string): Promise<void>;

// misc
/** @deprecated always returns false */
isGitHubAppEnabled(): Promise<boolean>;
/** @deprecated this is a no-op */
registerGithubApp(installationId: string): Promise<void>;

/**
Expand Down
1 change: 1 addition & 0 deletions components/gitpod-protocol/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from "./teams-projects-protocol";
export * from "./snapshot-url";
export * from "./webhook-event";
export * from "./redis";
export * from "./auth-providers";
3 changes: 3 additions & 0 deletions components/gitpod-protocol/src/messaging/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export const ErrorCodes = {
// 404 Not Found
NOT_FOUND: 404 as const,

// 408 Request Timeout
REQUEST_TIMEOUT: 408 as const,

// 409 Conflict (e.g. already existing)
CONFLICT: 409 as const,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ describe("PublicAPIConverter", () => {
expect(result.organizationId).to.equal(project.teamId);
expect(result.name).to.equal(project.name);
expect(result.cloneUrl).to.equal(project.cloneUrl);
expect(result.creationTime).to.deep.equal(Timestamp.fromDate(new Date(project.creationTime)));
expect(result.workspaceSettings).to.deep.equal(
new WorkspaceSettings({
workspaceClass: project.settings?.workspaceClasses?.regular,
Expand Down
4 changes: 4 additions & 0 deletions components/gitpod-protocol/src/public-api-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ export class PublicAPIConverter {
if (reason.code === ErrorCodes.INTERNAL_SERVER_ERROR) {
return new ConnectError(reason.message, Code.Internal, metadata, undefined, reason);
}
if (reason.code === ErrorCodes.REQUEST_TIMEOUT) {
return new ConnectError(reason.message, Code.Canceled, metadata, undefined, reason);
}
return new ConnectError(reason.message, Code.InvalidArgument, metadata, undefined, reason);
}
return ConnectError.from(reason, Code.Internal);
Expand Down Expand Up @@ -403,6 +406,7 @@ export class PublicAPIConverter {
result.organizationId = project.teamId;
result.name = project.name;
result.cloneUrl = project.cloneUrl;
result.creationTime = Timestamp.fromDate(new Date(project.creationTime));
result.workspaceSettings = this.toWorkspaceSettings(project.settings?.workspaceClasses?.regular);
result.prebuildSettings = this.toPrebuildSettings(project.settings?.prebuilds);
return result;
Expand Down
56 changes: 56 additions & 0 deletions components/gitpod-protocol/src/public-api-pagination.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/**
* 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 { expect } from "chai";
import { parsePagination } from "./public-api-pagination";

describe("PublicAPIConverter", () => {
describe("parsePagination", () => {
it("Happy path", () => {
// first path is { page: 0 }
const result = parsePagination({ page: 1, pageSize: 50 }, 50);
expect(result.limit).to.equal(50);
expect(result.offset).to.equal(50);
});

it("Default is more than max, limit to max", () => {
const result = parsePagination({ page: 2 }, 5000);
expect(result.limit).to.equal(100); // MAX_PAGE_SIZE
expect(result.offset).to.equal(200);
});

it("All undefined", () => {
const result = parsePagination({}, 20);
expect(result.limit).to.equal(20);
expect(result.offset).to.equal(0);
});

it("All undefined, default undefined, go to default 50", () => {
const result = parsePagination({});
expect(result.limit).to.equal(50); // DEFAULT_PAGE_SIZE
expect(result.offset).to.equal(0);
});

it("Page less than zero, should go to zero", () => {
const result = parsePagination({ page: -100, pageSize: 50 });
expect(result.limit).to.equal(50);
expect(result.offset).to.equal(0);
});

it("Not integer page to zero", () => {
const result = parsePagination({ page: 0.1, pageSize: 20 });
expect(result.limit).to.equal(20);
expect(result.offset).to.equal(0);
});

it("Not integer pageSize to default", () => {
const result = parsePagination({ page: 1, pageSize: 0.1 });
expect(result.limit).to.equal(50);
expect(result.offset).to.equal(50);
});
});
});
37 changes: 37 additions & 0 deletions components/gitpod-protocol/src/public-api-pagination.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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 { PaginationRequest } from "@gitpod/public-api/lib/gitpod/v1/pagination_pb";

export interface ParsedPagination {
offset: number;
limit: number;
}

const MAX_PAGE_SIZE = 100;
const DEFAULT_PAGE_SIZE = 50;
export function parsePagination(
pagination: Partial<PaginationRequest> | undefined,
defaultPageSize = DEFAULT_PAGE_SIZE,
): ParsedPagination {
let pageSize = pagination?.pageSize ?? defaultPageSize;
if (!Number.isInteger(pageSize)) {
pageSize = defaultPageSize;
}
if (pageSize < 0) {
pageSize = defaultPageSize;
} else if (pageSize > MAX_PAGE_SIZE) {
pageSize = MAX_PAGE_SIZE;
}
let page = pagination?.page ?? 0;
if (!Number.isInteger(page) || (page ?? 0) < 0) {
page = 0;
}
return {
offset: page * pageSize,
limit: pageSize,
};
}
Loading

0 comments on commit 4484109

Please sign in to comment.