Skip to content

Commit 0397677

Browse files
committed
feat: add userInfo to context
1 parent 889c7ba commit 0397677

File tree

8 files changed

+54
-63
lines changed

8 files changed

+54
-63
lines changed

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: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,26 @@ import MRCustomEditorProvider from './customEditors/mergeRequest';
66

77
import toast from './utils/toast';
88
import ACTIONS, { dispatch } from './utils/actions';
9-
import { IDepot, IMRItem } from './typings/common';
9+
import { IDepot, IMRItem, IUserInfo } from './typings/common';
1010
import * as DCloudService from './services/dcloud';
1111

1212
const { registerCommand } = hx.commands;
1313
const { createTreeView } = hx.window;
1414

1515
export function registerCommands(context: IContext) {
16-
const { codingServer } = context;
16+
const { codingServer, token } = context;
1717

1818
context.subscriptions.push(
19-
registerCommand('codingPlugin.mrTreeItemClick', async function ([team, mrItem]: [string, IMRItem]) {
19+
registerCommand('codingPlugin.mrTreeItemClick', async function ([userInfo, mrItem]: [IUserInfo, IMRItem]) {
2020
const matchRes = mrItem.path.match(/\/p\/([^/]+)\/d\/([^/]+)\/git\/merge\/([0-9]+)/);
2121
if (matchRes) {
2222
const [, project, repo, mergeRequestIId] = matchRes;
2323
context.webviewProvider.update({
24-
session: codingServer.session,
24+
token,
25+
userInfo,
2526
mergeRequestIId,
2627
repoInfo: {
27-
team,
28+
team: userInfo.team,
2829
project,
2930
repo,
3031
},
@@ -54,7 +55,7 @@ export function registerCommands(context: IContext) {
5455
return;
5556
}
5657

57-
const team = codingServer.session?.user?.team;
58+
const team = context.userInfo.team;
5859
const result = await codingServer.createDepot(team, depot, depot);
5960
if (result) {
6061
toast.info('仓库创建成功');
@@ -125,7 +126,7 @@ async function initCredentials(context: IContext) {
125126
const {
126127
ctx: { codingServer, repoInfo, token },
127128
} = context;
128-
const userData = await codingServer.getUserInfo(repoInfo.team, token);
129+
const userData = await codingServer.getUserInfo(token);
129130
toast.info(`logged in as coding user: ${userData.name} @ ${userData.team}`);
130131
} catch (err) {
131132
console.error(err);

src/services/codingServer.ts

Lines changed: 6 additions & 7 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
}
@@ -44,11 +43,11 @@ export default class CodingServer {
4443
}
4544
}
4645

47-
async getUserInfo(team: string, token: string = this._session?.accessToken || ``) {
46+
async getUserInfo(token: string) {
4847
try {
4948
const result = await axios({
5049
method: 'get',
51-
url: `https://${team}.coding.net/api/current_user`,
50+
url: `https://e.coding.net/api/current_user`,
5251
headers: this.getHeaders(token),
5352
});
5453

src/trees/depotMR.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { IDepot, IMRItem, IReviewer } from '../typings/common';
77
interface IItem extends ITreeItem {
88
_create: boolean;
99
_auth: boolean;
10+
_login: boolean;
1011
_disabled: boolean;
1112
_isDepot: boolean;
1213
}
@@ -15,6 +16,7 @@ type IElement = IDepot & IMRItem & IItem;
1516

1617
const getCommand = (element: IElement) => {
1718
if (element.children || element._disabled) return '';
19+
if (element._login) return 'codingPlugin.login';
1820
if (element._auth) return 'codingPlugin.auth';
1921
if (element._create) return 'codingPlugin.createDepot';
2022
if (element.vcsType) return 'codingPlugin.depotTreeItemClick';
@@ -25,30 +27,36 @@ class DepotMRTreeDataProvider extends hx.TreeDataProvider {
2527
constructor(context: IContext) {
2628
super();
2729
this.context = context;
28-
}
29-
30-
getUser() {
31-
return this.context.codingServer.session?.user;
30+
this.user = null;
3231
}
3332

3433
getRepoInfo() {
35-
const { selectedDepot, depots, codingServer } = this.context;
36-
const user = codingServer.session?.user;
37-
return getMrListParams(selectedDepot, depots, user);
34+
const { selectedDepot, depots } = this.context;
35+
return getMrListParams(selectedDepot, depots, this.user);
3836
}
3937

4038
async getChildren(element: IElement) {
41-
const user = this.getUser();
42-
if (!user) {
43-
toast.warn('请先绑定 CODING 账户');
39+
const { codingServer, token } = this.context;
40+
41+
if (!this.user) {
42+
this.user = await codingServer.getUserInfo(token);
43+
}
44+
45+
if (!this.user) {
4446
return Promise.resolve([
4547
{
46-
name: '绑定 CODING 账户',
47-
_auth: true,
48+
name: '登录 CODING',
49+
_login: true,
50+
_isDepot: true,
4851
},
4952
]);
5053
}
5154

55+
dispatch(ACTIONS.SET_USER_INFO, {
56+
context: this.context,
57+
value: this.user,
58+
});
59+
5260
if (element) {
5361
return Promise.resolve(element.children);
5462
}
@@ -65,8 +73,10 @@ class DepotMRTreeDataProvider extends hx.TreeDataProvider {
6573
let reviewerList: IMRItem[] = [];
6674
let others = [];
6775
if (list) {
68-
createdList = list.filter((item: IMRItem) => item.author.id === user.id);
69-
reviewerList = list.filter((item: IMRItem) => item.reviewers.find((r: IReviewer) => r.reviewer.id === user.id));
76+
createdList = list.filter((item: IMRItem) => item.author.id === this.user.id);
77+
reviewerList = list.filter((item: IMRItem) =>
78+
item.reviewers.find((r: IReviewer) => r.reviewer.id === this.user.id),
79+
);
7080
others = list.filter((item: IMRItem) => {
7181
const isNotInCreatedList = createdList.findIndex((i) => i.id === item.id) === -1;
7282
const isNotInReviewerList = reviewerList.findIndex((i) => i.id === item.id) === -1;
@@ -91,7 +101,7 @@ class DepotMRTreeDataProvider extends hx.TreeDataProvider {
91101
_isDepot: true,
92102
},
93103
{
94-
title: `合并请求列表(当前选中仓库 ${this.context.selectedDepot?.name || '-'})`,
104+
title: `合并请求列表(当前仓库 ${this.context.selectedDepot?.name || '-'})`,
95105
children: [
96106
{
97107
title: `我创建的 (${createdList?.length})`,
@@ -126,13 +136,12 @@ class DepotMRTreeDataProvider extends hx.TreeDataProvider {
126136
};
127137
}
128138

129-
const user = this.getUser();
130139
return {
131140
label: element.title,
132141
collapsibleState: element.children ? 1 : 0,
133142
command: {
134143
command: getCommand(element),
135-
arguments: [user?.team, element],
144+
arguments: [this.user, element],
136145
},
137146
};
138147
}

src/typings/common.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ export interface IUserInfo {
1919
}
2020

2121
export interface ISessionData {
22-
id: string;
23-
user: IUserInfo | null;
22+
id?: string;
23+
user?: IUserInfo | null;
2424
accessToken: string;
25-
refreshToken: string;
25+
refreshToken?: string;
2626
}
2727

2828
export interface IReviewer {

src/utils/actions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const enum ACTIONS {
22
SET_DEPOTS = 'SET_DEPOTS',
33
SET_SELECTED_DEPOT = 'SET_SELECTED_DEPOT',
4+
SET_USER_INFO = 'SET_USER_INFO',
45
SET_CTX = 'SET_CTX',
56
SET_MR_CUSTOM_EDITOR = 'SET_MR_CUSTOM_EDITOR',
67
}
@@ -18,6 +19,9 @@ export const dispatch = (type: ACTIONS, { context, value }: IPayload) => {
1819
case ACTIONS.SET_SELECTED_DEPOT:
1920
context.selectedDepot = value;
2021
break;
22+
case ACTIONS.SET_USER_INFO:
23+
context.userInfo = value;
24+
break;
2125
case ACTIONS.SET_CTX:
2226
context.ctx = value;
2327
break;

src/webviews/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ interface IMessage {
88
}
99

1010
export default class WebviewProvider {
11+
context: IContext;
1112
panel: IWebviewPanel;
1213

13-
constructor() {
14+
constructor(context: IContext) {
15+
this.context = context;
1416
this.panel = this.createPanel();
1517
this.listen();
1618
}

webviews/App.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ const toast = (msg: string) => {
2626

2727
const App = () => {
2828
const data = JSON.parse(window.__CODING__);
29-
const { session, repoInfo, mergeRequestIId } = data;
30-
const { accessToken } = session;
31-
const user = session?.user;
29+
const { token: accessToken, userInfo: user, repoInfo, mergeRequestIId } = data;
3230
const team = user?.team;
3331

3432
// Fix initialize isAgreed doesn't work

0 commit comments

Comments
 (0)