Skip to content

Commit f32951a

Browse files
committed
feat: optimize mr list logic
1 parent 3bebcd7 commit f32951a

File tree

10 files changed

+213
-136
lines changed

10 files changed

+213
-136
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"activitybar": [
6363
{
6464
"id": "CODING-MR",
65-
"title": "CODING MR"
65+
"title": "CODING 合并请求"
6666
},
6767
{
6868
"id": "CODING-DEPOT",
@@ -80,7 +80,7 @@
8080
"CODING-MR": [
8181
{
8282
"id": "codingPlugin.treeMR",
83-
"name": "CODING MR"
83+
"name": "CODING 合并请求"
8484
}
8585
],
8686
"CODING-DEPOT": [

src/extension.ts

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import hx from 'hbuilderx';
21
import init from './init';
32
import WebviewProvider from './webview';
43
import CodingServer from './services/codingServer';
54
import { proxyCtx } from './utils/proxy';
65

76
const accessToken = '1b7fca3bd7594a89b0f5e2a0250c1147';
7+
const user = {
8+
id: 8005956,
9+
avatar: 'https://coding-net-production-static-ci.codehub.cn/WM-TEXT-AVATAR-lvVeBfbGLtCPdcsAOPod.jpg',
10+
global_key: 'PDCOwrBjib',
11+
name: 'uniquemo',
12+
path: '/u/PDCOwrBjib',
13+
team: 'uniquemo',
14+
};
815

916
async function activate(context: IContext) {
1017
// TODO: 认证,拿到用户信息
@@ -13,41 +20,22 @@ async function activate(context: IContext) {
1320
const repoInfo = await CodingServer.getRepoParams();
1421
console.log('repoInfo ==> ', repoInfo);
1522

16-
if (!repoInfo) {
17-
hx.window.showWarningMessage('workspace中没有CODING的代码仓库');
18-
return;
19-
}
20-
2123
const codingServer = new CodingServer(
2224
{
23-
id: '123',
24-
user: {
25-
avatar: 'string',
26-
global_key: 'string',
27-
name: 'string',
28-
path: 'string',
29-
team: 'string'
30-
},
25+
id: 'abc',
26+
user,
3127
accessToken,
32-
refreshToken: 'abc'
28+
refreshToken: 'abc',
3329
},
34-
repoInfo || {
35-
team: 'codingcorp',
36-
project: 'mo-test',
37-
repo: 'mo-test'
38-
}
30+
repoInfo,
3931
);
4032

41-
let userInfo = null;
42-
if (repoInfo) {
43-
userInfo = await codingServer.getUserInfo(repoInfo.team);
44-
}
45-
4633
context.ctx = {
4734
webviewProvider,
4835
codingServer,
4936
repoInfo,
50-
userInfo
37+
depots: [],
38+
selectedDepot: null,
5139
};
5240

5341
proxyCtx(context);
@@ -58,7 +46,4 @@ function deactivate() {
5846
console.log('plugin deactivate');
5947
}
6048

61-
export {
62-
activate,
63-
deactivate
64-
};
49+
export { activate, deactivate };

src/init.ts

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,71 @@ import hx from 'hbuilderx';
22
import DepotTreeDataProvider from './trees/depot';
33
import MRTreeDataProvider from './trees/mr';
44
import { getMRUrl } from './utils/repo';
5+
import toast from './utils/toast';
56
import { IDepot, IMRItem } from './typings/common';
67

78
export function registerCommands(context: IContext) {
8-
const { webviewProvider, repoInfo, codingServer } = context;
9-
const { team, project } = repoInfo;
9+
const { repoInfo, codingServer } = context;
10+
const { team } = repoInfo;
1011

11-
context.subscriptions.push(hx.commands.registerCommand('codingPlugin.helloWorld', () => {
12-
hx.window.showInformationMessage('你好,这是我的第一个插件扩展。');
13-
}));
12+
context.subscriptions.push(
13+
hx.commands.registerCommand('codingPlugin.helloWorld', () => {
14+
toast.info('hello');
15+
}),
16+
);
1417

15-
context.subscriptions.push(hx.commands.registerCommand('codingPlugin.mrTreeItemClick', function(param: IMRItem) {
16-
// webviewProvider.update(param[0]);
17-
hx.env.openExternal(getMRUrl(team, param));
18-
}));
18+
context.subscriptions.push(
19+
hx.commands.registerCommand('codingPlugin.mrTreeItemClick', function ([team, mrItem]: [string, IMRItem]) {
20+
hx.env.openExternal(getMRUrl(team, mrItem));
21+
}),
22+
);
1923

20-
context.subscriptions.push(hx.commands.registerCommand('codingPlugin.depotTreeItemClick', function(param: IDepot) {
21-
console.log('选中了: ', param);
22-
}));
24+
context.subscriptions.push(
25+
hx.commands.registerCommand('codingPlugin.depotTreeItemClick', function (param: IDepot) {
26+
toast.info(param.name);
27+
}),
28+
);
2329

24-
context.subscriptions.push(hx.commands.registerCommand('codingPlugin.createProjectAndDepot', async function() {
25-
const project = await hx.window.showInputBox({
26-
prompt: '请输入项目名'
27-
});
28-
const depot = await hx.window.showInputBox({
29-
prompt: '请输入仓库名'
30-
});
31-
}));
30+
context.subscriptions.push(
31+
hx.commands.registerCommand('codingPlugin.createProjectAndDepot', async function () {
32+
const project = await hx.window.showInputBox({
33+
prompt: '请输入项目名',
34+
});
35+
const depot = await hx.window.showInputBox({
36+
prompt: '请输入仓库名',
37+
});
38+
}),
39+
);
3240

33-
context.subscriptions.push(hx.commands.registerCommand('codingPlugin.createDepot', async function(param: any) {
34-
const depot = await hx.window.showInputBox({
35-
prompt: '请输入仓库名'
36-
});
37-
const result = await codingServer.createDepot(team, depot, depot);
38-
// TODO: 拉取代码,更新workspace
39-
}));
41+
context.subscriptions.push(
42+
hx.commands.registerCommand('codingPlugin.createDepot', async function (param: any) {
43+
const depot = await hx.window.showInputBox({
44+
prompt: '请输入仓库名',
45+
});
46+
const result = await codingServer.createDepot(team, depot, depot);
47+
// TODO: 拉取代码,更新workspace
48+
}),
49+
);
4050
}
4151

4252
export function createTreeViews(context: IContext) {
43-
context.subscriptions.push(hx.window.createTreeView('codingPlugin.treeMR', {
44-
showCollapseAll: true,
45-
treeDataProvider: new MRTreeDataProvider(context)
46-
}));
53+
context.subscriptions.push(
54+
hx.window.createTreeView('codingPlugin.treeMR', {
55+
showCollapseAll: true,
56+
treeDataProvider: new MRTreeDataProvider(context),
57+
}),
58+
);
4759

48-
context.subscriptions.push(hx.window.createTreeView('codingPlugin.treeDepot', {
49-
showCollapseAll: true,
50-
treeDataProvider: new DepotTreeDataProvider(context)
51-
}));
60+
context.subscriptions.push(
61+
hx.window.createTreeView('codingPlugin.treeDepot', {
62+
showCollapseAll: true,
63+
treeDataProvider: new DepotTreeDataProvider(context),
64+
}),
65+
);
5266
}
5367

5468
export function workspaceInit() {
55-
hx.workspace.onDidChangeWorkspaceFolders(function(event: any) {
69+
hx.workspace.onDidChangeWorkspaceFolders(function (event: any) {
5670
if (event.added) {
5771
event.added.forEach((item: any) => console.log('新增了项目: ', item.name));
5872
}

src/services/codingServer.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import axios from '../utils/axios';
55
import git from 'isomorphic-git';
66
import { IRepoInfo, ISessionData } from '../typings/common';
77
import { parseCloneUrl } from '../utils/repo';
8-
import MOCK from '../mock';
98

109
export default class CodingServer {
1110
_session!: ISessionData;
12-
_repo: IRepoInfo = {} as IRepoInfo;
11+
_repo!: IRepoInfo;
1312

1413
constructor(session?: ISessionData, repo?: IRepoInfo) {
1514
if (session) {
@@ -20,6 +19,14 @@ export default class CodingServer {
2019
}
2120
}
2221

22+
get session() {
23+
return this._session;
24+
}
25+
26+
get repo() {
27+
return this._repo;
28+
}
29+
2330
static async getRepoParams() {
2431
const folders = await hx.workspace.getWorkspaceFolders();
2532

@@ -57,11 +64,7 @@ export default class CodingServer {
5764
}
5865
}
5966

60-
async getMrList(
61-
team: string = this._repo.team,
62-
project: string = this._repo.project,
63-
repo: string = this._repo.repo,
64-
) {
67+
async getMrList({ team, project, repo }: IRepoInfo) {
6568
try {
6669
const url = `https://${team}.coding.net/api/user/${team}/project/${project}/depot/${repo}/git/merges/query`;
6770
const result = await axios({
@@ -82,7 +85,8 @@ export default class CodingServer {
8285
}
8386
}
8487

85-
async getDepotList(team: string = this._repo.team, project: string = this._repo.project) {
88+
async getDepotList(team: string = this._repo?.team, project: string = this._repo?.project) {
89+
// TODO: 使用新接口
8690
try {
8791
const result = await axios({
8892
method: 'get',
@@ -101,7 +105,7 @@ export default class CodingServer {
101105
return 'createProjectAndDepot';
102106
}
103107

104-
async createDepot(team: string = this._repo.team, project: string = this._repo.project, depot: string) {
108+
async createDepot(team: string = this._repo?.team, project: string = this._repo?.project, depot: string) {
105109
try {
106110
const result = await axios({
107111
method: 'post',

src/trees/depot.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@ class DepotTreeDataProvider extends hx.TreeDataProvider {
2424

2525
try {
2626
const depots = await this.context.codingServer.getDepotList();
27+
this.context.depots = depots;
28+
2729
return Promise.resolve([
2830
{
2931
name: '创建仓库',
3032
_create: true,
3133
},
3234
{
3335
name: '仓库列表',
34-
children: depots
35-
}
36+
children: depots,
37+
},
3638
]);
3739
} catch {
3840
console.error('获取仓库列表失败');
@@ -45,9 +47,9 @@ class DepotTreeDataProvider extends hx.TreeDataProvider {
4547
collapsibleState: element.children ? 1 : 0,
4648
command: {
4749
command: getCommand(element),
48-
arguments: element
50+
arguments: element,
4951
},
50-
contextValue: 'createDepot'
52+
contextValue: 'createDepot',
5153
};
5254
}
5355
}

src/trees/mr.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,78 @@
11
import hx from 'hbuilderx';
2-
import { IMRItem, IReviewer } from '../typings/common';
2+
import toast from '../utils/toast';
3+
import { getMrListParams } from '../utils/mr';
4+
import { IMRItem, IRepoInfo, IReviewer } from '../typings/common';
5+
6+
interface IItem extends ITreeItem {
7+
_disabled: boolean;
8+
}
39

410
class MRTreeDataProvider extends hx.TreeDataProvider {
511
constructor(context: IContext) {
612
super();
713
this.context = context;
814
}
915

10-
async getChildren(element?: IMRItem & ITreeItem) {
16+
getRepoInfo() {
17+
const user = this.context.codingServer.session?.user;
18+
19+
const repoInfo = getMrListParams(this.context.repoInfo, this.context.selectedDepot, this.context.depots, user);
20+
21+
return repoInfo;
22+
}
23+
24+
async getChildren(element?: IMRItem & IItem) {
1125
if (element) {
1226
return Promise.resolve(element.children);
1327
}
1428

1529
try {
16-
const list = await this.context.codingServer.getMrList();
30+
const user = this.context.codingServer.session?.user;
31+
const userId = user?.id;
32+
33+
const repoInfo = this.getRepoInfo();
1734

18-
const userId = this.context.userInfo.id;
35+
if (!repoInfo) {
36+
toast.warn('请先到扩展视图`CODING 仓库`中创建仓库');
37+
return;
38+
}
39+
40+
const list = await this.context.codingServer.getMrList(repoInfo);
1941
const createdList = list.filter((item: IMRItem) => item.author.id === userId);
20-
const reviewerList = list.filter((item: IMRItem) => item.reviewers.find((r: IReviewer) => r.reviewer.id === userId));
42+
const reviewerList = list.filter((item: IMRItem) =>
43+
item.reviewers.find((r: IReviewer) => r.reviewer.id === userId),
44+
);
2145

2246
return Promise.resolve([
47+
{
48+
title: `当前代码仓库:${repoInfo.repo}`,
49+
_disabled: true,
50+
},
2351
{
2452
title: `Created By Me (${createdList.length})`,
25-
children: createdList
53+
children: createdList,
2654
},
2755
{
2856
title: `Waiting For My Review (${reviewerList.length})`,
29-
children: reviewerList
30-
}
57+
children: reviewerList,
58+
},
3159
]);
3260
} catch {
3361
console.error('获取MR列表失败');
3462
Promise.resolve([]);
3563
}
3664
}
3765

38-
getTreeItem(element: IMRItem & ITreeItem) {
66+
getTreeItem(element: IMRItem & IItem) {
67+
const repoInfo = this.getRepoInfo() as IRepoInfo;
68+
3969
return {
4070
label: element.title,
4171
collapsibleState: element.children ? 1 : 0,
4272
command: {
43-
command: element.children ? '' : 'codingPlugin.mrTreeItemClick',
44-
arguments: element
45-
}
73+
command: element.children || element._disabled ? '' : 'codingPlugin.mrTreeItemClick',
74+
arguments: [repoInfo.team, element],
75+
},
4676
};
4777
}
4878
}

0 commit comments

Comments
 (0)