Skip to content

Commit 3689b5a

Browse files
authored
Merge pull request cangzhang#3 from cangzhang/feat/optimize
feat: optimize
2 parents 3bebcd7 + 54aa311 commit 3689b5a

File tree

13 files changed

+325
-170
lines changed

13 files changed

+325
-170
lines changed

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
"command": "codingPlugin.depotTreeItemClick",
3232
"title": "Click Depot Tree Item"
3333
},
34-
{
35-
"command": "codingPlugin.createProjectAndDepot",
36-
"title": "Create Project and Depot"
37-
},
3834
{
3935
"command": "codingPlugin.createDepot",
4036
"title": "Create Depot"
@@ -62,7 +58,7 @@
6258
"activitybar": [
6359
{
6460
"id": "CODING-MR",
65-
"title": "CODING MR"
61+
"title": "CODING 合并请求"
6662
},
6763
{
6864
"id": "CODING-DEPOT",
@@ -80,7 +76,7 @@
8076
"CODING-MR": [
8177
{
8278
"id": "codingPlugin.treeMR",
83-
"name": "CODING MR"
79+
"name": "CODING 合并请求"
8480
}
8581
],
8682
"CODING-DEPOT": [

src/extension.ts

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
import hx from 'hbuilderx';
21
import init from './init';
32
import WebviewProvider from './webview';
43
import CodingServer from './services/codingServer';
4+
import ACTIONS, { dispatch } from './utils/actions';
55
import { proxyCtx } from './utils/proxy';
6+
import toast from './utils/toast';
67

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

918
async function activate(context: IContext) {
1019
// TODO: 认证,拿到用户信息
@@ -13,52 +22,32 @@ async function activate(context: IContext) {
1322
const repoInfo = await CodingServer.getRepoParams();
1423
console.log('repoInfo ==> ', repoInfo);
1524

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

41-
let userInfo = null;
42-
if (repoInfo) {
43-
userInfo = await codingServer.getUserInfo(repoInfo.team);
44-
}
45-
46-
context.ctx = {
47-
webviewProvider,
48-
codingServer,
49-
repoInfo,
50-
userInfo
51-
};
35+
dispatch(ACTIONS.SET_CTX, {
36+
context,
37+
value: {
38+
webviewProvider,
39+
codingServer,
40+
depots: [],
41+
selectedDepot: null,
42+
},
43+
});
5244

5345
proxyCtx(context);
5446
init(context);
5547
}
5648

5749
function deactivate() {
58-
console.log('plugin deactivate');
50+
toast.info('plugin deactivate');
5951
}
6052

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

src/init.ts

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,69 @@
11
import hx from 'hbuilderx';
22
import DepotTreeDataProvider from './trees/depot';
33
import MRTreeDataProvider from './trees/mr';
4+
5+
import toast from './utils/toast';
46
import { getMRUrl } from './utils/repo';
7+
import ACTIONS, { dispatch } from './utils/actions';
58
import { IDepot, IMRItem } from './typings/common';
69

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

11-
context.subscriptions.push(hx.commands.registerCommand('codingPlugin.helloWorld', () => {
12-
hx.window.showInformationMessage('你好,这是我的第一个插件扩展。');
13-
}));
12+
export function registerCommands(context: IContext) {
13+
const { codingServer } = context;
1414

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-
}));
15+
context.subscriptions.push(
16+
registerCommand('codingPlugin.helloWorld', () => {
17+
toast.info('hello');
18+
}),
19+
);
1920

20-
context.subscriptions.push(hx.commands.registerCommand('codingPlugin.depotTreeItemClick', function(param: IDepot) {
21-
console.log('选中了: ', param);
22-
}));
21+
context.subscriptions.push(
22+
registerCommand('codingPlugin.mrTreeItemClick', function ([team, mrItem]: [string, IMRItem]) {
23+
hx.env.openExternal(getMRUrl(team, mrItem));
24+
}),
25+
);
2326

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-
}));
27+
context.subscriptions.push(
28+
registerCommand('codingPlugin.depotTreeItemClick', function (param: IDepot) {
29+
toast.info(`选中仓库:${param.name}`);
30+
dispatch(ACTIONS.SET_SELECTED_DEPOT, {
31+
context,
32+
value: param,
33+
});
34+
}),
35+
);
3236

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-
}));
37+
context.subscriptions.push(
38+
registerCommand('codingPlugin.createDepot', async function (param: any) {
39+
const depot = await hx.window.showInputBox({
40+
prompt: '请输入仓库名',
41+
});
42+
const team = codingServer.session?.user?.team;
43+
const result = await codingServer.createDepot(team, depot, depot);
44+
// TODO: 拉取代码,更新workspace
45+
}),
46+
);
4047
}
4148

