-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |