Skip to content

Commit 937c53d

Browse files
author
winjo
committed
feat(code-service): 增加 commit 等 api 和 command
1 parent 029ccce commit 937c53d

18 files changed

Lines changed: 860 additions & 58 deletions

File tree

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@
119119
"publisher": "cloud-ide",
120120
"name": "vscode-lsif",
121121
"version": "0.1.6"
122+
},
123+
{
124+
"publisher": "alex",
125+
"name": "gitlens",
126+
"version": "10.2.3"
122127
}
123128
]
124129
}

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,34 @@ import {
1111
CodePlatform,
1212
IRepositoryModel,
1313
BranchOrTag,
14+
CommitParams,
15+
CommitFileStatus,
1416
} from '../common/types';
1517
import { CODE_PLATFORM_CONFIG } from '../common/config';
1618
import { HelperService } from '../common/service';
1719

20+
const toType = (d: API.ResponseCommitFileChange) => {
21+
if (d.new_file) return CommitFileStatus.Added;
22+
else if (d.deleted_file) return CommitFileStatus.Deleted;
23+
else if (d.renamed_file) return CommitFileStatus.Renamed;
24+
return CommitFileStatus.Modified;
25+
};
26+
27+
const toChangeLines = (diff: string) => {
28+
const diffLines = diff ? diff.split('\n') : [];
29+
return diffLines.reduce(
30+
(obj, line) => {
31+
if (line.startsWith('+')) {
32+
obj.additions += 1;
33+
} else if (line.startsWith('-')) {
34+
obj.deletions += 1;
35+
}
36+
return obj;
37+
},
38+
{ additions: 0, deletions: 0 }
39+
);
40+
};
41+
1842
@Injectable()
1943
export class AntCodeAPIService implements ICodeAPIService {
2044
@Autowired(IReporterService)
@@ -140,6 +164,19 @@ export class AntCodeAPIService implements ICodeAPIService {
140164
return new Uint8Array(buf);
141165
}
142166

167+
async getBlobByCommitPath(repo: IRepositoryModel, commit: string, path: string) {
168+
const buf = await this.request<ArrayBuffer>(
169+
`/api/v3/projects/${this.getProjectId(repo)}/repository/blobs/${commit}`,
170+
{
171+
responseType: 'arrayBuffer',
172+
params: {
173+
filepath: path,
174+
},
175+
}
176+
);
177+
return new Uint8Array(buf);
178+
}
179+
143180
async getEntryInfo(repo: IRepositoryModel, entry: EntryParam) {
144181
const data = await this.request<API.ResponseGetEntry>(
145182
`/api/v3/projects/${this.getProjectId(repo)}/repository/tree_entry`,
@@ -162,6 +199,12 @@ export class AntCodeAPIService implements ICodeAPIService {
162199
);
163200
}
164201

202+
async getBranchNames(repo: IRepositoryModel): Promise<string[]> {
203+
return this.request<string[]>(
204+
`/api/v3/projects/${this.getProjectId(repo)}/repository/branches_names`
205+
);
206+
}
207+
165208
async getTags(repo: IRepositoryModel): Promise<BranchOrTag[]> {
166209
return this.request<API.ResponseGetRefs>(
167210
`/api/v3/projects/${this.getProjectId(repo)}/repository/tags`
@@ -223,4 +266,57 @@ export class AntCodeAPIService implements ICodeAPIService {
223266
)
224267
);
225268
}
269+
270+
async getCommits(repo: IRepositoryModel, params: CommitParams) {
271+
const data = await this.request<API.ResponseCommit[]>(
272+
`/api/v3/projects/${this.getProjectId(repo)}/repository/commits`,
273+
{
274+
params: {
275+
ref_name: params.ref,
276+
path: params.path,
277+
page: params.page,
278+
per_page: params.pageSize,
279+
},
280+
}
281+
);
282+
return data.map((c) => ({
283+
id: c.id,
284+
parents: c.parent_ids,
285+
author: c.author_name,
286+
authorEmail: c.author_email,
287+
authorDate: c.authored_date,
288+
committer: c.committer_name,
289+
committerEmail: c.committer_email,
290+
committerDate: c.committed_date,
291+
message: c.message,
292+
title: c.title,
293+
}));
294+
}
295+
296+
async getCommitDiff(repo: IRepositoryModel, sha: string) {
297+
const data = await this.request<API.ResponseCommitFileChange[]>(
298+
`/api/v3/projects/${this.getProjectId(repo)}/repository/commits/${sha}/diff`
299+
);
300+
return data.map((d) => ({
301+
oldFilePath: d.old_path,
302+
newFilePath: d.new_path,
303+
type: toType(d),
304+
...toChangeLines(d.diff),
305+
}));
306+
}
307+
308+
async getCommitCompare(repo: IRepositoryModel, from: string, to: string) {
309+
const data = await this.request<API.ResponseCommitCompare>(
310+
`/api/v3/projects/${this.getProjectId(repo)}/repository/compare`,
311+
{
312+
params: { from, to },
313+
}
314+
);
315+
return (data.diffs || []).map((d) => ({
316+
oldFilePath: d.old_path,
317+
newFilePath: d.new_path,
318+
type: toType(d),
319+
...toChangeLines(d.diff),
320+
}));
321+
}
226322
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,41 @@ export namespace API {
3333
path: string;
3434
ref: string;
3535
}>;
36+
37+
export interface ResponseCommit {
38+
author_email: string;
39+
author_name: string;
40+
authored_date: string;
41+
committed_date: string;
42+
committer_email: string;
43+
committer_name: string;
44+
created_at: string;
45+
id: string;
46+
message: string;
47+
parent_ids: string[];
48+
short_id: string;
49+
title: string;
50+
}
51+
52+
export interface ResponseCommitFileChange {
53+
a_mode: string;
54+
b_mode: string;
55+
binary_file: boolean;
56+
charset_name: string;
57+
deleted_file: boolean;
58+
diff: string;
59+
new_file: boolean;
60+
new_path: string;
61+
old_path: string;
62+
renamed_file: boolean;
63+
too_large: boolean;
64+
}
65+
66+
export interface ResponseCommitCompare extends ResponseCommit {
67+
commits: ResponseCommit[];
68+
diffs: ResponseCommitFileChange[];
69+
compare_overflow: boolean;
70+
compare_same_ref: boolean;
71+
compare_timeout: boolean;
72+
}
3673
}

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

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,42 @@ export interface IRepositoryModel {
7474
commit: string;
7575
}
7676

77+
export interface CommitParams {
78+
ref?: string;
79+
path?: string;
80+
page: number;
81+
pageSize: number;
82+
}
83+
84+
export interface CommitRecord {
85+
id: string;
86+
parents: ReadonlyArray<string>;
87+
author: string;
88+
authorEmail: string;
89+
authorDate: string;
90+
committer: string;
91+
committerEmail: string;
92+
committerDate: string;
93+
// signature: null; // GPA 签名
94+
message: string;
95+
title?: string;
96+
}
97+
98+
export interface CommitFileChange {
99+
oldFilePath: string;
100+
newFilePath: string;
101+
type: CommitFileStatus;
102+
additions: number | null;
103+
deletions: number | null;
104+
}
105+
106+
export const enum CommitFileStatus {
107+
Added = 'A',
108+
Modified = 'M',
109+
Deleted = 'D',
110+
Renamed = 'R',
111+
}
112+
77113
export const ICodeAPIProvider = Symbol('ICodeAPIProvider');
78114

79115
export interface ICodeAPIProvider {
@@ -101,16 +137,24 @@ export interface ICodeAPIService {
101137
* 获取 blob
102138
*/
103139
getBlob(repo: IRepositoryModel, entry: EntryParam): Promise<Uint8Array>;
140+
/**
141+
* 获取 blob
142+
*/
143+
getBlobByCommitPath(repo: IRepositoryModel, commit: string, path: string): Promise<Uint8Array>;
104144
/**
105145
* 获取 entry 相关信息
106146
*/
107147
getEntryInfo?(repo: IRepositoryModel, entry: EntryParam): Promise<EntryInfo>;
108148
/**
109-
* 获取所有分支和标签
149+
* 获取所有分支
110150
*/
111151
getBranches(repo: IRepositoryModel): Promise<BranchOrTag[]>;
112152
/**
113-
* 获取所有分支和标签
153+
* 获取所有分支
154+
*/
155+
getBranchNames?(repo: IRepositoryModel): Promise<string[]>;
156+
/**
157+
* 获取所有标签
114158
*/
115159
getTags(repo: IRepositoryModel): Promise<BranchOrTag[]>;
116160
/**
@@ -136,7 +180,19 @@ export interface ICodeAPIService {
136180
/**
137181
* file blame
138182
*/
139-
getFileBlame(repo: IRepositoryModel, filepath: string): Promise<Uint8Array | void>;
183+
getFileBlame(repo: IRepositoryModel, filepath: string): Promise<Uint8Array>;
184+
/**
185+
* commits list
186+
*/
187+
getCommits(repo: IRepositoryModel, params: CommitParams): Promise<CommitRecord[]>;
188+
/**
189+
* commit diff
190+
*/
191+
getCommitDiff(repo: IRepositoryModel, sha: string): Promise<CommitFileChange[]>;
192+
/**
193+
* compare commit
194+
*/
195+
getCommitCompare(repo: IRepositoryModel, from: string, to: string): Promise<CommitFileChange[]>;
140196
}
141197

142198
export interface ICodeAPIServiceProvider extends ICodeAPIService {

0 commit comments

Comments
 (0)