diff --git a/action.yml b/action.yml index 7b93c0f..4a250ef 100644 --- a/action.yml +++ b/action.yml @@ -27,15 +27,18 @@ runs: uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 if: ${{ inputs.openai_api_key != '' }} env: - OPENAI_API_KEY: ${{ inputs.openai_api_key }} + OPENAI_API_KEY: ${{ inputs.openai_api_key }} + GITHUB_TOKEN: ${{ inputs.github_token }} + PRNUM: ${{ github.event.issue.number }} with: script: | const { default: explainPatch } = await import('${{ github.action_path }}/src/openaiExplainPatch.js') - console.log(process.env) + // console.log(process.env) console.log(await explainPatch({ openaiKey: process.env.OPENAI_API_KEY, + github: github, repo: process.env.GITHUB_ACTION_REPOSITORY, - prnum: process.env.GITHUB_REF_NAME.split('/')[0]})) + prnum: process.env.PRNUM})) console.log('rerun') console.log('implement me') diff --git a/package-lock.json b/package-lock.json index 73ed0c5..2c4ce4c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,9 +5,100 @@ "packages": { "": { "dependencies": { + "@octokit/core": "^5.0.2", "openai": "^4.20.1" } }, + "node_modules/@octokit/auth-token": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@octokit/auth-token/-/auth-token-4.0.0.tgz", + "integrity": "sha512-tY/msAuJo6ARbK6SPIxZrPBms3xPbfwBrulZe0Wtr/DIY9lje2HeV1uoebShn6mx7SjCHif6EjMvoREj+gZ+SA==", + "engines": { + "node": ">= 18" + } + }, + "node_modules/@octokit/core": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/@octokit/core/-/core-5.0.2.tgz", + "integrity": "sha512-cZUy1gUvd4vttMic7C0lwPed8IYXWYp8kHIMatyhY8t8n3Cpw2ILczkV5pGMPqef7v0bLo0pOHrEHarsau2Ydg==", + "dependencies": { + "@octokit/auth-token": "^4.0.0", + "@octokit/graphql": "^7.0.0", + "@octokit/request": "^8.0.2", + "@octokit/request-error": "^5.0.0", + "@octokit/types": "^12.0.0", + "before-after-hook": "^2.2.0", + "universal-user-agent": "^6.0.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/@octokit/endpoint": { + "version": "9.0.4", + "resolved": "https://registry.npmjs.org/@octokit/endpoint/-/endpoint-9.0.4.tgz", + "integrity": "sha512-DWPLtr1Kz3tv8L0UvXTDP1fNwM0S+z6EJpRcvH66orY6Eld4XBMCSYsaWp4xIm61jTWxK68BrR7ibO+vSDnZqw==", + "dependencies": { + "@octokit/types": "^12.0.0", + "universal-user-agent": "^6.0.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/@octokit/graphql": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@octokit/graphql/-/graphql-7.0.2.tgz", + "integrity": "sha512-OJ2iGMtj5Tg3s6RaXH22cJcxXRi7Y3EBqbHTBRq+PQAqfaS8f/236fUrWhfSn8P4jovyzqucxme7/vWSSZBX2Q==", + "dependencies": { + "@octokit/request": "^8.0.1", + "@octokit/types": "^12.0.0", + "universal-user-agent": "^6.0.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/@octokit/openapi-types": { + "version": "19.1.0", + "resolved": "https://registry.npmjs.org/@octokit/openapi-types/-/openapi-types-19.1.0.tgz", + "integrity": "sha512-6G+ywGClliGQwRsjvqVYpklIfa7oRPA0vyhPQG/1Feh+B+wU0vGH1JiJ5T25d3g1JZYBHzR2qefLi9x8Gt+cpw==" + }, + "node_modules/@octokit/request": { + "version": "8.1.6", + "resolved": "https://registry.npmjs.org/@octokit/request/-/request-8.1.6.tgz", + "integrity": "sha512-YhPaGml3ncZC1NfXpP3WZ7iliL1ap6tLkAp6MvbK2fTTPytzVUyUesBBogcdMm86uRYO5rHaM1xIWxigWZ17MQ==", + "dependencies": { + "@octokit/endpoint": "^9.0.0", + "@octokit/request-error": "^5.0.0", + "@octokit/types": "^12.0.0", + "universal-user-agent": "^6.0.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/@octokit/request-error": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-5.0.1.tgz", + "integrity": "sha512-X7pnyTMV7MgtGmiXBwmO6M5kIPrntOXdyKZLigNfQWSEQzVxR4a4vo49vJjTWX70mPndj8KhfT4Dx+2Ng3vnBQ==", + "dependencies": { + "@octokit/types": "^12.0.0", + "deprecation": "^2.0.0", + "once": "^1.4.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/@octokit/types": { + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@octokit/types/-/types-12.3.0.tgz", + "integrity": "sha512-nJ8X2HRr234q3w/FcovDlA+ttUU4m1eJAourvfUUtwAWeqL8AsyRqfnLvVnYn3NFbUnsmzQCzLNdFerPwdmcDQ==", + "dependencies": { + "@octokit/openapi-types": "^19.0.2" + } + }, "node_modules/@types/node": { "version": "18.18.13", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.18.13.tgz", @@ -57,6 +148,11 @@ "resolved": "https://registry.npmjs.org/base-64/-/base-64-0.1.0.tgz", "integrity": "sha512-Y5gU45svrR5tI2Vt/X9GPd3L0HNIKzGu202EjxrXMpuc2V2CiKgemAbUUsqYmZJvPtCXoUKjNZwBJzsNScUbXA==" }, + "node_modules/before-after-hook": { + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-2.2.3.tgz", + "integrity": "sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ==" + }, "node_modules/charenc": { "version": "0.0.2", "resolved": "https://registry.npmjs.org/charenc/-/charenc-0.0.2.tgz", @@ -92,6 +188,11 @@ "node": ">=0.4.0" } }, + "node_modules/deprecation": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/deprecation/-/deprecation-2.3.1.tgz", + "integrity": "sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ==" + }, "node_modules/digest-fetch": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/digest-fetch/-/digest-fetch-1.3.0.tgz", @@ -231,6 +332,14 @@ } } }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "dependencies": { + "wrappy": "1" + } + }, "node_modules/openai": { "version": "4.20.1", "resolved": "https://registry.npmjs.org/openai/-/openai-4.20.1.tgz", @@ -260,6 +369,11 @@ "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==" }, + "node_modules/universal-user-agent": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/universal-user-agent/-/universal-user-agent-6.0.1.tgz", + "integrity": "sha512-yCzhz6FN2wU1NiiQRogkTQszlQSlpWaw8SvVegAc+bDxbzHgh1vX8uIe8OYyMH6DwH+sdTJsgMl36+mSMdRJIQ==" + }, "node_modules/web-streams-polyfill": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz", @@ -281,6 +395,11 @@ "tr46": "~0.0.3", "webidl-conversions": "^3.0.0" } + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" } } } diff --git a/package.json b/package.json index 9da7a8a..96f9d09 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "email": "security@brave.com" }, "dependencies": { + "@octokit/core": "^5.0.2", "openai": "^4.20.1" } } diff --git a/src/openaiExplainPatch.js b/src/openaiExplainPatch.js index 38bb51e..fcbe8c3 100644 --- a/src/openaiExplainPatch.js +++ b/src/openaiExplainPatch.js @@ -1,6 +1,8 @@ import OpenAI from "openai"; -export default async function explainPatch({openaiKey, repo, prnum, +export default async function explainPatch({openaiKey, repo, prnum, + githubToken=null, + github=null, model="gpt-3.5-turbo-16k", system_prompt="Explain the patch:\n\n", max_tokens=256, @@ -8,10 +10,32 @@ export default async function explainPatch({openaiKey, repo, prnum, top_p=1, frequency_penalty=0, presence_penalty=0}) { + + if (!github && !githubToken) throw RangeError("github and githubToken arguments cannot be both null"); + const openai = new OpenAI({apiKey: openaiKey}); - const patchResponse = await fetch(`https://github.com/${repo}/pull/${prnum}.patch`); - const patchBody = await patchResponse.text(); + if (!github) { + const { Octokit } = await import("@octokit/core"); + + github = { + rest: new Octokit({auth: githubToken}) + } + } + + const [owner, reponame] = repo.split('/'); + + const {data: patchBody} = await github.rest.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', { + owner: owner, + repo: reponame, + pull_number: prnum, + headers: { + 'X-GitHub-Api-Version': '2022-11-28' + }, + mediaType: { + format: "patch", + }, + }) return patchBody;