Skip to content

Commit 8eb2c24

Browse files
authored
Merge pull request #11 from cangzhang/feat/overview
2 parents 4ed8c70 + 4dae656 commit 8eb2c24

27 files changed

+1328
-334
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Coding plugin for VS Code.
22

3+
## How to use
4+
5+
1. Install the plugin
6+
1. Open a repo host by `coding.net`
7+
1. Click on `Log in` button
8+
1. You will be redirected back to the editor, free to try & open an issue
9+
310
## Development
411

512
- Open this example in VS Code 1.47+

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "coding-plugin",
33
"description": "Coding plugin for VS Code.",
4-
"version": "1.0.0",
5-
"publisher": "alcheung",
4+
"version": "0.1.0",
5+
"publisher": "coding",
66
"license": "MIT",
77
"engines": {
88
"vscode": "^1.47.0"
@@ -148,6 +148,7 @@
148148
"styled-components": "^5.2.1"
149149
},
150150
"devDependencies": {
151+
"@svgr/webpack": "^5.5.0",
151152
"@types/react": "^16.9.53",
152153
"@types/react-dom": "^16.9.8",
153154
"@typescript-eslint/eslint-plugin": "^3.0.2",
@@ -162,7 +163,7 @@
162163
"style-loader": "^2.0.0",
163164
"svg-inline-loader": "^0.8.2",
164165
"ts-loader": "^8.0.5",
165-
"typescript": "^4.0.3",
166+
"typescript": "^4.1.2",
166167
"webpack": "4",
167168
"webpack-cli": "^4.0.0"
168169
},

src/codingServer.ts

Lines changed: 139 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import {
1414
ICreateMRBody,
1515
ICreateMRResp,
1616
IBranchListResp,
17+
IMemberListResp,
18+
IMRContentResp,
19+
ICreateCommentResp,
1720
} from 'src/typings/respResult';
1821
import { PromiseAdapter, promiseFromEvent, parseQuery, parseCloneUrl } from 'src/common/utils';
1922
import { GitService } from 'src/common/gitService';
@@ -25,7 +28,7 @@ const AUTH_SERVER = `https://x5p7m.csb.app`;
2528
const ClientId = `ff768664c96d04235b1cc4af1e3b37a8`;
2629
const ClientSecret = `d29ebb32cab8b5f0a643b5da7dcad8d1469312c7`;
2730

28-
export const ScopeList = [`user`, `user:email`, `project`, `project:depot`];
31+
export const ScopeList = [`user`, `user:email`, `project`, `project:depot`, `project:members`];
2932
const SCOPES = ScopeList.join(`,`);
3033
const NETWORK_ERROR = 'network error';
3134

@@ -246,14 +249,20 @@ export class CodingServer {
246249
throw new Error(`team not exist`);
247250
}
248251

249-
return `https://${repoInfo.team}.coding.net/api/user/${this._session?.user?.team}/project/${repoInfo.project}/depot/${repoInfo.repo}`;
252+
const projectApiPrefix = `https://${repoInfo.team}.coding.net/api/user/${this._session?.user?.team}/project/${repoInfo.project}`;
253+
return {
254+
projectApiPrefix,
255+
repoApiPrefix: `${projectApiPrefix}/depot/${repoInfo.repo}/git`,
256+
userApiPrefix: `https://${repoInfo.team}.coding.net/api/user/${this._session?.user?.global_key}`,
257+
rawFilePrefix: `https://${repoInfo.team}.coding.net/p/${repoInfo.project}/d/${repoInfo.repo}/git/raw`,
258+
};
250259
}
251260

