diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f8e5893..e2fcaa0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -59,6 +59,9 @@ jobs: - name: Test Local Action id: test-action uses: ./ + with: + gitHubToken: ${{ secrets.GIT_HUB_TOKEN }} + openAIKey: ${{ secrets.OPENAI_API_KEY }} - name: Print Output id: output diff --git a/action.yml b/action.yml index 8e08b17..c7e27ab 100644 --- a/action.yml +++ b/action.yml @@ -1,6 +1,15 @@ -name: "Ai PR summarizer" +name: "pull request summarizer" description: "Ai enabled summarizer for PRs" author: "bemijonathan" + +inputs: + gitHubToken: + description: "GitHub token" + required: true + openAIKey: + description: "OpenAI key" + required: true + branding: color: "blue" icon: "align-left" diff --git a/src/index.ts b/src/index.ts index 40aa32c..b876eb9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,7 +34,7 @@ export async function run(): Promise { const summary = await summarizeChanges(changes) if (!summary) { - Logger.warn('Could not summarize changes, exiting') + Logger.warn('Summary is empty, exiting') return } diff --git a/src/prompts.ts b/src/prompts.ts index 07a072f..08e5ab4 100644 --- a/src/prompts.ts +++ b/src/prompts.ts @@ -1,37 +1,11 @@ export const prompt = ` As an expert PR reviewer with extensive knowledge of version control systems, provide a concise summary of the review for the git diff. - ### Instructions - Your summary should include an analysis of the changes made in the git diff, highlighting significant modifications, additions, and deletions. - Be specific and descriptive, accurately identifying the affected files and lines of code. - Present your summary in a clear and concise manner, ensuring readability and comprehension for all stakeholders involved in the code review process. - -Length: Aim for a summary of around 3-5 sentences. - -Style: Maintain a professional and objective tone in your review, emphasizing the technical aspects and impact of the changes rather than personal opinions. - -Formatting: Use Markdown to format your summary. -e.g. - ## Title - - # Issue Reference - - - This PR fixes/closes/relates to issue #[issue number] - - ## Description - - - Detailed explanation of what this PR does and why it's needed. - - ## Why? - - - Explanation of the reasoning behind the changes. - - ## Testing Scope - - - Scenarios to be tested based on the changes of this PR e.g table was updated so all CRUD operations should be tested. +----------------------------- +{diff} ` -export const generatePrompt = () => {} +export const generatePrompt = () => { } diff --git a/src/steps/get-changes.ts b/src/steps/get-changes.ts index 5d0a39e..e3e1085 100644 --- a/src/steps/get-changes.ts +++ b/src/steps/get-changes.ts @@ -8,7 +8,7 @@ export async function getChanges( ): Promise { try { Logger.log('getting changes', pullRequestNumber) - const githubToken = core.getInput('token') + const githubToken = core.getInput('gitHubToken') const octokit = github.getOctokit(githubToken) const repo = github.context.repo @@ -22,12 +22,14 @@ export async function getChanges( Logger.log('got changes diff', files) - const response = await axios.get(files.diff_url) + // const response = await axios.get(files.diff_url) - Logger.log('diff', response.data) + // Logger.log('diff', response.data) - return response.data + return files as unknown as string + + // return response.data } catch (error) { - Logger.error('error getting changes') + Logger.error('error getting changes', JSON.stringify(error)) } } diff --git a/src/steps/post-comment.ts b/src/steps/post-comment.ts index 6ee5b14..a78c37a 100644 --- a/src/steps/post-comment.ts +++ b/src/steps/post-comment.ts @@ -3,7 +3,7 @@ import * as github from '@actions/github' import { Logger } from 'src/utils.js' export async function postComment(pullRequestNumber: number, summary: string) { - const githubToken = core.getInput('token') + const githubToken = core.getInput('gitHubToken') const octokit = github.getOctokit(githubToken) const repo = github.context.repo Logger.log('posted comment', github.context) diff --git a/src/steps/summarize-changes.ts b/src/steps/summarize-changes.ts index 73bc36e..b054292 100644 --- a/src/steps/summarize-changes.ts +++ b/src/steps/summarize-changes.ts @@ -4,39 +4,57 @@ const { RecursiveCharacterTextSplitter } = require('langchain/text_splitter') const { PromptTemplate } = require('langchain/prompts') import { prompt } from 'src/prompts.js' import { Logger } from 'src/utils.js' +import * as core from '@actions/core' export async function summarizeChanges( diff: string ): Promise { try { - Logger.log('summarizing changes') - const model = new OpenAI({ temperature: 0 }) - Logger.log('created model') + + const openAiKey = core.getInput('openAIKey') + + Logger.log('creating openai model', openAiKey.length ? 'with key' : 'without key') + + const model = new OpenAI( + { temperature: 0.7, openAIApiKey: openAiKey, "model": "davinci" }, + ) + const textSplitter = new RecursiveCharacterTextSplitter({ - chunkSize: 1000, - separators: ['diff --git'], chunkOverlap: 0, - keepSeparator: true + keepSeparator: true, + chunkSize: 5000 }) + Logger.log('created text splitter') + const docs = await textSplitter.createDocuments([diff]) - const basePromptTemplate = PromptTemplate.fromTemplate(prompt) + + const basePromptTemplate = new PromptTemplate({ + template: prompt, + inputVariables: ["diff"] + }) + Logger.log('created prompt template') + const chain = loadSummarizationChain(model, { - prompt: basePromptTemplate, + type: "refine", verbose: true, - type: 'stuff' + refinePrompt: basePromptTemplate }) + Logger.log('loaded summarization chain') + const res = await chain.call({ - input_documents: docs + input_documents: docs, + diff: diff }) + Logger.log('summarized changes') console.log({ res }) - - return res.output.join('\n') + return res.output_text } catch (e) { Logger.log('error summarizing changes') - Logger.error(JSON.stringify(e as unknown as string)) + console.log(e) + Logger.log(e) } }