Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upload annotations in github pull requests #501

Merged
merged 33 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
81b8bf1
upload annotations
pelikhan Jun 1, 2024
97fce50
put the review in comment, don't comment from ollama
pelikhan Jun 1, 2024
ef0aade
added commit comments
pelikhan Jun 1, 2024
dd29388
only review .ts files
pelikhan Jun 1, 2024
48feadf
comment and review with pr-review
pelikhan Jun 1, 2024
48055fe
consistent logging of gh calls
pelikhan Jun 1, 2024
6ac1431
more tuning of describe
pelikhan Jun 1, 2024
b050ad1
include runid link in generated comment
pelikhan Jun 1, 2024
e0c2f88
more logging
pelikhan Jun 1, 2024
3930791
typo in run url
pelikhan Jun 1, 2024
a90189b
fix description slicing
pelikhan Jun 1, 2024
8095010
ensure relative path in diagnostics
pelikhan Jun 1, 2024
d4165dc
more logging
pelikhan Jun 1, 2024
be633a5
and logging
pelikhan Jun 1, 2024
454b541
generate pull review comments
pelikhan Jun 1, 2024
11505d5
renamed option
pelikhan Jun 1, 2024
212414d
don't emit start_line if same lines
pelikhan Jun 1, 2024
4b54950
missing start_side
pelikhan Jun 1, 2024
c10eeaf
only consider last commit_id
pelikhan Jun 1, 2024
13fc5c8
remove subject_type
pelikhan Jun 1, 2024
3e9055e
store pull request head sha
pelikhan Jun 1, 2024
a34818c
archive results
pelikhan Jun 1, 2024
77bcf2e
tune review
pelikhan Jun 2, 2024
c700997
type in change
pelikhan Jun 2, 2024
2dbd9b0
reduce logging
pelikhan Jun 2, 2024
e06b93b
don't inject line numbers in pr-review-commit
pelikhan Jun 2, 2024
19f45ce
don't log empty errors
pelikhan Jun 2, 2024
2925359
don't retry on github resources
pelikhan Jun 2, 2024
c800911
more logging
pelikhan Jun 2, 2024
2b789ea
don't inline code snippets
pelikhan Jun 2, 2024
95fbdf6
ensure annotations are unique when parsing
pelikhan Jun 2, 2024
f00a820
fix tracing of diagnostics
pelikhan Jun 2, 2024
5dd85e8
only report 3 errors
pelikhan Jun 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions .github/workflows/genai-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,30 @@ jobs:
- name: git stuff
run: git fetch origin && git pull origin main:main
- name: genaiscript pr-describe
run: node packages/cli/built/genaiscript.cjs run pr-describe --out ./temp -prd pr-describe
run: node packages/cli/built/genaiscript.cjs run pr-describe --out ./temp/genai/pr-describe -prd pr-describe
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_API_TYPE: ${{ secrets.OPENAI_API_TYPE }}
OPENAI_API_BASE: ${{ secrets.OPENAI_API_BASE }}
- name: genaiscript pr-review
run: node packages/cli/built/genaiscript.cjs run pr-review --out ./temp -prd pr-review
run: node packages/cli/built/genaiscript.cjs run pr-review --out ./temp/genai/pr-review -prc pr-review
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_COMMIT_SHA: ${{ github.event.pull_request.base.sha}}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_API_TYPE: ${{ secrets.OPENAI_API_TYPE }}
OPENAI_API_BASE: ${{ secrets.OPENAI_API_BASE }}
- name: genaiscript pr-review-commit
run: node packages/cli/built/genaiscript.cjs run pr-review-commit --out ./temp/genai/pr-review-commit -prr
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_COMMIT_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 }}
- name: Archive genai results
uses: actions/upload-artifact@v4
with:
name: genai-results
path: ./temp/genai/**
6 changes: 1 addition & 5 deletions .github/workflows/ollama.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ on:
- "packages/core/**/*"
- "packages/cli/**/*"
- "packages/samples/**/*"
permissions:
pull-requests: write
jobs:
tests:
runs-on: ubuntu-latest
Expand All @@ -39,6 +37,4 @@ 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 -prc ollama
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: yarn test:summarize --model ollama:phi3 --out ./temp
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"execa",
"genai",
"Genaiscript",
"genaisrc",
"gpspec",
"gpspecs",
"gptool",
Expand Down
1 change: 1 addition & 0 deletions docs/src/content/docs/reference/cli/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Options:
-ocl, --out-changelog <string> output file for changelogs
-prc, --pull-request-comment <string> create comment on a pull request.
-prd, --pull-request-description <string> upsert comment on a pull request description.
-prr, --pull-request-reviews create pull request reviews from annotations
-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 expanded prompt
Expand Down
8 changes: 5 additions & 3 deletions genaisrc/pr-describe.genai.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const { stdout: changes } = await host.exec("git", [
"main",
"--",
":!**/genaiscript.d.ts",
":!genaisrc/*",
":!.github/*",
":!.vscode/*",
":!yarn.lock",
])
Expand All @@ -19,13 +21,13 @@ $`You are an expert software developer and architect.

## Task

- Describe the changes in GIT_DIFF in a way that a software engineer will understand.
- Describe a summary of the changes in GIT_DIFF in a way that a software engineer will understand.

## Instructions

- use bullet points to list the changes
- use emojis to make the description more engaging
- if needed inline code snippets can be used. The code snippets should be taken
from the changes in GIT_DIFF.
- focus on the most important changes
- ignore comments about imports (like added, remove, changed, etc.)

`
39 changes: 39 additions & 0 deletions genaisrc/pr-review-commit.genai.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
script({
model: "openai:gpt-4",
files: [],
title: "pull request commit review",
system: [
"system",
"system.typescript",
"system.fs_find_files",
"system.fs_read_file",
"system.annotations",
],
})
const { stdout: changes } = await host.exec("git", [
"diff",
"HEAD^",
"HEAD",
"--",
"**.ts",
])

