Skip to content

Commit

Permalink
feat: Run Github app directly with Node & Node watch
Browse files Browse the repository at this point in the history
  • Loading branch information
richiemccoll committed Mar 13, 2023
1 parent 0222080 commit 9114d51
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 93 deletions.
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"main": "index.js",
"scripts": {
"dev": "node --watch server.js &",
"dev": "node --watch server.js",
"debug": "node --inspect-brk server.js"
},
"repository": {
Expand Down
92 changes: 92 additions & 0 deletions github-app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const parseGitPatch = require("parse-git-patch").default;

/**
* This is the main entrypoint to the Probot app
* @param {import('probot').Probot} app
*/
module.exports = async (app) => {
app.log.info("[reviewbot] - server started");

/* For the MVP, it's fine to just listen for this event.
In the future, we would want to handle different scenarios such as:
- issue_comment.edited
- pull_request_review_comment.created
- pull_request_review_comment.edited */

app.on("issue_comment.created", async (context) => {
if (context.payload.sender.login === "reviewbot-ai[bot]") {
return;
}

try {
const issue = context.issue();
const { body } = context.payload.comment;
const common = {
owner: issue.owner,
repo: issue.repo,
};

const botCall = "/reviewbot review";
if (body.indexOf(botCall) === -1) {
return;
}

app.log.info("[reviewbot] - ack author comment");

await context.octokit.reactions.createForIssueComment({
content: "eyes",
comment_id: context.payload.comment.id,
...common,
});

app.log.info("[reviewbot] - getting git diff for files changed");

const pullRequest = context.pullRequest();

const { data: diff } = await context.octokit.rest.pulls.get({
...common,
pull_number: pullRequest.pull_number,
mediaType: {
format: "patch",
},
});

const { files, hash } = parseGitPatch(diff);

app.log.info("[reviewbot] - scheduling review request");

const response = await fetch(process.env.REVIEWBOT_SERVICE, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(files),
});

if (response.status !== 200) {
app.log.error("[reviewbot] - error scheduling review request");
throw new Error(
`received status code ${response.status} instead of 200`
);
}

const filesWithSuggestions = await response.json();
const comments = filesWithSuggestions.map((f) => {
return context.octokit.pulls.createReviewComment({
...common,
pull_number: pullRequest.pull_number,
path: f.filename,
body: f.suggestions,
start_line: f.lineRange.start,
// line === end line when using multi line comments
line: f.lineRange.end,
start_side: "RIGHT",
commit_id: hash,
});
});
await Promise.all(comments);
} catch (error) {
app.log.error(`[reviewbot] - encountered an error - ${error.message}`);
}
});
};
94 changes: 3 additions & 91 deletions github-app/index.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,4 @@
const parseGitPatch = require("parse-git-patch").default;
const { run } = require("probot");
const app = require("./app.js");

/**
* This is the main entrypoint to the Probot app
* @param {import('probot').Probot} app
*/
module.exports = async (app) => {
app.log.info("[reviewbot] - server started");

/* For the MVP, it's fine to just listen for this event.
In the future, we would want to handle different scenarios such as:
- issue_comment.edited
- pull_request_review_comment.created
- pull_request_review_comment.edited */

app.on("issue_comment.created", async (context) => {
if (context.payload.sender.login === "reviewbot-ai[bot]") {
return;
}

try {
const issue = context.issue();
const { body } = context.payload.comment;
const common = {
owner: issue.owner,
repo: issue.repo,
};

const botCall = "/reviewbot review";
if (body.indexOf(botCall) === -1) {
return;
}

app.log.info("[reviewbot] - ack author comment");

await context.octokit.reactions.createForIssueComment({
content: "eyes",
comment_id: context.payload.comment.id,
...common,
});

app.log.info("[reviewbot] - getting git diff for files changed");

const pullRequest = context.pullRequest();

const { data: diff } = await context.octokit.rest.pulls.get({
...common,
pull_number: pullRequest.pull_number,
mediaType: {
format: "patch",
},
});

const { files, hash } = parseGitPatch(diff);

app.log.info("[reviewbot] - scheduling review request");

const response = await fetch(process.env.REVIEWBOT_SERVICE, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(files),
});

if (response.status !== 200) {
app.log.error("[reviewbot] - error scheduling review request");
throw new Error(
`received status code ${response.status} instead of 200`
);
}

const filesWithSuggestions = await response.json();
const comments = filesWithSuggestions.map((f) => {
return context.octokit.pulls.createReviewComment({
...common,
pull_number: pullRequest.pull_number,
path: f.filename,
body: f.suggestions,
start_line: f.lineRange.start,
// line === end line when using multi line comments
line: f.lineRange.end,
start_side: "RIGHT",
commit_id: hash,
});
});
await Promise.all(comments);
} catch (error) {
app.log.error(`[reviewbot] - encountered an error - ${error.message}`);
}
});
};
run(app);
2 changes: 1 addition & 1 deletion github-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"probot-app"
],
"scripts": {
"dev": "probot run ./index.js"
"dev": "node --watch ./index.js"
},
"dependencies": {
"probot": "^12.2.4"
Expand Down

0 comments on commit 9114d51

Please sign in to comment.