Skip to content

Commit 1af1316

Browse files
authored
Merge pull request cangzhang#8 from cangzhang/feat/perfect
feat: optimize logic, add depotMR tree view
2 parents e9c04c7 + 4cc7541 commit 1af1316

File tree

13 files changed

+309
-99
lines changed

13 files changed

+309
-99
lines changed

package.json

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"main": "./out/extension",
1414
"activationEvents": [
1515
"onCustomEditor:customEditor.mrDetail",
16+
"onView:codingPlugin.treeDepotMR",
1617
"onView:codingPlugin.treeMR",
1718
"onView:codingPlugin.treeDepot",
1819
"onView:codingPlugin.webview"
@@ -55,12 +56,8 @@
5556
"viewsContainers": {
5657
"activitybar": [
5758
{
58-
"id": "CODING-MR",
59-
"title": "CODING 合并请求"
60-
},
61-
{
62-
"id": "CODING-DEPOT",
63-
"title": "CODING 仓库"
59+
"id": "CODING-DEPOT-MR",
60+
"title": "CODING 代码仓库"
6461
}
6562
],
6663
"rightside": [
@@ -71,16 +68,10 @@
7168
]
7269
},
7370
"views": {
74-
"CODING-MR": [
75-
{
76-
"id": "codingPlugin.treeMR",
77-
"name": "CODING 合并请求"
78-
}
79-
],
80-
"CODING-DEPOT": [
71+
"CODING-DEPOT-MR": [
8172
{
82-
"id": "codingPlugin.treeDepot",
83-
"name": "CODING 仓库"
73+
"id": "codingPlugin.treeDepotMR",
74+
"name": "CODING 代码仓库"
8475
}
8576
],
8677
"WebviewContainerId": [

src/extension.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,9 @@ import { proxyCtx } from './utils/proxy';
66
import toast from './utils/toast';
77
import { readConfig } from './services/dcloud';
88

9-
// const accessToken = '7e4d9d17f87875e731d536d13635a700ddf52b12';
10-
// const user = {
11-
// id: 8005956,
12-
// avatar: 'https://coding-net-production-static-ci.codehub.cn/WM-TEXT-AVATAR-lvVeBfbGLtCPdcsAOPod.jpg',
13-
// global_key: 'PDCOwrBjib',
14-
// name: 'uniquemo',
15-
// path: '/u/PDCOwrBjib',
16-
// team: 'uniquemo',
17-
// };
18-
19-
const accessToken = '6e15bbb9960810111c90086a6efc07a923dea5a3';
20-
const user = {
21-
id: 8003868,
22-
avatar: 'https://coding-net-production-static-ci.codehub.cn/WM-TEXT-AVATAR-lvVeBfbGLtCPdcsAOPod.jpg',
23-
global_key: 'dHzOCagiSb',
24-
name: '莫泳欣',
25-
path: '/u/dHzOCagiSb',
26-
team: 'codingcorp',
27-
};
28-
299
async function activate(context: IContext) {
3010
// TODO: 认证,拿到用户信息
31-
const webviewProvider = new WebviewProvider();
11+
const webviewProvider = new WebviewProvider(context);
3212

3313
const repoInfo = await CodingServer.getRepoParams();
3414
console.log('repoInfo ==> ', repoInfo);
@@ -39,10 +19,7 @@ async function activate(context: IContext) {
3919

4020
const codingServer = new CodingServer(
4121
{
42-
id: 'abc',
43-
user,
44-
accessToken,
45-
refreshToken: 'abc',
22+
accessToken: token,
4623
},
4724
repoInfo,
4825
);
@@ -55,6 +32,7 @@ async function activate(context: IContext) {
5532
depots: [],
5633
selectedDepot: null,
5734
token,
35+
userInfo: null,
5836
repoInfo,
5937
},
6038
});

src/init.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
import hx from 'hbuilderx';
22
import DepotTreeDataProvider from './trees/depot';
33
import MRTreeDataProvider from './trees/mr';
4+
import DepotMRTreeDataProvider from './trees/depotMR';
45
import MRCustomEditorProvider from './customEditors/mergeRequest';
56

67
import toast from './utils/toast';
78
import ACTIONS, { dispatch } from './utils/actions';
8-
import { IDepot, IMRItem } from './typings/common';
9+
import { IDepot, IMRItem, IUserInfo } from './typings/common';
910
import * as DCloudService from './services/dcloud';
1011

1112
const { registerCommand } = hx.commands;
13+
const { createTreeView } = hx.window;
1214

1315
export function registerCommands(context: IContext) {
14-
const { codingServer } = context;
16+
const { codingServer, token } = context;
1517

1618
context.subscriptions.push(
17-
registerCommand('codingPlugin.mrTreeItemClick', async function ([team, mrItem]: [string, IMRItem]) {
19+
registerCommand('codingPlugin.mrTreeItemClick', async function ([userInfo, mrItem]: [IUserInfo, IMRItem]) {
1820
const matchRes = mrItem.path.match(/\/p\/([^/]+)\/d\/([^/]+)\/git\/merge\/([0-9]+)/);
1921
if (matchRes) {
2022
const [, project, repo, mergeRequestIId] = matchRes;
2123
context.webviewProvider.update({
22-
session: codingServer.session,
24+
token,
25+
userInfo,
2326
mergeRequestIId,
2427
repoInfo: {
25-
team,
28+
team: userInfo.team,
2629
project,
2730
repo,
2831
},
@@ -52,27 +55,36 @@ export function registerCommands(context: IContext) {
5255
return;
5356
}
5457

55-
const team = codingServer.session?.user?.team;
56-
await codingServer.createDepot(team, depot, depot);
57-
toast.info('仓库创建成功');
58+
const team = context.userInfo.team;
59+
const result = await codingServer.createDepot(team, depot, depot);
60+
if (result) {
61+
toast.info('仓库创建成功');
62+
}
5863
}),
5964
);
6065
}
6166

6267
export function createTreeViews(context: IContext) {
6368
context.subscriptions.push(
64-
hx.window.createTreeView('codingPlugin.treeMR', {
69+
createTreeView('codingPlugin.treeMR', {
6570
showCollapseAll: false,
6671
treeDataProvider: new MRTreeDataProvider(context),
6772
}),
6873
);
6974

7075
context.subscriptions.push(
71-
hx.window.createTreeView('codingPlugin.treeDepot', {
72-
showCollapseAll: true,
76+
createTreeView('codingPlugin.treeDepot', {
77+
showCollapseAll: false,
7378
treeDataProvider: new DepotTreeDataProvider(context),
7479
}),
7580
);
81+
82+
context.subscriptions.push(
83+
createTreeView('codingPlugin.treeDepotMR', {
84+
showCollapseAll: false,
85+
treeDataProvider: new DepotMRTreeDataProvider(context),
86+
}),
87+
);
7688
}
7789

7890
export function workspaceInit() {
@@ -114,7 +126,7 @@ async function initCredentials(context: IContext) {
114126
const {
115127
ctx: { codingServer, repoInfo, token },
116128
} = context;
117-
const userData = await codingServer.getUserInfo(repoInfo.team, token);
129+
const userData = await codingServer.getUserInfo(token);
118130
toast.info(`logged in as coding user: ${userData.name} @ ${userData.team}`);
119131
} catch (err) {
120132
console.error(err);

src/services/codingServer.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ import { parseCloneUrl } from '../utils/repo';
88
import toast from '../utils/toast';
99

1010
export default class CodingServer {
11-
_session!: ISessionData;
11+
_session: ISessionData;
1212
_repo!: IRepoInfo;
1313

14-
constructor(session?: ISessionData, repo?: IRepoInfo) {
15-
if (session) {
16-
this._session = session;
17-
}
14+
constructor(session: ISessionData, repo?: IRepoInfo) {
15+
this._session = session;
16+
1817
if (repo) {
1918
this._repo = repo;
2019
}
@@ -32,23 +31,22 @@ export default class CodingServer {
3231
const folders = await hx.workspace.getWorkspaceFolders();
3332

3433
if (!folders.length) {
35-
toast.warn('workspace 中没有目录');
3634
return;
3735
}
3836

3937
try {
4038
const remotes = await git.listRemotes({ fs, dir: folders[0].uri.path });
4139
return parseCloneUrl(remotes[0].url);
4240
} catch {
43-
toast.error('该目录没有进行 git 初始化');
41+
console.error('该目录没有进行 git 初始化');
4442
}
4543
}
4644

47-
async getUserInfo(team: string, token: string = this._session?.accessToken || ``) {
45+
async getUserInfo(token: string) {
4846
try {
4947
const result = await axios({
5048
method: 'get',
51-
url: `https://${team}.coding.net/api/current_user`,
49+
url: `https://e.coding.net/api/current_user`,
5250
headers: this.getHeaders(token),
5351
});
5452

@@ -77,6 +75,11 @@ export default class CodingServer {
7775
sortDirection: `DESC`,
7876
},
7977
});
78+
79+
if (result.code) {
80+
return Promise.reject(result);
81+
}
82+
8083
return result?.data?.list || [];
8184
} catch (err) {
8285
console.error(err);
@@ -91,13 +94,17 @@ export default class CodingServer {
9194
headers: this.getHeaders(),
9295
});
9396

97+
if (result.code) {
98+
return Promise.reject(result);
99+
}
100+
94101
return result.data;
95102
} catch (err) {
96103
console.error(err);
97104
}
98105
}
99106

100-
async getDepotList(team: string = this._repo?.team, project: string = this._repo?.project) {
107+
async getDepotList(team: string, project: string = this._repo?.project) {
101108
// TODO: 使用新接口
102109
try {
103110
const result = await axios({
@@ -106,6 +113,10 @@ export default class CodingServer {
106113
headers: this.getHeaders(),
107114
});
108115

116+
if (result.code) {
117+
return Promise.reject(result);
118+
}
119+
109120
return result?.data?.depots || [];
110121
} catch (err) {
111122
console.error(err);
@@ -126,6 +137,10 @@ export default class CodingServer {
126137
},
127138
});
128139

140+
if (result.code) {
141+
return Promise.reject(result);
142+
}
143+
129144
return result.data;
130145
} catch (err) {
131146
console.error(err);
@@ -134,7 +149,9 @@ export default class CodingServer {
134149

135150
async createDepot(team: string, project: string, depot: string) {
136151
try {
137-
await this.createProject(team, project);
152+
const projectRes = await this.createProject(team, project);
153+
154+
if (!projectRes) return;
138155

139156
const result = await axios({
140157
method: 'post',
@@ -150,6 +167,12 @@ export default class CodingServer {
150167
shared: false,
151168
}),
152169
});
170+
171+
if (result.code) {
172+
return Promise.reject(result);
173+
}
174+
175+
return result.data;
153176
} catch (err) {
154177
console.error(err);
155178
}

0 commit comments

Comments
 (0)