Skip to content

Commit

Permalink
Merge branch 'epic/ai-summary' of https://github.com/githru/githru-vs…
Browse files Browse the repository at this point in the history
…code-ext into epic/ai-summary
  • Loading branch information
mdgarden committed Oct 5, 2024
2 parents 68d4aaa + 5dac108 commit a8316c6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
10 changes: 6 additions & 4 deletions packages/analysis-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { buildCSMDict } from "./csm";
import getCommitRaws from "./parser";
import { PluginOctokit } from "./pluginOctokit";
import { buildStemDict } from "./stem";
import { getSummary } from "./summary";
import { getCurrentUserCommitSummary, getLatestCommitSummary } from "./summary";

type AnalysisEngineArgs = {
isDebugMode?: boolean;
Expand Down Expand Up @@ -75,9 +75,11 @@ export class AnalysisEngine {
if (this.isDebugMode) console.log("stemDict: ", stemDict);
const csmDict = buildCSMDict(commitDict, stemDict, this.baseBranchName, pullRequests);
if (this.isDebugMode) console.log("csmDict: ", csmDict);
const nodes = stemDict.get(this.baseBranchName)?.nodes?.map(({commit}) => commit);
const geminiCommitSummary = await getSummary(nodes ? nodes?.slice(-10) : []);
if (this.isDebugMode) console.log("GeminiCommitSummary: ", geminiCommitSummary);
const latestCommitSummary = await getLatestCommitSummary(stemDict, this.baseBranchName);
if (this.isDebugMode) console.log("latestCommitSummary: ", latestCommitSummary);

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

return {
isPRSuccess,
Expand Down
46 changes: 38 additions & 8 deletions packages/analysis-engine/src/summary.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import type { CommitRaw } from "./types";
import type PluginOctokit from "./pluginOctokit";
import type { CommitRaw, StemDict } from "./types";

const apiKey = process.env.GEMENI_API_KEY || '';
const apiUrl = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=";
const API_KEY = process.env.GEMENI_API_KEY || "";
const API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=";
const MERGE_BRANCH = "Merge branch";
const MERGE_PULL_REQUEST = "Merge pull request";

export async function getSummary(csmNodes: CommitRaw[]) {
const commitMessages = csmNodes.map((csmNode) => csmNode.message.split('\n')[0]).join(', ');
async function getSummary(csmNodes: CommitRaw[]) {
const commitMessages = csmNodes.map((csmNode) => csmNode.message.split("\n")[0]).join(", ");

console.log("commitMessages: ", commitMessages);
try {
const response = await fetch(apiUrl + apiKey, {
const response = await fetch(API_URL + API_KEY, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
contents: [{parts: [{text: `${prompt} \n${commitMessages}`}]}],
contents: [{ parts: [{ text: `${prompt} \n${commitMessages}` }] }],
}),
});

Expand All @@ -29,6 +33,32 @@ export async function getSummary(csmNodes: CommitRaw[]) {
}
}

function isNonMergeCommit(message: string) {
return !message.includes(MERGE_BRANCH) && !message.includes(MERGE_PULL_REQUEST);
}

export async function getLatestCommitSummary(stemDict: StemDict, baseBranchName: string) {
const nodes = stemDict
.get(baseBranchName)
?.nodes?.map(({ commit }) => commit)
.filter(({ message }) => isNonMergeCommit(message));

return await getSummary(nodes ? nodes?.slice(-10) : []);
}

export async function getCurrentUserCommitSummary(stemDict: StemDict, baseBranchName: string, octokit: PluginOctokit) {
const { data } = await octokit.rest.users.getAuthenticated();
const currentUserNodes = stemDict
.get(baseBranchName)
?.nodes?.filter(
({ commit: { author, message } }) =>
(author.name === data.login || author.name === data.name) && isNonMergeCommit(message)
)
?.map(({ commit }) => commit);

return await getSummary(currentUserNodes ? currentUserNodes?.slice(-10) : []);
}

const prompt = `Proceed with the task of summarising the contents of the commit message provided.
Procedure:
Expand All @@ -53,4 +83,4 @@ Output format:
- {prefix (if any)}:{commit summary3}
‘’
Commits:`
Commits:`;

0 comments on commit a8316c6

Please sign in to comment.