From 175d33146e6378d59d876677b60797c72c885fb3 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 26 Sep 2024 16:35:03 -0700 Subject: [PATCH] -pr option in run (#729) * break build * Add pull request option to CLI and refactor GitHub info resolution in scripts --- .github/workflows/genai-investigator.yml | 2 +- docs/src/content/docs/reference/cli/commands.md | 1 + packages/cli/src/cli.ts | 1 + packages/cli/src/run.ts | 17 +++++++++++++---- packages/core/src/server/messages.ts | 1 + packages/sample/genaisrc/fs.genai.mjs | 4 ++-- 6 files changed, 19 insertions(+), 7 deletions(-) diff --git a/.github/workflows/genai-investigator.yml b/.github/workflows/genai-investigator.yml index 5acf58c43c..9ffafae486 100644 --- a/.github/workflows/genai-investigator.yml +++ b/.github/workflows/genai-investigator.yml @@ -32,7 +32,7 @@ jobs: - name: compile run: yarn compile - name: genaiscript gai - run: node packages/cli/built/genaiscript.cjs run gai -prc --vars "failure_run_id=${{ github.event.workflow_run.id }}" --vars "branch=${{ github.event.workflow_run.head_branch }}" --out-trace $GITHUB_STEP_SUMMARY + run: node packages/cli/built/genaiscript.cjs run gai -pr ${{ github.event.workflow_run.pull_requests[0].number }} -prc --vars "failure_run_id=${{ github.event.workflow_run.id }}" --vars "branch=${{ github.event.workflow_run.head_branch }}" --out-trace $GITHUB_STEP_SUMMARY env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} diff --git a/docs/src/content/docs/reference/cli/commands.md b/docs/src/content/docs/reference/cli/commands.md index cd6bde471d..839312b527 100644 --- a/docs/src/content/docs/reference/cli/commands.md +++ b/docs/src/content/docs/reference/cli/commands.md @@ -24,6 +24,7 @@ Options: -od, --out-data output file for data (.jsonl/ndjson will be aggregated). JSON schema information and validation will be included if available. -oa, --out-annotations output file for annotations (.csv will be rendered as csv, .jsonl/ndjson will be aggregated) -ocl, --out-changelog output file for changelogs + -pr, --pull-request pull request identifier -prc, --pull-request-comment [string] create comment on a pull request with a unique id (defaults to script id) -prd, --pull-request-description [string] create comment on a pull request description with a unique id (defaults to script id) -prr, --pull-request-reviews create pull request reviews from annotations diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index e8b3b6e027..342904a5f9 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -114,6 +114,7 @@ export async function cli() { "output file for annotations (.csv will be rendered as csv, .jsonl/ndjson will be aggregated)" ) .option("-ocl, --out-changelog ", "output file for changelogs") + .option("-pr, --pull-request ", "pull request identifier") .option( "-prc, --pull-request-comment [string]", "create comment on a pull request with a unique id (defaults to script id)" diff --git a/packages/cli/src/run.ts b/packages/cli/src/run.ts index 05fa72931e..d3ee4cefa6 100644 --- a/packages/cli/src/run.ts +++ b/packages/cli/src/run.ts @@ -148,6 +148,7 @@ export async function runScript( const outAnnotations = options.outAnnotations const failOnErrors = options.failOnErrors const outChangelogs = options.outChangelogs + const pullRequest = normalizeInt(options.pullRequest) const pullRequestComment = options.pullRequestComment const pullRequestDescription = options.pullRequestDescription const pullRequestReviews = options.pullRequestReviews @@ -427,11 +428,19 @@ export async function runScript( } } - let ghInfo: GithubConnectionInfo = undefined + let _ghInfo: GithubConnectionInfo = undefined + const resolveGitHubInfo = async () => { + if (!_ghInfo) { + _ghInfo = await githubParseEnv(process.env) + if (pullRequest) _ghInfo.issue = pullRequest + } + return _ghInfo + } let adoInfo: AzureDevOpsEnv = undefined + if (pullRequestReviews && result.annotations?.length) { // github action or repo - ghInfo = ghInfo ?? (await githubParseEnv(process.env)) + const ghInfo = await resolveGitHubInfo() if (ghInfo.repository && ghInfo.issue && ghInfo.commitSha) { await githubCreatePullRequestReviews( script, @@ -443,7 +452,7 @@ export async function runScript( if (pullRequestComment && result.text) { // github action or repo - ghInfo = ghInfo ?? (await githubParseEnv(process.env)) + const ghInfo = await resolveGitHubInfo() if (ghInfo.repository && ghInfo.issue) { await githubCreateIssueComment( script, @@ -473,7 +482,7 @@ export async function runScript( if (pullRequestDescription && result.text) { // github action or repo - ghInfo = ghInfo ?? (await githubParseEnv(process.env)) + const ghInfo = await resolveGitHubInfo() if (ghInfo.repository && ghInfo.issue) { await githubUpdatePullRequestDescription( script, diff --git a/packages/core/src/server/messages.ts b/packages/core/src/server/messages.ts index 3bd55bea11..4b1f6193e9 100644 --- a/packages/core/src/server/messages.ts +++ b/packages/core/src/server/messages.ts @@ -56,6 +56,7 @@ export interface PromptScriptRunOptions { outTrace: string outAnnotations: string outChangelogs: string + pullRequest: string pullRequestComment: string | boolean pullRequestDescription: string | boolean pullRequestReviews: boolean diff --git a/packages/sample/genaisrc/fs.genai.mjs b/packages/sample/genaisrc/fs.genai.mjs index 7422ec5985..5bf415a1ba 100644 --- a/packages/sample/genaisrc/fs.genai.mjs +++ b/packages/sample/genaisrc/fs.genai.mjs @@ -1,7 +1,7 @@ script({ model: "openai:gpt-3.5-turbo", tools: ["fs"], - tests: {} + tests: {}, }) -$`List the cities in the src folder markdown files as a CSV table. The file should contain the word "city".` \ No newline at end of file +$`List the cities in the src folder markdown files as a CSV table. The file should contain the word "city".`