mirror of
https://github.com/ingress-it-solutions/gitea-code-review-action.git
synced 2025-04-20 11:06:47 +00:00
Merge pull request #11 from ingress-it-solutions/dev
FIXED ISSUE FOR GITHUB
This commit is contained in:
commit
7d61ccc420
5 changed files with 173 additions and 86 deletions
1
.github/workflows/main.yaml
vendored
1
.github/workflows/main.yaml
vendored
|
@ -25,3 +25,4 @@ jobs:
|
||||||
FULL_REVIEW_COMMENT: 'openai'
|
FULL_REVIEW_COMMENT: 'openai'
|
||||||
OPENAI_TOKEN: ${{ secrets.OPENAI_TOKEN }}
|
OPENAI_TOKEN: ${{ secrets.OPENAI_TOKEN }}
|
||||||
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
|
||||||
|
SOURCE_AT: 'github'
|
|
@ -25,7 +25,9 @@ inputs:
|
||||||
PROMPT_TEMPLATE:
|
PROMPT_TEMPLATE:
|
||||||
description: 'The template for the FULL_REVIEW_COMMENT prompt.'
|
description: 'The template for the FULL_REVIEW_COMMENT prompt.'
|
||||||
default: 'Please analyze the pull request''s code and inform me whether it requires optimization, and provide an explanation for your decision:
|
default: 'Please analyze the pull request''s code and inform me whether it requires optimization, and provide an explanation for your decision:
|
||||||
|
SOURCE_AT:
|
||||||
|
description: 'Where is the source code located'
|
||||||
|
default: 'github'
|
||||||
|
|
||||||
\`\`\`
|
\`\`\`
|
||||||
${code}
|
${code}
|
||||||
|
|
46
dist/index.js
vendored
46
dist/index.js
vendored
|
@ -39214,6 +39214,7 @@ async function run() {
|
||||||
const maxCodeLength = core.getInput('MAX_CODE_LENGTH');
|
const maxCodeLength = core.getInput('MAX_CODE_LENGTH');
|
||||||
const answerTemplate = core.getInput('ANSWER_TEMPLATE');
|
const answerTemplate = core.getInput('ANSWER_TEMPLATE');
|
||||||
const giteaToken = core.getInput('GITHUB_TOKEN');
|
const giteaToken = core.getInput('GITHUB_TOKEN');
|
||||||
|
const sourceAt = core.getInput('SOURCE_AT');
|
||||||
|
|
||||||
core.debug(`programmingLanguage: ${programmingLanguage}`);
|
core.debug(`programmingLanguage: ${programmingLanguage}`);
|
||||||
core.debug(`openaiToken length: ${openaiToken.length}`);
|
core.debug(`openaiToken length: ${openaiToken.length}`);
|
||||||
|
@ -39224,6 +39225,7 @@ async function run() {
|
||||||
core.debug(`promptTemplate: ${promptTemplate}`);
|
core.debug(`promptTemplate: ${promptTemplate}`);
|
||||||
core.debug(`maxCodeLength: ${maxCodeLength}`);
|
core.debug(`maxCodeLength: ${maxCodeLength}`);
|
||||||
core.debug(`answerTemplate: ${answerTemplate}`);
|
core.debug(`answerTemplate: ${answerTemplate}`);
|
||||||
|
core.debug(`SourceAt: ${sourceAt}`);
|
||||||
|
|
||||||
// Get information about the pull request review
|
// Get information about the pull request review
|
||||||
const comment = github.context.payload.comment;
|
const comment = github.context.payload.comment;
|
||||||
|
@ -39232,10 +39234,10 @@ async function run() {
|
||||||
const prNumber = github.context.payload.number || github.context.payload.issue.number; // get number from a pull request event or comment event
|
const prNumber = github.context.payload.number || github.context.payload.issue.number; // get number from a pull request event or comment event
|
||||||
|
|
||||||
// Get the code to analyze from the review comment
|
// Get the code to analyze from the review comment
|
||||||
|
|
||||||
|
|
||||||
var content = comment && comment.body || '';
|
var content = comment && comment.body || '';
|
||||||
|
|
||||||
|
if(sourceAt === 'github') {
|
||||||
|
|
||||||
const url = `${githubBaseURL}/api/v1/repos/${repoOwner}/${repoName}/pulls/${prNumber}/diff`;
|
const url = `${githubBaseURL}/api/v1/repos/${repoOwner}/${repoName}/pulls/${prNumber}/diff`;
|
||||||
console.log(`diff url: ${url}`);
|
console.log(`diff url: ${url}`);
|
||||||
var response = await axios.get(url, {
|
var response = await axios.get(url, {
|
||||||
|
@ -39268,7 +39270,42 @@ async function run() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
content = content.substring(0, maxCodeLength);
|
content = content.substring(0, maxCodeLength);
|
||||||
|
}
|
||||||
|
else if(sourceAt === 'gitea')
|
||||||
|
{
|
||||||
|
const url = `${githubBaseURL}/api/v1/repos/${repoOwner}/${repoName}/pulls/${prNumber}/diff`;
|
||||||
|
console.log(`diff url: ${url}`);
|
||||||
|
var response = await axios.get(url, {
|
||||||
|
headers: {
|
||||||
|
Authorization: `token ${githubToken}`,
|
||||||
|
Accept: 'application/vnd.github.diff'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const code = response.data;
|
||||||
|
core.debug(`diff code: ${code}`);
|
||||||
|
const files = parsePullRequestDiff(code);
|
||||||
|
core.debug(`diff files: ${files}`);
|
||||||
|
|
||||||
|
if (!content || content == fullReviewComment) {
|
||||||
|
// Extract the code from the pull request content
|
||||||
|
content = promptTemplate.replace('${code}', code);
|
||||||
|
} else {
|
||||||
|
content = content.substring(reviewCommentPrefix.length);
|
||||||
|
content = content.replace('${code}', code);
|
||||||
|
const fileNames = findFileNames(content);
|
||||||
|
core.debug(`found files name in commment: ${fileNames}`);
|
||||||
|
for (const fileName of fileNames) {
|
||||||
|
for (const key of Object.keys(files)) {
|
||||||
|
if (key.includes(fileName)) {
|
||||||
|
core.debug(`replace \${file:${fileName}} with ${key}'s diff`);
|
||||||
|
content = content.replace(`\${file:${fileName}}`, files[key]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
content = content.substring(0, maxCodeLength);
|
||||||
|
}
|
||||||
// Determine the programming language if it was not provided
|
// Determine the programming language if it was not provided
|
||||||
if (programmingLanguage == 'auto') {
|
if (programmingLanguage == 'auto') {
|
||||||
const detectedLanguage = detect(code);
|
const detectedLanguage = detect(code);
|
||||||
|
@ -39300,6 +39337,7 @@ async function run() {
|
||||||
const answer = response.data.choices[0].message.content;
|
const answer = response.data.choices[0].message.content;
|
||||||
core.debug(`openai response: ${answer}`);
|
core.debug(`openai response: ${answer}`);
|
||||||
|
|
||||||
|
if(sourceAt === 'github') {
|
||||||
// Reply to the review comment with the OpenAI response
|
// Reply to the review comment with the OpenAI response
|
||||||
const client = new github.GitHub(githubToken, {
|
const client = new github.GitHub(githubToken, {
|
||||||
baseUrl: githubBaseURL
|
baseUrl: githubBaseURL
|
||||||
|
@ -39312,6 +39350,10 @@ async function run() {
|
||||||
body: answerTemplate.replace('${answer}', answer)
|
body: answerTemplate.replace('${answer}', answer)
|
||||||
|
|
||||||
});
|
});
|
||||||
|
} else if (sourceAt === 'gitea')
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.setFailed(error.message);
|
core.setFailed(error.message);
|
||||||
}
|
}
|
||||||
|
|
2
dist/index.js.map
vendored
2
dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
46
index.js
46
index.js
|
@ -28,6 +28,7 @@ async function run() {
|
||||||
const maxCodeLength = core.getInput('MAX_CODE_LENGTH');
|
const maxCodeLength = core.getInput('MAX_CODE_LENGTH');
|
||||||
const answerTemplate = core.getInput('ANSWER_TEMPLATE');
|
const answerTemplate = core.getInput('ANSWER_TEMPLATE');
|
||||||
const giteaToken = core.getInput('GITHUB_TOKEN');
|
const giteaToken = core.getInput('GITHUB_TOKEN');
|
||||||
|
const sourceAt = core.getInput('SOURCE_AT');
|
||||||
|
|
||||||
core.debug(`programmingLanguage: ${programmingLanguage}`);
|
core.debug(`programmingLanguage: ${programmingLanguage}`);
|
||||||
core.debug(`openaiToken length: ${openaiToken.length}`);
|
core.debug(`openaiToken length: ${openaiToken.length}`);
|
||||||
|
@ -38,6 +39,7 @@ async function run() {
|
||||||
core.debug(`promptTemplate: ${promptTemplate}`);
|
core.debug(`promptTemplate: ${promptTemplate}`);
|
||||||
core.debug(`maxCodeLength: ${maxCodeLength}`);
|
core.debug(`maxCodeLength: ${maxCodeLength}`);
|
||||||
core.debug(`answerTemplate: ${answerTemplate}`);
|
core.debug(`answerTemplate: ${answerTemplate}`);
|
||||||
|
core.debug(`SourceAt: ${sourceAt}`);
|
||||||
|
|
||||||
// Get information about the pull request review
|
// Get information about the pull request review
|
||||||
const comment = github.context.payload.comment;
|
const comment = github.context.payload.comment;
|
||||||
|
@ -46,10 +48,10 @@ async function run() {
|
||||||
const prNumber = github.context.payload.number || github.context.payload.issue.number; // get number from a pull request event or comment event
|
const prNumber = github.context.payload.number || github.context.payload.issue.number; // get number from a pull request event or comment event
|
||||||
|
|
||||||
// Get the code to analyze from the review comment
|
// Get the code to analyze from the review comment
|
||||||
|
|
||||||
|
|
||||||
var content = comment && comment.body || '';
|
var content = comment && comment.body || '';
|
||||||
|
|
||||||
|
if(sourceAt === 'github') {
|
||||||
|
|
||||||
const url = `${githubBaseURL}/api/v1/repos/${repoOwner}/${repoName}/pulls/${prNumber}/diff`;
|
const url = `${githubBaseURL}/api/v1/repos/${repoOwner}/${repoName}/pulls/${prNumber}/diff`;
|
||||||
console.log(`diff url: ${url}`);
|
console.log(`diff url: ${url}`);
|
||||||
var response = await axios.get(url, {
|
var response = await axios.get(url, {
|
||||||
|
@ -82,7 +84,42 @@ async function run() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
content = content.substring(0, maxCodeLength);
|
content = content.substring(0, maxCodeLength);
|
||||||
|
}
|
||||||
|
else if(sourceAt === 'gitea')
|
||||||
|
{
|
||||||
|
const url = `${githubBaseURL}/api/v1/repos/${repoOwner}/${repoName}/pulls/${prNumber}/diff`;
|
||||||
|
console.log(`diff url: ${url}`);
|
||||||
|
var response = await axios.get(url, {
|
||||||
|
headers: {
|
||||||
|
Authorization: `token ${githubToken}`,
|
||||||
|
Accept: 'application/vnd.github.diff'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const code = response.data;
|
||||||
|
core.debug(`diff code: ${code}`);
|
||||||
|
const files = parsePullRequestDiff(code);
|
||||||
|
core.debug(`diff files: ${files}`);
|
||||||
|
|
||||||
|
if (!content || content == fullReviewComment) {
|
||||||
|
// Extract the code from the pull request content
|
||||||
|
content = promptTemplate.replace('${code}', code);
|
||||||
|
} else {
|
||||||
|
content = content.substring(reviewCommentPrefix.length);
|
||||||
|
content = content.replace('${code}', code);
|
||||||
|
const fileNames = findFileNames(content);
|
||||||
|
core.debug(`found files name in commment: ${fileNames}`);
|
||||||
|
for (const fileName of fileNames) {
|
||||||
|
for (const key of Object.keys(files)) {
|
||||||
|
if (key.includes(fileName)) {
|
||||||
|
core.debug(`replace \${file:${fileName}} with ${key}'s diff`);
|
||||||
|
content = content.replace(`\${file:${fileName}}`, files[key]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
content = content.substring(0, maxCodeLength);
|
||||||
|
}
|
||||||
// Determine the programming language if it was not provided
|
// Determine the programming language if it was not provided
|
||||||
if (programmingLanguage == 'auto') {
|
if (programmingLanguage == 'auto') {
|
||||||
const detectedLanguage = detect(code);
|
const detectedLanguage = detect(code);
|
||||||
|
@ -114,6 +151,7 @@ async function run() {
|
||||||
const answer = response.data.choices[0].message.content;
|
const answer = response.data.choices[0].message.content;
|
||||||
core.debug(`openai response: ${answer}`);
|
core.debug(`openai response: ${answer}`);
|
||||||
|
|
||||||
|
if(sourceAt === 'github') {
|
||||||
// Reply to the review comment with the OpenAI response
|
// Reply to the review comment with the OpenAI response
|
||||||
const client = new github.GitHub(githubToken, {
|
const client = new github.GitHub(githubToken, {
|
||||||
baseUrl: githubBaseURL
|
baseUrl: githubBaseURL
|
||||||
|
@ -126,6 +164,10 @@ async function run() {
|
||||||
body: answerTemplate.replace('${answer}', answer)
|
body: answerTemplate.replace('${answer}', answer)
|
||||||
|
|
||||||
});
|
});
|
||||||
|
} else if (sourceAt === 'gitea')
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.setFailed(error.message);
|
core.setFailed(error.message);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue