Skip to content

Commit 442ea26

Browse files
authored
feat: support register custom code api (#86)
1 parent 1b26147 commit 442ea26

20 files changed

Lines changed: 226 additions & 173 deletions

dprint.json

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22
"typescript": {
33
"quoteStyle": "alwaysSingle"
44
},
5-
"json": {
6-
},
7-
"markdown": {
8-
},
9-
"toml": {
10-
},
11-
"excludes": [
12-
"**/node_modules",
13-
"**/*-lock.json"
14-
],
5+
"json": {},
6+
"markdown": {},
7+
"toml": {},
8+
"excludes": ["**/node_modules", "**/*-lock.json"],
159
"plugins": [
1610
"https://plugins.dprint.dev/typescript-0.91.0.wasm",
1711
"https://plugins.dprint.dev/json-0.19.3.wasm",

packages/code-api/src/atomgit/atomgit.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { request, RequestOptions } from '@codeblitzjs/ide-common';
22
import { Autowired, Injectable } from '@opensumi/di';
33
import { isObject, MessageType, URI } from '@opensumi/ide-core-common';
4-
import { CODE_PLATFORM_CONFIG, HelperService } from '../common';
4+
import { CodePlatformRegistry, HelperService } from '../common';
55
import {
66
Branch,
77
BranchOrTag,
@@ -28,7 +28,7 @@ export class AtomGitAPIService implements ICodeAPIService {
2828
@Autowired(HelperService)
2929
helper: HelperService;
3030

31-
private config = CODE_PLATFORM_CONFIG[CodePlatform.atomgit];
31+
private config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.atomgit);
3232

3333
private _PRIVATE_TOKEN: string | null;
3434

packages/code-api/src/code-api.provider.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import { IMainLayoutService } from '@opensumi/ide-main-layout';
55
import { IIconService } from '@opensumi/ide-theme';
66
import { AtomGitAPIService } from './atomgit/atomgit.service';
77
import { CodeUPAPIService } from './codeup/codeup.service';
8-
import { CodePlatform, ICodeAPIProvider, ICodeAPIService, ICodePlatform } from './common/types';
8+
import { CodePlatformRegistry, ICodePlatformConfig } from './common';
9+
import {
10+
CodePlatform,
11+
ICodeAPIProvider,
12+
ICodeAPIService,
13+
ICodePlatform,
14+
ICodePlatformAPIProvider,
15+
} from './common/types';
916
import { GiteeAPIService } from './gitee/gitee.service';
1017
import { GitHubAPIService } from './github/github.service';
1118
import { GitHubView } from './github/github.view';
@@ -27,13 +34,10 @@ export class CodeAPIProvider implements ICodeAPIProvider, ClientAppContribution
2734
private started = new Deferred<void>();
2835

2936
private apiProviderMap = new Map<
30-
ICodePlatform,
31-
{
32-
provider: ConstructorOf<ICodeAPIService>;
33-
onCreate?: () => void;
34-
}
37+
string,
38+
ICodePlatformAPIProvider
3539
>();
36-
private apiServiceMap = new Map<ICodePlatform, ICodeAPIService>();
40+
private apiServiceMap = new Map<string, ICodeAPIService>();
3741

3842
constructor() {
3943
this.registerPlatformProvider(CodePlatform.github, {
@@ -95,8 +99,8 @@ export class CodeAPIProvider implements ICodeAPIProvider, ClientAppContribution
9599
}
96100

97101
registerPlatformProvider(
98-
platform: ICodePlatform,
99-
provider: { provider: ConstructorOf<ICodeAPIService>; onCreate?: () => void },
102+
platform: string,
103+
provider: ICodePlatformAPIProvider,
100104
) {
101105
if (!this.apiProviderMap.has(platform)) {
102106
this.apiProviderMap.set(platform, provider);
@@ -136,6 +140,7 @@ export class CodeAPIProvider implements ICodeAPIProvider, ClientAppContribution
136140
get codeup() {
137141
return this.asPlatform(CodePlatform.codeup) as CodeUPAPIService;
138142
}
143+
139144
onStart() {
140145
this.started.resolve();
141146
}

packages/code-api/src/codeup/codeup.service.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
CommitParams,
1616
CommitFileStatus,
1717
} from '../common/types';
18-
import { CODE_PLATFORM_CONFIG } from '../common/config';
18+
import { CodePlatformRegistry } from '../common/config';
1919
import { HelperService } from '../common/service';
2020
import { DEFAULT_SEARCH_IN_WORKSPACE_LIMIT } from '@opensumi/ide-search';
2121
const toType = (d: API.ResponseCommitFileChange) => {
@@ -48,10 +48,7 @@ export class CodeUPAPIService implements ICodeAPIService {
4848
@Autowired(HelperService)
4949
helper: HelperService;
5050

51-
52-
53-
config = CODE_PLATFORM_CONFIG[CodePlatform.codeup];
54-
51+
config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.codeup);
5552

5653
// 只保留上一次的缓存,用于匹配过滤
5754
private readonly searchContentLRU = new LRUCache<string, ISearchResults>(1);

packages/code-api/src/common/config.ts

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { CodePlatform, ICodePlatform } from './types';
22

33
export interface ICodePlatformConfig {
4-
platform: CodePlatform;
4+
platform: CodePlatform | string;
55
origin: string;
66
hostname: string[];
77
endpoint: string;
@@ -17,7 +17,7 @@ export interface ICodePlatformConfig {
1717
}
1818

1919
// 代码托管平台配置
20-
export const CODE_PLATFORM_CONFIG: Record<ICodePlatform, ICodePlatformConfig> = {
20+
const CODE_PLATFORM_CONFIG: Record<ICodePlatform, ICodePlatformConfig> = {
2121
[CodePlatform.github]: {
2222
platform: CodePlatform.github,
2323
hostname: ['github.com'],
@@ -175,29 +175,77 @@ export const CODE_PLATFORM_CONFIG: Record<ICodePlatform, ICodePlatformConfig> =
175175
},
176176
};
177177

178-
export const extendPlatformConfig = (
179-
platform: ICodePlatform,
180-
data: {
181-
hostname?: string[];
182-
origin?: string;
183-
endpoint?: string;
184-
token?: string;
185-
},
186-
) => {
187-
const config = CODE_PLATFORM_CONFIG[platform];
188-
if (!config) {
189-
return;
178+
export class CodePlatformRegistry {
179+
protected platformMap = new Map<string, ICodePlatformConfig>();
180+
181+
protected constructor() {
182+
this.load(CODE_PLATFORM_CONFIG);
190183
}
191-
if (Array.isArray(data.hostname)) {
192-
config.hostname.push(...data.hostname);
184+
185+
protected static _instance: CodePlatformRegistry;
186+
static instance() {
187+
if (!CodePlatformRegistry._instance) {
188+
CodePlatformRegistry._instance = new CodePlatformRegistry();
189+
}
190+
return CodePlatformRegistry._instance;
193191
}
194-
if (data.origin) {
195-
config.origin = data.origin;
192+
193+
registerPlatformConfig(
194+
platform: string,
195+
provider: ICodePlatformConfig,
196+
) {
197+
if (!this.platformMap.has(platform)) {
198+
this.platformMap.set(platform, provider);
199+
}
196200
}
197-
if (data.endpoint) {
198-
config.endpoint = data.endpoint;
201+
202+
getPlatformConfig(platform: string): ICodePlatformConfig {
203+
if (this.platformMap.has(platform)) {
204+
return this.platformMap.get(platform)!;
205+
}
206+
throw new Error(`[Code API]: no config found for ${platform}`);
199207
}
200-
if (data.token) {
201-
config.token = data.token;
208+
209+
extendPlatformConfig(
210+
platform: string,
211+
data: {
212+
hostname?: string[] | undefined;
213+
origin?: string | undefined;
214+
endpoint?: string | undefined;
215+
token?: string | undefined;
216+
},
217+
): void {
218+
const provider = this.platformMap.get(platform);
219+
if (!provider) {
220+
return;
221+
}
222+
if (Array.isArray(data.hostname)) {
223+
provider.hostname.push(...data.hostname);
224+
}
225+
if (data.origin) {
226+
provider.origin = data.origin;
227+
}
228+
if (data.endpoint) {
229+
provider.endpoint = data.endpoint;
230+
}
231+
if (data.token) {
232+
provider.token = data.token;
233+
}
202234
}
203-
};
235+
236+
load(configs: Record<string, ICodePlatformConfig>) {
237+
Object.keys(configs).forEach((key) => {
238+
this.registerPlatformConfig(key, configs[key]);
239+
});
240+
}
241+
242+
getCodePlatformConfigs(): Record<string, ICodePlatformConfig> {
243+
const result = {} as Record<string, ICodePlatformConfig>;
244+
245+
this.platformMap.forEach((config, key) => {
246+
result[key] = config;
247+
});
248+
249+
return result;
250+
}
251+
}

packages/code-api/src/common/service.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from '@opensumi/ide-core-common';
1313
import { IDialogService, IMessageService } from '@opensumi/ide-overlay';
1414
import { DialogService } from '@opensumi/ide-overlay/lib/browser/dialog.service';
15-
import { CODE_PLATFORM_CONFIG } from './config';
15+
import { CodePlatformRegistry } from './config';
1616
import { ATOMGIT_PRIVATE_TOKEN, GITEE_PRIVATE_TOKEN, GITHUB_OAUTH_TOKEN, GITLAB_PRIVATE_TOKEN } from './constant';
1717
import { ICodePlatform } from './types';
1818

@@ -121,19 +121,21 @@ export class HelperService {
121121
}
122122

123123
showMessage(
124-
platform: ICodePlatform,
124+
platform: ICodePlatform | string,
125125
msg: { type: MessageType; status?: number; symbol?: string; args?: any[]; message?: string },
126126
config?: { buttons?: string[]; closable?: boolean },
127127
) {
128128
const message = `${msg.status ? `${msg.status} - ` : ''}${
129129
msg.symbol ? localize(msg.symbol, ...(msg.args || [])) : msg.message
130130
}`;
131+
const platformConfig = CodePlatformRegistry.instance().getPlatformConfig(platform);
132+
131133
return this.messageService.open(
132134
message,
133135
msg.type,
134136
config?.buttons,
135137
config?.closable,
136-
CODE_PLATFORM_CONFIG[platform].brand,
138+
platformConfig.brand,
137139
);
138140
}
139141

packages/code-api/src/common/types.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ICodePlatformConfig } from './config';
2+
13
export namespace CodeAPI {
24
interface Entry {
35
id: string;
@@ -209,7 +211,7 @@ export type ISearchResults = Array<{
209211
}>;
210212

211213
export interface IRepositoryModel {
212-
platform: ICodePlatform;
214+
platform: ICodePlatform | string;
213215
owner: string;
214216
name: string;
215217
commit: string;
@@ -246,7 +248,7 @@ export interface CommitFileChange {
246248
deletions: number | null;
247249
}
248250

249-
export const enum CommitFileStatus {
251+
export enum CommitFileStatus {
250252
Added = 'A',
251253
Modified = 'M',
252254
Deleted = 'D',
@@ -344,14 +346,19 @@ export interface Iteration {
344346
}
345347
export enum IterationPlatform {}
346348

349+
export interface ICodePlatformAPIProvider {
350+
provider: ConstructorOf<ICodeAPIService>;
351+
onCreate?: () => void;
352+
}
353+
347354
export const ICodeAPIProvider = Symbol('ICodeAPIProvider');
348355

349356
export interface ICodeAPIProvider {
350357
registerPlatformProvider(
351-
platform: ICodePlatform,
352-
provider: { provider: ConstructorOf<ICodeAPIService>; onView?: () => void },
358+
platform: string,
359+
provider: ICodePlatformAPIProvider,
353360
): void;
354-
asPlatform(platform: ICodePlatform): ICodeAPIService;
361+
asPlatform(platform: string): ICodeAPIService;
355362
}
356363

357364
export interface RequestFailed {

packages/code-api/src/gitee/gitee.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
CodeAPI,
2222
} from '../common/types';
2323
import { request, RequestOptions } from '@codeblitzjs/ide-common';
24-
import { CODE_PLATFORM_CONFIG, HelperService } from '../common';
24+
import { CodePlatformRegistry, HelperService } from '../common';
2525
import { URI, MessageType, isObject } from '@opensumi/ide-core-common';
2626
import { API } from './types';
2727

@@ -43,7 +43,7 @@ export class GiteeAPIService implements ICodeAPIService {
4343
@Autowired(HelperService)
4444
helper: HelperService;
4545

46-
private config = CODE_PLATFORM_CONFIG[CodePlatform.gitee];
46+
private config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.gitee);
4747

4848
private _PRIVATE_TOKEN: string | null;
4949

packages/code-api/src/github/github.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
} from '../common/types';
2525
import type { IRepositoryModel, EntryParam } from '../common/types';
2626
import { CodePlatform, CommitFileStatus, CodeAPI as ConflictAPI } from '../common/types';
27-
import { CODE_PLATFORM_CONFIG } from '../common/config';
27+
import { CodePlatformRegistry } from '../common/config';
2828

2929
const toType = (status: string) => {
3030
switch (status) {
@@ -52,7 +52,7 @@ export class GitHubAPIService implements ICodeAPIService {
5252
@Autowired(CommandService)
5353
commandService: CommandService;
5454

55-
private config = CODE_PLATFORM_CONFIG[CodePlatform.github];
55+
private config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.github);
5656

5757
/** 资源限制信息 */
5858
@observable

packages/code-api/src/gitlab/gitlab.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { localize, IReporterService, formatLocalize, MessageType } from '@opensu
33
import { request, RequestOptions } from '@codeblitzjs/ide-common';
44
import { API } from './types';
55
import { HelperService } from '../common/service';
6-
import { CODE_PLATFORM_CONFIG } from '../common/config';
6+
import { CodePlatformRegistry } from '../common/config';
77
import type {
88
TreeEntry,
99
EntryParam,
@@ -51,7 +51,7 @@ export class GitLabAPIService implements ICodeAPIService {
5151
@Autowired()
5252
helper: HelperService;
5353

54-
private config = CODE_PLATFORM_CONFIG[CodePlatform.gitlab];
54+
private config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.gitlab);
5555

5656
private _PRIVATE_TOKEN: string | null;
5757

0 commit comments

Comments
 (0)