Skip to content

Commit 916e56e

Browse files
authored
Use gh auth token for the ai-tools script (#58121)
1 parent 19e529a commit 916e56e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/ai-tools/scripts/ai-tools.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import fs from 'fs'
44
import yaml from 'js-yaml'
55
import path from 'path'
66
import ora from 'ora'
7+
import { execSync } from 'child_process'
78
import { callModelsApi } from '@/ai-tools/lib/call-models-api'
89
import dotenv from 'dotenv'
910
dotenv.config({ quiet: true })
@@ -13,7 +14,18 @@ const promptDir = path.join(__dirname, '../prompts')
1314
const promptTemplatePath = path.join(promptDir, 'prompt-template.yml')
1415

1516
if (!process.env.GITHUB_TOKEN) {
16-
throw new Error('Error! You must have a GITHUB_TOKEN set in an .env file to run this script.')
17+
// Try to find a token via the CLI before throwing an error
18+
const token = execSync('gh auth token').toString()
19+
if (token.startsWith('gh')) {
20+
process.env.GITHUB_TOKEN = token
21+
} else {
22+
console.warn(`🔑 A token is needed to run this script. Please do one of the following and try again:
23+
24+
1. Add a GITHUB_TOKEN to a local .env file.
25+
2. Install https://cli.github.com and authenticate via 'gh auth login'.
26+
`)
27+
process.exit(1)
28+
}
1729
}
1830

1931
interface EditorType {

0 commit comments

Comments
 (0)