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

Use the description if the author hasn't added an assertion #3

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**.env
node_modules
coverage
coverage
.idea
18 changes: 17 additions & 1 deletion dist/github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFileContent = exports.getChangedFiles = exports.getDiff = exports.getAssertion = exports.getSinglePR = exports.getPullRequests = exports.getRepo = exports.testConnection = exports.github = void 0;
exports.getFileContent = exports.getChangedFiles = exports.getDiff = exports.getAssertion = exports.getDescription = exports.getSinglePR = exports.getPullRequests = exports.getRepo = exports.testConnection = exports.github = void 0;
const dotenv_1 = __importDefault(require("dotenv"));
const core = __importStar(require("@actions/core"));
const rest_1 = require("@octokit/rest");
Expand Down Expand Up @@ -87,6 +87,22 @@ const getSinglePR = async (owner, repo, prNumber) => {
}
};
exports.getSinglePR = getSinglePR;
// If we can get a PR, we can parse the description and try to isolate the description
const getDescription = (description) => {
// find everything after ## Description and before the next ## heading
const regexDescription = /(?<=## Description)(.*?)(?=##|$)/gm;
const regexBetweenComments = /(?<=-->)(.*?)(?=<!--)/gm;
const descriptionWithComments = regexDescription.exec(description);
if (descriptionWithComments) {
const DescriptionActual = regexBetweenComments.exec(descriptionWithComments[1]);
if (DescriptionActual) {
console.log(`✅ Got actual description: ${DescriptionActual[1]}`);
return DescriptionActual[1];
}
}
return null;
};
exports.getDescription = getDescription;
// If we can get a PR, we can parse the description and isolate the assertion using the comments
const getAssertion = async (description) => {
// find everything in between <!-- DX:Assertion-start --> and <!-- DX:Assertion-end -->
Expand Down
52 changes: 37 additions & 15 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getFileContent = exports.getChangedFiles = exports.getDiff = exports.getAssertion = exports.getSinglePR = exports.getPullRequests = exports.getRepo = exports.testConnection = exports.github = void 0;
exports.getFileContent = exports.getChangedFiles = exports.getDiff = exports.getAssertion = exports.getDescription = exports.getSinglePR = exports.getPullRequests = exports.getRepo = exports.testConnection = exports.github = void 0;
const dotenv_1 = __importDefault(__nccwpck_require__(2437));
const core = __importStar(__nccwpck_require__(2186));
const rest_1 = __nccwpck_require__(5375);
Expand Down Expand Up @@ -94,6 +94,22 @@ const getSinglePR = async (owner, repo, prNumber) => {
}
};
exports.getSinglePR = getSinglePR;
// If we can get a PR, we can parse the description and try to isolate the description
const getDescription = (description) => {
// find everything after ## Description and before the next ## heading
const regexDescription = /(?<=## Description)(.*?)(?=##|$)/gm;
const regexBetweenComments = /(?<=-->)(.*?)(?=<!--)/gm;
const descriptionWithComments = regexDescription.exec(description);
if (descriptionWithComments) {
const DescriptionActual = regexBetweenComments.exec(descriptionWithComments[1]);
if (DescriptionActual) {
console.log(`✅ Got actual description: ${DescriptionActual[1]}`);
return DescriptionActual[1];
}
}
return null;
};
exports.getDescription = getDescription;
// If we can get a PR, we can parse the description and isolate the assertion using the comments
const getAssertion = async (description) => {
// find everything in between <!-- DX:Assertion-start --> and <!-- DX:Assertion-end -->
Expand Down Expand Up @@ -200,29 +216,35 @@ const github_1 = __nccwpck_require__(4321);
const open_ai_1 = __nccwpck_require__(2580);
dotenv_1.default.config();
// Our configuration variables using GitHub Actions for production and dotenv for local development
const prNumber = parseInt(core.getInput('PR_NUMBER') || process.env.PR_NUMBER);
const org = core.getInput('GITHUB_ORG') || process.env.GITHUB_ORG;
const repo = core.getInput('GITHUB_REPOSITORY') || process.env.GITHUB_REPOSITORY;
const prNumber = parseInt(core.getInput('PR_NUMBER') || process.env.PR_NUMBER || '');
const org = core.getInput('GITHUB_ORG') || process.env.GITHUB_ORG || '';
const repo = core.getInput('GITHUB_REPOSITORY') || process.env.GITHUB_REPOSITORY || '';
// We'll need to parse the repo variable to remove the owner and the / from the string
const repoName = repo.split('/')[1];
async function main() {
const PR = await (0, github_1.getSinglePR)(org, repoName, prNumber);
const assertion = await (0, github_1.getAssertion)(PR?.body ?? '');
if (assertion?.length === 0 || assertion === null) {
console.log('No assertion found');
core.setFailed('No assertion found');
return;
}
else {
try {
const PR = await (0, github_1.getSinglePR)(org, repoName, prNumber);
// Try to use actual assertion, if not use description
let assertionToUse = await (0, github_1.getAssertion)(PR?.body ?? '');
if (!assertionToUse) {
assertionToUse = (0, github_1.getDescription)(PR?.body ?? '');
if (!assertionToUse) {
core.setFailed('No assertion or description found');
return;
}
}
const diff = await (0, github_1.getDiff)(prNumber, org, repoName);
const changedFiles = (0, github_1.getChangedFiles)(diff);
const file = await (0, github_1.getFileContent)(changedFiles, org, repoName);
const prompt = (0, open_ai_1.generatePrompt)(diff, assertion, file);
const prompt = (0, open_ai_1.generatePrompt)(diff, assertionToUse, file);
const rawAnalysis = await (0, open_ai_1.testAssertion)(prompt);
const analysis = (0, open_ai_1.writeAnalysis)(rawAnalysis);
console.log(analysis);
core.setOutput('analysis', analysis);
return analysis;
core.info('Analysis completed successfully');
}
catch (error) {
core.setFailed(`Error during execution: ${error.message}`);
}
}
main();
Expand Down Expand Up @@ -335,7 +357,7 @@ const testAssertion = async (prompt) => {
];
try {
const chatCompletion = await openai.chat.completions.create({
model: 'gpt-4-1106-preview',
model: 'gpt-4o',
messages: conversation,
response_format: { type: 'json_object' },
});
Expand Down
2 changes: 1 addition & 1 deletion dist/open_ai/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const testAssertion = async (prompt) => {
];
try {
const chatCompletion = await openai.chat.completions.create({
model: 'gpt-4-1106-preview',
model: 'gpt-4o',
messages: conversation,
response_format: { type: 'json_object' },
});
Expand Down
18 changes: 18 additions & 0 deletions src/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ export const getSinglePR = async (
}
};


// If we can get a PR, we can parse the description and try to isolate the description
export const getDescription = (description: string): string | null => {
// find everything after ## Description and before the next ## heading
const regexDescription = /(?<=## Description)(.*?)(?=##|$)/gm
const regexBetweenComments = /(?<=-->)(.*?)(?=<!--)/gm
const descriptionWithComments = regexDescription.exec(description);
if (descriptionWithComments) {
const DescriptionActual = regexBetweenComments.exec(descriptionWithComments[1]);
if (DescriptionActual) {
console.log(`✅ Got actual description: ${DescriptionActual[1]}`);
return DescriptionActual[1];
}
}
return null;
};


// If we can get a PR, we can parse the description and isolate the assertion using the comments
export const getAssertion = async (description: string): Promise<string | null> => {
// find everything in between <!-- DX:Assertion-start --> and <!-- DX:Assertion-end -->
Expand Down
38 changes: 23 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
import dotenv from 'dotenv';
import * as core from '@actions/core';
import { getSinglePR, getAssertion, getDiff, getChangedFiles, getFileContent } from './github';
import { getSinglePR, getAssertion, getDiff, getChangedFiles, getFileContent, getDescription } from './github';
import { generatePrompt, testAssertion, writeAnalysis, openAiFeedback } from './open_ai';

dotenv.config();

// Our configuration variables using GitHub Actions for production and dotenv for local development
const prNumber: number = parseInt(core.getInput('PR_NUMBER') || (process.env.PR_NUMBER as string));
const org: string = core.getInput('GITHUB_ORG') || (process.env.GITHUB_ORG as string);
const repo: string = core.getInput('GITHUB_REPOSITORY') || (process.env.GITHUB_REPOSITORY as string);
const prNumber: number = parseInt(core.getInput('PR_NUMBER') || process.env.PR_NUMBER || '');
const org: string = core.getInput('GITHUB_ORG') || process.env.GITHUB_ORG || '';
const repo: string = core.getInput('GITHUB_REPOSITORY') || process.env.GITHUB_REPOSITORY || '';

// We'll need to parse the repo variable to remove the owner and the / from the string
const repoName = repo.split('/')[1];

async function main() {
const PR = await getSinglePR(org, repoName, prNumber);
const assertion = await getAssertion(PR?.body ?? '');
if (assertion?.length === 0 || assertion === null) {
console.log('No assertion found');
core.setFailed('No assertion found');
return;
} else {
try {
const PR = await getSinglePR(org, repoName, prNumber);

// Try to use actual assertion, if not use description
let assertionToUse: string | null = await getAssertion(PR?.body ?? '');
if (!assertionToUse) {
assertionToUse = getDescription(PR?.body ?? '');
if (!assertionToUse) {
core.setFailed('No assertion or description found');
return;
}
}

const diff: string = await getDiff(prNumber, org, repoName);
const changedFiles = getChangedFiles(diff);
const changedFiles: string[] = getChangedFiles(diff);
const file: any = await getFileContent(changedFiles, org, repoName);
const prompt: string = generatePrompt(diff, assertion, file);
const prompt: string = generatePrompt(diff, assertionToUse, file);
const rawAnalysis = await testAssertion(prompt);
const analysis = writeAnalysis(rawAnalysis);
const analysis: string = writeAnalysis(rawAnalysis);
console.log(analysis);
core.setOutput('analysis', analysis);
return analysis;
core.info('Analysis completed successfully');
} catch (error) {
core.setFailed(`Error during execution: ${(error as Error).message}`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/open_ai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const testAssertion = async (prompt: string): Promise<openAiFeedback | nu

try {
const chatCompletion = await openai.chat.completions.create({
model: 'gpt-4-1106-preview',
model: 'gpt-4o',
messages: conversation,
response_format: { type: 'json_object' },
});
Expand Down