Skip to content

Commit

Permalink
upload annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jun 1, 2024
1 parent 047e703 commit 81b8bf1
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 24 deletions.
20 changes: 5 additions & 15 deletions packages/cli/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ import {
CSV_REGEX,
CLI_RUN_FILES_FOLDER,
parseGHTokenFromEnv,
githubCreateIssueComment,
prettifyMarkdown,
GITHUB_COMMENT_ID_NONE,
githubUpsetPullRequest,
githubCreateIssueComments,
} from "genaiscript-core"
import { capitalize } from "inflection"
import { basename, resolve, join, relative } from "node:path"
Expand Down Expand Up @@ -368,22 +367,13 @@ ${Array.from(files)
if (pullRequestComment && res.text) {
const info = parseGHTokenFromEnv(process.env)
if (info.repository && info.issue) {
const ghres = await githubCreateIssueComment(
const ghres = await githubCreateIssueComments(
script,
info,
prettifyMarkdown(
`${res.text}\n\n> generated by genaiscript ${script.id}.`
),
res,
pullRequestComment
)
if (!ghres.created) {
logError(
`pull request ${info.repository}/pull/${info.issue} comment failed ${ghres.statusText}`
)
process.exit(CONFIGURATION_ERROR_CODE)
}
logVerbose(
`pull request ${info.repository}/pull/${info.issue} comment created at ${ghres.html_url}`
)
if (!ghres) process.exit(CONFIGURATION_ERROR_CODE)
}
}

Expand Down
97 changes: 88 additions & 9 deletions packages/core/src/github.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { GITHUB_API_VERSION } from "./constants"
import { GenerationResult } from "./expander"
import { createFetch } from "./fetch"
import { host } from "./host"
import { logError, normalizeInt } from "./util"
import { prettifyMarkdown } from "./markdown"
import { logError, logVerbose, normalizeInt } from "./util"

export interface GithubConnectionInfo {
token: string
Expand Down Expand Up @@ -92,22 +94,26 @@ export async function githubUpsetPullRequest(
}
}

function appendGeneratedComment(script: PromptScript, text: string) {
return prettifyMarkdown(
`${text}\n\n> generated by genaiscript ${script.id}`
)
}

// https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#create-an-issue-comment
export async function githubCreateIssueComment(
async function githubCreateIssueComment(
script: PromptScript,
info: GithubConnectionInfo,
token: string,
body: string,
commentTag?: string
commentTag: string
): Promise<{ created: boolean; statusText: string; html_url?: string }> {
const { apiUrl, repository, issue } = info

if (!issue) return { created: false, statusText: "missing issue number" }

const token = await host.readSecret("GITHUB_TOKEN")
if (!token) return { created: false, statusText: "missing token" }

const fetch = await createFetch()
const url = `${apiUrl}/repos/${repository}/issues/${issue}/comments`

body = appendGeneratedComment(script, body)

if (commentTag) {
const tag = `<!-- genaiscript ${commentTag} -->`
body = `${body}\n\n${tag}\n\n`
Expand Down Expand Up @@ -157,3 +163,76 @@ export async function githubCreateIssueComment(
html_url: resp.html_url,
}
}

async function githubCreateCommitComment(
script: PromptScript,
info: GithubConnectionInfo,
token: string,
annotation: Diagnostic
) {
const { apiUrl, repository, sha } = info
const fetch = await createFetch()
const url = `${apiUrl}/repos/${repository}/commits/${sha}/comments`

const res = await fetch(url, {
method: "POST",
headers: {
Accept: "application/vnd.github+json",
Authorization: `Bearer ${token}`,
"X-GitHub-Api-Version": GITHUB_API_VERSION,
},
body: JSON.stringify({
body: appendGeneratedComment(script, annotation.message),
path: annotation.filename,
position: annotation.range?.[0]?.[0],
}),
})
const resp: { id: string; html_url: string } = await res.json()
return {
created: res.status === 201,
statusText: res.statusText,
html_url: resp.html_url,
}
}

export async function githubCreateIssueComments(
script: PromptScript,
info: GithubConnectionInfo,
gen: GenerationResult,
commentTag: string
): Promise<boolean> {
const { issue, sha } = info
if (!issue) {
logError("missing issue number")
return false
}
const token = await host.readSecret("GITHUB_TOKEN")
if (!token) {
logError("missing token")
return false
}
const { text, annotations } = gen

if (text) {
// output text
const r = await githubCreateIssueComment(
script,
info,
token,
text,
commentTag
)
if (!r.created) {
logError(`pull request comment failed ${r.statusText}`)
return false
}
logVerbose(`pull request comment created at ${r.html_url}`)
}

if (annotations?.length && sha) {
// code annotations
for (const annotation of annotations)
await githubCreateCommitComment(script, info, token, annotation)
}
return true
}

0 comments on commit 81b8bf1

Please sign in to comment.