def("GIT_DIFF", changes, { maxTokens: 20000, lineNumbers: false })

$`You are an expert software developer and architect. You are
an expert in software reliability, security, scalability, and performance.

## Task

Review the changes in GIT_DIFF which contains the changes of the last commit in the pull request branch.
Provide feedback to the author using annotations.

Think step by step and for each annotation explain your result.

- report errors only, ignore notes and warnings
- only report issues you are absolutely certain about
- use a friendly tone
- use emojis
- read the full source code of the files if you need more context
- only report issues about code in GIT_DIFF
`
14 changes: 8 additions & 6 deletions genaisrc/pr-review.genai.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ script({
"system.typescript",
"system.fs_find_files",
"system.fs_read_file",
"system.annotations"
],
})

const { stdout: changes } = await host.exec("git", [
"diff",
"main",
"--",
":!**/genaiscript.d.ts",
":!genaisrc/*",
":!.github/*",
":!.vscode/*",
":!yarn.lock",
])
Expand All @@ -26,10 +28,10 @@ an expert in software reliability, security, scalability, and performance.

## Task

Review the changes in GIT_DIFF and provide feedback to the author using annotations.
GIT_DIFF contains the changes the pull request branch.

Provide a high level review of the changes in the pull request. Do not enter into details.

If the changes look good, respond LGTM (Looks Good To Me). If you have any concerns, provide a brief description of the concerns.

