Skip to content

Commit bb25d4f

Browse files
committed
refactor: repo info & auth.
1 parent bb55ec8 commit bb25d4f

File tree

4 files changed

+53
-31
lines changed

4 files changed

+53
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Coding plugin for VS Code.
22

3-
## Running the example
3+
## Development
44

55
- Open this example in VS Code 1.47+
66
- `yarn install`

src/codingServer.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,16 @@ const onDidManuallyProvideToken = new vscode.EventEmitter<string>();
3636
export class CodingServer {
3737
private _pendingStates = new Map<string, string[]>();
3838
private _codeExchangePromises = new Map<string, Promise<AuthSuccessResult>>();
39-
private _repo: RepoInfo | undefined;
39+
4040
private _loggedIn: boolean = false;
41+
private _context: vscode.ExtensionContext;
4142
private _session: SessionData | null = null;
4243

43-
constructor(repo?: RepoInfo | null) {
44-
if (repo) {
45-
this._repo = repo;
46-
}
44+
constructor(context: vscode.ExtensionContext) {
45+
this._context = context;
4746
}
4847

49-
public async initialize(context: vscode.ExtensionContext): Promise<void> {
48+
public async initialize(): Promise<void> {
5049
try {
5150
this._session = await this._readSessions();
5251
} catch (e) {
@@ -78,7 +77,12 @@ export class CodingServer {
7877

7978
public async getSessionData(accessToken: TokenType.AccessToken, refreshToken: TokenType.RefreshToken): Promise<SessionData> {
8079
try {
81-
const result = await this.getUserInfo(this._repo?.team || ``, accessToken);
80+
const repoInfo = this._context.workspaceState.get(`repoInfo`) as RepoInfo;
81+
if (!repoInfo?.team) {
82+
throw new Error(`team not exist`);
83+
}
84+
85+
const result = await this.getUserInfo(repoInfo.team || ``, accessToken);
8286
const { data: userInfo } = result;
8387
const ret: SessionData = {
8488
id: nanoid(),
@@ -206,13 +210,14 @@ export class CodingServer {
206210
return parseCloneUrl(url || ``);
207211
}
208212

209-
public async getMRList(
210-
team: string | undefined = this._repo?.team,
211-
project: string | undefined = this._repo?.project,
212-
repo: string | undefined = this._repo?.repo,
213-
) {
213+
public async getMRList(repo?: string) {
214214
try {
215-
const result = await got.get(`https://${team}.coding.net/api/user/${team}/project/${project}/depot/${repo}/git/merges/query`, {
215+
const repoInfo = this._context.workspaceState.get(`repoInfo`) as RepoInfo;
216+
if (!repoInfo?.team) {
217+
throw new Error(`team not exist`);
218+
}
219+
220+
const result = await got.get(`https://${repoInfo.team}.coding.net/api/user/${repoInfo.team}/project/${repoInfo.project}/depot/${repo || repoInfo.repo}/git/merges/query`, {
216221
searchParams: {
217222
status: `all`,
218223
sort: `action_at`,

src/extension.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,23 @@ import { ListItem, ListProvider } from './tree';
77

88
export async function activate(context: vscode.ExtensionContext) {
99
const repoInfo = await CodingServer.getRepoParams();
10-
const codingAuth = new CodingServer(repoInfo);
11-
await codingAuth.initialize(context);
1210

13-
if (!codingAuth.session?.user) {
11+
if (!repoInfo?.team) {
12+
vscode.window.showWarningMessage(`Please open a repo hosted by coding.net.`);
13+
} else {
14+
context.workspaceState.update(`repoInfo`, repoInfo);
15+
}
16+
17+
const codingSrv = new CodingServer(context);
18+
await codingSrv.initialize();
19+
20+
if (!codingSrv.session?.user) {
1421
vscode.window.showWarningMessage(`Please login first.`);
22+
} else {
23+
context.workspaceState.update(`session`, codingSrv.session);
1524
}
1625

17-
const treeDataProvider = new ListProvider(context, codingAuth, repoInfo);
26+
const treeDataProvider = new ListProvider(context, codingSrv);
1827
const tree = vscode.window.createTreeView(`treeViewSample`, { treeDataProvider });
1928

2029
context.subscriptions.push(vscode.window.registerUriHandler(uriHandler));
@@ -32,7 +41,7 @@ export async function activate(context: vscode.ExtensionContext) {
3241
);
3342
context.subscriptions.push(
3443
vscode.commands.registerCommand('codingPlugin.login', async () => {
35-
const session = await codingAuth.login(repoInfo?.team || ``);
44+
const session = await codingSrv.login(repoInfo?.team || ``);
3645
if (!session?.accessToken) {
3746
console.error(`No token provided.`);
3847
} else {
@@ -43,7 +52,7 @@ export async function activate(context: vscode.ExtensionContext) {
4352
context.subscriptions.push(
4453
vscode.commands.registerCommand('codingPlugin.logout', async () => {
4554
try {
46-
await codingAuth.logout();
55+
await codingSrv.logout();
4756
vscode.window.showInformationMessage(`Logout successfully.`);
4857
} finally {
4958
treeDataProvider.refresh();
@@ -65,3 +74,5 @@ export async function activate(context: vscode.ExtensionContext) {
6574
});
6675
}
6776
}
77+
78+
export function deactivate() {}

src/tree.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,15 @@ import { RepoInfo } from './typings/commonTypes';
66
import { MRData } from './typings/respResult';
77

88
export class ListProvider implements vscode.TreeDataProvider<ListItem> {
9-
private _service: CodingServer;
10-
private _repo: RepoInfo = {
11-
team: ``,
12-
project: ``,
13-
repo: ``,
14-
};
159
private _onDidChangeTreeData: vscode.EventEmitter<ListItem | undefined | void> = new vscode.EventEmitter<ListItem | undefined | void>();
1610
readonly onDidChangeTreeData: vscode.Event<ListItem | undefined | void> = this._onDidChangeTreeData.event;
1711

18-
constructor(context: vscode.ExtensionContext, service: CodingServer, repo: RepoInfo | null) {
12+
private _context : vscode.ExtensionContext;
13+
private _service: CodingServer;
14+
15+
constructor(context: vscode.ExtensionContext, service: CodingServer) {
16+
this._context = context;
1917
this._service = service;
20-
if (repo) {
21-
this._repo = repo;
22-
}
2318
}
2419

2520
public refresh(): any {
@@ -40,6 +35,11 @@ export class ListProvider implements vscode.TreeDataProvider<ListItem> {
4035
return Promise.resolve([]);
4136
}
4237

38+
const repoInfo = this._context.workspaceState.get(`repoInfo`) as RepoInfo;
39+
if (!repoInfo?.team) {
40+
throw new Error(`team not exist`);
41+
}
42+
4343
return this._service.getMRList()
4444
.then(resp => {
4545
if (resp.code) {
@@ -55,12 +55,18 @@ export class ListProvider implements vscode.TreeDataProvider<ListItem> {
5555
}
5656

5757
vscode.commands.executeCommand('setContext', 'noMRResult', false);
58+
59+
const repoInfo = this._context.workspaceState.get(`repoInfo`) as RepoInfo;
60+
if (!repoInfo?.team) {
61+
throw new Error(`team not exist`);
62+
}
63+
5864
return list.map((i: MRData) => {
5965
return new ListItem(i.title, i.iid, vscode.TreeItemCollapsibleState.None, {
6066
command: 'codingPlugin.openConvertPage',
6167
title: `${i.iid} ${i.title}`,
6268
arguments: [{
63-
...this._repo,
69+
...repoInfo,
6470
iid: i.iid,
6571
type: `mr`,
6672
accessToken: this._service.session?.accessToken,

0 commit comments

Comments
 (0)