4249
export function createTreeViews(context: IContext) {
43-
context.subscriptions.push(hx.window.createTreeView('codingPlugin.treeMR', {
44-
showCollapseAll: true,
45-
treeDataProvider: new MRTreeDataProvider(context)
46-
}));
50+
context.subscriptions.push(
51+
hx.window.createTreeView('codingPlugin.treeMR', {
52+
showCollapseAll: false,
53+
treeDataProvider: new MRTreeDataProvider(context),
54+
}),
55+
);
4756

48-
context.subscriptions.push(hx.window.createTreeView('codingPlugin.treeDepot', {
49-
showCollapseAll: true,
50-
treeDataProvider: new DepotTreeDataProvider(context)
51-
}));
57+
context.subscriptions.push(
58+
hx.window.createTreeView('codingPlugin.treeDepot', {
59+
showCollapseAll: true,
60+
treeDataProvider: new DepotTreeDataProvider(context),
61+
}),
62+
);
5263
}
5364

5465
export function workspaceInit() {
55-
hx.workspace.onDidChangeWorkspaceFolders(function(event: any) {
66+
hx.workspace.onDidChangeWorkspaceFolders(function (event: any) {
5667
if (event.added) {
5768
event.added.forEach((item: any) => console.log('新增了项目: ', item.name));
5869
}

src/services/codingServer.ts

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ 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';
8+
import toast from '../utils/toast';
99

1010
export default class CodingServer {
1111
_session!: ISessionData;
12-
_repo: IRepoInfo = {} as IRepoInfo;
12+
_repo!: IRepoInfo;
1313

1414
constructor(session?: ISessionData, repo?: IRepoInfo) {
1515
if (session) {
@@ -20,19 +20,27 @@ export default class CodingServer {
2020
}
2121
}
2222

23+
get session() {
24+
return this._session;
25+
}
26+
27+
get repo() {
28+
return this._repo;
29+
}
30+
2331
static async getRepoParams() {
2432
const folders = await hx.workspace.getWorkspaceFolders();
2533

2634
if (!folders.length) {
27-
console.warn('workspace中没有目录');
35+
toast.warn('workspace 中没有目录');
2836
return;
2937
}
3038

3139
try {
3240
const remotes = await git.listRemotes({ fs, dir: folders[0].uri.path });
3341
return parseCloneUrl(remotes[0].url);
3442
} catch {
35-
console.error('该目录没有进行git初始化');
43+
toast.error('该目录没有进行 git 初始化');
3644
}
3745
}
3846

@@ -46,22 +54,13 @@ export default class CodingServer {
4654
},
4755
});
4856

49-
if (result.code) {
50-
console.error(result.msg);
51-
return Promise.reject(result.msg);
52-
}
53-
5457
return result?.data;
5558
} catch (err) {
56-
throw new Error(err);
59+
console.error(err);
5760
}
5861
}
5962

60-
async getMrList(
61-
team: string = this._repo.team,
62-
project: string = this._repo.project,
63-
repo: string = this._repo.repo,
64-
) {
63+
async getMrList({ team, project, repo }: IRepoInfo) {
6564
try {
6665
const url = `https://${team}.coding.net/api/user/${team}/project/${project}/depot/${repo}/git/merges/query`;
6766
const result = await axios({
@@ -78,11 +77,12 @@ export default class CodingServer {
7877
});
7978
return result?.data?.list || [];
8079
} catch (err) {
81-
throw new Error(err);
80+
console.error(err);
8281
}
8382
}
8483

85-
async getDepotList(team: string = this._repo.team, project: string = this._repo.project) {
84+
async getDepotList(team: string = this._repo?.team, project: string = this._repo?.project) {
85+
// TODO: 使用新接口
8686
try {
8787
const result = await axios({
8888
method: 'get',
@@ -91,18 +91,36 @@ export default class CodingServer {
9191
access_token: this._session.accessToken,
9292
},
9393
});
94+
9495
return result?.data?.depots || [];
9596
} catch (err) {
96-
throw new Error(err);
97+
console.error(err);
9798
}
9899
}
99100

100-
async createProjectAndDepot(team: string, payload: { project: string; depot: string }) {
101-
return 'createProjectAndDepot';
101+
async createProject(team: string, project: string) {
102+
try {
103+
const result = await axios({
104+
method: 'post',
105+
url: `https://${team}.coding.net/api/team/${team}/template-project?access_token=${this._session.accessToken}`,
106+
data: {
107+
name: project,
108+
displayName: project,
109+
projectTemplate: 'DEV_OPS',
110+
icon: '/static/project_icon/scenery-version-2-5.svg',
111+
},
112+
});
113+
114+
return result.data;
115+
} catch (err) {
116+
console.error(err);
117+
}
102118
}
103119

104-
async createDepot(team: string = this._repo.team, project: string = this._repo.project, depot: string) {
120+
async createDepot(team: string, project: string, depot: string) {
105121
try {
122+
await this.createProject(team, project);
123+
106124
const result = await axios({
107125
method: 'post',
108126
url: `https://${team}.coding.net/api/user/${team}/project/${project}/depot?access_token=${this._session.accessToken}`,
@@ -116,9 +134,8 @@ export default class CodingServer {
116134
shared: false,
117135
}),
118136
});
119-
console.log('result => ', result);
120137
} catch (err) {
121-
throw new Error(err);
138+
console.error(err);
122139
}
123140
}
124141
}

0 commit comments

Comments
 (0)