mirror of
https://github.com/kekxv/AiReviewPR.git
synced 2025-02-11 22:41:50 +01:00
ai 代码审核
This commit is contained in:
75
src/utils.ts
Normal file
75
src/utils.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import http from "http";
|
||||
import https from "https";
|
||||
|
||||
export function split_message(files: string) {
|
||||
console.log("files debug:",files)
|
||||
files = files.trim()
|
||||
if (!files) {
|
||||
let t = files.split("\n");
|
||||
if (t.length > 0) return t.map(str => str.trim());
|
||||
return files.split(",").map(str => str.trim())
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
export function doesAnyPatternMatch(patterns: Array<string>, str: string) {
|
||||
// 遍历正则表达式数组
|
||||
return patterns.some(pattern => {
|
||||
// 创建正则表达式对象,匹配模式
|
||||
const regex = new RegExp(pattern);
|
||||
// 测试字符串是否与正则表达式匹配
|
||||
return regex.test(str);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* post data
|
||||
* @param url url
|
||||
* @param body post data
|
||||
* @param header post header
|
||||
* @param json is json res
|
||||
*/
|
||||
export async function post({url, body, header, json}: any): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
json = typeof json === "boolean" ? json : true;
|
||||
const data = typeof body === "string" ? body : JSON.stringify(body);
|
||||
let url_ = new URL(url);
|
||||
header = header || {};
|
||||
header['Content-Type'] = header['Content-Type'] || 'application/json';
|
||||
header['Content-Length'] = Buffer.byteLength(data)
|
||||
const options = {
|
||||
hostname: url_.hostname, // 确保去掉协议部分
|
||||
path: url_.pathname + (url_.search || ''),
|
||||
method: 'POST',
|
||||
headers: header
|
||||
};
|
||||
|
||||
// noinspection DuplicatedCode
|
||||
const req = (url_.protocol === "http" ? http : https).request(options, (res) => {
|
||||
let responseBody = '';
|
||||
|
||||
res.on('data', (chunk) => {
|
||||
responseBody += chunk;
|
||||
});
|
||||
|
||||
res.on('end', () => {
|
||||
try {
|
||||
if (json) {
|
||||
resolve(JSON.parse(responseBody));
|
||||
} else {
|
||||
resolve(responseBody);
|
||||
}
|
||||
} catch (error) {
|
||||
reject(new Error('Failed to parse JSON response'));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', (error) => {
|
||||
reject(new Error(`Request failed: ${error.message}`));
|
||||
});
|
||||
|
||||
req.write(data);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user