Unverified Commit 68f8c974 authored by Sam Ko's avatar Sam Ko Committed by GitHub

chore(github): run opened issues through ai (#68303)

## Why?

To help flag severe issues, we can use utilize AI to digest the
`context.payload.issue` object each time an issue is opened and
determine whether that issue is severe enough to flag to the Next.js
team on Slack.

## How?

Making a Tool Call using using the :ai-stonks: SDK. The :ai-stonks: will call the Tool and
report to Slack (to the `#next-info` channel`) if it determines that the
issue is severe enough, using the context of the issue, latest Next.js
versions, and a Triage Guideline.

## Testing

This was tested on a separate test repo with issues
https://github.com/vercel/next.js/issues/68328 (real issue) and
https://github.com/vercel/next.js/issues/68336 (blank test issue).

![CleanShot 2024-07-30 at 12 07
43@2x](https://github.com/user-attachments/assets/7a35514e-840d-41db-b1a7-6021fe389458)

![CleanShot 2024-07-30 at 12 10
26@2x](https://github.com/user-attachments/assets/c7e17612-59b6-4ce3-8b7f-ea96c3e09680)
parent b80e0f6c
**Triage Guidelines:**
Hard rules:
- Ignore the bug, Documentation labels.
- Ignore the url to the code that reproduces the issue.
Questions to ask:
- Is the issue reproducible on the latest version of Next.js? If not, it's not a severe issue.
- Is there a body of text that explains the issue? If not, it's not a severe issue.
- Is the issue related to an experimental feature? If yes, it's not a severe issue.
- Did build/compile times increase drastically? If yes, it's a severe issue.
- Is there a significant performance regression? If yes, it's a severe issue.
- Is CSS not working as expected? If yes, it's a severe issue.
- Are multiple stages (next dev, next start, deploy) affected? If yes, it's likely a severe issue.
import { z } from 'zod'
const userSchema = z
.object({
avatar_url: z.string(),
deleted: z.boolean(),
email: z.string().nullable(),
event_url: z.string(),
followers_url: z.string(),
following_url: z.string(),
gists_url: z.string(),
gravatar_id: z.string(),
html_url: z.string(),
id: z.number(),
login: z.string(),
name: z.string().optional(),
node_id: z.string(),
organizations_url: z.string(),
received_events_url: z.string(),
repos_url: z.string(),
site_admin: z.boolean(),
starred_url: z.string(),
subscriptions_url: z.string(),
type: z.enum(['Bot', 'User', 'Organization', 'Mannequin']),
url: z.string(),
})
.strict()
const labelSchema = z
.object({
color: z.string(),
default: z.boolean(),
description: z.string().nullable(),
id: z.number(),
name: z.string(),
node_id: z.string(),
url: z.string(),
})
.strict()
const milestoneSchema = z
.object({
closed_at: z.string().nullable(),
closed_issues: z.number(),
created_at: z.string(),
creator: userSchema.nullable(),
description: z.string().nullable(),
due_on: z.string().nullable(),
html_url: z.string(),
id: z.number(),
labels_url: z.string(),
node_id: z.string(),
number: z.number(),
open_issues: z.number(),
state: z.enum(['closed', 'open']),
title: z.string(),
updated_at: z.string(),
url: z.string(),
})
.strict()
.describe('A collection of related issues.')
const permissionsSchema = z
.object({
actions: z.enum(['read', 'write']),
administration: z.enum(['read', 'write']),
content_references: z.enum(['read', 'write']),
contents: z.enum(['read', 'write']),
deployments: z.enum(['read', 'write']),
discussions: z.enum(['read', 'write']),
emails: z.enum(['read', 'write']),
environments: z.enum(['read', 'write']),
issues: z.enum(['read', 'write']),
keys: z.enum(['read', 'write']),
members: z.enum(['read', 'write']),
metadata: z.enum(['read', 'write']),
organization_administration: z.enum(['read', 'write']),
organization_hooks: z.enum(['read', 'write']),
organization_packages: z.enum(['read', 'write']),
organization_plan: z.enum(['read', 'write']),
organization_projects: z.enum(['read', 'write']),
organization_secrets: z.enum(['read', 'write']),
organization_self_hosted_runners: z.enum(['read', 'write']),
organization_user_blocking: z.enum(['read', 'write']),
packages: z.enum(['read', 'write']),
pages: z.enum(['read', 'write']),
pull_requests: z.enum(['read', 'write']),
repository_hooks: z.enum(['read', 'write']),
repository_projects: z.enum(['read', 'write']),
secret_scanning_alerts: z.enum(['read', 'write']),
secrets: z.enum(['read', 'write']),
security_events: z.enum(['read', 'write']),
security_scanning_alert: z.enum(['read', 'write']),
single_file: z.enum(['read', 'write']),
statuses: z.enum(['read', 'write']),
team_discussions: z.enum(['read', 'write']),
vulnerability_alerts: z.enum(['read', 'write']),
workflows: z.enum(['read', 'write']),
})
.strict()
const performedViaGitHubAppSchema = z
.object({
created_at: z.string().nullable(),
description: z.string().nullable(),
events: z.enum([
'branch_protection_rule',
'check_run',
'check_suite',
'code_scanning_alert',
'commit_comment',
'content_reference',
'create',
'delete',
'deployment',
'deployment_review',
'deployment_status',
'deploy_key',
'discussion',
'discussion_comment',
'fork',
'gollum',
'issues',
'issue_comment',
'label',
'member',
'membership',
'milestone',
'organization',
'org_block',
'page_build',
'project',
'project_card',
'project_column',
'public',
'pull_request',
'pull_request_review',
'pull_request_review_comment',
'push',
'registry_package',
'release',
'repository',
'repository_dispatch',
'secret_scanning_alert',
'star',
'status',
'team',
'team_add',
'watch',
'workflow_dispatch',
'workflow_run',
'reminder',
'pull_request_review_thread',
]),
external_url: z.string().nullable(),
html_url: z.string(),
id: z.number().nullable(),
name: z.string(),
node_id: z.string(),
owner: userSchema.nullable(),
permissions: permissionsSchema,
slug: z.string(),
updated_at: z.string().nullable(),
})
.strict()
const pullRequestSchema = z
.object({
diff_url: z.string(),
html_url: z.string(),
merged_at: z.string().nullable(),
patch_url: z.string(),
url: z.string(),
})
.strict()
const reactionSchema = z
.object({
'+1': z.number(),
'-1': z.number(),
confused: z.number(),
eyes: z.number(),
heart: z.number(),
hooray: z.number(),
laugh: z.number(),
rocket: z.number(),
total_count: z.number(),
url: z.string(),
})
.strict()
export const issueSchema = z
.object({
issue: z.object({
active_lock_reason: z
.enum(['resolved', 'off-topic', 'too_heated', 'spam'])
.nullable(),
assignee: userSchema.nullable(),
assignees: z.array(userSchema).default([]),
author_association: z.enum([
'COLLABORATOR',
'CONTRIBUTOR',
'FIRST_TIMER',
'FIRST_TIME_CONTRIBUTOR',
'MANNEQUIN',
'MEMBER',
'NONE',
'OWNER',
]),
body: z.string().nullable(),
closed_at: z.string().nullable(),
comments: z.number(),
comments_url: z.string(),
created_at: z.string(),
events_url: z.string(),
html_url: z.string(),
id: z.number(),
labels: z.array(labelSchema).default([]),
labels_url: z.string(),
locked: z.boolean(),
milestone: milestoneSchema.nullable(),
node_id: z.string(),
number: z.number(),
performed_via_github_app: performedViaGitHubAppSchema.nullable(),
reactions: reactionSchema,
repository_url: z.string(),
state: z.enum(['closed', 'open']),
state_reason: z.string().nullable(),
timeline_url: z.string(),
title: z.string(),
updated_at: z.string(),
url: z.string(),
user: userSchema.nullable(),
}),
})
.strict()
.describe('A GitHub issue.')
export type Issue = z.infer<typeof issueSchema>
import { getExecOutput } from '@actions/exec'
import { setFailed } from '@actions/core'
// format date to <month|short> <day|numeric>, <year|numeric> // format date to <month|short> <day|numeric>, <year|numeric>
export function formattedDate(createdAt) { export function formattedDate(createdAt) {
const date = new Date(createdAt) const date = new Date(createdAt)
...@@ -15,3 +18,43 @@ export function ninetyDaysAgo() { ...@@ -15,3 +18,43 @@ export function ninetyDaysAgo() {
date.setDate(date.getDate() - 90) date.setDate(date.getDate() - 90)
return date.toISOString().split('T')[0] return date.toISOString().split('T')[0]
} }
export async function getLatestCanaryVersion() {
let latestCanaryVersion
try {
const { stdout } = await getExecOutput('pnpm', [
'view',
'next',
'dist-tags',
'--json',
])
const distTags = JSON.parse(stdout)
latestCanaryVersion = distTags.canary || null
} catch (error) {
setFailed(`Error fetching latest Next.js canary version, skipping update.`)
}
return latestCanaryVersion
}
export async function getLatestVersion() {
let latestVersion
try {
const { stdout } = await getExecOutput('pnpm', [
'view',
'next',
'dist-tags',
'--json',
])
const distTags = JSON.parse(stdout)
latestVersion = distTags.latest || null
} catch (error) {
setFailed(`Error fetching latest Next.js version, skipping update.`)
}
return latestVersion
}
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
}, },
"dependencies": { "dependencies": {
"@actions/core": "^1.10.1", "@actions/core": "^1.10.1",
"@actions/exec": "^1.1.1",
"@actions/github": "6.0.0", "@actions/github": "6.0.0",
"@ai-sdk/openai": "^0.0.40", "@ai-sdk/openai": "^0.0.40",
"@slack/web-api": "^7.0.4", "@slack/web-api": "^7.0.4",
......
import { info } from '@actions/core' import { WebClient } from '@slack/web-api'
import * as path from 'node:path'
import { readFileSync } from 'node:fs'
import { info, setFailed } from '@actions/core'
import { context } from '@actions/github' import { context } from '@actions/github'
import { generateText, tool } from 'ai'
import { openai } from '@ai-sdk/openai'
import { BlockCollection, Divider, Section } from 'slack-block-builder'
import { getLatestCanaryVersion, getLatestVersion } from '../lib/util.mjs'
import { issueSchema } from '../lib/types'
async function main() { async function main() {
if (!process.env.OPENAI_API_KEY) throw new TypeError('OPENAI_API_KEY not set')
if (!process.env.SLACK_TOKEN) throw new TypeError('SLACK_TOKEN not set')
const slackClient = new WebClient(process.env.SLACK_TOKEN)
const model = 'gpt-4o'
const channel = '#next-info'
const issue = context.payload.issue const issue = context.payload.issue
info(`Issue: ${issue}`) const filePath = path.join(
info(`Issue: ${JSON.stringify(issue, null, 2)}`) process.cwd(),
'.github/actions/next-repo-actions/lib/triage_guidelines.txt'
)
const fileText = readFileSync(filePath, 'utf8')
let latestVersion
let latestCanaryVersion
try {
latestVersion = await getLatestVersion()
latestCanaryVersion = await getLatestCanaryVersion()
const result = await generateText({
model: openai(model),
maxAutomaticRoundtrips: 1,
tools: {
report_to_slack: tool({
description: 'Report to Slack if a GitHub issue is severe enough.',
parameters: issueSchema,
execute: async ({ issue }) => ({
html_url: issue.html_url,
number: issue.number,
title: issue.title,
}),
}),
},
prompt: `${JSON.stringify(issue)}\n${fileText}\nlatestVersion: ${latestVersion}\nlatestCanaryVersion: ${latestCanaryVersion}\nWith the above GitHub issue (JSON), the triage guidelines for determining whether an issue is severe, and the latest versions of Next.js, can you determine whether the given issue is severe enough to report to Slack? If severe enough, report to Slack with an approximately 300 character summary (don't repeat the triage guidelines while doing so) of why you think it is severe enough to report to Slack. If not severe enough, do not report to Slack.`,
})
// the ai determined that the issue was severe enough to report on slack
if (result.roundtrips.length > 1) {
const blocks = BlockCollection([
Section({
text: `:github2: <${result.roundtrips[0].toolResults[0].result.html_url}|#${result.roundtrips[0].toolResults[0].result.number}>: ${result.roundtrips[0].toolResults[0].result.title}\n_Note: This issue was summarized and reported on Slack with the *${model}* model._`,
}),
Divider(),
Section({
text: `_${result.text}_`,
}),
])
await slackClient.chat.postMessage({
blocks,
channel,
icon_emoji: ':github:',
username: 'GitHub Notifier',
})
} else {
// the ai will also provide a reason why the issue was not severe enough to report on slack
info(`${result.text}`)
}
} catch (error) {
setFailed(error)
}
} }
main() main()
...@@ -61,6 +61,9 @@ importers: ...@@ -61,6 +61,9 @@ importers:
'@actions/core': '@actions/core':
specifier: ^1.10.1 specifier: ^1.10.1
version: 1.10.1 version: 1.10.1
'@actions/exec':
specifier: ^1.1.1
version: 1.1.1
'@actions/github': '@actions/github':
specifier: 6.0.0 specifier: 6.0.0
version: 6.0.0 version: 6.0.0
......
...@@ -11,8 +11,8 @@ jobs: ...@@ -11,8 +11,8 @@ jobs:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: actions/setup-node@v4 - uses: actions/setup-node@v4
- run: corepack enable - run: corepack enable
- name: 'Send notification to Slack (AI-powered)' - name: 'Send report to Slack (AI-powered)'
run: node ./.github/actions/next-repo-actions/dist/triage-issues-with-ai/index.js run: node ./.github/actions/next-repo-actions/dist/triage-issues-with-ai/index.js
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
SLACK_TOKEN: ${{ secrets.SLACK_TOKEN }} SLACK_TOKEN: ${{ secrets.SLACK_TOKEN }}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment