Skip to content

Commit

Permalink
Merge pull request #8 from tlarbals824/fix/file-patch
Browse files Browse the repository at this point in the history
fix : 각각의 파일이 아닌 모든 파일에 대해서 한 번에 코드리뷰하도록 수정
  • Loading branch information
tlarbals824 authored Mar 19, 2024
2 parents 3132dbd + d6209cd commit 29b2912
Showing 1 changed file with 56 additions and 59 deletions.
115 changes: 56 additions & 59 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,63 @@ import logger from "winston";

import { getCodeReviewResult } from "./code_review_handler.js";
import { hashString } from "./hash_handler.js";
import {validateAlreadyReview } from "./code_review_validator.js"
import { validateAlreadyReview } from "./code_review_validator.js";
import { append } from "./code_review_appender.js";

export default (app: Probot) => {
app.on(
["pull_request.opened", "pull_request.synchronize"],
async (context) => {
const repo = context.repo();
logger.info(repo);

const data = await context.octokit.repos.compareCommits({
owner: repo.owner,
repo: repo.repo,
head: context.payload.pull_request.head.sha,
base: context.payload.pull_request.base.sha,
});

let { files: changedFiles, commits } = data.data;

if (!changedFiles?.length) {
logger.error("no changed");
return "no changed";
}

const api_key = process.env.OPEN_AI_API_KEY;

if(!api_key){
return "api key not setting";
}

for (let i = 0; i < changedFiles.length; i++) {
const file = changedFiles[i];
const patch = file.patch || "";

const hashValue = hashString(patch)

if(await validateAlreadyReview(repo.repo, hashValue))
continue;


const message = (await getCodeReviewResult(api_key, patch)).message;


if (!!message) {
await context.octokit.pulls.createReviewComment({
repo: repo.repo,
owner: repo.owner,
pull_number: context.pullRequest().pull_number,
commit_id: commits[commits.length - 1].sha,
path: file.filename,
body: message,
position: patch.split("\n").length - 1,
});

append(repo.repo, hashValue);
}


}
return "success";
}
);
app.on(
["pull_request.opened", "pull_request.synchronize"],
async (context) => {
const repo = context.repo();
logger.info(repo);

const data = await context.octokit.repos.compareCommits({
owner: repo.owner,
repo: repo.repo,
head: context.payload.pull_request.head.sha,
base: context.payload.pull_request.base.sha,
});

let { files: changedFiles } = data.data;

if (!changedFiles?.length) {
logger.error("no changed");
return "no changed";
}

const api_key = process.env.OPEN_AI_API_KEY;

if (!api_key) {
return "api key not setting";
}

let request_code = "";

for (let i = 0; i < changedFiles.length; i++) {
const file = changedFiles[i];
const patch = file.patch || "";

const hashValue = hashString(patch);

if (await validateAlreadyReview(repo.repo, hashValue)) continue;

request_code = request_code + "\n file name: "+ file.filename + "\n" + patch;

append(repo.repo, hashValue);
}

const message = (await getCodeReviewResult(api_key, request_code)).message;

if (!!message) {
await context.octokit.issues.createComment({
repo: repo.repo,
owner: repo.owner,
issue_number: context.pullRequest().pull_number,
body: message
});
}

return "success";
}
);
};

0 comments on commit 29b2912

Please sign in to comment.