From fde35bbc459fcbd74fc00bcd9b65339d700dbe27 Mon Sep 17 00:00:00 2001 From: choisohyun Date: Sat, 5 Oct 2024 21:49:15 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=ED=98=84=EC=9E=AC=20=EC=9E=88?= =?UTF-8?q?=EB=8A=94=20=EB=B8=8C=EB=9E=9C=EC=B9=98=EC=9D=98=20=EA=B0=80?= =?UTF-8?q?=EC=9E=A5=20=EC=B5=9C=EC=8B=A0=20=EC=BB=A4=EB=B0=8B=EA=B3=BC=20?= =?UTF-8?q?main=EC=9D=98=20=EC=B5=9C=EC=8B=A0=EC=BB=A4=EB=B0=8B=EC=9D=84?= =?UTF-8?q?=20=EB=B9=84=EA=B5=90=ED=95=98=EC=97=AC=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=EC=A0=90=EC=9D=84=20=EC=9A=94=EC=95=BD=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/analysis-engine/src/index.ts | 5 ++++- packages/analysis-engine/src/summary.ts | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/analysis-engine/src/index.ts b/packages/analysis-engine/src/index.ts index 1149d621..6c2dce67 100644 --- a/packages/analysis-engine/src/index.ts +++ b/packages/analysis-engine/src/index.ts @@ -7,7 +7,7 @@ import { buildCSMDict } from "./csm"; import getCommitRaws from "./parser"; import { PluginOctokit } from "./pluginOctokit"; import { buildStemDict } from "./stem"; -import { getCurrentUserCommitSummary, getLatestCommitSummary } from "./summary"; +import { getCurrentUserCommitSummary, getDiffSummary, getLatestCommitSummary } from "./summary"; type AnalysisEngineArgs = { isDebugMode?: boolean; @@ -81,6 +81,9 @@ 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); + if (this.isDebugMode) console.log("diffSummary: ", diffSummary); + return { isPRSuccess, csmDict, diff --git a/packages/analysis-engine/src/summary.ts b/packages/analysis-engine/src/summary.ts index 66d7f5bd..fcdd99ea 100644 --- a/packages/analysis-engine/src/summary.ts +++ b/packages/analysis-engine/src/summary.ts @@ -59,6 +59,30 @@ 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 ""; + } + + 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]; + + if (mainCommitMessage === currentCommitMessage) { + return "No changes"; + } + + const mainCommitSummary = await getSummary([mainCommit]); + const currentCommitSummary = await getSummary([currentCommit]); + + return `main: ${mainCommitSummary}\n${baseBranchName}: ${currentCommitSummary}`; +} + const prompt = `Proceed with the task of summarising the contents of the commit message provided. Procedure: From e1016c8d217825e8790f9e7ae2620785f1a565f5 Mon Sep 17 00:00:00 2001 From: choisohyun Date: Sat, 5 Oct 2024 22:11:48 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20getDiffCommits=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/analysis-engine/src/index.ts | 2 +- packages/analysis-engine/src/summary.ts | 104 ++++++++++++++++++++---- 2 files changed, 88 insertions(+), 18 deletions(-) diff --git a/packages/analysis-engine/src/index.ts b/packages/analysis-engine/src/index.ts index 6c2dce67..38df71ff 100644 --- a/packages/analysis-engine/src/index.ts +++ b/packages/analysis-engine/src/index.ts @@ -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 { diff --git a/packages/analysis-engine/src/summary.ts b/packages/analysis-engine/src/summary.ts index fcdd99ea..38cc46f3 100644 --- a/packages/analysis-engine/src/summary.ts +++ b/packages/analysis-engine/src/summary.ts @@ -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="; @@ -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 { + 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.