Skip to content

Commit

Permalink
action.yml: new subtle mode
Browse files Browse the repository at this point in the history
  • Loading branch information
thypon committed Apr 19, 2024
1 parent 83b1802 commit cd3dee5
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,23 @@ runs:
repo: context.repo.repo,
prnum: context.issue.number,
max_tokens: '2048',
subtle_mode: 'false'
}, config, properties, inputs)
// convert to numbers some options
options.debounce_time = parseFloat(options.debounce_time, 10)
options.amplification = parseFloat(options.amplification, 10)
options.prnum = parseFloat(options.prnum, 10)
options.max_tokens = parseFloat(options.max_tokens, 10)
options.subtle_mode = options.subtle_mode === 'true'
const { default: explainPatch } = options.anthropic_api_key
? await import('${{ github.action_path }}/src/anthropicExplainPatch.js')
: await import('${{ github.action_path }}/src/openaiExplainPatch.js')
const { default: submitReview } = await import('${{ github.action_path }}/src/submitReview.js')
const { default: submitReview } = options.subtle_mode
? await import('${{ github.action_path }}/src/subtleSubmitReview.js')
: await import('${{ github.action_path }}/src/submitReview.js')
options.key = options.anthropic_api_key || options.openai_api_key
options.models = options.anthropic_api_key ? options.anthropic_models : options.openai_models
Expand Down
86 changes: 86 additions & 0 deletions src/subtleSubmitReview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
function isOlderThanXHours (date, hours) {
// Get the current date and time
const now = new Date()

// Calculate the time difference in milliseconds
const timeDiff = now - new Date(date)

// Convert the time difference to hours
const timeDiffInHours = timeDiff / (1000 * 60 * 60)

// Check if the time difference is greater than the specified number of hours
return timeDiffInHours > hours
}

function escapeRegex (string) {
return string.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&')
}

export default async function subtleSubmitReview ({
owner, repo, prnum, watermark, debounceTime,
explainPatch = async () => 'no explanation provided<!-- Generated by STUB -->',
githubToken = null,
github = null,
debug = false
}) {
if (!github && githubToken) {
const { Octokit } = await import('@octokit/core')

github = new Octokit({ auth: githubToken })
}

if (debug) { console.log(`submitReview ${owner} ${repo} ${prnum}`) }

if (!github && !githubToken) {
throw new Error('You must provide a githubToken to use this function')
}

// check if the PR description already contains an AI review
const query = `query($owner:String!, $name:String!, $prnumber:Int!) {
repository(owner:$owner, name:$name) {
pullRequest(number:$prnumber) {
id
body
}
}
}`
const variables = {
owner,
name: repo,
prnumber: prnum
}
const msg = (await github.graphql(query, variables)).repository.pullRequest

// debounce if the PR description contains a watermark and the debounce time is not expired yet
if (msg.body.includes(watermark) && !isOlderThanXHours(msg.updatedAt, debounceTime)) {
throw new Error('debounce')
}

// TODO: modify the PR description in place, to include the AI summarization (2 cases)
// 1. if the PR AI description is empty, just add the AI summarization at the end
// 2. if the PR AI description is not empty, delete the previous AI summarization and add the new one
const begin = escapeRegex('<details><summary>AI Review</summary>\n\n') + escapeRegex(watermark)
const end = escapeRegex('<!-- Generated by ') + '\\w+' + escapeRegex(' --></details>')
const re = new RegExp(`${begin}.*${end}`, 's')

const newExplaination = '<details><summary>AI Review</summary>' + '\n\n' + watermark + '\n\n' + await explainPatch() + '</details>'

const newBody = msg.body.match(re)
? msg.body.replace(re, newExplaination)
: msg.body + '\n\n' + newExplaination

const updatePRBodyQuery = `mutation UpdatePRDescription($prId: ID!, $body: String!) {
updatePullRequest(input:{pullRequestId:$prId, body:$body}) {
pullRequest {
id
body
}
}
}`
const updateVariables = {
prId: msg.id,
body: newBody
}

await github.graphql(updatePRBodyQuery, updateVariables)
}

0 comments on commit cd3dee5

Please sign in to comment.