Skip to content

Commit

Permalink
Prepare release v0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
ahusiev committed May 10, 2023
1 parent 7283885 commit eb956e1
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 60 deletions.
24 changes: 10 additions & 14 deletions dist/config/errorsConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.ErrorMessage = void 0;
var ErrorMessage;
(function (ErrorMessage) {
ErrorMessage[ErrorMessage["No_GitHub_Token"] = 0] = "No_GitHub_Token";
ErrorMessage[ErrorMessage["No_OpenAi_Token"] = 1] = "No_OpenAi_Token";
ErrorMessage[ErrorMessage["No_PullRequest_In_Context"] = 2] = "No_PullRequest_In_Context";
ErrorMessage[ErrorMessage["No_Patch_For_OpenAi_Suggestion"] = 3] = "No_Patch_For_OpenAi_Suggestion";
ErrorMessage[ErrorMessage["No_Changed_Files_In_PullRequest"] = 4] = "No_Changed_Files_In_PullRequest";
ErrorMessage[ErrorMessage["Not_Match_Status_Of_Changed_File"] = 5] = "Not_Match_Status_Of_Changed_File";
ErrorMessage[ErrorMessage["No_Patch_File"] = 6] = "No_Patch_File";
ErrorMessage[ErrorMessage["MISSING_GITHUB_TOKEN"] = 0] = "MISSING_GITHUB_TOKEN";
ErrorMessage[ErrorMessage["MISSING_OPENAI_TOKEN"] = 1] = "MISSING_OPENAI_TOKEN";
ErrorMessage[ErrorMessage["NO_PULLREQUEST_IN_CONTEXT"] = 2] = "NO_PULLREQUEST_IN_CONTEXT";
ErrorMessage[ErrorMessage["MISSING_PATCH_FOR_OPENAI_SUGGESTION"] = 3] = "MISSING_PATCH_FOR_OPENAI_SUGGESTION";
ErrorMessage[ErrorMessage["NO_CHANGED_FILES_IN_PULL_REQUEST"] = 4] = "NO_CHANGED_FILES_IN_PULL_REQUEST";
})(ErrorMessage || (ErrorMessage = {}));
exports.ErrorMessage = ErrorMessage;
const errorsConfig = {
[ErrorMessage.No_GitHub_Token]: 'A GitHub token must be provided to use the Octokit API.',
[ErrorMessage.No_OpenAi_Token]: 'An OpenAI API token must be provided to use the OpenAI API. Make sure you have add a token with a name OPENAI_API_KEY in https://github.com/{user}/{repository}/settings/secrets/actions',
[ErrorMessage.No_PullRequest_In_Context]: 'Pull request data must be provided, check payload and try again.',
[ErrorMessage.No_Patch_For_OpenAi_Suggestion]: 'The patch must be exist to provide a suggestions with Open AI',
[ErrorMessage.No_Changed_Files_In_PullRequest]: 'There are not any changed files in provided pull request',
[ErrorMessage.Not_Match_Status_Of_Changed_File]: 'The status of the file should be one of ["added", "modified", "renamed", "changed"], provided status is:',
[ErrorMessage.No_Patch_File]: 'Patch file must be provided',
[ErrorMessage.MISSING_GITHUB_TOKEN]: 'A GitHub token must be provided to use the Octokit API.',
[ErrorMessage.MISSING_OPENAI_TOKEN]: 'An OpenAI API token must be provided to use the OpenAI API. Make sure you have add a token with a name OPENAI_API_KEY in https://github.com/{user}/{repository}/settings/secrets/actions',
[ErrorMessage.NO_PULLREQUEST_IN_CONTEXT]: 'Pull request data must be provided, check payload and try again.',
[ErrorMessage.MISSING_PATCH_FOR_OPENAI_SUGGESTION]: 'The patch must be exist to provide a suggestions with Open AI',
[ErrorMessage.NO_CHANGED_FILES_IN_PULL_REQUEST]: 'There are not any changed files in provided pull request',
};
exports.default = errorsConfig;
2 changes: 1 addition & 1 deletion dist/config/promptsConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ var Prompt;
})(Prompt || (Prompt = {}));
exports.Prompt = Prompt;
const promptsConfig = {
[Prompt.Check_Patch]: 'Based on the patch provide a list of suggestions how to improve the code with examples.',
[Prompt.Check_Patch]: 'You now assume the role of a code reviewer. Based on the patch provide a list of suggestions how to improve the code with examples according to coding standards and best practices.',
};
exports.default = promptsConfig;
4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

