Skip to content

Commit

Permalink
include runid link in generated comment
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jun 1, 2024
1 parent 6ac1431 commit b050ad1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
26 changes: 20 additions & 6 deletions packages/core/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -15,6 +15,8 @@ export interface GithubConnectionInfo {
ref?: string
sha?: string
issue?: number
runId?: string
runUrl?: string
}

export function parseGHTokenFromEnv(
Expand All @@ -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\/(?<issue>\d+)\/merge$/.exec(ref || "")?.groups?.issue
)
Expand All @@ -39,6 +47,8 @@ export function parseGHTokenFromEnv(
ref,
sha,
issue,
runId,
runUrl,
}
}

Expand All @@ -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}`
Expand Down Expand Up @@ -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`
)
}

Expand All @@ -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 = `<!-- genaiscript ${commentTag} -->`
Expand Down Expand Up @@ -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],
}),
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit b050ad1

Please sign in to comment.