Skip to content

Commit 90f3d8f

Browse files
author
guqiankun.gqk
committed
feat: 增加解决冲突能力
1 parent 39d38ec commit 90f3d8f

13 files changed

Lines changed: 329 additions & 52 deletions

File tree

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"generate": "node scripts/generate",
2222
"editor": "INTEGRATION=editor npm run dev",
2323
"acr": "INTEGRATION=antcode-cr npm run dev",
24+
"fs": "INTEGRATION=filesystem npm run dev",
2425
"build": "node scripts/build",
2526
"bundle": "node scripts/bundle",
2627
"create": "node scripts/create",
@@ -224,6 +225,11 @@
224225
"publisher": "alex",
225226
"name": "vditor-markdown",
226227
"version": "0.0.1"
228+
},
229+
{
230+
"publisher": "opensumi-lite-extensions",
231+
"name": "merge-conflict",
232+
"version": "1.0.0"
227233
}
228234
]
229235
}

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

Lines changed: 91 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,16 @@ export class AntCodeAPIService implements ICodeAPIService {
7171
return `${this.config.origin}/${repo.owner}/${repo.name}/raw/${repo.commit}/${path}`;
7272
}
7373

74-
protected async request<T>(path: string, options?: RequestOptions): Promise<T> {
74+
protected async request<T>(
75+
path: string,
76+
options?: RequestOptions,
77+
responseOptions?: API.RequestResponseOptions
78+
): Promise<T> {
7579
const { platform, origin, endpoint } = this.config;
76-
const privateToken = this.PRIVATE_TOKEN;
77-
80+
let privateToken = this.PRIVATE_TOKEN;
81+
if (path.startsWith('/webapi/')) {
82+
privateToken = '';
83+
}
7884
try {
7985
const data = await request(path, {
8086
baseURL: endpoint,
@@ -91,7 +97,7 @@ export class AntCodeAPIService implements ICodeAPIService {
9197
: options),
9298
});
9399
return data;
94-
} catch (err: unknown) {
100+
} catch (err: any) {
95101
if (isResponseError(err)) {
96102
const { status } = err.response;
97103
this.reporter.point(REPORT_NAME.CODE_SERVICE_REQUEST_ERROR, err.message, {
@@ -118,24 +124,17 @@ export class AntCodeAPIService implements ICodeAPIService {
118124
window.open(origin);
119125
}
120126
});
121-
} else if (status === 403) {
122-
this.helper.showMessage(CodePlatform.antcode, {
123-
type: MessageType.Error,
124-
symbol: 'api.response.project-no-access',
125-
status: 401,
126-
});
127-
} else if (status === 404) {
128-
// TODO 更精细化的错误提示
129-
this.helper.showMessage(CodePlatform.antcode, {
130-
type: MessageType.Error,
131-
symbol: 'error.resource-not-found',
132-
status: 404,
133-
});
134127
} else {
128+
if (responseOptions?.errorOption === false) {
129+
// 此处为了web-scm查询 新增文件无需提示
130+
console.log(err);
131+
return undefined as any;
132+
}
135133
this.helper.showMessage(CodePlatform.antcode, {
136134
type: MessageType.Error,
137-
symbol: 'error.request',
138-
status,
135+
status: status,
136+
symbol: err?.message ? '' : 'error.request',
137+
message: err?.message,
139138
});
140139
}
141140
} else {
@@ -185,15 +184,21 @@ export class AntCodeAPIService implements ICodeAPIService {
185184
return new Uint8Array(buf);
186185
}
187186

188-
async getBlobByCommitPath(repo: IRepositoryModel, commit: string, path: string) {
187+
async getBlobByCommitPath(
188+
repo: IRepositoryModel,
189+
commit: string,
190+
path: string,
191+
options?: API.RequestResponseOptions
192+
) {
189193
const buf = await this.request<ArrayBuffer>(
190194
`/api/v3/projects/${this.getProjectId(repo)}/repository/blobs/${commit}`,
191195
{
192196
responseType: 'arrayBuffer',
193197
params: {
194198
filepath: path,
195199
},
196-
}
200+
},
201+
options
197202
);
198203
return new Uint8Array(buf);
199204
}
@@ -391,10 +396,69 @@ export class AntCodeAPIService implements ICodeAPIService {
391396
return await this.request<Project>(`/api/v3/projects/${this.getProjectId(repo)}`);
392397
}
393398

394-
// async function createPullRequest(data: CodeRegistryPullRequestCreateParameters): Promise<CodeRegistryPullRequest> {
395-
// return (await this.request(`/api/v3/projects/${this.getProjectId(repo)}/pull_requests`,{
396-
// data: data,
397-
// method: 'post',
398-
// }));
399-
// }
399+
async canResolveConflict(
400+
repo: IRepositoryModel,
401+
sourceBranch: string,
402+
targetBranch: string,
403+
prId?: string
404+
): Promise<API.CanResolveConflictResponse> {
405+
if (prId) {
406+
return await this.request<API.CanResolveConflictResponse>(
407+
`/api/v3/projects/${this.getProjectId(
408+
repo
409+
)}/pull_requests/${prId}/can_resolve_conflicts_inweb`
410+
);
411+
}
412+
return await this.request<API.CanResolveConflictResponse>(
413+
`/webapi/projects/${this.getProjectId(repo)}/repository/can_resolve_conflicts_inweb`,
414+
{
415+
params: {
416+
source_branch: sourceBranch,
417+
target_branch: targetBranch,
418+
},
419+
}
420+
);
421+
}
422+
423+
async resolveConflict(
424+
repo: IRepositoryModel,
425+
content: API.ResolveConflict,
426+
sourceBranch: string,
427+
targetBranch: string,
428+
prId?: string
429+
): Promise<API.ResolveConflictResponse> {
430+
if (prId) {
431+
return await this.request<API.ResolveConflictResponse>(
432+
`/api/v3/projects/${this.getProjectId(repo)}/pull_requests/${prId}/resolve_conflicts`,
433+
{
434+
method: 'put',
435+
data: content,
436+
responseType: undefined,
437+
}
438+
);
439+
} else {
440+
return await this.request<API.ResolveConflictResponse>(
441+
`/webapi/projects/${this.getProjectId(
442+
repo
443+
)}/repository/resolve_conflicts?source_branch=${sourceBranch}&target_branch=${targetBranch}`,
444+
{
445+
method: 'put',
446+
data: content,
447+
responseType: undefined,
448+
}
449+
);
450+
}
451+
}
452+
453+
async getConflict(
454+
repo: IRepositoryModel,
455+
sourceBranch: string,
456+
targetBranch: string
457+
): Promise<API.ConflictResponse> {
458+
return await this.request<API.ConflictResponse>(
459+
`/api/v3/projects/${this.getProjectId(
460+
repo
461+
)}/merge/conflicts?source_branch=${sourceBranch}&target_branch=${targetBranch}`
462+
);
463+
}
400464
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,38 @@ export namespace API {
8888
web_url: string;
8989
website_url: string;
9090
}
91+
92+
export interface RequestResponseOptions {
93+
errorOption?: boolean;
94+
}
95+
96+
export interface CanResolveConflictResponse {
97+
online_resolve: boolean;
98+
unsupport_type: string;
99+
}
100+
101+
export interface ResolveConflict {
102+
commit_message: string;
103+
files: {
104+
content: string;
105+
our_path: string;
106+
their_path: string;
107+
}[];
108+
head_sha: string;
109+
start_sha: string;
110+
}
111+
112+
export interface ResolveConflictResponse {
113+
body: any;
114+
statusCode: string;
115+
statusCodeValue: number;
116+
}
117+
118+
export type ConflictResponse = Conflict[];
119+
120+
export interface Conflict {
121+
content: string;
122+
our_path: string;
123+
their_path: string;
124+
}
91125
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class HelperService {
9797
msg: { type: MessageType; status?: number; symbol?: string; args?: any[]; message?: string },
9898
config?: { buttons?: string[]; closable?: boolean }
9999
) {
100-
const message = `${status ? `${status} - ` : ''}${
100+
const message = `${msg.status ? `${msg.status} - ` : ''}${
101101
msg.symbol ? localize(msg.symbol, ...(msg.args || [])) : msg.message
102102
}`;
103103
return this.messageService.open(

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { API as AntCodeAPI } from '../antcode/types';
2+
export { AntCodeAPI };
13
type ConstructorOf<T = any> = new (...args: any[]) => T;
24

35
export enum CodePlatform {
@@ -58,6 +60,7 @@ export interface BranchOrTag {
5860
commit: {
5961
id: string;
6062
};
63+
protected?: boolean;
6164
}
6265

6366
export interface RefsParam {
@@ -252,7 +255,12 @@ export interface ICodeAPIService {
252255
* 获取 blob
253256
* git graph 获取文件内容
254257
*/
255-
getBlobByCommitPath(repo: IRepositoryModel, commit: string, path: string): Promise<Uint8Array>;
258+
getBlobByCommitPath(
259+
repo: IRepositoryModel,
260+
commit: string,
261+
path: string,
262+
options?: any
263+
): Promise<Uint8Array>;
256264
/**
257265
* 获取 entry 相关信息
258266
*/
@@ -333,6 +341,33 @@ export interface ICodeAPIService {
333341
* 获取仓库信息
334342
*/
335343
getProject(repo: IRepositoryModel): Promise<Project>;
344+
/* 解决冲突场景 */
345+
346+
// 是否可以在线解决冲突
347+
canResolveConflict(
348+
repo: IRepositoryModel,
349+
sourceBranch: string,
350+
targetBranch: string,
351+
prId: string
352+
): Promise<AntCodeAPI.CanResolveConflictResponse>;
353+
/**
354+
* 解决冲突提交
355+
*/
356+
resolveConflict(
357+
repo: IRepositoryModel,
358+
content: AntCodeAPI.ResolveConflict,
359+
sourceBranch: string,
360+
targetBranch: string,
361+
prId?: string
362+
): Promise<AntCodeAPI.ResolveConflictResponse>;
363+
/**
364+
* 获取解决冲突信息
365+
*/
366+
getConflict(
367+
repo: IRepositoryModel,
368+
sourceBranch: string,
369+
targetBranch: string
370+
): Promise<AntCodeAPI.ConflictResponse>;
336371
}
337372

338373
export interface ICodeAPIServiceProvider extends ICodeAPIService {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import type {
1616
CommitFileChange,
1717
Branch,
1818
Project,
19+
EntryInfo,
1920
} from '../common/types';
2021
import { CodePlatform, CommitFileStatus } from '../common/types';
2122
import { CODE_PLATFORM_CONFIG } from '../common/config';
23+
import { API as ConflictAPI } from '../antcode/types';
2224

2325
const toType = (status: string) => {
2426
switch (status) {
@@ -67,6 +69,28 @@ export class GitHubAPIService implements ICodeAPIService {
6769
constructor() {
6870
this._OAUTH_TOKEN = this.config.token || this.helper.GITHUB_TOKEN;
6971
}
72+
getEntryInfo?(repo: IRepositoryModel, entry: EntryParam): Promise<EntryInfo> {
73+
throw new Error('Method not implemented.');
74+
}
75+
getBranchNames?(repo: IRepositoryModel): Promise<string[]> {
76+
throw new Error('Method not implemented.');
77+
}
78+
canResolveConflict(
79+
repo: IRepositoryModel,
80+
prId: string
81+
): Promise<ConflictAPI.CanResolveConflictResponse> {
82+
throw new Error('Method not implemented.');
83+
}
84+
resolveConflict(): Promise<ConflictAPI.ResolveConflictResponse> {
85+
throw new Error('Method not implemented.');
86+
}
87+
getConflict(
88+
repo: IRepositoryModel,
89+
sourceBranch: string,
90+
targetBranch: string
91+
): Promise<ConflictAPI.ConflictResponse> {
92+
throw new Error('Method not implemented.');
93+
}
7094
async getProject(repo: IRepositoryModel): Promise<Project> {
7195
const data = await this.requestByREST<Project>(`/repos/${this.getProjectPath(repo)}`, {
7296
responseType: 'json',

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import type {
1313
CommitParams,
1414
Branch,
1515
Project,
16+
EntryInfo,
1617
} from '../common/types';
1718
import { CodePlatform, CommitFileStatus } from '../common/types';
19+
import { API as ConflictAPI } from '../antcode/types';
1820

1921
const toType = (d: API.ResponseCommitFileChange) => {
2022
if (d.new_file) return CommitFileStatus.Added;
@@ -64,6 +66,28 @@ export class GitLabAPIService implements ICodeAPIService {
6466
constructor() {
6567
this._PRIVATE_TOKEN = this.config.token || this.helper.GITLAB_TOKEN;
6668
}
69+
getEntryInfo?(repo: IRepositoryModel, entry: EntryParam): Promise<EntryInfo> {
70+
throw new Error('Method not implemented.');
71+
}
72+
getBranchNames?(repo: IRepositoryModel): Promise<string[]> {
73+
throw new Error('Method not implemented.');
74+
}
75+
canResolveConflict(
76+
repo: IRepositoryModel,
77+
prId: string
78+
): Promise<ConflictAPI.CanResolveConflictResponse> {
79+
throw new Error('Method not implemented.');
80+
}
81+
resolveConflict(): Promise<ConflictAPI.ResolveConflictResponse> {
82+
throw new Error('Method not implemented.');
83+
}
84+
getConflict(
85+
repo: IRepositoryModel,
86+
sourceBranch: string,
87+
targetBranch: string
88+
): Promise<ConflictAPI.ConflictResponse> {
89+
throw new Error('Method not implemented.');
90+
}
6791
getUser(repo: IRepositoryModel): Promise<any> {
6892
throw new Error('Method not implemented.');
6993
}

0 commit comments

Comments
 (0)