Skip to content

Commit e11a5a9

Browse files
author
winjo
committed
feat(code-service): 增加内置 blame 和 link 命令
1 parent c3ade56 commit e11a5a9

7 files changed

Lines changed: 128 additions & 0 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,17 @@ export class AntCodeAPIService implements ICodeAPIService {
210210
);
211211
return reqRes;
212212
}
213+
214+
async getFileBlame(repo: IRepositoryModel, path: string) {
215+
return new Uint8Array(
216+
await this.request(
217+
`/api/v3/projects/${this.getProjectId(repo)}/repository/blame?sha=${
218+
repo.commit
219+
}&file_path=${path}`,
220+
{
221+
responseType: 'arrayBuffer',
222+
}
223+
)
224+
);
225+
}
213226
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ export interface ICodeAPIService {
133133
searchString: string,
134134
options: { limit: number }
135135
): Promise<string[]>;
136+
/**
137+
* file blame
138+
*/
139+
getFileBlame(repo: IRepositoryModel, filepath: string): Promise<Uint8Array | void>;
136140
}
137141

138142
export interface ICodeAPIServiceProvider extends ICodeAPIService {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,4 +540,7 @@ export class GitHubAPIService implements ICodeAPIService {
540540
}
541541
return this.recursiveTreeMap.get(key)!;
542542
}
543+
544+
// TODO: graphql 下才支持
545+
async getFileBlame() {}
543546
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,7 @@ export class GitLabAPIService implements ICodeAPIService {
236236
async searchFile() {
237237
return [];
238238
}
239+
240+
// 不支持
241+
async getFileBlame() {}
239242
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { Autowired } from '@ali/common-di';
2+
import {
3+
CommandContribution,
4+
Domain,
5+
CommandRegistry,
6+
Command,
7+
Disposable,
8+
} from '@ali/ide-core-common';
9+
import { IOpenerService } from '@ali/ide-core-browser';
10+
import { CODE_PLATFORM_CONFIG, CodePlatform } from '@alipay/alex-code-api';
11+
import { CodeModelService } from './code-model.service';
12+
13+
// 对外暴露的服务命令
14+
namespace CODE_SERVICE_COMMANDS {
15+
const CATEGORY = 'CodeService';
16+
17+
export const BLAME: Command = {
18+
id: 'codeService.getFileBlame',
19+
category: CATEGORY,
20+
};
21+
22+
export type BLAME = (filepath: string) => Promise<Uint8Array | void>;
23+
24+
export const OPEN_IN_REMOTE: Command = {
25+
id: 'codeService.openInRemote',
26+
category: CATEGORY,
27+
};
28+
29+
export type OPEN_IN_REMOTE = (filepath: string, res: RemoteResource) => void;
30+
}
31+
32+
export enum RemoteResourceType {
33+
Branch = 'branch',
34+
Branches = 'branches',
35+
Commit = 'commit',
36+
File = 'file',
37+
Repo = 'repo',
38+
Revision = 'revision',
39+
}
40+
41+
export type RemoteResource =
42+
| {
43+
type: RemoteResourceType.Branch;
44+
branch: string;
45+
}
46+
| {
47+
type: RemoteResourceType.Branches;
48+
}
49+
| {
50+
type: RemoteResourceType.Commit;
51+
sha: string;
52+
}
53+
| {
54+
type: RemoteResourceType.File;
55+
branch?: string;
56+
fileName: string;
57+
range?: Range;
58+
}
59+
| {
60+
type: RemoteResourceType.Repo;
61+
};
62+
63+
@Domain(CommandContribution)
64+
export class CommandsContribution extends Disposable implements CommandContribution {
65+
@Autowired(CodeModelService)
66+
private readonly codeModel: CodeModelService;
67+
68+
@Autowired(IOpenerService)
69+
private readonly openerService: IOpenerService;
70+
71+
registerCommands(registry: CommandRegistry) {
72+
this.addDispose([
73+
// TODO: 这里直接返回 Uint8Array,减少序列化耗时,但考虑到多平台又需转换成统一格式数据
74+
registry.registerCommand<CODE_SERVICE_COMMANDS.BLAME>(CODE_SERVICE_COMMANDS.BLAME, {
75+
execute: async (filepath) => {
76+
const repo = this.codeModel.getRepository(filepath);
77+
if (repo && repo.platform === CodePlatform.antcode) {
78+
return repo.request.getFileBlame(repo.asRelativePath(filepath));
79+
}
80+
},
81+
}),
82+
83+
registry.registerCommand<CODE_SERVICE_COMMANDS.OPEN_IN_REMOTE>(
84+
CODE_SERVICE_COMMANDS.OPEN_IN_REMOTE,
85+
{
86+
execute: (filepath, res) => {
87+
const repo = this.codeModel.getRepository(filepath);
88+
if (repo) {
89+
if (res.type === RemoteResourceType.Commit) {
90+
const { origin } = CODE_PLATFORM_CONFIG[repo.platform];
91+
this.openerService.open(`${origin}/${repo.owner}/${repo.name}/commit/${res.sha}`);
92+
}
93+
}
94+
},
95+
}
96+
),
97+
]);
98+
}
99+
}

packages/code-service/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { ICodeServiceConfig } from './types';
1414
import { StatusbarContribution } from './statusbar';
1515
import { DecorationProvider } from './decoration.provider';
1616
import { LineDecorationContribution } from './line-decoration.contribution';
17+
import { CommandsContribution } from './commands.contribution';
1718

1819
export { CodeModelService };
1920

@@ -43,6 +44,7 @@ export class CodeServiceModule extends BrowserModule {
4344
DecorationProvider,
4445
SearchContribution,
4546
LineDecorationContribution,
47+
CommandsContribution,
4648
{
4749
token: ContentSearchServerPath,
4850
useClass: ContentSearchService,

packages/code-service/src/repository.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ export class Repository implements IRepositoryModel {
122122
return [];
123123
}
124124
}
125+
126+
asRelativePath(absolutePath: string) {
127+
return path.relative(this.root, absolutePath);
128+
}
125129
}
126130

127131
@Injectable({ multiple: true })

0 commit comments

Comments
 (0)