Skip to content

Commit 6a2fdbe

Browse files
committed
change: context management.
1 parent 7ed7da9 commit 6a2fdbe

File tree

3 files changed

+38
-40
lines changed

3 files changed

+38
-40
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,17 @@
7878
{
7979
"view": "mrTreeView",
8080
"contents": "Please open a repo hosted by [coding.net](https://coding.net).\n [Open folder](command:vscode.openFolder)",
81-
"when": "!hasTeam"
81+
"when": "!hasRepo"
8282
},
8383
{
8484
"view": "mrTreeView",
8585
"contents": "Please login first.\n [Login](command:codingPlugin.login)",
86-
"when": "!loggedIn && hasTeam"
86+
"when": "!loggedIn && hasRepo"
8787
},
8888
{
8989
"view": "mrTreeView",
9090
"contents": "No merge request for now.\n[Create Merge Request](command:codingPlugin.createMr)",
91-
"when": "noMRResult && loggedIn"
91+
"when": "noMRResult && loggedIn && hasRepo"
9292
}
9393
],
9494
"viewsContainers": {

src/codingServer.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ export class CodingServer {
8989
refreshToken: TokenType.RefreshToken,
9090
): Promise<ISessionData> {
9191
try {
92-
const repoInfo = this._context.workspaceState.get(`repoInfo`) as IRepoInfo;
93-
vscode.commands.executeCommand('setContext', 'hasTeam', !!repoInfo?.team);
94-
const result = await this.getUserInfo(repoInfo.team || ``, accessToken);
92+
const repoInfo = this._context.workspaceState.get(`repoInfo`, {}) as IRepoInfo;
93+
await vscode.commands.executeCommand('setContext', 'hasRepo', !!repoInfo?.repo);
94+
const result = await this.getUserInfo(repoInfo?.team || ``, accessToken);
9595
const { data: userInfo } = result;
9696
const ret: ISessionData = {
9797
id: nanoid(),
@@ -219,12 +219,12 @@ export class CodingServer {
219219
if (result.code) {
220220
console.error(result.msg);
221221
this._loggedIn = false;
222-
vscode.commands.executeCommand('setContext', 'loggedIn', this._loggedIn);
222+
await vscode.commands.executeCommand('setContext', 'loggedIn', this._loggedIn);
223223
return Promise.reject(result.msg);
224224
}
225225

226226
this._loggedIn = true;
227-
vscode.commands.executeCommand('setContext', 'loggedIn', this._loggedIn);
227+
await vscode.commands.executeCommand('setContext', 'loggedIn', this._loggedIn);
228228
return result;
229229
} catch (err) {
230230
throw Error(err);
@@ -237,20 +237,21 @@ export class CodingServer {
237237
return result?.[0];
238238
}
239239

240-
public getApiPrefix() {
240+
public async getApiPrefix() {
241241
const repoInfo = this._context.workspaceState.get(`repoInfo`) as IRepoInfo;
242-
if (!repoInfo?.team) {
243-
vscode.commands.executeCommand('setContext', 'hasTeam', false);
242+
const hasRepo = !!repoInfo?.repo;
243+
await vscode.commands.executeCommand('setContext', 'hasRepo', hasRepo);
244+
245+
if (!hasRepo) {
244246
throw new Error(`team not exist`);
245247
}
246248

247-
vscode.commands.executeCommand('setContext', 'hasTeam', true);
248249
return `https://${repoInfo.team}.coding.net/api/user/${this._session?.user?.team}/project/${repoInfo.project}/depot/${repoInfo.repo}`;
249250
}
250251

251252
public async getMRList(repo?: string, status?: string): Promise<CodingResponse> {
252253
try {
253-
const url = this.getApiPrefix();
254+
const url = await this.getApiPrefix();
254255
const result: CodingResponse = await got
255256
.get(`${url}/git/merges/query`, {
256257
searchParams: {
@@ -301,7 +302,7 @@ export class CodingServer {
301302

302303
public async getMRDiff(iid: number) {
303304
try {
304-
const url = this.getApiPrefix();
305+
const url = await this.getApiPrefix();
305306
const diff: IMRDiffResponse = await got
306307
.get(`${url}/git/merge/${iid}/diff`, {
307308
searchParams: {
@@ -320,7 +321,7 @@ export class CodingServer {
320321

321322
public async getMRDetail(iid: string) {
322323
try {
323-
const url = this.getApiPrefix();
324+
const url = await this.getApiPrefix();
324325
const resp: IMRDetailResponse = await got
325326
.get(`${url}/git/merge/${iid}/detail`, {
326327
searchParams: {
@@ -341,7 +342,7 @@ export class CodingServer {
341342

342343
public async getMRActivities(iid: string) {
343344
try {
344-
const url = this.getApiPrefix();
345+
const url = await this.getApiPrefix();
345346
const result: IMRActivitiesResponse = await got
346347
.get(`${url}/git/merge/${iid}/activities`, {
347348
searchParams: {
@@ -361,7 +362,7 @@ export class CodingServer {
361362

362363
public async getMRReviewers(iid: string) {
363364
try {
364-
const url = this.getApiPrefix();
365+
const url = await this.getApiPrefix();
365366
const result: IMRReviewersResponse = await got
366367
.get(`${url}/git/merge/${iid}/reviewers`, {
367368
searchParams: {
@@ -381,7 +382,7 @@ export class CodingServer {
381382

382383
public async getMRComments(iid: string) {
383384
try {
384-
const url = this.getApiPrefix();
385+
const url = await this.getApiPrefix();
385386
const result: CodingResponse = await got
386387
.get(`${url}/git/merge/${iid}/comments`, {
387388
searchParams: {
@@ -401,7 +402,7 @@ export class CodingServer {
401402

402403
public async closeMR(iid: string) {
403404
try {
404-
const url = this.getApiPrefix();
405+
const url = await this.getApiPrefix();
405406
const result: CodingResponse = await got
406407
.post(`${url}/git/merge/${iid}/refuse`, {
407408
searchParams: {
@@ -421,7 +422,7 @@ export class CodingServer {
421422

422423
public async approveMR(iid: string) {
423424
try {
424-
const url = this.getApiPrefix();
425+
const url = await this.getApiPrefix();
425426
const result: CodingResponse = await got
426427
.post(`${url}/git/merge/${iid}/good`, {
427428
searchParams: {
@@ -441,7 +442,7 @@ export class CodingServer {
441442

442443
public async disapproveMR(iid: string) {
443444
try {
444-
const url = this.getApiPrefix();
445+
const url = await this.getApiPrefix();
445446
const result: CodingResponse = await got
446447
.delete(`${url}/git/merge/${iid}/good`, {
447448
searchParams: {
@@ -461,7 +462,7 @@ export class CodingServer {
461462

462463
public async mergeMR(iid: string) {
463464
try {
464-
const url = this.getApiPrefix();
465+
const url = await this.getApiPrefix();
465466
const result: CodingResponse = await got
466467
.post(`${url}/git/merge/${iid}/merge`, {
467468
searchParams: {
@@ -484,7 +485,7 @@ export class CodingServer {
484485

485486
public async updateMRTitle(iid: string, title: string) {
486487
try {
487-
const url = this.getApiPrefix();
488+
const url = await this.getApiPrefix();
488489
const result: CodingResponse = await got
489490
.put(`${url}/git/merge/${iid}/update-title`, {
490491
searchParams: {
@@ -508,7 +509,7 @@ export class CodingServer {
508509

509510
public async commentMR(mrId: number, comment: string) {
510511
try {
511-
const url = this.getApiPrefix();
512+
const url = await this.getApiPrefix();
512513
const result: CodingResponse = await got
513514
.post(`${url}/git/line_notes`, {
514515
searchParams: {
@@ -558,7 +559,7 @@ export class CodingServer {
558559

559560
public async createMR(data: ICreateMRBody) {
560561
try {
561-
const url = this.getApiPrefix();
562+
const url = await this.getApiPrefix();
562563
const resp: ICreateMRResp = await got.post(`${url}/git/merge`, {
563564
resolveBodyOnly: true,
564565
responseType: `json`,
@@ -578,7 +579,7 @@ export class CodingServer {
578579

579580
public async getBranchList() {
580581
try {
581-
const url = this.getApiPrefix();
582+
const url = await this.getApiPrefix();
582583
const resp: IBranchListResp = await got
583584
.get(`${url}/git/list_branches`, {
584585
searchParams: {
@@ -610,7 +611,7 @@ export class CodingServer {
610611
keychain.deleteToken(TokenType.RefreshToken),
611612
]);
612613
this._session = null;
613-
vscode.commands.executeCommand('setContext', 'loggedIn', false);
614+
await vscode.commands.executeCommand('setContext', 'loggedIn', false);
614615
return true;
615616
} catch (e) {
616617
throw Error(e);

src/extension.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@ import { GitService } from 'src/common/gitService';
1111
export async function activate(context: vscode.ExtensionContext) {
1212
await GitService.init();
1313
const repoInfo = await CodingServer.getRepoParams();
14-
15-
if (!repoInfo?.team) {
16-
vscode.commands.executeCommand('setContext', 'hasTeam', false);
17-
vscode.window.showInformationMessage(`Please open a repo hosted by coding.net.`);
18-
} else {
19-
vscode.commands.executeCommand('setContext', 'hasTeam', true);
20-
context.workspaceState.update(`repoInfo`, repoInfo);
21-
}
14+
const hasRepo = !!repoInfo?.repo;
15+
await vscode.commands.executeCommand('setContext', 'hasRepo', hasRepo);
16+
await context.workspaceState.update(`repoInfo`, repoInfo);
2217

2318
const codingSrv = new CodingServer(context);
2419
await codingSrv.initialize();
@@ -28,10 +23,12 @@ export async function activate(context: vscode.ExtensionContext) {
2823
} else {
2924
await context.workspaceState.update(`session`, codingSrv.session);
3025
const rInfo = context.workspaceState.get(`repoInfo`, {});
31-
await context.workspaceState.update(`repoInfo`, {
32-
...rInfo,
33-
team: codingSrv.session.user.team,
34-
});
26+
if (repoInfo?.repo) {
27+
await context.workspaceState.update(`repoInfo`, {
28+
...rInfo,
29+
team: codingSrv.session.user.team,
30+
});
31+
}
3532
}
3633

3734
const mrDataProvider = new MRTreeDataProvider(context, codingSrv);
@@ -170,7 +167,7 @@ export async function activate(context: vscode.ExtensionContext) {
170167
if (!selection) return;
171168

172169
const r = context.workspaceState.get(`repoInfo`, {}) as IRepoInfo;
173-
context.workspaceState.update(`repoInfo`, {
170+
await context.workspaceState.update(`repoInfo`, {
174171
team: r?.team,
175172
project: selection?.description.replace(`/${selection?.label}`, ``),
176173
repo: selection?.label,

0 commit comments

Comments
 (0)