-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
238 additions
and
50 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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, | ||
}; | ||
} | ||
} |
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
56 changes: 56 additions & 0 deletions
56
components/gitpod-protocol/src/public-api-pagination.spec.ts
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,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); | ||
}); | ||
}); | ||
}); |
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,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, | ||
}; | ||
} |
Oops, something went wrong.