Skip to content

Commit 20c4ef6

Browse files
committed
WIP: refactor server.
1 parent 4ed8c70 commit 20c4ef6

File tree

13 files changed

+801
-130
lines changed

13 files changed

+801
-130
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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",

src/codingServer.ts

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,20 @@ export class CodingServer {
246246
throw new Error(`team not exist`);
247247
}
248248

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

252258
public async getMRList(repo?: string, status?: string): Promise<CodingResponse> {
253259
try {
254-
const url = await this.getApiPrefix();
260+
const { repoApiPrefix } = await this.getApiPrefix();
255261
const result: CodingResponse = await got
256-
.get(`${url}/git/merges/query`, {
262+
.get(`${repoApiPrefix}/merges/query`, {
257263
searchParams: {
258264
status,
259265
sort: `action_at`,
@@ -272,20 +278,13 @@ export class CodingServer {
272278

273279
public async getRepoList() {
274280
try {
275-
const repoInfo = this._context.workspaceState.get(`repoInfo`) as IRepoInfo;
276-
if (!repoInfo?.team) {
277-
throw new Error(`team not exist`);
278-
}
279-
281+
const { userApiPrefix } = await this.getApiPrefix();
280282
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-
},
283+
.get(`${userApiPrefix}/depots`, {
284+
searchParams: {
285+
access_token: this._session?.accessToken,
287286
},
288-
)
287+
})
289288
.json();
290289
if (code) {
291290
return Promise.reject({ code, msg });
@@ -302,9 +301,9 @@ export class CodingServer {
302301

303302
public async getMRDiff(iid: number) {
304303
try {
305-
const url = await this.getApiPrefix();
304+
const { repoApiPrefix } = await this.getApiPrefix();
306305
const diff: IMRDiffResponse = await got
307-
.get(`${url}/git/merge/${iid}/diff`, {
306+
.get(`${repoApiPrefix}/merge/${iid}/diff`, {
308307
searchParams: {
309308
access_token: this._session?.accessToken,
310309
},
@@ -321,9 +320,9 @@ export class CodingServer {
321320

322321
public async getMRDetail(iid: string) {
323322
try {
324-
const url = await this.getApiPrefix();
323+
const { repoApiPrefix } = await this.getApiPrefix();
325324
const resp: IMRDetailResponse = await got
326-
.get(`${url}/git/merge/${iid}/detail`, {
325+
.get(`${repoApiPrefix}/merge/${iid}/detail`, {
327326
searchParams: {
328327
access_token: this._session?.accessToken,
329328
},
@@ -342,9 +341,9 @@ export class CodingServer {
342341

343342
public async getMRActivities(iid: string) {
344343
try {
345-
const url = await this.getApiPrefix();
344+
const { repoApiPrefix } = await this.getApiPrefix();
346345
const result: IMRActivitiesResponse = await got
347-
.get(`${url}/git/merge/${iid}/activities`, {
346+
.get(`${repoApiPrefix}/merge/${iid}/activities`, {
348347
searchParams: {
349348
access_token: this._session?.accessToken,
350349
},
@@ -362,9 +361,9 @@ export class CodingServer {
362361

363362
public async getMRReviewers(iid: string) {
364363
try {
365-
const url = await this.getApiPrefix();
364+
const { repoApiPrefix } = await this.getApiPrefix();
366365
const result: IMRReviewersResponse = await got
367-
.get(`${url}/git/merge/${iid}/reviewers`, {
366+
.get(`${repoApiPrefix}/merge/${iid}/reviewers`, {
368367
searchParams: {
369368
access_token: this._session?.accessToken,
370369
},
@@ -382,9 +381,9 @@ export class CodingServer {
382381

383382
public async getMRComments(iid: string) {
384383
try {
385-
const url = await this.getApiPrefix();
384+
const { repoApiPrefix } = await this.getApiPrefix();
386385
const result: CodingResponse = await got
387-
.get(`${url}/git/merge/${iid}/comments`, {
386+
.get(`${repoApiPrefix}/merge/${iid}/comments`, {
388387
searchParams: {
389388
access_token: this._session?.accessToken,
390389
},
@@ -402,9 +401,9 @@ export class CodingServer {
402401

403402
public async closeMR(iid: string) {
404403
try {
405-
const url = await this.getApiPrefix();
404+
const { repoApiPrefix } = await this.getApiPrefix();
406405
const result: CodingResponse = await got
407-
.post(`${url}/git/merge/${iid}/refuse`, {
406+
.post(`${repoApiPrefix}/merge/${iid}/refuse`, {
408407
searchParams: {
409408
access_token: this._session?.accessToken,
410409
},
@@ -422,9 +421,9 @@ export class CodingServer {
422421

423422
public async approveMR(iid: string) {
424423
try {
425-
const url = await this.getApiPrefix();
424+
const { repoApiPrefix } = await this.getApiPrefix();
426425
const result: CodingResponse = await got
427-
.post(`${url}/git/merge/${iid}/good`, {
426+
.post(`${repoApiPrefix}/merge/${iid}/good`, {
428427
searchParams: {
429428
access_token: this._session?.accessToken,
430429
},
@@ -442,9 +441,9 @@ export class CodingServer {
442441

443442
public async disapproveMR(iid: string) {
444443
try {
445-
const url = await this.getApiPrefix();
444+
const { repoApiPrefix } = await this.getApiPrefix();
446445
const result: CodingResponse = await got
447-
.delete(`${url}/git/merge/${iid}/good`, {
446+
.delete(`${repoApiPrefix}/merge/${iid}/good`, {
448447
searchParams: {
449448
access_token: this._session?.accessToken,
450449
},
@@ -462,9 +461,9 @@ export class CodingServer {
462461

463462
public async mergeMR(iid: string) {
464463
try {
465-
const url = await this.getApiPrefix();
464+
const { repoApiPrefix } = await this.getApiPrefix();
466465
const result: CodingResponse = await got
467-
.post(`${url}/git/merge/${iid}/merge`, {
466+
.post(`${repoApiPrefix}/merge/${iid}/merge`, {
468467
searchParams: {
469468
access_token: this._session?.accessToken,
470469
},
@@ -485,9 +484,9 @@ export class CodingServer {
485484

486485
public async updateMRTitle(iid: string, title: string) {
487486
try {
488-
const url = await this.getApiPrefix();
487+
const { repoApiPrefix } = await this.getApiPrefix();
489488
const result: CodingResponse = await got
490-
.put(`${url}/git/merge/${iid}/update-title`, {
489+
.put(`${repoApiPrefix}/merge/${iid}/update-title`, {
491490
searchParams: {
492491
access_token: this._session?.accessToken,
493492
title,
@@ -509,9 +508,9 @@ export class CodingServer {
509508

510509
public async commentMR(mrId: number, comment: string) {
511510
try {
512-
const url = await this.getApiPrefix();
511+
const { repoApiPrefix } = await this.getApiPrefix();
513512
const result: CodingResponse = await got
514-
.post(`${url}/git/line_notes`, {
513+
.post(`${repoApiPrefix}/line_notes`, {
515514
searchParams: {
516515
access_token: this._session?.accessToken,
517516
line: 0,
@@ -539,12 +538,8 @@ export class CodingServer {
539538

540539
public async getRemoteFileContent(path: string) {
541540
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}`;
541+
const { rawFilePrefix } = await this.getApiPrefix();
542+
const url = `${rawFilePrefix}/${path}`;
548543
const { body } = await got.get(url, {
549544
searchParams: {
550545
access_token: this._session?.accessToken,
@@ -559,8 +554,8 @@ export class CodingServer {
559554

560555
public async createMR(data: ICreateMRBody) {
561556
try {
562-
const url = await this.getApiPrefix();
563-
const resp: ICreateMRResp = await got.post(`${url}/git/merge`, {
557+
const { repoApiPrefix } = await this.getApiPrefix();
558+
const resp: ICreateMRResp = await got.post(`${repoApiPrefix}/merge`, {
564559
resolveBodyOnly: true,
565560
responseType: `json`,
566561
searchParams: {
@@ -579,9 +574,9 @@ export class CodingServer {
579574

580575
public async getBranchList() {
581576
try {
582-
const url = await this.getApiPrefix();
577+
const { repoApiPrefix } = await this.getApiPrefix();
583578
const resp: IBranchListResp = await got
584-
.get(`${url}/git/list_branches`, {
579+
.get(`${repoApiPrefix}/list_branches`, {
585580
searchParams: {
586581
access_token: this._session?.accessToken,
587582
},

src/extension.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ export async function activate(context: vscode.ExtensionContext) {
200200
);
201201
}),
202202
);
203+
context.subscriptions.push(
204+
vscode.commands.registerCommand(`codingPlugin.mr.addReviewer`, async (file: IFileNode) => {}),
205+
);
203206

204207
if (vscode.window.registerWebviewPanelSerializer) {
205208
// Make sure we register a serializer in activation event

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = {
3636
},
3737
{
3838
test: /\.svg$/,
39-
loader: 'svg-inline-loader',
39+
use: ['@svgr/webpack'],
4040
},
4141
],
4242
},

webviews/App.tsx

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,23 @@
11
import React, { useRef, useState } from 'react';
2-
import styled from 'styled-components';
32

43
import { view } from '@risingstack/react-easy-state';
54
import appStore from 'webviews/store/appStore';
65
import persistDataHook from 'webviews/hooks/persistDataHook';
76
import Activities from 'webviews/components/Activities';
87
import Reviewers from 'webviews/components/Reviewers';
98
import messageTransferHook from 'webviews/hooks/messageTransferHook';
10-
11-
const EmptyWrapper = styled.div`
12-
font-size: 16px;
13-
`;
14-
const TitleWrapper = styled.div`
15-
display: flex;
16-
align-items: center;
17-
font-size: 20px;
18-
19-
.edit {
20-
display: none;
21-
}
22-
23-
&:hover .edit {
24-
display: block;
25-
}
26-
`;
27-
const Row = styled.div`
28-
display: flex;
29-
align-items: center;
30-
margin: 16px 0 0;
31-
padding-bottom: 15px;
32-
border-bottom: 1px solid var(--vscode-list-inactiveSelectionBackground);
33-
`;
34-
const Desc = styled.article`
35-
border: 1px solid gray;
36-
padding: 10px;
37-
`;
38-
const BodyWrap = styled.div`
39-
display: flex;
40-
`;
41-
const Body = styled.div`
42-
flex: 1;
43-
`;
44-
const Sidebar = styled.div`
45-
width: 200px;
46-
margin-left: 20px;
47-
`;
48-
const EditBtn = styled.span`
49-
width: 16px;
50-
height: 16px;
51-
margin-left: 10px;
52-
cursor: pointer;
53-
`;
54-
const Empty = styled.div`
55-
text-align: center;
56-
`;
9+
import {
10+
EmptyWrapper,
11+
TitleWrapper,
12+
Row,
13+
Desc,
14+
BodyWrap,
15+
Body,
16+
Sidebar,
17+
Empty,
18+
BranchName,
19+
EditBtn,
20+
} from 'webviews/styles/MROverview';
5721

5822
function App() {
5923
const { currentMR, updateMRTitle } = appStore;
@@ -116,17 +80,14 @@ function App() {
11680
#{currentMR.iid}
11781
</a>
11882
)
119-
<EditBtn
120-
className='edit'
121-
onClick={handleEdit}
122-
dangerouslySetInnerHTML={{ __html: require('./assets/edit.svg') }}
123-
/>
83+
<EditBtn onClick={handleEdit} />
12484
</>
12585
)}
12686
</TitleWrapper>
12787
<Row>
12888
<div id='status'>{mergeRequest?.merge_status}</div>
129-
<code>{mergeRequest?.srcBranch}</code><code>{mergeRequest?.desBranch}</code>
89+
<BranchName>{mergeRequest?.srcBranch}</BranchName>
90+
<BranchName>{mergeRequest?.desBranch}</BranchName>
13091
</Row>
13192
<BodyWrap>
13293
<Body>

webviews/assets/delete.svg

Lines changed: 3 additions & 0 deletions
Loading

webviews/assets/plus.svg

Lines changed: 3 additions & 0 deletions
Loading

webviews/components/AddComment.tsx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,46 @@ function AddComment() {
7070
return (
7171
<ActionWrap>
7272
{showAllowMergeBtn && !getAgreed() && (
73-
<button data-command='approveMR' onClick={handleClick} disabled={isBusy}>
73+
<button
74+
className={`colored`}
75+
data-command='approveMR'
76+
onClick={handleClick}
77+
disabled={isBusy}>
7478
Approve
7579
</button>
7680
)}
7781
{showAllowMergeBtn && getAgreed() && (
78-
<button data-command='disapproveMR' onClick={handleClick} disabled={isBusy}>
82+
<button
83+
className={`colored`}
84+
data-command='disapproveMR'
85+
onClick={handleClick}
86+
disabled={isBusy}>
7987
Disapprove
8088
</button>
8189
)}
8290
{showMergeBtn && (
83-
<button data-command='mergeMR' onClick={handleClick} disabled={isBusy}>
91+
<button
92+
className={`colored`}
93+
data-command='mergeMR'
94+
onClick={handleClick}
95+
disabled={isBusy}>
8496
Merge
8597
</button>
8698
)}
8799
{showCloseBtn && (
88-
<button data-command='closeMR' onClick={handleClick} disabled={isBusy}>
100+
<button
101+
className={`colored`}
102+
data-command='closeMR'
103+
onClick={handleClick}
104+
disabled={isBusy}>
89105
Close Merge Request
90106
</button>
91107
)}
92-
<button data-command='commentMR' onClick={handleClick} disabled={isBusy || !comment}>
108+
<button
109+
className={`colored`}
110+
data-command='commentMR'
111+
onClick={handleClick}
112+
disabled={isBusy || !comment}>
93113
Comment
94114
</button>
95115
</ActionWrap>

0 commit comments

Comments
 (0)