Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Feature/uninstall email #366

Merged
merged 7 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
19 changes: 11 additions & 8 deletions app/api/actions/github/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { NextResponse } from "next/server";
import getAllServices from "../../../../utils/actions/getAllServices";
import randomText from "../../../../utils/actions/markdownHelpers/randomText";
import createTeamAndMatchUser from "../../../../utils/db/teams/createTeamAndMatchUser";
import sendUninstall from "../../../../utils/sendgrid/sendUninstall";

const app = new App({
appId: process.env.GITHUB_APP_ID!,
Expand All @@ -41,11 +42,11 @@ export async function POST(request: Request) {
try {
// Verify and parse the webhook event
const eventName = headers["x-github-event"];

let actionName = req.action;
if (
req.action === "opened" ||
req.action === "reopened" ||
req.action === "synchronize"
actionName === "opened" ||
actionName === "reopened" ||
actionName === "synchronize"
) {
const { missingParams } = validateParams(req, [
"pull_request",
Expand Down Expand Up @@ -502,7 +503,7 @@ export async function POST(request: Request) {
repo,
owner,
number,
action: req.action,
action: actionName,
textToWrite,
},
});
Expand Down Expand Up @@ -557,15 +558,15 @@ export async function POST(request: Request) {
repo,
owner,
number,
action: req.action,
action: actionName,
textToWrite,
},
});
return NextResponse.json({
message: "success",
textToWrite,
});
} else if (req.action === "created" || req.action === "edited") {
} else if (actionName === "created" || actionName === "edited") {
console.log("comment keys", Object.keys(req));
const { missingParams } = validateParams(req, [
"installation",
Expand Down Expand Up @@ -623,11 +624,13 @@ export async function POST(request: Request) {
repo,
owner,
number,
action: req.action,
action: actionName,
businessLogicSummary,
},
});
}
} else if (actionName === "deleted") {
sendUninstall({ emails: [req.sender.email] });
}
return NextResponse.json({
message: "wat",
Expand Down
52 changes: 30 additions & 22 deletions utils/actions/detectConsoleLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ export default async function detectConsoleLogs({
],
})
.then((result) => {
const openAIResult = result.data.choices[0].message.content.split(",");
const openAIResult =
result.data.choices[0].message.content.split(",");

const addtionsHaveConsoleLog = openAIResult[0];
const individualLine = openAIResult[1];
Expand All @@ -161,27 +162,34 @@ export default async function detectConsoleLogs({

const consoleLogPosition = getConsoleLogPosition({
filePatch: file.patch ?? "",
individualLine
})

return octokit.request(
"POST /repos/{owner}/{repo}/pulls/{pull_number}/reviews",
{
owner,
repo,
pull_number: issue_number,
commit_id: latestCommitHash,
event: "COMMENT",
path: file.filename,
comments: [
{
path: file.filename,
position: consoleLogPosition || 1, // comment at the beggining of the file by default
body: "This file contains at least one console log. Please remove any present.",
},
],
}
);
individualLine,
});

return octokit
.request(
"POST /repos/{owner}/{repo}/pulls/{pull_number}/reviews",
{
owner,
repo,
pull_number: issue_number,
commit_id: latestCommitHash,
event: "COMMENT",
path: file.filename,
comments: [
{
path: file.filename,
position: consoleLogPosition || 1, // comment at the beggining of the file by default
body: "This file contains at least one console log. Please remove any present.",
},
],
}
)
.catch((err) => {
console.log(err);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file contains at least one console log. Please remove any present.

});
})
.catch((err) => {
console.log(err);
});
};

Expand Down
47 changes: 28 additions & 19 deletions utils/actions/labelPullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export default async function flagPullRequest({
let botComment = comments.data.find((comment) => {
if (comment.body.includes("This PR contains console logs")) {
// concat to the prompt
prompt += "Since the PR contains console logs, make the maximum rating 8.";
prompt +=
"Since the PR contains console logs, make the maximum rating 8.";
}
});

Expand All @@ -67,26 +68,34 @@ export default async function flagPullRequest({
DONT_MERGE: "🚨 Don't Merge",
};
function deleteLabel(labelName: string) {
octokit.request(
"DELETE /repos/{owner}/{repo}/issues/{issue_number}/labels/{name}",
{
owner,
repo,
issue_number,
name: labelName,
}
);
octokit
.request(
"DELETE /repos/{owner}/{repo}/issues/{issue_number}/labels/{name}",
{
owner,
repo,
issue_number,
name: labelName,
}
)
.catch((error) => {
console.error("Label not found", error);
});
}
function addLabel(labelName: string) {
octokit.request(
"POST /repos/{owner}/{repo}/issues/{issue_number}/labels", //add label
{
owner,
repo,
issue_number,
labels: [labelName],
}
);
octokit
.request(
"POST /repos/{owner}/{repo}/issues/{issue_number}/labels", //add label
{
owner,
repo,
issue_number,
labels: [labelName],
}
)
.catch((error) => {
console.error("add laber", error);
});
}

try {
Expand Down
18 changes: 18 additions & 0 deletions utils/sendgrid/sendUninstall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default async function sendUninstall({ emails }: { emails: string[] }) {
const sgMail = require("@sendgrid/mail");
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: emails,
from: "[email protected]",
templateId: "d-21dcf37205e4479d830dbcfd35cb41d6",
};
sgMail
.sendMultiple(msg)
.then(() => {
console.log("Email sent");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file contains at least one console log. Please remove any present.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There you go! just fix this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh so should it be sent to Posthog or...?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a plan, but don't be too strict about it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file contains at least one console log. Please remove any present.

return { success: true };
})
.catch((error) => {
console.error(error);
});
}