Skip to content

Commit

Permalink
Merge pull request #61 from Louis-7/development
Browse files Browse the repository at this point in the history
feat: listen to pull request labeled web hook
  • Loading branch information
Louis-7 authored Feb 27, 2024
2 parents 2128799 + a89e99f commit 189a0bf
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-code-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ permissions:
pull-requests: write
on:
pull_request:
types: [opened, synchronize]
types: [opened, synchronize, labeled]
jobs:
code-review:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion action/core/code-review.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export declare class CodeReview {
CUSTOMIZED_PROMPT: string;
openai: OpenAI;
constructor();
review(context: Context<'pull_request.opened' | 'pull_request.synchronize'>): Promise<void>;
review(context: Context<'pull_request.opened' | 'pull_request.synchronize' | 'pull_request.labeled'>): Promise<void>;
private generatePrompt;
private isExcluded;
private preProcessPullRequestContext;
Expand Down
10 changes: 9 additions & 1 deletion action/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144307,6 +144307,11 @@ var CodeReview = /** @class */ (function () {
baseRef = context.payload['before'];
headRef = context.payload['after'];
break;
case "labeled":
welcomeMessage = "\uD83E\uDD16 Label detected! \uD83E\uDD16";
baseRef = contextPullRequest.base.ref;
headRef = contextPullRequest.head.ref;
break;
default:
welcomeMessage = undefined;
baseRef = '';
Expand Down Expand Up @@ -144596,7 +144601,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
};
var code_review_1 = __nccwpck_require__(69617);
module.exports = function (app) {
app.on(['pull_request.opened', 'pull_request.synchronize'], function (context) { return __awaiter(void 0, void 0, void 0, function () {
app.on(['pull_request.opened', 'pull_request.synchronize', 'pull_request.labeled'], function (context) { return __awaiter(void 0, void 0, void 0, function () {
var codeReview;
return __generator(this, function (_a) {
switch (_a.label) {
Expand All @@ -144608,6 +144613,9 @@ module.exports = function (app) {
case 'synchronize':
console.log('pull request synchronize!');
break;
case 'labeled':
console.log('pull request labeled');
break;
default:
break;
}
Expand Down
11 changes: 8 additions & 3 deletions src/core/code-review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class CodeReview {
this.openai = new OpenAI();
}

async review(context: Context<'pull_request.opened' | 'pull_request.synchronize'>) {
async review(context: Context<'pull_request.opened' | 'pull_request.synchronize' | 'pull_request.labeled'>) {
const pullRequest = new PullRequest(context as any);

if (!await this.openai.test()) {
Expand Down Expand Up @@ -124,7 +124,7 @@ export class CodeReview {
return false;
}

private preProcessPullRequestContext(context: Context<'pull_request.opened' | 'pull_request.synchronize'>): { welcomeMessage: string | undefined, baseRef: string, headRef: string } {
private preProcessPullRequestContext(context: Context<'pull_request.opened' | 'pull_request.synchronize' | 'pull_request.labeled'>): { welcomeMessage: string | undefined, baseRef: string, headRef: string } {
const action = context.payload.action;
const contextPullRequest = context.payload.pull_request;

Expand All @@ -143,6 +143,11 @@ export class CodeReview {
baseRef = context.payload['before'];
headRef = context.payload['after'];
break;
case "labeled":
welcomeMessage = `🤖 Label detected! 🤖`;
baseRef = contextPullRequest.base.ref;
headRef = contextPullRequest.head.ref;
break;
default:
welcomeMessage = undefined;
baseRef = ''
Expand All @@ -153,7 +158,7 @@ export class CodeReview {

}

private async getDiffFiles(baseRef: string, headRef: string, context: Context<'pull_request.opened' | 'pull_request.synchronize'>): Promise<components["schemas"]["diff-entry"][] | undefined> {
private async getDiffFiles(baseRef: string, headRef: string, context: Context<'pull_request.opened' | 'pull_request.synchronize' | 'pull_request.labeled'>): Promise<components["schemas"]["diff-entry"][] | undefined> {
const repo = context.repo();
const diff = await context.octokit.repos.compareCommitsWithBasehead({
owner: repo.owner,
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import { Context, Probot } from "probot";
import { CodeReview } from './core/code-review';

export = (app: Probot) => {
app.on(['pull_request.opened', 'pull_request.synchronize'], async (context: Context<'pull_request.opened' | 'pull_request.synchronize'>) => {
app.on(['pull_request.opened', 'pull_request.synchronize', 'pull_request.labeled'], async (context: Context<'pull_request.opened' | 'pull_request.synchronize' | 'pull_request.labeled'>) => {
switch (context.payload.action) {
case 'opened':
console.log('pull request opened!');
break;
case 'synchronize':
console.log('pull request synchronize!');
break;
case 'labeled':
console.log('pull request labeled')
break;
default:
break;
}
Expand Down

0 comments on commit 189a0bf

Please sign in to comment.