- report errors only, ignore notes and warnings.
- use a friendly tone
- use emojis
- read the full source code of the files if you need more context
`
6 changes: 5 additions & 1 deletion packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export async function cli() {
program.on("option:quiet", () => setQuiet(true))

program
.command("run")
.command("run", { isDefault: true })
.description("Runs a GenAIScript against files.")
.arguments("<script> [files...]")
.option("-ef, --excluded-files <string...>", "excluded files")
Expand All @@ -109,6 +109,10 @@ export async function cli() {
"-prd, --pull-request-description <string>",
"upsert comment on a pull request description."
)
.option(
"-prr, --pull-request-reviews",
"create pull request reviews from annotations"
)
.option("-j, --json", "emit full JSON response to output")
.option("-y, --yaml", "emit full YAML response to output")
.option(
Expand Down
45 changes: 16 additions & 29 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,
githubCreatePullRequestReviews,
githubCreateIssueComment,
} from "genaiscript-core"
import { capitalize } from "inflection"
import { basename, resolve, join, relative } from "node:path"
Expand All @@ -62,6 +61,7 @@ export async function runScript(
outChangelogs: string
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pullRequestComment: string
pullRequestDescription: string
pullRequestReviews: boolean
outData: string
label: string
temperature: string
Expand Down Expand Up @@ -92,6 +92,7 @@ export async function runScript(
const outChangelogs = options.outChangelogs
const pullRequestComment = options.pullRequestComment
const pullRequestDescription = options.pullRequestDescription
const pullRequestReviews = options.pullRequestReviews
const outData = options.outData
const label = options.label
const temperature = normalizeFloat(options.temperature)
Expand Down Expand Up @@ -365,47 +366,33 @@ ${Array.from(files)
}
}

if (pullRequestReviews && res.annotations?.length) {
const info = parseGHTokenFromEnv(process.env)
if (info.repository && info.issue)
await githubCreatePullRequestReviews(script, info, res.annotations)
}

if (pullRequestComment && res.text) {
const info = parseGHTokenFromEnv(process.env)
if (info.repository && info.issue) {
const ghres = await githubCreateIssueComment(
await githubCreateIssueComment(
script,
info,
prettifyMarkdown(
`${res.text}\n\n> generated by genaiscript ${script.id}.`
),
res.text,
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 (pullRequestDescription && res.text) {
const info = parseGHTokenFromEnv(process.env)
if (info.repository && info.issue) {
const ghres = await githubUpsetPullRequest(
await githubUpsetPullRequest(
script,
info,
prettifyMarkdown(
`${res.text}\n\n> generated by genaiscript ${script.id}.`
),
res.text,
pullRequestDescription
)
if (!ghres.updated) {
logError(
`pull request ${info.repository}/pull/${info.issue} update failed ${ghres.statusText}`
)
process.exit(CONFIGURATION_ERROR_CODE)
}
logVerbose(
`pull request ${info.repository}/pull/${info.issue} updated`
)
}
}
// final fail
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/annotations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { host } from "./host"
import { relativePath } from "./util"

const GITHUB_ANNOTATIONS_RX =
/^::(?<severity>notice|warning|error)\s*file=(?<file>[^,]+),\s*line=(?<line>\d+),\s*endLine=(?<endLine>\d+)\s*::(?<message>.*)$/gim
Expand All @@ -24,9 +25,12 @@ export function parseAnnotations(text: string): Diagnostic[] {
text?.replace(
GITHUB_ANNOTATIONS_RX,
(_, severity, file, line, endLine, message) => {
const filename = /^[^\/]/.test(file)
? host.resolvePath(projectFolder, file)
: file
const filename = relativePath(
projectFolder,
/^[^\/]/.test(file)
? host.resolvePath(projectFolder, file)
: file
)
const annotation: Diagnostic = {
severity: sevMap[severity] || severity,
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
filename,
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 @@ -170,4 +170,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_COMMENT_ID_NONE = "none"
export const GITHUB_COMMENT_ID_NONE = "none"
export const GITHUB_TOKEN = "GITHUB_TOKEN"
3 changes: 3 additions & 0 deletions packages/core/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export async function createFetch(
retryDelay = FETCH_RETRY_DEFAULT_DEFAULT,
maxDelay = FETCH_RETRY_MAX_DELAY_DEFAULT,
} = options || {}

if (!retryOn?.length) return crossFetch

const fetchRetry = await wrapFetch(crossFetch, {
retryOn,
retries,
Expand Down
Loading
Loading