mirror of
https://github.com/kekxv/AiReviewPR.git
synced 2025-02-11 22:41:50 +01:00
Optimize internal logic.
This commit is contained in:
@@ -6,6 +6,9 @@ inputs:
|
|||||||
repository:
|
repository:
|
||||||
description: 'Repository name with owner. For example, actions/checkout'
|
description: 'Repository name with owner. For example, actions/checkout'
|
||||||
default: ${{ github.repository }}
|
default: ${{ github.repository }}
|
||||||
|
REVIEW_PULL_REQUEST:
|
||||||
|
description: 'Whether to compare the records from the beginning of the commit to the latest one; false indicates that only the most recent commit will be reviewed. 是否比对提交开始到最新的记录,false表示只审核最新一次提交审核'
|
||||||
|
default: false
|
||||||
BASE_REF:
|
BASE_REF:
|
||||||
description: 'github event pull_request base ref'
|
description: 'github event pull_request base ref'
|
||||||
default: ${{ github.event.pull_request.base.ref }}
|
default: ${{ github.event.pull_request.base.ref }}
|
||||||
|
|||||||
59
dist/index.js
vendored
59
dist/index.js
vendored
@@ -10,6 +10,7 @@ const reviewers_prompt = (process.env.INPUT_REVIEWERS_PROMPT || "");
|
|||||||
useChinese = language.toLowerCase() === "chinese";
|
useChinese = language.toLowerCase() === "chinese";
|
||||||
const include_files = (0, utils_1.split_message)(process.env.INPUT_INCLUDE_FILES || "");
|
const include_files = (0, utils_1.split_message)(process.env.INPUT_INCLUDE_FILES || "");
|
||||||
const exclude_files = (0, utils_1.split_message)(process.env.INPUT_EXCLUDE_FILES || "");
|
const exclude_files = (0, utils_1.split_message)(process.env.INPUT_EXCLUDE_FILES || "");
|
||||||
|
const review_pull_request = (!process.env.INPUT_REVIEW_PULL_REQUEST) ? false : (process.env.INPUT_REVIEW_PULL_REQUEST.toLowerCase() === "true");
|
||||||
const system_prompt = reviewers_prompt || (0, prompt_1.take_system_prompt)(prompt_genre, language);
|
const system_prompt = reviewers_prompt || (0, prompt_1.take_system_prompt)(prompt_genre, language);
|
||||||
// 获取输入参数
|
// 获取输入参数
|
||||||
const url = process.env.INPUT_HOST; // INPUT_HOST 是从 action.yml 中定义的输入
|
const url = process.env.INPUT_HOST; // INPUT_HOST 是从 action.yml 中定义的输入
|
||||||
@@ -53,7 +54,8 @@ async function aiGenerate({ host, token, prompt, model, system }) {
|
|||||||
header: { 'Authorization': token ? `Bearer ${token}` : "", }
|
header: { 'Authorization': token ? `Bearer ${token}` : "", }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
async function aiCheckDiffContext() {
|
async function getPrDiffContext() {
|
||||||
|
let items = [];
|
||||||
const BASE_REF = process.env.INPUT_BASE_REF;
|
const BASE_REF = process.env.INPUT_BASE_REF;
|
||||||
try {
|
try {
|
||||||
(0, node_child_process_1.execSync)(`git fetch origin ${BASE_REF}`, { encoding: 'utf-8' });
|
(0, node_child_process_1.execSync)(`git fetch origin ${BASE_REF}`, { encoding: 'utf-8' });
|
||||||
@@ -61,9 +63,9 @@ async function aiCheckDiffContext() {
|
|||||||
const diffOutput = (0, node_child_process_1.execSync)(`git diff --name-only origin/${BASE_REF}...HEAD`, { encoding: 'utf-8' });
|
const diffOutput = (0, node_child_process_1.execSync)(`git diff --name-only origin/${BASE_REF}...HEAD`, { encoding: 'utf-8' });
|
||||||
let files = diffOutput.trim().split("\n");
|
let files = diffOutput.trim().split("\n");
|
||||||
for (let key in files) {
|
for (let key in files) {
|
||||||
|
// noinspection DuplicatedCode
|
||||||
if (!files[key])
|
if (!files[key])
|
||||||
continue;
|
continue;
|
||||||
console.log("check diff context:", files[key]);
|
|
||||||
if ((include_files.length > 0) && (!(0, utils_1.doesAnyPatternMatch)(include_files, files[key]))) {
|
if ((include_files.length > 0) && (!(0, utils_1.doesAnyPatternMatch)(include_files, files[key]))) {
|
||||||
console.log("exclude(include):", files[key]);
|
console.log("exclude(include):", files[key]);
|
||||||
continue;
|
continue;
|
||||||
@@ -73,12 +75,61 @@ async function aiCheckDiffContext() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const fileDiffOutput = (0, node_child_process_1.execSync)(`git diff origin/${BASE_REF}...HEAD -- "${files[key]}"`, { encoding: 'utf-8' });
|
const fileDiffOutput = (0, node_child_process_1.execSync)(`git diff origin/${BASE_REF}...HEAD -- "${files[key]}"`, { encoding: 'utf-8' });
|
||||||
|
items.push({
|
||||||
|
path: files[key],
|
||||||
|
context: fileDiffOutput,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error('Error executing git diff:', error);
|
||||||
|
}
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
async function getHeadDiffContext() {
|
||||||
|
let items = [];
|
||||||
|
try {
|
||||||
|
// exec git diff get diff files
|
||||||
|
const diffOutput = (0, node_child_process_1.execSync)(`git diff --name-only HEAD^`, { encoding: 'utf-8' });
|
||||||
|
let files = diffOutput.trim().split("\n");
|
||||||
|
for (let key in files) {
|
||||||
|
// noinspection DuplicatedCode
|
||||||
|
if (!files[key])
|
||||||
|
continue;
|
||||||
|
if ((include_files.length > 0) && (!(0, utils_1.doesAnyPatternMatch)(include_files, files[key]))) {
|
||||||
|
console.log("exclude(include):", files[key]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if ((exclude_files.length > 0) && ((0, utils_1.doesAnyPatternMatch)(exclude_files, files[key]))) {
|
||||||
|
console.log("exclude(exclude):", files[key]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const fileDiffOutput = (0, node_child_process_1.execSync)(`git diff HEAD^ -- "${files[key]}"`, { encoding: 'utf-8' });
|
||||||
|
items.push({
|
||||||
|
path: files[key],
|
||||||
|
context: fileDiffOutput,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error('Error executing git diff:', error);
|
||||||
|
}
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
async function aiCheckDiffContext() {
|
||||||
|
try {
|
||||||
|
let commit_sha_url = `${process.env.GITHUB_SERVER_URL}/${process.env.INPUT_REPOSITORY}/src/commit/${process.env.GITHUB_SHA}`;
|
||||||
|
let items = review_pull_request ? await getPrDiffContext() : await getHeadDiffContext();
|
||||||
|
for (let key in items) {
|
||||||
|
if (!items[key])
|
||||||
|
continue;
|
||||||
|
let item = items[key];
|
||||||
// ai generate
|
// ai generate
|
||||||
try {
|
try {
|
||||||
let response = await aiGenerate({
|
let response = await aiGenerate({
|
||||||
host: url,
|
host: url,
|
||||||
token: process.env.INPUT_AI_TOKEN,
|
token: process.env.INPUT_AI_TOKEN,
|
||||||
prompt: fileDiffOutput,
|
prompt: item.context,
|
||||||
model: model,
|
model: model,
|
||||||
system: process.env.INPUT_REVIEW_PROMPT
|
system: process.env.INPUT_REVIEW_PROMPT
|
||||||
});
|
});
|
||||||
@@ -96,7 +147,7 @@ async function aiCheckDiffContext() {
|
|||||||
commit = commit.substring(0, commit.length - 3);
|
commit = commit.substring(0, commit.length - 3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let comments = `# ${Review} \r\n${process.env.GITHUB_SERVER_URL}/${process.env.INPUT_REPOSITORY}/src/commit/${process.env.GITHUB_SHA}/${files[key]} \r\n\r\n\r\n${commit}`;
|
let comments = `# ${Review} \r\n${commit_sha_url}/${item.path} \r\n\r\n\r\n${commit}`;
|
||||||
let resp = await pushComments(comments);
|
let resp = await pushComments(comments);
|
||||||
if (!resp.id) {
|
if (!resp.id) {
|
||||||
// noinspection ExceptionCaughtLocallyJS
|
// noinspection ExceptionCaughtLocallyJS
|
||||||
|
|||||||
57
src/index.ts
57
src/index.ts
@@ -9,6 +9,7 @@ const reviewers_prompt = (process.env.INPUT_REVIEWERS_PROMPT || "");
|
|||||||
useChinese = language.toLowerCase() === "chinese"
|
useChinese = language.toLowerCase() === "chinese"
|
||||||
const include_files = split_message(process.env.INPUT_INCLUDE_FILES || "");
|
const include_files = split_message(process.env.INPUT_INCLUDE_FILES || "");
|
||||||
const exclude_files = split_message(process.env.INPUT_EXCLUDE_FILES || "");
|
const exclude_files = split_message(process.env.INPUT_EXCLUDE_FILES || "");
|
||||||
|
const review_pull_request = (!process.env.INPUT_REVIEW_PULL_REQUEST) ? false : (process.env.INPUT_REVIEW_PULL_REQUEST.toLowerCase() === "true")
|
||||||
|
|
||||||
const system_prompt = reviewers_prompt || take_system_prompt(prompt_genre, language);
|
const system_prompt = reviewers_prompt || take_system_prompt(prompt_genre, language);
|
||||||
|
|
||||||
@@ -58,7 +59,8 @@ async function aiGenerate({host, token, prompt, model, system}: any): Promise<an
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function aiCheckDiffContext() {
|
async function getPrDiffContext() {
|
||||||
|
let items = [];
|
||||||
const BASE_REF = process.env.INPUT_BASE_REF
|
const BASE_REF = process.env.INPUT_BASE_REF
|
||||||
try {
|
try {
|
||||||
execSync(`git fetch origin ${BASE_REF}`, {encoding: 'utf-8'});
|
execSync(`git fetch origin ${BASE_REF}`, {encoding: 'utf-8'});
|
||||||
@@ -66,8 +68,8 @@ async function aiCheckDiffContext() {
|
|||||||
const diffOutput = execSync(`git diff --name-only origin/${BASE_REF}...HEAD`, {encoding: 'utf-8'});
|
const diffOutput = execSync(`git diff --name-only origin/${BASE_REF}...HEAD`, {encoding: 'utf-8'});
|
||||||
let files = diffOutput.trim().split("\n");
|
let files = diffOutput.trim().split("\n");
|
||||||
for (let key in files) {
|
for (let key in files) {
|
||||||
|
// noinspection DuplicatedCode
|
||||||
if (!files[key]) continue;
|
if (!files[key]) continue;
|
||||||
console.log("check diff context:", files[key])
|
|
||||||
if ((include_files.length > 0) && (!doesAnyPatternMatch(include_files, files[key]))) {
|
if ((include_files.length > 0) && (!doesAnyPatternMatch(include_files, files[key]))) {
|
||||||
console.log("exclude(include):", files[key])
|
console.log("exclude(include):", files[key])
|
||||||
continue;
|
continue;
|
||||||
@@ -77,12 +79,59 @@ async function aiCheckDiffContext() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const fileDiffOutput = execSync(`git diff origin/${BASE_REF}...HEAD -- "${files[key]}"`, {encoding: 'utf-8'});
|
const fileDiffOutput = execSync(`git diff origin/${BASE_REF}...HEAD -- "${files[key]}"`, {encoding: 'utf-8'});
|
||||||
|
items.push({
|
||||||
|
path: files[key],
|
||||||
|
context: fileDiffOutput,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error executing git diff:', error);
|
||||||
|
}
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getHeadDiffContext() {
|
||||||
|
let items = [];
|
||||||
|
try {
|
||||||
|
// exec git diff get diff files
|
||||||
|
const diffOutput = execSync(`git diff --name-only HEAD^`, {encoding: 'utf-8'});
|
||||||
|
let files = diffOutput.trim().split("\n");
|
||||||
|
for (let key in files) {
|
||||||
|
// noinspection DuplicatedCode
|
||||||
|
if (!files[key]) continue;
|
||||||
|
if ((include_files.length > 0) && (!doesAnyPatternMatch(include_files, files[key]))) {
|
||||||
|
console.log("exclude(include):", files[key])
|
||||||
|
continue;
|
||||||
|
} else if ((exclude_files.length > 0) && (doesAnyPatternMatch(exclude_files, files[key]))) {
|
||||||
|
console.log("exclude(exclude):", files[key])
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileDiffOutput = execSync(`git diff HEAD^ -- "${files[key]}"`, {encoding: 'utf-8'});
|
||||||
|
items.push({
|
||||||
|
path: files[key],
|
||||||
|
context: fileDiffOutput,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error executing git diff:', error);
|
||||||
|
}
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function aiCheckDiffContext() {
|
||||||
|
try {
|
||||||
|
let commit_sha_url = `${process.env.GITHUB_SERVER_URL}/${process.env.INPUT_REPOSITORY}/src/commit/${process.env.GITHUB_SHA}`;
|
||||||
|
let items: Array<any> = review_pull_request ? await getPrDiffContext() : await getHeadDiffContext();
|
||||||
|
for (let key in items) {
|
||||||
|
if (!items[key]) continue;
|
||||||
|
let item = items[key];
|
||||||
// ai generate
|
// ai generate
|
||||||
try {
|
try {
|
||||||
let response = await aiGenerate({
|
let response = await aiGenerate({
|
||||||
host: url,
|
host: url,
|
||||||
token: process.env.INPUT_AI_TOKEN,
|
token: process.env.INPUT_AI_TOKEN,
|
||||||
prompt: fileDiffOutput,
|
prompt: item.context,
|
||||||
model: model,
|
model: model,
|
||||||
system: process.env.INPUT_REVIEW_PROMPT
|
system: process.env.INPUT_REVIEW_PROMPT
|
||||||
})
|
})
|
||||||
@@ -100,7 +149,7 @@ async function aiCheckDiffContext() {
|
|||||||
commit = commit.substring(0, commit.length - 3);
|
commit = commit.substring(0, commit.length - 3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let comments = `# ${Review} \r\n${process.env.GITHUB_SERVER_URL}/${process.env.INPUT_REPOSITORY}/src/commit/${process.env.GITHUB_SHA}/${files[key]} \r\n\r\n\r\n${commit}`
|
let comments = `# ${Review} \r\n${commit_sha_url}/${item.path} \r\n\r\n\r\n${commit}`
|
||||||
let resp = await pushComments(comments);
|
let resp = await pushComments(comments);
|
||||||
if (!resp.id) {
|
if (!resp.id) {
|
||||||
// noinspection ExceptionCaughtLocallyJS
|
// noinspection ExceptionCaughtLocallyJS
|
||||||
|
|||||||
Reference in New Issue
Block a user