From b050ad1b4f19c9ba55c90a4ad811ad129094a8f0 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Sat, 1 Jun 2024 20:30:02 +0000 Subject: [PATCH] include runid link in generated comment --- packages/core/src/github.ts | 26 ++++++++++++++++++++------ packages/core/src/markdown.ts | 4 ++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/packages/core/src/github.ts b/packages/core/src/github.ts index a842ead5e9..82616c0672 100644 --- a/packages/core/src/github.ts +++ b/packages/core/src/github.ts @@ -3,7 +3,7 @@ import { GITHUB_API_VERSION, GITHUB_TOKEN } from "./constants" import { GenerationResult } from "./expander" import { createFetch } from "./fetch" import { host } from "./host" -import { prettifyMarkdown } from "./markdown" +import { link, prettifyMarkdown } from "./markdown" import { logError, logVerbose, normalizeInt } from "./util" export interface GithubConnectionInfo { @@ -15,6 +15,8 @@ export interface GithubConnectionInfo { ref?: string sha?: string issue?: number + runId?: string + runUrl?: string } export function parseGHTokenFromEnv( @@ -26,6 +28,12 @@ export function parseGHTokenFromEnv( const [owner, repo] = repository?.split("/", 2) || [undefined, undefined] const ref = env.GITHUB_REF const sha = env.GITHUB_SHA + const runId = env.GITHUB_RUN_ID + const serverUrl = env.GITHUB_SERVER_URL + const runUrl = + serverUrl && runId + ? `${serverUrl}/${repository}}/actions/runs/${runId}` + : undefined const issue = normalizeInt( /^refs\/pull\/(?\d+)\/merge$/.exec(ref || "")?.groups?.issue ) @@ -39,6 +47,8 @@ export function parseGHTokenFromEnv( ref, sha, issue, + runId, + runUrl, } } @@ -55,7 +65,7 @@ export async function githubUpsetPullRequest( const token = await host.readSecret(GITHUB_TOKEN) if (!token) return { updated: false, statusText: "missing github token" } - text = appendGeneratedComment(script, text) + text = appendGeneratedComment(script, info, text) const fetch = await createFetch() const url = `${apiUrl}/repos/${repository}/pulls/${issue}` @@ -106,9 +116,13 @@ export async function githubUpsetPullRequest( return r } -function appendGeneratedComment(script: PromptScript, text: string) { +function appendGeneratedComment( + script: PromptScript, + info: GithubConnectionInfo, + text: string +) { return prettifyMarkdown( - `${text}\n\n> generated by genaiscript ${script.id}\n\n` + `${text}\n\n> generated by genaiscript ${link(script.id, info.runUrl)}\n\n` ) } @@ -128,7 +142,7 @@ export async function githubCreateIssueComment( const fetch = await createFetch() const url = `${apiUrl}/repos/${repository}/issues/${issue}/comments` - body = appendGeneratedComment(script, body) + body = appendGeneratedComment(script, info, body) if (commentTag) { const tag = `` @@ -203,7 +217,7 @@ async function githubCreateCommitComment( "X-GitHub-Api-Version": GITHUB_API_VERSION, }, body: JSON.stringify({ - body: appendGeneratedComment(script, annotation.message), + body: appendGeneratedComment(script, info, annotation.message), path: annotation.filename, position: annotation.range?.[0]?.[0], }), diff --git a/packages/core/src/markdown.ts b/packages/core/src/markdown.ts index ea6596a75e..b64f9ed11b 100644 --- a/packages/core/src/markdown.ts +++ b/packages/core/src/markdown.ts @@ -13,3 +13,7 @@ export function fenceMD(t: string, contentType?: string) { while (t.includes(f) && f.length < 8) f += "`" return `\n${f}${contentType}\n${trimNewlines(t)}\n${f}\n` } + +export function link(text: string, href: string) { + return href ? `[${text}](${href})` : text +}