Skip to content

Commit

Permalink
allow for unique comments (#499)
Browse files Browse the repository at this point in the history
* allow for unique comments

* don't upsert, delete

* add AI disclaimer

* add ai generated comment
  • Loading branch information
pelikhan authored May 31, 2024
1 parent e2651d8 commit 3286b32
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ollama.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ jobs:
- name: download ollama docker
run: docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
- name: run summarize-ollama-phi3
run: yarn test:summarize --model ollama:phi3 --out ./temp -ghc
run: yarn test:summarize --model ollama:phi3 --out ./temp -ghc ollama
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 3 additions & 2 deletions docs/src/content/docs/reference/cli/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ Options:
rendered as csv, .jsonl/ndjson will be
aggregated)
-ocl, --out-changelog <string> output file for changelogs
-ghc, --github-comment create github comment on a pull request or
commit with output
-ghc, --github-comment [string] create github comment on a pull request or
commit with output. Use commentid to
upsert comment. (default: "none")
-j, --json emit full JSON response to output
-y, --yaml emit full YAML response to output
-p, --prompt dry run, don't execute LLM and return
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
UNHANDLED_ERROR_CODE,
errorMessage,
dotGenaiscriptPath,
GITHUB_COMMENT_ID_NONE,
} from "genaiscript-core"
import { NodeHost } from "./nodehost"
import { program } from "commander"
Expand Down Expand Up @@ -101,8 +102,9 @@ export async function cli() {
)
.option("-ocl, --out-changelog <string>", "output file for changelogs")
.option(
"-ghc, --github-comment",
"create github comment on a pull request or commit with output"
"-ghc, --github-comment [string]",
"create github comment on a pull request or commit with output. Use commentid to upsert comment.",
GITHUB_COMMENT_ID_NONE
)
.option("-j, --json", "emit full JSON response to output")
.option("-y, --yaml", "emit full YAML response to output")
Expand Down
12 changes: 9 additions & 3 deletions packages/cli/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import {
CLI_RUN_FILES_FOLDER,
parseGHTokenFromEnv,
githubCreateIssueComment,
pretifyMarkdown,
prettifyMarkdown,
GITHUB_COMMENT_ID_NONE,
} from "genaiscript-core"
import { capitalize } from "inflection"
import { basename, resolve, join, relative } from "node:path"
Expand All @@ -58,7 +59,7 @@ export async function runScript(
outTrace: string
outAnnotations: string
outChangelogs: string
githubComment: boolean
githubComment: string
outData: string
label: string
temperature: string
Expand Down Expand Up @@ -366,7 +367,12 @@ ${Array.from(files)
if (info.repository && info.issue) {
const ghres = await githubCreateIssueComment(
info,
pretifyMarkdown(res.text)
prettifyMarkdown(
`${res.text}\n\n> Comment generated by genaiscript ${script.id}.`
),
gitHubComment !== GITHUB_COMMENT_ID_NONE
? gitHubComment
: undefined
)
if (!ghres.created) {
logError(
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,5 @@ export const DOCKER_CONTAINER_VOLUME = "/app"

export const CLI_RUN_FILES_FOLDER = "files"

export const GITHUB_API_VERSION = "2022-11-28"
export const GITHUB_API_VERSION = "2022-11-28"
export const GITHUB_COMMENT_ID_NONE = "none"
34 changes: 32 additions & 2 deletions packages/core/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ export function parseGHTokenFromEnv(
// https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#create-an-issue-comment
export async function githubCreateIssueComment(
info: GithubConnectionInfo,
body: string
body: string,
commentTag?: string
): Promise<{ created: boolean; statusText: string; html_url?: string }> {
const { apiUrl, repository, issue, sha } = info
const { apiUrl, repository, issue } = info

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

Expand All @@ -54,6 +55,35 @@ export async function githubCreateIssueComment(
const fetch = await createFetch()
const url = `${apiUrl}/repos/${repository}/issues/${issue}/comments`

if (commentTag) {
const tag = `<!-- genaiscript ${commentTag} -->`
body = `${body}\n\n${tag}\n\n`
// try to find the existing comment
const resListComments = await fetch(`${url}?per_page=100`, {
headers: {
Accept: "application/vnd.github+json",
Authorization: `Bearer ${token}`,
"X-GitHub-Api-Version": GITHUB_API_VERSION,
},
})
if (resListComments.status !== 200)
return { created: false, statusText: resListComments.statusText }
const comments = (await resListComments.json()) as {
id: string
body: string
}[]
const comment = comments.find((c) => c.body.includes(tag))
if (comment) {
await fetch(`${url}/${comment.id}`, {
method: "DELETE",
headers: {
Authorization: `Bearer ${token}`,
"X-GitHub-Api-Version": GITHUB_API_VERSION,
},
})
}
}

const res = await fetch(url, {
method: "POST",
headers: {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { convertAnnotationsToMarkdown } from "./annotations"
import { trimNewlines } from "./util"

export function pretifyMarkdown(md: string) {
export function prettifyMarkdown(md: string) {
let res = md
res = convertAnnotationsToMarkdown(res)
return res
Expand Down
4 changes: 2 additions & 2 deletions packages/vscode/src/markdowndocumentprovider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
extractFenced,
fenceMD,
getChatCompletionCache,
pretifyMarkdown,
prettifyMarkdown,
renderFencedVariables,
GENAI_JS_REGEX,
} from "genaiscript-core"
Expand Down Expand Up @@ -67,7 +67,7 @@ class MarkdownTextDocumentContentProvider
if (!aiRequest) return noRequest
if (!md) return noResponse
return `${computing ? `> **GenAiScript run in progress.**\n` : ""}
${pretifyMarkdown(md)}
${prettifyMarkdown(md)}
`
}

Expand Down

0 comments on commit 3286b32

Please sign in to comment.