Skip to content

Commit b5b5f95

Browse files
authoredFeb 15, 2023
handle sync event (#21)
* handle sync event * add code review keyword
1 parent 7ba4e59 commit b5b5f95

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed
 

‎package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
"keywords": [
1111
"probot",
1212
"github",
13-
"probot-app"
13+
"probot-app",
14+
"code review"
1415
],
1516
"scripts": {
16-
"start": "node -r dotenv/config ./dist/lib/index.js",
17+
"start": "node -r dotenv/config ./dist/index.js",
1718
"test": "jest",
18-
"build": "rm -rf dist && rollup -c rollup.config.ts --configPlugin @rollup/plugin-typescript",
19-
"deploy": "node -r dotenv/config"
19+
"build": "rm -rf dist && rollup -c rollup.config.ts --configPlugin @rollup/plugin-typescript"
2020
},
2121
"dependencies": {
2222
"@vercel/edge": "^0.2.7",

‎src/bot.ts

+26-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ export const robot = (app: Probot) => {
2626
app.on(['pull_request.opened', 'pull_request.synchronize'], async (context) => {
2727
const repo = context.repo();
2828
const chat = await loadChat(context);
29+
const pull_request = context.payload.pull_request;
30+
31+
if (pull_request.state === 'closed' || pull_request.locked || pull_request.draft) {
32+
return;
33+
}
2934

3035
const data = await context.octokit.request(
3136
`GET /repos/{owner}/{repo}/compare/{basehead}`,
@@ -36,14 +41,31 @@ export const robot = (app: Probot) => {
3641
}
3742
);
3843

39-
const { files, commits } = data.data;
44+
let { files: changedFiles, commits } = data.data;
45+
46+
if (context.payload.action === 'synchronize') {
47+
if (commits.length >= 2) {
48+
const { data: { files } } = await context.octokit.request(
49+
`GET /repos/{owner}/{repo}/compare/{basehead}`,
50+
{
51+
owner: repo.owner,
52+
repo: repo.repo,
53+
basehead: `${commits[commits.length - 2].sha}...${commits[commits.length - 1].sha}`,
54+
}
55+
);
56+
57+
const filesNames = files?.map(file => file.filename) || [];
58+
changedFiles = changedFiles?.filter(file => filesNames.includes(file.filename))
59+
}
60+
}
61+
4062

41-
if (!files?.length) {
63+
if (!changedFiles?.length) {
4264
return;
4365
}
4466

45-
for (let i = 0; i < files.length; i++) {
46-
const file = files[i];
67+
for (let i = 0; i < changedFiles.length; i++) {
68+
const file = changedFiles[i];
4769
const patch = file.patch || '';
4870

4971
if (!patch || patch.length > MAX_PATCH_COUNT) {

0 commit comments

Comments
 (0)
Failed to load comments.