77 changes: 36 additions & 41 deletions dist/services/commentOnPullRequestService.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,42 @@ const errorsConfig_1 = __importStar(require("../config/errorsConfig"));
const promptsConfig_1 = __importStar(require("../config/promptsConfig"));
class CommentOnPullRequestService {
constructor() {
var _a, _b, _c, _d, _e, _f;
var _a, _b, _c;
if (!process.env.GITHUB_TOKEN) {
throw new Error(errorsConfig_1.default[errorsConfig_1.ErrorMessage.No_GitHub_Token]);
throw new Error(errorsConfig_1.default[errorsConfig_1.ErrorMessage.MISSING_GITHUB_TOKEN]);
}
if (!process.env.OPENAI_API_KEY) {
throw new Error(errorsConfig_1.default[errorsConfig_1.ErrorMessage.No_OpenAi_Token]);
throw new Error(errorsConfig_1.default[errorsConfig_1.ErrorMessage.MISSING_OPENAI_TOKEN]);
}
if (!github_1.context.payload.pull_request) {
throw new Error(errorsConfig_1.default[errorsConfig_1.ErrorMessage.No_PullRequest_In_Context]);
throw new Error(errorsConfig_1.default[errorsConfig_1.ErrorMessage.NO_PULLREQUEST_IN_CONTEXT]);
}
this._octokitApi = (0, github_1.getOctokit)(process.env.GITHUB_TOKEN);
this._openAiApi = new openai_1.OpenAIApi(new openai_1.Configuration({ apiKey: process.env.OPENAI_API_KEY }));
this._pullRequest = {
this.octokitApi = (0, github_1.getOctokit)(process.env.GITHUB_TOKEN);
this.openAiApi = new openai_1.OpenAIApi(new openai_1.Configuration({ apiKey: process.env.OPENAI_API_KEY }));
this.pullRequest = {
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
pullHead: (_b = (_a = github_1.context.payload) === null || _a === void 0 ? void 0 : _a.pull_request) === null || _b === void 0 ? void 0 : _b.head.ref,
pullBase: (_d = (_c = github_1.context.payload) === null || _c === void 0 ? void 0 : _c.pull_request) === null || _d === void 0 ? void 0 : _d.base.ref,
pullNumber: (_f = (_e = github_1.context.payload) === null || _e === void 0 ? void 0 : _e.pull_request) === null || _f === void 0 ? void 0 : _f.number,
pullHeadRef: (_a = github_1.context.payload) === null || _a === void 0 ? void 0 : _a.pull_request.head.ref,
pullBaseRef: (_b = github_1.context.payload) === null || _b === void 0 ? void 0 : _b.pull_request.base.ref,
pullNumber: (_c = github_1.context.payload) === null || _c === void 0 ? void 0 : _c.pull_request.number,
};
}
getBranchDiff() {
return __awaiter(this, void 0, void 0, function* () {
const { owner, repo, pullBase, pullHead } = this._pullRequest;
const { data: branchDiff } = yield this._octokitApi.rest.repos.compareCommits({
const { owner, repo, pullBaseRef, pullHeadRef } = this.pullRequest;
const { data: branchDiff } = yield this.octokitApi.rest.repos.compareCommits({
owner,
repo,
base: pullBase,
head: pullHead,
base: pullBaseRef,
head: pullHeadRef,
});
return branchDiff;
});
}
getCommitsList() {
return __awaiter(this, void 0, void 0, function* () {
const { owner, repo, pullNumber } = this._pullRequest;
const { data: commitsList } = yield this._octokitApi.rest.pulls.listCommits({
const { owner, repo, pullNumber } = this.pullRequest;
const { data: commitsList } = yield this.octokitApi.rest.pulls.listCommits({
owner,
repo,
per_page: 50,
Expand All @@ -86,25 +86,22 @@ class CommentOnPullRequestService {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
if (!patch) {
throw new Error(errorsConfig_1.default[errorsConfig_1.ErrorMessage.No_Patch_For_OpenAi_Suggestion]);
throw new Error(errorsConfig_1.default[errorsConfig_1.ErrorMessage.MISSING_PATCH_FOR_OPENAI_SUGGESTION]);
}
const prompt = `
${promptsConfig_1.default[promptsConfig_1.Prompt.Check_Patch]}\n
Patch:\n\n"${patch}"
`;
const openAIResult = yield this._openAiApi.createChatCompletion({
const openAIResult = yield this.openAiApi.createChatCompletion({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: prompt }],
});
const responseText = ((_b = (_a = openAIResult.data.choices.shift()) === null || _a === void 0 ? void 0 : _a.message) === null || _b === void 0 ? void 0 : _b.content) || '';
return responseText;
});
}
getFirstChangedLineFromThePatch(patch) {
static getFirstChangedLineFromPatch(patch) {
return __awaiter(this, void 0, void 0, function* () {
if (!patch) {
throw new Error(errorsConfig_1.default[errorsConfig_1.ErrorMessage.No_Patch_File]);
}
const lineHeaderRegExp = /^@@ -\d+,\d+ \+(\d+),(\d+) @@/;
const lines = patch.split('\n');
const lineHeaderMatch = lines[0].match(lineHeaderRegExp);
Expand All @@ -119,27 +116,25 @@ class CommentOnPullRequestService {
return __awaiter(this, void 0, void 0, function* () {
const { files } = yield this.getBranchDiff();
if (!files) {
throw new Error(errorsConfig_1.default[errorsConfig_1.ErrorMessage.No_Changed_Files_In_PullRequest]);
throw new Error(errorsConfig_1.default[errorsConfig_1.ErrorMessage.NO_CHANGED_FILES_IN_PULL_REQUEST]);
}
for (const file of files) {
const isFileStatusMatch = ['added', 'modified', 'renamed', 'changed'].includes(file.status);
if (!isFileStatusMatch) {
throw new Error(`${errorsConfig_1.default[errorsConfig_1.ErrorMessage.Not_Match_Status_Of_Changed_File]} ${file.status}`);
files.forEach((file) => __awaiter(this, void 0, void 0, function* () {
if (file.patch) {
const openAiSuggestions = yield this.getOpenAiSuggestions(file.patch);
const commitsList = yield this.getCommitsList();
const { owner, repo, pullNumber } = this.pullRequest;
const firstChangedLineFromPatch = yield CommentOnPullRequestService.getFirstChangedLineFromPatch(file.patch);
yield this.octokitApi.rest.pulls.createReviewComment({
owner,
repo,
pull_number: pullNumber,
line: firstChangedLineFromPatch,
path: file.filename,
body: `[ChatGPTReviewer]\n${openAiSuggestions}`,
commit_id: commitsList[commitsList.length - 1].sha,
});
}
const openAiSuggestions = yield this.getOpenAiSuggestions(file.patch);
const commitsList = yield this.getCommitsList();
const { owner, repo, pullNumber } = this._pullRequest;
const firstChangedLineFromThePatch = yield this.getFirstChangedLineFromThePatch(file.patch);
yield this._octokitApi.rest.pulls.createReviewComment({
owner,
repo,
pull_number: pullNumber,
line: firstChangedLineFromThePatch,
path: file.filename,
body: `[ChatGPTReviewer]\n${openAiSuggestions}`,
commit_id: commitsList[commitsList.length - 1].sha,
});
}
}));
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chatgpt-reviewer-github-action",
"version": "0.0.1",
"version": "0.0.4",
"description": "",
"main": "dist/index.js",
"scripts": {
Expand Down

0 comments on commit eb956e1

Please sign in to comment.