-
Notifications
You must be signed in to change notification settings - Fork 1
/
example-summarize-pr.ts
52 lines (44 loc) · 1.7 KB
/
example-summarize-pr.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* eslint-disable @typescript-eslint/no-non-null-assertion */
// For the full app, see https://what-did-we-work-on.vercel.app/
import {initSDK} from '@opensdks/runtime'
import type {oasTypes as GithubSDKTypes} from '@opensdks/sdk-github/github.oas.types'
import {githubSdkDef} from '@opensdks/sdk-github'
import {openaiSdkDef} from '@opensdks/sdk-openai'
const github = initSDK(githubSdkDef, {
headers: {authorization: `Bearer ${process.env['GITHUB_TOKEN']}`},
})
const openai = initSDK(openaiSdkDef, {
headers: {authorization: `Bearer ${process.env['OPENAI_API_KEY']}`},
})
export async function fetchCommits(prLink: string) {
const prUrl = new URL(prLink)
const [, owner, repo, , prNumber] = prUrl.pathname.split('/')
return github
.GET('/repos/{owner}/{repo}/pulls/{pull_number}/commits', {
params: {
path: {owner: owner!, repo: repo!, pull_number: Number(prNumber)},
},
})
.then((r) => r.data)
}
type Commit = GithubSDKTypes['components']['schemas']['commit']
export const summarizeCommits = async (commits: Commit[]) => {
const messages = commits.map((commit) => commit.commit.message).join('\n')
const prompt = `I have a list of software commit messages and need a summary of the changes. Here are the commit messages:\n${messages}\nCan you provide a summary?`
try {
const response = await openai.POST('/chat/completions', {
body: {
model: 'gpt-3.5-turbo',
max_tokens: 1000,
messages: [
{role: 'system', content: prompt},
{role: 'user', content: messages},
],
},
})
return response.data.choices[0]?.message.content
} catch (err) {
console.error('Error summarizing commits:', err)
throw err
}
}