252261
public async getMRList(repo?: string, status?: string): Promise<CodingResponse> {
253262
try {
254-
const url = await this.getApiPrefix();
263+
const { repoApiPrefix } = await this.getApiPrefix();
255264
const result: CodingResponse = await got
256-
.get(`${url}/git/merges/query`, {
265+
.get(`${repoApiPrefix}/merges/query`, {
257266
searchParams: {
258267
status,
259268
sort: `action_at`,
@@ -272,20 +281,13 @@ export class CodingServer {
272281

273282
public async getRepoList() {
274283
try {
275-
const repoInfo = this._context.workspaceState.get(`repoInfo`) as IRepoInfo;
276-
if (!repoInfo?.team) {
277-
throw new Error(`team not exist`);
278-
}
279-
284+
const { userApiPrefix } = await this.getApiPrefix();
280285
const { code, data, msg }: IRepoListResponse = await got
281-
.get(
282-
`https://${repoInfo.team}.coding.net/api/user/${this._session?.user?.global_key}/depots`,
283-
{
284-
searchParams: {
285-
access_token: this._session?.accessToken,
286-
},
286+
.get(`${userApiPrefix}/depots`, {
287+
searchParams: {
288+
access_token: this._session?.accessToken,
287289
},
288-
)
290+
})
289291
.json();
290292
if (code) {
291293
return Promise.reject({ code, msg });
@@ -302,9 +304,9 @@ export class CodingServer {
302304

303305
public async getMRDiff(iid: number) {
304306
try {
305-
const url = await this.getApiPrefix();
307+
const { repoApiPrefix } = await this.getApiPrefix();
306308
const diff: IMRDiffResponse = await got
307-
.get(`${url}/git/merge/${iid}/diff`, {
309+
.get(`${repoApiPrefix}/merge/${iid}/diff`, {
308310
searchParams: {
309311
access_token: this._session?.accessToken,
310312
},
@@ -321,9 +323,9 @@ export class CodingServer {
321323

322324
public async getMRDetail(iid: string) {
323325
try {
324-
const url = await this.getApiPrefix();
326+
const { repoApiPrefix } = await this.getApiPrefix();
325327
const resp: IMRDetailResponse = await got
326-
.get(`${url}/git/merge/${iid}/detail`, {
328+
.get(`${repoApiPrefix}/merge/${iid}/detail`, {
327329
searchParams: {
328330
access_token: this._session?.accessToken,
329331
},
@@ -342,9 +344,9 @@ export class CodingServer {
342344

343345
public async getMRActivities(iid: string) {
344346
try {
345-
const url = await this.getApiPrefix();
347+
const { repoApiPrefix } = await this.getApiPrefix();
346348
const result: IMRActivitiesResponse = await got
347-
.get(`${url}/git/merge/${iid}/activities`, {
349+
.get(`${repoApiPrefix}/merge/${iid}/activities`, {
348350
searchParams: {
349351
access_token: this._session?.accessToken,
350352
},
@@ -362,9 +364,9 @@ export class CodingServer {
362364

363365
public async getMRReviewers(iid: string) {
364366
try {
365-
const url = await this.getApiPrefix();
367+
const { repoApiPrefix } = await this.getApiPrefix();
366368
const result: IMRReviewersResponse = await got
367-
.get(`${url}/git/merge/${iid}/reviewers`, {
369+
.get(`${repoApiPrefix}/merge/${iid}/reviewers`, {
368370
searchParams: {
369371
access_token: this._session?.accessToken,
370372
},
@@ -382,9 +384,9 @@ export class CodingServer {
382384

383385
public async getMRComments(iid: string) {
384386
try {
385-
const url = await this.getApiPrefix();
387+
const { repoApiPrefix } = await this.getApiPrefix();
386388
const result: CodingResponse = await got
387-
.get(`${url}/git/merge/${iid}/comments`, {
389+
.get(`${repoApiPrefix}/merge/${iid}/comments`, {
388390
searchParams: {
389391
access_token: this._session?.accessToken,
390392
},
@@ -402,9 +404,9 @@ export class CodingServer {
402404

403405
public async closeMR(iid: string) {
404406
try {
405-
const url = await this.getApiPrefix();
407+
const { repoApiPrefix } = await this.getApiPrefix();
406408
const result: CodingResponse = await got
407-
.post(`${url}/git/merge/${iid}/refuse`, {
409+
.post(`${repoApiPrefix}/merge/${iid}/refuse`, {
408410
searchParams: {
409411
access_token: this._session?.accessToken,
410412
},
@@ -422,9 +424,9 @@ export class CodingServer {
422424

423425
public async approveMR(iid: string) {
424426
try {
425-
const url = await this.getApiPrefix();
427+
const { repoApiPrefix } = await this.getApiPrefix();
426428
const result: CodingResponse = await got
427-
.post(`${url}/git/merge/${iid}/good`, {
429+
.post(`${repoApiPrefix}/merge/${iid}/good`, {
428430
searchParams: {
429431
access_token: this._session?.accessToken,
430432
},
@@ -442,9 +444,9 @@ export class CodingServer {
442444

443445
public async disapproveMR(iid: string) {
444446
try {
445-
const url = await this.getApiPrefix();
447+
const { repoApiPrefix } = await this.getApiPrefix();
446448
const result: CodingResponse = await got
447-
.delete(`${url}/git/merge/${iid}/good`, {
449+
.delete(`${repoApiPrefix}/merge/${iid}/good`, {
448450
searchParams: {
449451
access_token: this._session?.accessToken,
450452
},
@@ -462,9 +464,9 @@ export class CodingServer {
462464

463465
public async mergeMR(iid: string) {
464466
try {
465-
const url = await this.getApiPrefix();
467+
const { repoApiPrefix } = await this.getApiPrefix();
466468
const result: CodingResponse = await got
467-
.post(`${url}/git/merge/${iid}/merge`, {
469+
.post(`${repoApiPrefix}/merge/${iid}/merge`, {
468470
searchParams: {
469471
access_token: this._session?.accessToken,
470472
},
@@ -485,9 +487,9 @@ export class CodingServer {
485487

486488
public async updateMRTitle(iid: string, title: string) {
487489
try {
488-
const url = await this.getApiPrefix();
490+
const { repoApiPrefix } = await this.getApiPrefix();
489491
const result: CodingResponse = await got
490-
.put(`${url}/git/merge/${iid}/update-title`, {
492+
.put(`${repoApiPrefix}/merge/${iid}/update-title`, {
491493
searchParams: {
492494
access_token: this._session?.accessToken,
493495
title,
@@ -509,9 +511,9 @@ export class CodingServer {
509511

510512
public async commentMR(mrId: number, comment: string) {
511513
try {
512-
const url = await this.getApiPrefix();
513-
const result: CodingResponse = await got
514-
.post(`${url}/git/line_notes`, {
514+
const { repoApiPrefix } = await this.getApiPrefix();
515+
const result: ICreateCommentResp = await got
516+
.post(`${repoApiPrefix}/line_notes`, {
515517
searchParams: {
516518
access_token: this._session?.accessToken,
517519
line: 0,
@@ -539,12 +541,8 @@ export class CodingServer {
539541

540542
public async getRemoteFileContent(path: string) {
541543
try {
542-
const repoInfo = this._context.workspaceState.get(`repoInfo`) as IRepoInfo;
543-
if (!repoInfo?.team) {
544-
throw new Error(`team not exist`);
545-
}
546-
547-
const url = `https://${repoInfo.team}.coding.net/p/${repoInfo.project}/d/${repoInfo.repo}/git/raw/${path}`;
544+
const { rawFilePrefix } = await this.getApiPrefix();
545+
const url = `${rawFilePrefix}/${path}`;
548546
const { body } = await got.get(url, {
549547
searchParams: {
550548
access_token: this._session?.accessToken,
@@ -559,8 +557,8 @@ export class CodingServer {
559557

560558
public async createMR(data: ICreateMRBody) {
561559
try {
562-
const url = await this.getApiPrefix();
563-
const resp: ICreateMRResp = await got.post(`${url}/git/merge`, {
560+
const { repoApiPrefix } = await this.getApiPrefix();
561+
const resp: ICreateMRResp = await got.post(`${repoApiPrefix}/merge`, {
564562
resolveBodyOnly: true,
565563
responseType: `json`,
566564
searchParams: {
@@ -579,9 +577,9 @@ export class CodingServer {
579577

580578
public async getBranchList() {
581579
try {
582-
const url = await this.getApiPrefix();
580+
const { repoApiPrefix } = await this.getApiPrefix();
583581
const resp: IBranchListResp = await got
584-
.get(`${url}/git/list_branches`, {
582+
.get(`${repoApiPrefix}/list_branches`, {
585583
searchParams: {
586584
access_token: this._session?.accessToken,
587585
},
@@ -596,6 +594,98 @@ export class CodingServer {
596594
}
597595
}
598596

597+
public async getProjectMembers() {
598+
try {
599+
const { projectApiPrefix } = await this.getApiPrefix();
600+
const resp: IMemberListResp = await got
601+
.get(`${projectApiPrefix}/members`, {
602+
searchParams: {
603+
pageSize: 9999,
604+
access_token: this._session?.accessToken,
605+
},
606+
})
607+
.json();
608+
609+
if (resp.code) {
610+
return Promise.reject(resp);
611+
}
612+
613+
return resp;
614+
} catch (err) {
615+
return Promise.reject(err);
616+
}
617+
}
618+
619+
public async addMRReviewers(iid: string, ids: number[]): Promise<number[]> {
620+
const { repoApiPrefix } = await this.getApiPrefix();
621+
const tasks: Promise<CodingResponse>[] = ids.map((id) => {
622+
return got
623+
.post(`${repoApiPrefix}/merge/${iid}/reviewers`, {
624+
searchParams: {
625+
user_id: id,
626+
access_token: this._session?.accessToken,
627+
},
628+
})
629+
.json();
630+
});
631+
const result: PromiseSettledResult<CodingResponse>[] = await Promise.allSettled(tasks);
632+
const fulfilled = ids.reduce((res, cur, idx) => {
633+
if (result[idx].status === `fulfilled`) {
634+
res = res.concat(cur);
635+
}
636+
637+
return res;
638+
}, [] as number[]);
639+
return fulfilled;
640+
}
641+
642+
public async removeMRReviewers(iid: string, ids: number[]): Promise<number[]> {
643+
const { repoApiPrefix } = await this.getApiPrefix();
644+
const tasks: Promise<CodingResponse>[] = ids.map((id) => {
645+
return got
646+
.delete(`${repoApiPrefix}/merge/${iid}/reviewers`, {
647+
searchParams: {
648+
user_id: id,
649+
access_token: this._session?.accessToken,
650+
},
651+
})
652+
.json();
653+
});
654+
const result: PromiseSettledResult<CodingResponse>[] = await Promise.allSettled(tasks);
655+
const fulfilled = ids.reduce((res, cur, idx) => {
656+
if (result[idx].status === `fulfilled`) {
657+
res = res.concat(cur);
658+
}
659+
660+
return res;
661+
}, [] as number[]);
662+
return fulfilled;
663+
}
664+
665+
public async updateMRDesc(iid: string, content: string) {
666+
try {
667+
const { repoApiPrefix } = await this.getApiPrefix();
668+
const resp: IMRContentResp = await got
669+
.put(`${repoApiPrefix}/merge/${iid}/update-content`, {
670+
form: {
671+
content,
672+
},
673+
searchParams: {
674+
access_token: this._session?.accessToken,
675+
},
676+
})
677+
.json();
678+
679+
if (resp.code) {
680+
return Promise.reject(resp);
681+
}
682+
683+
return resp;
684+
} catch (e) {
685+
return Promise.reject(e);
686+
}
687+
}
688+
599689
get loggedIn() {
600690
return this._loggedIn;
601691
}

src/extension.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ export async function activate(context: vscode.ExtensionContext) {
5858
});
5959
});
6060
codingSrv.getMRActivities(mr.iid).then((activityResp) => {
61-
Panel.currentPanel?.broadcast(`mr.update.activities`, activityResp.data);
61+
Panel.currentPanel?.broadcast(`mr.activities.init`, activityResp.data);
6262
});
6363
codingSrv.getMRReviewers(mr.iid).then((reviewerResp) => {
64-
Panel.currentPanel?.broadcast(`mr.update.reviewers`, reviewerResp.data);
64+
Panel.currentPanel?.broadcast(`mr.reviewers.init`, reviewerResp.data);
6565
});
6666
codingSrv.getMRComments(mr.iid).then((commentResp) => {
67-
Panel.currentPanel?.broadcast(`mr.udpate.comments`, commentResp.data);
67+
Panel.currentPanel?.broadcast(`mr.update.comments`, commentResp.data);
6868
});
6969
}),
7070
);

0 commit comments

Comments
 (0)