diff --git a/app/functions.ts b/app/functions.ts index a20ebd4..1eba274 100644 --- a/app/functions.ts +++ b/app/functions.ts @@ -18,6 +18,7 @@ import updateIssue from './functions/updateIssue'; import listDiscussions from './functions/listDiscussions'; import getDiscussion from './functions/getDiscussion'; import createPullRequestReview from './functions/createPullRequestReview'; +import listPullRequestReviews from './functions/listPullRequestReviews'; import analyzeImage from './functions/analyzeImage'; import { type Tool } from 'ai'; @@ -29,6 +30,7 @@ type FunctionModule = { export const availableFunctions: Record = { analyzeImage, createPullRequestReview, + listPullRequestReviews, listDiscussions, getDiscussion, createIssue, @@ -89,6 +91,11 @@ export async function runFunction(name: string, args: any) { args['event'], args['comments'] ); + case 'listPullRequestReviews': + return await listPullRequestReviews.run( + args['repository'], + args['pullNumber'] + ); case 'listDiscussions': return await listDiscussions.run(args['repository']); case 'getDiscussion': diff --git a/app/functions/listPullRequestReviews.ts b/app/functions/listPullRequestReviews.ts new file mode 100644 index 0000000..2a1819e --- /dev/null +++ b/app/functions/listPullRequestReviews.ts @@ -0,0 +1,47 @@ +import { githubApiRequest } from '@/app/utils/github'; +import { Endpoints } from '@octokit/types'; +import OpenAI from 'openai'; +const ENDPOINT = 'GET /repos/{owner}/{repo}/pulls/{pull_number}/reviews'; + +const meta: OpenAI.FunctionDefinition = { + name: 'listPullRequestReviews', + description: `Retrieves a list of reviews for a given pull request.`, + parameters: { + type: 'object', + properties: { + repository: { + type: 'string', + description: + 'Required. The owner and name of a repository represented as :owner/:name. Do not guess. Confirm with the user if you are unsure.', + }, + pullNumber: { + type: 'string', + description: 'The number that identifies the pull request.', + }, + }, + required: ['repository', 'pullNumber'], + }, +}; + +async function run(repository: string, pullNumber: string) { + const [owner, repo] = repository.split('/'); + type ListReviewsResponse = Endpoints[typeof ENDPOINT]['response'] | undefined; + try { + const response = await githubApiRequest(ENDPOINT, { + owner, + repo, + pull_number: pullNumber, + }); + return response?.data.map((review) => ({ + reviewer: review.user?.login, + state: review.state, + body: review.body, + })); + } catch (error) { + console.log('Failed to fetch pull request reviews!'); + console.log(error); + return 'An error occured when trying to fetch pull request reviews.'; + } +} + +export default { run, meta };