FIXED ISSUE FOR GITHUB

This commit is contained in:
Bhavik MacBook PRO 16 2023-04-23 16:33:58 +05:30
parent 990db6e3da
commit 45e0c32b09
5 changed files with 173 additions and 86 deletions

View file

@ -25,3 +25,4 @@ jobs:
FULL_REVIEW_COMMENT: 'openai'
OPENAI_TOKEN: ${{ secrets.OPENAI_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
SOURCE_AT: 'github'

View file

@ -25,7 +25,9 @@ inputs:
PROMPT_TEMPLATE:
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:
SOURCE_AT:
description: 'Where is the source code located'
default: 'github'
\`\`\`
${code}

46
dist/index.js vendored
View file

@ -39214,6 +39214,7 @@ async function run() {
const maxCodeLength = core.getInput('MAX_CODE_LENGTH');
const answerTemplate = core.getInput('ANSWER_TEMPLATE');
const giteaToken = core.getInput('GITHUB_TOKEN');
const sourceAt = core.getInput('SOURCE_AT');
core.debug(`programmingLanguage: ${programmingLanguage}`);
core.debug(`openaiToken length: ${openaiToken.length}`);
@ -39224,6 +39225,7 @@ async function run() {
core.debug(`promptTemplate: ${promptTemplate}`);
core.debug(`maxCodeLength: ${maxCodeLength}`);
core.debug(`answerTemplate: ${answerTemplate}`);
core.debug(`SourceAt: ${sourceAt}`);
// Get information about the pull request review
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
// Get the code to analyze from the review comment
var content = comment && comment.body || '';
if(sourceAt === 'github') {
const url = `${githubBaseURL}/api/v1/repos/${repoOwner}/${repoName}/pulls/${prNumber}/diff`;
console.log(`diff url: ${url}`);
var response = await axios.get(url, {
@ -39268,7 +39270,42 @@ async function run() {
}
}
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
if (programmingLanguage == 'auto') {
const detectedLanguage = detect(code);
@ -39300,6 +39337,7 @@ async function run() {
const answer = response.data.choices[0].message.content;
core.debug(`openai response: ${answer}`);
if(sourceAt === 'github') {
// Reply to the review comment with the OpenAI response
const client = new github.GitHub(githubToken, {
baseUrl: githubBaseURL
@ -39312,6 +39350,10 @@ async function run() {
body: answerTemplate.replace('${answer}', answer)
});
} else if (sourceAt === 'gitea')
{
}
} catch (error) {
core.setFailed(error.message);
}

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -28,6 +28,7 @@ async function run() {
const maxCodeLength = core.getInput('MAX_CODE_LENGTH');
const answerTemplate = core.getInput('ANSWER_TEMPLATE');
const giteaToken = core.getInput('GITHUB_TOKEN');
const sourceAt = core.getInput('SOURCE_AT');
core.debug(`programmingLanguage: ${programmingLanguage}`);
core.debug(`openaiToken length: ${openaiToken.length}`);
@ -38,6 +39,7 @@ async function run() {
core.debug(`promptTemplate: ${promptTemplate}`);
core.debug(`maxCodeLength: ${maxCodeLength}`);
core.debug(`answerTemplate: ${answerTemplate}`);
core.debug(`SourceAt: ${sourceAt}`);
// Get information about the pull request review
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
// Get the code to analyze from the review comment
var content = comment && comment.body || '';
if(sourceAt === 'github') {
const url = `${githubBaseURL}/api/v1/repos/${repoOwner}/${repoName}/pulls/${prNumber}/diff`;
console.log(`diff url: ${url}`);
var response = await axios.get(url, {
@ -82,7 +84,42 @@ async function run() {
}
}
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
if (programmingLanguage == 'auto') {
const detectedLanguage = detect(code);
@ -114,6 +151,7 @@ async function run() {
const answer = response.data.choices[0].message.content;
core.debug(`openai response: ${answer}`);
if(sourceAt === 'github') {
// Reply to the review comment with the OpenAI response
const client = new github.GitHub(githubToken, {
baseUrl: githubBaseURL
@ -126,6 +164,10 @@ async function run() {
body: answerTemplate.replace('${answer}', answer)
});
} else if (sourceAt === 'gitea')
{
}
} catch (error) {
core.setFailed(error.message);
}