Skip to content

Commit 96e1f2f

Browse files
committed
Merge branch 'master' of https://github.com/cangzhang/coding-hbuilderx into feat/oauth
2 parents 4c77060 + a8c5897 commit 96e1f2f

File tree

12 files changed

+394
-56
lines changed

12 files changed

+394
-56
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"rightside": [
6969
{
7070
"id": "WebviewContainerId",
71-
"title": "WEBVIEW"
71+
"title": "CODING 合并请求详情"
7272
}
7373
]
7474
},
@@ -88,7 +88,7 @@
8888
"WebviewContainerId": [
8989
{
9090
"id": "codingPlugin.webview",
91-
"title": "WEBVIEW"
91+
"title": "CODING 合并请求详情"
9292
}
9393
]
9494
},
@@ -135,12 +135,14 @@
135135
},
136136
"dependencies": {
137137
"axios": "^0.21.0",
138+
"classnames": "^2.2.6",
138139
"isomorphic-git": "^1.8.0",
139140
"querystring": "^0.2.0",
140141
"react": "^17.0.1",
141142
"react-dom": "^17.0.1"
142143
},
143144
"devDependencies": {
145+
"@types/classnames": "^2.2.11",
144146
"@types/node": "^14.14.7",
145147
"@types/react": "^16.9.56",
146148
"@types/react-dom": "^16.9.9",

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import ACTIONS, { dispatch } from './utils/actions';
55
import { proxyCtx } from './utils/proxy';
66
import toast from './utils/toast';
77

8-
const accessToken = '1b7fca3bd7594a89b0f5e2a0250c1147';
8+
const accessToken = '7e4d9d17f87875e731d536d13635a700ddf52b12';
99
const user = {
1010
id: 8005956,
1111
avatar: 'https://coding-net-production-static-ci.codehub.cn/WM-TEXT-AVATAR-lvVeBfbGLtCPdcsAOPod.jpg',

src/init.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ import DepotTreeDataProvider from './trees/depot';
33
import MRTreeDataProvider from './trees/mr';
44

55
import toast from './utils/toast';
6-
import { getMRUrl } from './utils/repo';
76
import ACTIONS, { dispatch } from './utils/actions';
87
import { IDepot, IMRItem } from './typings/common';
98
import * as DCloudService from './services/dcloud';
109

1110
const { registerCommand } = hx.commands;
1211

1312
export function registerCommands(context: IContext) {
14-
const { codingServer } = context;
13+
const { codingServer, webviewProvider } = context;
1514

1615
context.subscriptions.push(
1716
registerCommand('codingPlugin.helloWorld', () => {
@@ -20,8 +19,21 @@ export function registerCommands(context: IContext) {
2019
);
2120

2221
context.subscriptions.push(
23-
registerCommand('codingPlugin.mrTreeItemClick', function ([team, mrItem]: [string, IMRItem]) {
24-
hx.env.openExternal(getMRUrl(team, mrItem));
22+
registerCommand('codingPlugin.mrTreeItemClick', async function ([team, mrItem]: [string, IMRItem]) {
23+
const matchRes = mrItem.path.match(/\/p\/([^/]+)\/d\/([^/]+)\/git\/merge\/([0-9]+)/);
24+
if (matchRes) {
25+
const [, project, repo, mergeRequestIId] = matchRes;
26+
const result = await codingServer.getMrDetail({ team, project, repo, mergeRequestIId });
27+
webviewProvider.update({
28+
session: codingServer.session,
29+
mrItem: result,
30+
repoInfo: {
31+
team,
32+
project,
33+
repo,
34+
},
35+
});
36+
}
2537
}),
2638
);
2739

@@ -41,8 +53,8 @@ export function registerCommands(context: IContext) {
4153
prompt: '请输入仓库名',
4254
});
4355
const team = codingServer.session?.user?.team;
44-
const result = await codingServer.createDepot(team, depot, depot);
45-
// TODO: 拉取代码,更新workspace
56+
await codingServer.createDepot(team, depot, depot);
57+
toast.info('仓库创建成功');
4658
}),
4759
);
4860
}

src/services/codingServer.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ export default class CodingServer {
2424
return this._session;
2525
}
2626

27-
get repo() {
28-
return this._repo;
29-
}
27+
getHeaders = (token?: string) => ({
28+
Authorization: `token ${token || this._session.accessToken}`,
29+
});
3030

3131
static async getRepoParams() {
3232
const folders = await hx.workspace.getWorkspaceFolders();
@@ -49,9 +49,7 @@ export default class CodingServer {
4949
const result = await axios({
5050
method: 'get',
5151
url: `https://${team}.coding.net/api/current_user`,
52-
params: {
53-
access_token: token,
54-
},
52+
headers: this.getHeaders(),
5553
});
5654

5755
return result?.data;
@@ -66,13 +64,13 @@ export default class CodingServer {
6664
const result = await axios({
6765
method: 'get',
6866
url,
67+
headers: this.getHeaders(),
6968
params: {
7069
status: `open`,
7170
sort: `action_at`,
7271
page: 1,
7372
PageSize: 100,
7473
sortDirection: `DESC`,
75-
access_token: this._session.accessToken,
7674
},
7775
});
7876
return result?.data?.list || [];
@@ -81,15 +79,27 @@ export default class CodingServer {
8179
}
8280
}
8381

82+
async getMrDetail({ team, project, repo, mergeRequestIId }: IRepoInfo & { mergeRequestIId: number }) {
83+
try {
84+
const result = await axios({
85+
method: 'get',
86+
url: `https://${team}.coding.net/api/user/${team}/project/${project}/depot/${repo}/git/merge/${mergeRequestIId}/detail`,
87+
headers: this.getHeaders(),
88+
});
89+
90+
return result.data;
91+
} catch (err) {
92+
console.error(err);
93+
}
94+
}
95+
8496
async getDepotList(team: string = this._repo?.team, project: string = this._repo?.project) {
8597
// TODO: 使用新接口
8698
try {
8799
const result = await axios({
88100
method: 'get',
89101
url: `https://${team}.coding.net/api/user/${team}/project/${project}/repos`,
90-
params: {
91-
access_token: this._session.accessToken,
92-
},
102+
headers: this.getHeaders(),
93103
});
94104

95105
return result?.data?.depots || [];
@@ -102,7 +112,8 @@ export default class CodingServer {
102112
try {
103113
const result = await axios({
104114
method: 'post',
105-
url: `https://${team}.coding.net/api/team/${team}/template-project?access_token=${this._session.accessToken}`,
115+
url: `https://${team}.coding.net/api/team/${team}/template-project`,
116+
headers: this.getHeaders(),
106117
data: {
107118
name: project,
108119
displayName: project,
@@ -123,9 +134,10 @@ export default class CodingServer {
123134

124135
const result = await axios({
125136
method: 'post',
126-
url: `https://${team}.coding.net/api/user/${team}/project/${project}/depot?access_token=${this._session.accessToken}`,
137+
url: `https://${team}.coding.net/api/user/${team}/project/${project}/depot`,
127138
headers: {
128139
'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
140+
...this.getHeaders(),
129141
},
130142
data: qs.stringify({
131143
name: depot,

src/webview.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import hx from 'hbuilderx';
2-
import fs from 'fs';
32
import path from 'path';
3+
import toast from './utils/toast';
4+
5+
interface IMessage {
6+
command: string;
7+
data: any;
8+
}
49

510
export default class WebviewProvider {
611
panel: IWebviewPanel;
@@ -11,8 +16,21 @@ export default class WebviewProvider {
1116
}
1217

1318
listen() {
14-
this.panel.webView.onDidReceiveMessage((message: any) => {
19+
this.panel.webView.onDidReceiveMessage((message: IMessage) => {
1520
console.log('webview receive message => ', message);
21+
const { command, data } = message;
22+
23+
switch (command) {
24+
case 'webview.mrDetail':
25+
hx.env.openExternal(data);
26+
break;
27+
case 'webview.toast':
28+
toast.error(data);
29+
break;
30+
default:
31+
hx.commands.executeCommand(command);
32+
return;
33+
}
1634
});
1735
}
1836

@@ -24,35 +42,17 @@ export default class WebviewProvider {
2442
return webviewPanel;
2543
}
2644

27-
update(itemId: string) {
45+
update(data: any) {
2846
const webview = this.panel.webView;
2947
const fileInfo = hx.Uri.file(path.resolve(__dirname, '../out/webviews/main.js'));
3048

3149
webview.html = `
3250
<body>
33-
<div style="max-width:200px;">
51+
<div>
3452
<div id='root'></div>
35-
<button onclick="test()">测试</button>
36-
<div>${itemId}</div>
3753
</div>
3854
<script>
39-
window.__TEXT__ = '${itemId}'
40-
function test() {
41-
alert('abc')
42-
43-
window.fetch("https://api.github.com/search/repositories?q=react", {
44-
"method": "GET",
45-
"mode": "cors",
46-
"credentials": "include"
47-
}).then((res) => {
48-
res.json().then(data => alert(JSON.stringify(data.items[0])))
49-
})
50-
}
51-
</script>
52-
<script>
53-
window.addEventListener("message", (msg) => {
54-
console.log(msg);
55-
});
55+
window.__CODING__ = '${JSON.stringify(data)}'
5656
</script>
5757
<script src='${fileInfo}'></script>
5858
</body>

webpack.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ module.exports = {
2727
},
2828
{
2929
loader: 'css-loader',
30+
options: {
31+
modules: {
32+
localIdentName: '[path][name]__[local]--[hash:base64:5]',
33+
},
34+
},
3035
},
3136
],
3237
},

0 commit comments

Comments
 (0)