Skip to content

Commit

Permalink
fix: getDiffCommits 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
choisohyun committed Oct 5, 2024
1 parent fde35bb commit e1016c8
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/analysis-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class AnalysisEngine {
const currentUserCommitSummary = await getCurrentUserCommitSummary(stemDict, this.baseBranchName, this.octokit);
if (this.isDebugMode) console.log("currentUserCommitSummary: ", currentUserCommitSummary);

const diffSummary = await getDiffSummary(stemDict, this.baseBranchName);
const diffSummary = await getDiffSummary(this.octokit);
if (this.isDebugMode) console.log("diffSummary: ", diffSummary);

return {
Expand Down
104 changes: 87 additions & 17 deletions packages/analysis-engine/src/summary.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type PluginOctokit from "./pluginOctokit";
import type { CommitRaw, StemDict } from "./types";
import {
type CommitMessageType,
CommitMessageTypeList,
type CommitRaw,
type DifferenceStatistic,
type StemDict,
} from "./types";

const API_KEY = process.env.GEMENI_API_KEY || "";
const API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=";
Expand Down Expand Up @@ -59,28 +65,92 @@ export async function getCurrentUserCommitSummary(stemDict: StemDict, baseBranch
return await getSummary(currentUserNodes ? currentUserNodes?.slice(-10) : []);
}

export async function getDiffSummary(stemDict: StemDict, baseBranchName: string) {
const mainNodes = stemDict.get("main")?.nodes;
const currentNodes = stemDict.get(baseBranchName)?.nodes;

if (!mainNodes || !currentNodes) {
return "";
function parseCommitMessageType(message: string): CommitMessageType {
const firstLine = message.split("\n")[0].toLowerCase();
for (const type of CommitMessageTypeList) {
if (firstLine.startsWith(type + ":")) {
return type;
}
}
return "chore"; // Default type if no match found
}

const mainCommit = mainNodes.slice(-1)[0].commit;
const currentCommit = currentNodes.slice(-1)[0].commit;

const mainCommitMessage = mainCommit.message.split("\n")[0];
const currentCommitMessage = currentCommit.message.split("\n")[0];
async function getDiffCommits(
octokit: PluginOctokit,
owner: string,
repo: string,
baseBranch: string = "main",
compareBranch: string = "HEAD"
): Promise<CommitRaw[]> {
try {
const response = await octokit.rest.repos.compareCommits({
owner,
repo,
base: baseBranch,
head: compareBranch,
});

if (mainCommitMessage === currentCommitMessage) {
return "No changes";
return await Promise.all(
response.data.commits.map(async (commit, index) => {
const detailedCommit = await octokit.rest.repos.getCommit({
owner,
repo,
ref: commit.sha,
});

const differenceStatistic: DifferenceStatistic = {
totalInsertionCount: 0,
totalDeletionCount: 0,
fileDictionary: {},
};

detailedCommit.data.files?.forEach((file) => {
differenceStatistic.totalInsertionCount += file.additions;
differenceStatistic.totalDeletionCount += file.deletions;
differenceStatistic.fileDictionary[file.filename] = {
insertionCount: file.additions,
deletionCount: file.deletions,
};
});

return {
sequence: index + 1,
id: commit.sha,
parents: commit.parents.map((parent) => parent.sha),
branches: [], // GitHub API doesn't provide this information directly
tags: [], // GitHub API doesn't provide this information directly
author: {
name: commit.commit.author?.name ?? "",
email: commit.commit.author?.email ?? "",
},
authorDate: new Date(commit.commit.author?.date ?? ""),
committer: {
name: commit.commit.committer?.name ?? "",
email: commit.commit.committer?.email ?? "",
},
committerDate: new Date(commit.commit.committer?.date ?? ""),
message: commit.commit.message,
differenceStatistic,
commitMessageType: parseCommitMessageType(commit.commit.message),
};
})
);
} catch (error) {
console.error("Error fetching commit differences:", (error as Error).message);
return [];
}
}

const mainCommitSummary = await getSummary([mainCommit]);
const currentCommitSummary = await getSummary([currentCommit]);
export async function getDiffSummary(
octokit: PluginOctokit,
owner: string,
repo: string,
baseBranch: string = "main",
compareBranch: string = "HEAD"
) {
const diffCommits = await getDiffCommits(octokit, owner, repo, baseBranch, compareBranch);

return `main: ${mainCommitSummary}\n${baseBranchName}: ${currentCommitSummary}`;
return await getSummary(diffCommits);
}

const prompt = `Proceed with the task of summarising the contents of the commit message provided.
Expand Down

0 comments on commit e1016c8

Please sign in to comment.