From 3e9055ef8a505f87f6f4f9763de925131b55e997 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Sat, 1 Jun 2024 22:44:24 +0000 Subject: [PATCH] store pull request head sha --- .github/workflows/genai-pr.yml | 1 + packages/cli/src/run.ts | 2 +- packages/core/src/github.ts | 16 ++++++++++++---- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/.github/workflows/genai-pr.yml b/.github/workflows/genai-pr.yml index 6fed6ed9e5..50337a1f7e 100644 --- a/.github/workflows/genai-pr.yml +++ b/.github/workflows/genai-pr.yml @@ -46,6 +46,7 @@ jobs: run: node packages/cli/built/genaiscript.cjs run pr-review --out ./temp/pr-review -prc pr-review -prr env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_PULL_REQUEST_HEAD_SHA: ${{ github.event.pull_request.head.sha}} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} OPENAI_API_TYPE: ${{ secrets.OPENAI_API_TYPE }} OPENAI_API_BASE: ${{ secrets.OPENAI_API_BASE }} diff --git a/packages/cli/src/run.ts b/packages/cli/src/run.ts index c2f24ff5f9..52766f2b2a 100644 --- a/packages/cli/src/run.ts +++ b/packages/cli/src/run.ts @@ -368,7 +368,7 @@ ${Array.from(files) if (pullRequestReviews && res.annotations?.length) { const info = parseGHTokenFromEnv(process.env) - if (info.repository && info.issue && info.sha) + if (info.repository && info.issue) await githubCreatePullRequestReviews(script, info, res.annotations) } diff --git a/packages/core/src/github.ts b/packages/core/src/github.ts index b5f2351e17..6ebc9e2bd1 100644 --- a/packages/core/src/github.ts +++ b/packages/core/src/github.ts @@ -18,6 +18,7 @@ export interface GithubConnectionInfo { issue?: number runId?: string runUrl?: string + pullRequestHeadSha?: string } export function parseGHTokenFromEnv( @@ -29,6 +30,7 @@ export function parseGHTokenFromEnv( const [owner, repo] = repository?.split("/", 2) || [undefined, undefined] const ref = env.GITHUB_REF const sha = env.GITHUB_SHA + const pullRequestHeadSha = env.GITHUB_PULL_REQUEST_HEAD_SHA const runId = env.GITHUB_RUN_ID const serverUrl = env.GITHUB_SERVER_URL const runUrl = @@ -50,6 +52,7 @@ export function parseGHTokenFromEnv( issue, runId, runUrl, + pullRequestHeadSha, } } @@ -209,13 +212,13 @@ async function githubCreatePullRequestReview( annotation: Diagnostic ) { assert(token) - const { apiUrl, repository, issue, sha } = info + const { apiUrl, repository, issue, pullRequestHeadSha } = info const fetch = await createFetch() const url = `${apiUrl}/repos/${repository}/pulls/${issue}/comments` logVerbose(url) const body = { body: appendGeneratedComment(script, info, annotation.message), - commit_id: sha, + commit_id: pullRequestHeadSha, path: annotation.filename, line: annotation.range?.[0]?.[0], side: "RIGHT", @@ -238,8 +241,13 @@ async function githubCreatePullRequestReview( html_url: resp.html_url, } if (!r.created) - logError(`commit ${sha} comment creation failed, ${r.statusText}`) - else logVerbose(`commit ${sha} comment created at ${r.html_url}`) + logError( + `commit ${pullRequestHeadSha} comment creation failed, ${r.statusText}` + ) + else + logVerbose( + `commit ${pullRequestHeadSha} comment created at ${r.html_url}` + ) return r }