Skip to content

Commit

Permalink
Improve handling of rm or mv files
Browse files Browse the repository at this point in the history
  • Loading branch information
robertjdominguez committed Jan 9, 2024
1 parent ea961d2 commit 7150a7b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 42 deletions.
29 changes: 16 additions & 13 deletions dist/github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ const getDiff = async (prNumber, owner, repo) => {
},
});
// We'll have to convert the diff to a string, then we can return it
const diffString = diff.toString();
let diffString = diff.toString();
// We'll try adding a note here to see if it drives the point home to GPT
diffString = `Diff for PR #${prNumber}:\n\n${diffString}`;
console.log(`✅ Got diff for PR #${prNumber}`);
return diffString;
};
Expand All @@ -130,19 +132,20 @@ const getChangedFiles = (diff) => {
exports.getChangedFiles = getChangedFiles;
// We'll also need to get the whole file using the files changed from
async function getFileContent(path, owner, repo) {
let content = '';
// loop over the array of files
let content = 'Original files before the PR:\n\n';
for (let i = 0; i < path.length; i++) {
// get the file content
const { data } = await exports.github.repos.getContent({
owner,
repo,
path: path[i],
});
// decode the file content
const decodedContent = Buffer.from(data.content, 'base64').toString();
// add the decoded content to the content string
content += decodedContent;
try {
const { data } = await exports.github.repos.getContent({
owner,
repo,
path: path[i],
});
const decodedContent = Buffer.from(data.content, 'base64').toString();
content += decodedContent;
}
catch (error) {
console.error(`Skipping new file as it doesn't exist in the trunk yet`);
}
}
console.log(`✅ Got file(s) contents`);
return content;
Expand Down
34 changes: 20 additions & 14 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ const getDiff = async (prNumber, owner, repo) => {
},
});
// We'll have to convert the diff to a string, then we can return it
const diffString = diff.toString();
let diffString = diff.toString();
// We'll try adding a note here to see if it drives the point home to GPT
diffString = `Diff for PR #${prNumber}:\n\n${diffString}`;
console.log(`✅ Got diff for PR #${prNumber}`);
return diffString;
};
Expand All @@ -137,19 +139,20 @@ const getChangedFiles = (diff) => {
exports.getChangedFiles = getChangedFiles;
// We'll also need to get the whole file using the files changed from
async function getFileContent(path, owner, repo) {
let content = '';
// loop over the array of files
let content = 'Original files before the PR:\n\n';
for (let i = 0; i < path.length; i++) {
// get the file content
const { data } = await exports.github.repos.getContent({
owner,
repo,
path: path[i],
});
// decode the file content
const decodedContent = Buffer.from(data.content, 'base64').toString();
// add the decoded content to the content string
content += decodedContent;
try {
const { data } = await exports.github.repos.getContent({
owner,
repo,
path: path[i],
});
const decodedContent = Buffer.from(data.content, 'base64').toString();
content += decodedContent;
}
catch (error) {
console.error(`Skipping new file as it doesn't exist in the trunk yet`);
}
}
console.log(`✅ Got file(s) contents`);
return content;
Expand Down Expand Up @@ -270,7 +273,7 @@ const openai = new openAi({
});
// This wil generate our prompt using the diff, assertion, and whole file
const generatePrompt = (diff, assertion, file) => {
const comboPrompt = `As a senior engineer, you're tasked with reviewing a documentation PR. Your review will be conducted through two distinct lenses, both centered around an assertion related to usability. The first lens will focus on examining the diff itself — providing targeted feedback on what the PR author actually contributed. The second lens will compare the diff to the entire set of changed files, assessing how the contribution fits within the larger context in relation to the usability assertion. For each lens, provide feedback and determine if the usability assertion is satisfied. You should speak directly to the author and refer to them in second person. Your output should be a JSON-formatted array with two objects and not be wrapped in backticks with the json declaration. Each object should contain the following properties: 'satisfied' (either a ✅ or ❌ to indicate if the assertion is met), 'scope' (either 'Diff' or 'Integrated'), and 'feedback' (a string providing your targeted feedback for that lens). Here's the assertion: ${assertion}\n\nHere's the diff:\n\n${diff}\n\nHere's the whole files:\n\n${file}\n\n`;
const comboPrompt = `As a senior engineer, you're tasked with reviewing a documentation PR. Your review will be conducted through two distinct lenses, both centered around an assertion related to usability. The first lens will focus on examining the diff itself — providing targeted feedback on what the PR author actually contributed. The second lens will compare the diff to the entire set of changed files, assessing how the contribution fits within the larger context in relation to the usability assertion. For each lens, provide feedback and determine if the usability assertion is satisfied. You should speak directly to the author and refer to them in second person. Your output should be a JSON-formatted array with two objects. Each object should contain the following properties: 'satisfied' (either a ✅ or ❌ to indicate if the assertion is met), 'scope' (either 'Diff' or 'Integrated'), and 'feedback' (a string providing your targeted feedback for that lens). Here's the assertion: ${assertion}\n\nHere's the diff:\n\n${diff}\n\nHere's the original files:\n\n${file}\n\nBear in mind that some of the files may have been renamed. Remember, do not wrap the JSON in a code block.`;
return comboPrompt;
};
exports.generatePrompt = generatePrompt;
Expand Down Expand Up @@ -312,6 +315,9 @@ const testAssertion = async (prompt) => {
exports.testAssertion = testAssertion;
// We decided to send things back as JSON so we can manipulate the data in the response we'll be sending back to GitHub
const writeAnalysis = (analysis) => {
// We've still got to double-check because ChatGPT will sometimes return a string that's not valid JSON by wrapping it in code blocks
const regex = /^```(json)?/gm;
analysis = analysis.replace(regex, '');
const analysisJSON = JSON.parse(analysis);
let message = `## DX: Assertion Testing\n\n`;
const feedback = analysisJSON.map((item) => {
Expand Down
5 changes: 4 additions & 1 deletion dist/open_ai/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const openai = new openAi({
});
// This wil generate our prompt using the diff, assertion, and whole file
const generatePrompt = (diff, assertion, file) => {
const comboPrompt = `As a senior engineer, you're tasked with reviewing a documentation PR. Your review will be conducted through two distinct lenses, both centered around an assertion related to usability. The first lens will focus on examining the diff itself — providing targeted feedback on what the PR author actually contributed. The second lens will compare the diff to the entire set of changed files, assessing how the contribution fits within the larger context in relation to the usability assertion. For each lens, provide feedback and determine if the usability assertion is satisfied. You should speak directly to the author and refer to them in second person. Your output should be a JSON-formatted array with two objects and not be wrapped in backticks with the json declaration. Each object should contain the following properties: 'satisfied' (either a ✅ or ❌ to indicate if the assertion is met), 'scope' (either 'Diff' or 'Integrated'), and 'feedback' (a string providing your targeted feedback for that lens). Here's the assertion: ${assertion}\n\nHere's the diff:\n\n${diff}\n\nHere's the whole files:\n\n${file}\n\n`;
const comboPrompt = `As a senior engineer, you're tasked with reviewing a documentation PR. Your review will be conducted through two distinct lenses, both centered around an assertion related to usability. The first lens will focus on examining the diff itself — providing targeted feedback on what the PR author actually contributed. The second lens will compare the diff to the entire set of changed files, assessing how the contribution fits within the larger context in relation to the usability assertion. For each lens, provide feedback and determine if the usability assertion is satisfied. You should speak directly to the author and refer to them in second person. Your output should be a JSON-formatted array with two objects. Each object should contain the following properties: 'satisfied' (either a ✅ or ❌ to indicate if the assertion is met), 'scope' (either 'Diff' or 'Integrated'), and 'feedback' (a string providing your targeted feedback for that lens). Here's the assertion: ${assertion}\n\nHere's the diff:\n\n${diff}\n\nHere's the original files:\n\n${file}\n\nBear in mind that some of the files may have been renamed. Remember, do not wrap the JSON in a code block.`;
return comboPrompt;
};
exports.generatePrompt = generatePrompt;
Expand Down Expand Up @@ -79,6 +79,9 @@ const testAssertion = async (prompt) => {
exports.testAssertion = testAssertion;
// We decided to send things back as JSON so we can manipulate the data in the response we'll be sending back to GitHub
const writeAnalysis = (analysis) => {
// We've still got to double-check because ChatGPT will sometimes return a string that's not valid JSON by wrapping it in code blocks
const regex = /^```(json)?/gm;
analysis = analysis.replace(regex, '');
const analysisJSON = JSON.parse(analysis);
let message = `## DX: Assertion Testing\n\n`;
const feedback = analysisJSON.map((item) => {
Expand Down
28 changes: 15 additions & 13 deletions src/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ export const getDiff = async (prNumber: number, owner: string, repo: string): Pr
});

// We'll have to convert the diff to a string, then we can return it
const diffString = diff.toString();
let diffString = diff.toString();
// We'll try adding a note here to see if it drives the point home to GPT
diffString = `Diff for PR #${prNumber}:\n\n${diffString}`;
console.log(`✅ Got diff for PR #${prNumber}`);
return diffString;
};
Expand All @@ -113,19 +115,19 @@ export const getChangedFiles = (diff: string): string[] => {

// We'll also need to get the whole file using the files changed from
export async function getFileContent(path: string[], owner: string, repo: string) {
let content: string = '';
// loop over the array of files
let content: string = 'Original files before the PR:\n\n';
for (let i = 0; i < path.length; i++) {
// get the file content
const { data }: any = await github.repos.getContent({
owner,
repo,
path: path[i],
});
// decode the file content
const decodedContent = Buffer.from(data.content, 'base64').toString();
// add the decoded content to the content string
content += decodedContent;
try {
const { data }: any = await github.repos.getContent({
owner,
repo,
path: path[i],
});
const decodedContent = Buffer.from(data.content, 'base64').toString();
content += decodedContent;
} catch (error) {
console.error(`Skipping new file as it doesn't exist in the trunk yet`);
}
}
console.log(`✅ Got file(s) contents`);
return content;
Expand Down
5 changes: 4 additions & 1 deletion src/open_ai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const openai = new openAi({

// This wil generate our prompt using the diff, assertion, and whole file
export const generatePrompt = (diff: string, assertion: string | null, file: string): string => {
const comboPrompt = `As a senior engineer, you're tasked with reviewing a documentation PR. Your review will be conducted through two distinct lenses, both centered around an assertion related to usability. The first lens will focus on examining the diff itself — providing targeted feedback on what the PR author actually contributed. The second lens will compare the diff to the entire set of changed files, assessing how the contribution fits within the larger context in relation to the usability assertion. For each lens, provide feedback and determine if the usability assertion is satisfied. You should speak directly to the author and refer to them in second person. Your output should be a JSON-formatted array with two objects and not be wrapped in backticks with the json declaration. Each object should contain the following properties: 'satisfied' (either a ✅ or ❌ to indicate if the assertion is met), 'scope' (either 'Diff' or 'Integrated'), and 'feedback' (a string providing your targeted feedback for that lens). Here's the assertion: ${assertion}\n\nHere's the diff:\n\n${diff}\n\nHere's the whole files:\n\n${file}\n\n`;
const comboPrompt = `As a senior engineer, you're tasked with reviewing a documentation PR. Your review will be conducted through two distinct lenses, both centered around an assertion related to usability. The first lens will focus on examining the diff itself — providing targeted feedback on what the PR author actually contributed. The second lens will compare the diff to the entire set of changed files, assessing how the contribution fits within the larger context in relation to the usability assertion. For each lens, provide feedback and determine if the usability assertion is satisfied. You should speak directly to the author and refer to them in second person. Your output should be a JSON-formatted array with two objects. Each object should contain the following properties: 'satisfied' (either a ✅ or ❌ to indicate if the assertion is met), 'scope' (either 'Diff' or 'Integrated'), and 'feedback' (a string providing your targeted feedback for that lens). Here's the assertion: ${assertion}\n\nHere's the diff:\n\n${diff}\n\nHere's the original files:\n\n${file}\n\nBear in mind that some of the files may have been renamed. Remember, do not wrap the JSON in a code block.`;
return comboPrompt;
};

Expand Down Expand Up @@ -54,6 +54,9 @@ export const testAssertion = async (prompt: string): Promise<string | null> => {

// We decided to send things back as JSON so we can manipulate the data in the response we'll be sending back to GitHub
export const writeAnalysis = (analysis: string): string => {
// We've still got to double-check because ChatGPT will sometimes return a string that's not valid JSON by wrapping it in code blocks
const regex = /^```(json)?/gm;
analysis = analysis.replace(regex, '');
const analysisJSON = JSON.parse(analysis);
let message = `## DX: Assertion Testing\n\n`;
const feedback = analysisJSON.map((item: any) => {
Expand Down

0 comments on commit 7150a7b

Please sign in to comment.