Skip to content

Commit

Permalink
feat: support register custom code api (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain authored Jun 4, 2024
1 parent 1b26147 commit 442ea26
Show file tree
Hide file tree
Showing 20 changed files with 226 additions and 173 deletions.
14 changes: 4 additions & 10 deletions dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@
"typescript": {
"quoteStyle": "alwaysSingle"
},
"json": {
},
"markdown": {
},
"toml": {
},
"excludes": [
"**/node_modules",
"**/*-lock.json"
],
"json": {},
"markdown": {},
"toml": {},
"excludes": ["**/node_modules", "**/*-lock.json"],
"plugins": [
"https://plugins.dprint.dev/typescript-0.91.0.wasm",
"https://plugins.dprint.dev/json-0.19.3.wasm",
Expand Down
4 changes: 2 additions & 2 deletions packages/code-api/src/atomgit/atomgit.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { request, RequestOptions } from '@codeblitzjs/ide-common';
import { Autowired, Injectable } from '@opensumi/di';
import { isObject, MessageType, URI } from '@opensumi/ide-core-common';
import { CODE_PLATFORM_CONFIG, HelperService } from '../common';
import { CodePlatformRegistry, HelperService } from '../common';
import {
Branch,
BranchOrTag,
Expand All @@ -28,7 +28,7 @@ export class AtomGitAPIService implements ICodeAPIService {
@Autowired(HelperService)
helper: HelperService;

private config = CODE_PLATFORM_CONFIG[CodePlatform.atomgit];
private config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.atomgit);

private _PRIVATE_TOKEN: string | null;

Expand Down
23 changes: 14 additions & 9 deletions packages/code-api/src/code-api.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import { IMainLayoutService } from '@opensumi/ide-main-layout';
import { IIconService } from '@opensumi/ide-theme';
import { AtomGitAPIService } from './atomgit/atomgit.service';
import { CodeUPAPIService } from './codeup/codeup.service';
import { CodePlatform, ICodeAPIProvider, ICodeAPIService, ICodePlatform } from './common/types';
import { CodePlatformRegistry, ICodePlatformConfig } from './common';
import {
CodePlatform,
ICodeAPIProvider,
ICodeAPIService,
ICodePlatform,
ICodePlatformAPIProvider,
} from './common/types';
import { GiteeAPIService } from './gitee/gitee.service';
import { GitHubAPIService } from './github/github.service';
import { GitHubView } from './github/github.view';
Expand All @@ -27,13 +34,10 @@ export class CodeAPIProvider implements ICodeAPIProvider, ClientAppContribution
private started = new Deferred<void>();

private apiProviderMap = new Map<
ICodePlatform,
{
provider: ConstructorOf<ICodeAPIService>;
onCreate?: () => void;
}
string,
ICodePlatformAPIProvider
>();
private apiServiceMap = new Map<ICodePlatform, ICodeAPIService>();
private apiServiceMap = new Map<string, ICodeAPIService>();

constructor() {
this.registerPlatformProvider(CodePlatform.github, {
Expand Down Expand Up @@ -95,8 +99,8 @@ export class CodeAPIProvider implements ICodeAPIProvider, ClientAppContribution
}

registerPlatformProvider(
platform: ICodePlatform,
provider: { provider: ConstructorOf<ICodeAPIService>; onCreate?: () => void },
platform: string,
provider: ICodePlatformAPIProvider,
) {
if (!this.apiProviderMap.has(platform)) {
this.apiProviderMap.set(platform, provider);
Expand Down Expand Up @@ -136,6 +140,7 @@ export class CodeAPIProvider implements ICodeAPIProvider, ClientAppContribution
get codeup() {
return this.asPlatform(CodePlatform.codeup) as CodeUPAPIService;
}

onStart() {
this.started.resolve();
}
Expand Down
7 changes: 2 additions & 5 deletions packages/code-api/src/codeup/codeup.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
CommitParams,
CommitFileStatus,
} from '../common/types';
import { CODE_PLATFORM_CONFIG } from '../common/config';
import { CodePlatformRegistry } from '../common/config';
import { HelperService } from '../common/service';
import { DEFAULT_SEARCH_IN_WORKSPACE_LIMIT } from '@opensumi/ide-search';
const toType = (d: API.ResponseCommitFileChange) => {
Expand Down Expand Up @@ -48,10 +48,7 @@ export class CodeUPAPIService implements ICodeAPIService {
@Autowired(HelperService)
helper: HelperService;



config = CODE_PLATFORM_CONFIG[CodePlatform.codeup];

config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.codeup);

// 只保留上一次的缓存,用于匹配过滤
private readonly searchContentLRU = new LRUCache<string, ISearchResults>(1);
Expand Down
94 changes: 71 additions & 23 deletions packages/code-api/src/common/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CodePlatform, ICodePlatform } from './types';

export interface ICodePlatformConfig {
platform: CodePlatform;
platform: CodePlatform | string;
origin: string;
hostname: string[];
endpoint: string;
Expand All @@ -17,7 +17,7 @@ export interface ICodePlatformConfig {
}

// 代码托管平台配置
export const CODE_PLATFORM_CONFIG: Record<ICodePlatform, ICodePlatformConfig> = {
const CODE_PLATFORM_CONFIG: Record<ICodePlatform, ICodePlatformConfig> = {
[CodePlatform.github]: {
platform: CodePlatform.github,
hostname: ['github.com'],
Expand Down Expand Up @@ -175,29 +175,77 @@ export const CODE_PLATFORM_CONFIG: Record<ICodePlatform, ICodePlatformConfig> =
},
};

export const extendPlatformConfig = (
platform: ICodePlatform,
data: {
hostname?: string[];
origin?: string;
endpoint?: string;
token?: string;
},
) => {
const config = CODE_PLATFORM_CONFIG[platform];
if (!config) {
return;
export class CodePlatformRegistry {
protected platformMap = new Map<string, ICodePlatformConfig>();

protected constructor() {
this.load(CODE_PLATFORM_CONFIG);
}
if (Array.isArray(data.hostname)) {
config.hostname.push(...data.hostname);

protected static _instance: CodePlatformRegistry;
static instance() {
if (!CodePlatformRegistry._instance) {
CodePlatformRegistry._instance = new CodePlatformRegistry();
}
return CodePlatformRegistry._instance;
}
if (data.origin) {
config.origin = data.origin;

registerPlatformConfig(
platform: string,
provider: ICodePlatformConfig,
) {
if (!this.platformMap.has(platform)) {
this.platformMap.set(platform, provider);
}
}
if (data.endpoint) {
config.endpoint = data.endpoint;

getPlatformConfig(platform: string): ICodePlatformConfig {
if (this.platformMap.has(platform)) {
return this.platformMap.get(platform)!;
}
throw new Error(`[Code API]: no config found for ${platform}`);
}
if (data.token) {
config.token = data.token;

extendPlatformConfig(
platform: string,
data: {
hostname?: string[] | undefined;
origin?: string | undefined;
endpoint?: string | undefined;
token?: string | undefined;
},
): void {
const provider = this.platformMap.get(platform);
if (!provider) {
return;
}
if (Array.isArray(data.hostname)) {
provider.hostname.push(...data.hostname);
}
if (data.origin) {
provider.origin = data.origin;
}
if (data.endpoint) {
provider.endpoint = data.endpoint;
}
if (data.token) {
provider.token = data.token;
}
}
};

load(configs: Record<string, ICodePlatformConfig>) {
Object.keys(configs).forEach((key) => {
this.registerPlatformConfig(key, configs[key]);
});
}

getCodePlatformConfigs(): Record<string, ICodePlatformConfig> {
const result = {} as Record<string, ICodePlatformConfig>;

this.platformMap.forEach((config, key) => {
result[key] = config;
});

return result;
}
}
8 changes: 5 additions & 3 deletions packages/code-api/src/common/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '@opensumi/ide-core-common';
import { IDialogService, IMessageService } from '@opensumi/ide-overlay';
import { DialogService } from '@opensumi/ide-overlay/lib/browser/dialog.service';
import { CODE_PLATFORM_CONFIG } from './config';
import { CodePlatformRegistry } from './config';
import { ATOMGIT_PRIVATE_TOKEN, GITEE_PRIVATE_TOKEN, GITHUB_OAUTH_TOKEN, GITLAB_PRIVATE_TOKEN } from './constant';
import { ICodePlatform } from './types';

Expand Down Expand Up @@ -121,19 +121,21 @@ export class HelperService {
}

showMessage(
platform: ICodePlatform,
platform: ICodePlatform | string,
msg: { type: MessageType; status?: number; symbol?: string; args?: any[]; message?: string },
config?: { buttons?: string[]; closable?: boolean },
) {
const message = `${msg.status ? `${msg.status} - ` : ''}${
msg.symbol ? localize(msg.symbol, ...(msg.args || [])) : msg.message
}`;
const platformConfig = CodePlatformRegistry.instance().getPlatformConfig(platform);

return this.messageService.open(
message,
msg.type,
config?.buttons,
config?.closable,
CODE_PLATFORM_CONFIG[platform].brand,
platformConfig.brand,
);
}

Expand Down
17 changes: 12 additions & 5 deletions packages/code-api/src/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ICodePlatformConfig } from './config';

export namespace CodeAPI {
interface Entry {
id: string;
Expand Down Expand Up @@ -209,7 +211,7 @@ export type ISearchResults = Array<{
}>;

export interface IRepositoryModel {
platform: ICodePlatform;
platform: ICodePlatform | string;
owner: string;
name: string;
commit: string;
Expand Down Expand Up @@ -246,7 +248,7 @@ export interface CommitFileChange {
deletions: number | null;
}

export const enum CommitFileStatus {
export enum CommitFileStatus {
Added = 'A',
Modified = 'M',
Deleted = 'D',
Expand Down Expand Up @@ -344,14 +346,19 @@ export interface Iteration {
}
export enum IterationPlatform {}

export interface ICodePlatformAPIProvider {
provider: ConstructorOf<ICodeAPIService>;
onCreate?: () => void;
}

export const ICodeAPIProvider = Symbol('ICodeAPIProvider');

export interface ICodeAPIProvider {
registerPlatformProvider(
platform: ICodePlatform,
provider: { provider: ConstructorOf<ICodeAPIService>; onView?: () => void },
platform: string,
provider: ICodePlatformAPIProvider,
): void;
asPlatform(platform: ICodePlatform): ICodeAPIService;
asPlatform(platform: string): ICodeAPIService;
}

export interface RequestFailed {
Expand Down
4 changes: 2 additions & 2 deletions packages/code-api/src/gitee/gitee.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
CodeAPI,
} from '../common/types';
import { request, RequestOptions } from '@codeblitzjs/ide-common';
import { CODE_PLATFORM_CONFIG, HelperService } from '../common';
import { CodePlatformRegistry, HelperService } from '../common';
import { URI, MessageType, isObject } from '@opensumi/ide-core-common';
import { API } from './types';

Expand All @@ -43,7 +43,7 @@ export class GiteeAPIService implements ICodeAPIService {
@Autowired(HelperService)
helper: HelperService;

private config = CODE_PLATFORM_CONFIG[CodePlatform.gitee];
private config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.gitee);

private _PRIVATE_TOKEN: string | null;

Expand Down
4 changes: 2 additions & 2 deletions packages/code-api/src/github/github.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from '../common/types';
import type { IRepositoryModel, EntryParam } from '../common/types';
import { CodePlatform, CommitFileStatus, CodeAPI as ConflictAPI } from '../common/types';
import { CODE_PLATFORM_CONFIG } from '../common/config';
import { CodePlatformRegistry } from '../common/config';

const toType = (status: string) => {
switch (status) {
Expand Down Expand Up @@ -52,7 +52,7 @@ export class GitHubAPIService implements ICodeAPIService {
@Autowired(CommandService)
commandService: CommandService;

private config = CODE_PLATFORM_CONFIG[CodePlatform.github];
private config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.github);

/** 资源限制信息 */
@observable
Expand Down
4 changes: 2 additions & 2 deletions packages/code-api/src/gitlab/gitlab.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { localize, IReporterService, formatLocalize, MessageType } from '@opensu
import { request, RequestOptions } from '@codeblitzjs/ide-common';
import { API } from './types';
import { HelperService } from '../common/service';
import { CODE_PLATFORM_CONFIG } from '../common/config';
import { CodePlatformRegistry } from '../common/config';
import type {
TreeEntry,
EntryParam,
Expand Down Expand Up @@ -51,7 +51,7 @@ export class GitLabAPIService implements ICodeAPIService {
@Autowired()
helper: HelperService;

private config = CODE_PLATFORM_CONFIG[CodePlatform.gitlab];
private config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.gitlab);

private _PRIVATE_TOKEN: string | null;

Expand Down
4 changes: 2 additions & 2 deletions packages/code-api/src/gitlink/gitlink.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { request, RequestOptions } from '@codeblitzjs/ide-common';
import { Autowired, Injectable } from '@opensumi/di';
import { formatLocalize, IReporterService, MessageType } from '@opensumi/ide-core-common';
import { DEFAULT_SEARCH_IN_WORKSPACE_LIMIT } from '@opensumi/ide-search';
import { CODE_PLATFORM_CONFIG } from '../common/config';
import { CodePlatformRegistry } from '../common/config';
import { HelperService } from '../common/service';
import type {
BranchOrTag,
Expand Down Expand Up @@ -45,7 +45,7 @@ export class GitLinkAPIService implements ICodeAPIService {
@Autowired()
helper: HelperService;

private config = CODE_PLATFORM_CONFIG[CodePlatform.gitlink];
private config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.gitlink);

private _PRIVATE_TOKEN: string | null;

Expand Down
Loading

0 comments on commit 442ea26

Please sign in to comment.