Skip to content

Commit

Permalink
Prettified Code!
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamDavidHarrison authored and actions-user committed Dec 21, 2023
1 parent 961c1dc commit 12ff0e9
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 200 deletions.
12 changes: 6 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ We'd also love PRs. If you're thinking of a large PR, we advise opening up an is

Here are a few things you can do that will increase the likelihood of your pull request being accepted:

- Write and update tests.
- Keep your changes as focused as possible. If there are multiple changes you would like to make that are not dependent upon each other, consider submitting them as separate pull requests.
- Write a [good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html).
- Write and update tests.
- Keep your changes as focused as possible. If there are multiple changes you would like to make that are not dependent upon each other, consider submitting them as separate pull requests.
- Write a [good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html).

Work in Progress pull requests are also welcome to get feedback early on, or if there is something blocked you.

## Resources

- [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/)
- [Using Pull Requests](https://help.github.com/articles/about-pull-requests/)
- [GitHub Help](https://help.github.com)
- [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/)
- [Using Pull Requests](https://help.github.com/articles/about-pull-requests/)
- [GitHub Help](https://help.github.com)
280 changes: 157 additions & 123 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,102 @@ const IGNORE_TITLES = ["no-rm", "rm-skip"];
* @param {import('probot').Probot} app
*/
module.exports = (app) => {
if (!process.env.SCREENSHOTLAYER_KEY) throw new Error("Missing Environment Variable: SCREENSHOTLAYER_KEY");
if (!process.env.IMGBB_KEY) throw new Error("Missing Environment Variable: IMGBB_KEY");

app.log.info("The app was loaded!");
app.on(["pull_request.opened", "pull_request.synchronize", "pull_request.ready_for_review"],
async (context) => {
const { owner, repo, pull_number } = context.pullRequest();
const { data: changedFiles } = await context.octokit.pulls.listFiles({
owner,
repo,
pull_number,
});

labels = await context.octokit.issues.listLabelsOnIssue({
owner,
repo,
issue_number: pull_number,
});

labels = labels.data.map((label) => label.name);

if (IGNORE_LABELS.some((label) => labels.includes(label))) return;

const title = context.payload.pull_request.title;

if (IGNORE_TITLES.some((ignore_title) => title.includes(ignore_title))) return;

for (const file of changedFiles) {
if (file.status === "modified" || file.status === "renamed" || file.status === "changed") {
// Get the actual file content
const fileContent = await utils.getRawFileContent(file.raw_url);

let url;
if ("CNAME" in fileContent.record) {
url = `http://${fileContent.record.CNAME}`;
} else if ("URL" in fileContent.record) {
url = fileContent.record.URL;
} else {
continue;
}

const screenshot = await utils.screenshotUrl(url);
const imgbb = await utils.uploadImageToImgbb(screenshot.toString("base64"));
const imageUrl = imgbb.data.url;
const description = fileContent.description || "N/A";
const repository = fileContent.repo || "N/A";

const oldRawFileUrl = file.raw_url.replace(/\/[0-9a-f]{40}\//, "/main/");
const oldFile = await utils.getRawFileContent(oldRawFileUrl);
const oldFileOwner = oldFile.owner.username;
const newFileOwner = fileContent.owner.username;
const prOwner = context.payload.pull_request.user.login;

let authorized = false;

if (oldFileOwner.toLowerCase() === prOwner.toLowerCase() || newFileOwner.toLowerCase() && oldFileOwner.toLowerCase() === prOwner.toLowerCase()) {
authorized = true;
}

// Create a formatted message or comment
const commentMessage = `
if (!process.env.SCREENSHOTLAYER_KEY)
throw new Error("Missing Environment Variable: SCREENSHOTLAYER_KEY");
if (!process.env.IMGBB_KEY)
throw new Error("Missing Environment Variable: IMGBB_KEY");

app.log.info("The app was loaded!");
app.on(
[
"pull_request.opened",
"pull_request.synchronize",
"pull_request.ready_for_review",
],
async (context) => {
const { owner, repo, pull_number } = context.pullRequest();
const { data: changedFiles } = await context.octokit.pulls.listFiles({
owner,
repo,
pull_number,
});

labels = await context.octokit.issues.listLabelsOnIssue({
owner,
repo,
issue_number: pull_number,
});

labels = labels.data.map((label) => label.name);

if (IGNORE_LABELS.some((label) => labels.includes(label))) return;

const title = context.payload.pull_request.title;

if (IGNORE_TITLES.some((ignore_title) => title.includes(ignore_title)))
return;

for (const file of changedFiles) {
if (
file.status === "modified" ||
file.status === "renamed" ||
file.status === "changed"
) {
// Get the actual file content
const fileContent = await utils.getRawFileContent(file.raw_url);

let url;
if ("CNAME" in fileContent.record) {
url = `http://${fileContent.record.CNAME}`;
} else if ("URL" in fileContent.record) {
url = fileContent.record.URL;
} else {
continue;
}

const screenshot = await utils.screenshotUrl(url);
const imgbb = await utils.uploadImageToImgbb(
screenshot.toString("base64"),
);
const imageUrl = imgbb.data.url;
const description = fileContent.description || "N/A";
const repository = fileContent.repo || "N/A";

const oldRawFileUrl = file.raw_url.replace(
/\/[0-9a-f]{40}\//,
"/main/",
);
const oldFile = await utils.getRawFileContent(oldRawFileUrl);
const oldFileOwner = oldFile.owner.username;
const newFileOwner = fileContent.owner.username;
const prOwner = context.payload.pull_request.user.login;

let authorized = false;

if (
oldFileOwner.toLowerCase() === prOwner.toLowerCase() ||
(newFileOwner.toLowerCase() &&
oldFileOwner.toLowerCase() === prOwner.toLowerCase())
) {
authorized = true;
}

// Create a formatted message or comment
const commentMessage = `
# 🔍 ReviewMate Analysis
File: [${file.filename}](${file.blob_url})
Content URL: ${url}
Description: ${description}
Repository: ${repository}
### 🔒 Authorized: ${authorized ? "✅" : "❌"}
${authorized ? "" : `**File Owner**: ${oldFileOwner}${newFileOwner ? `**New File Owner**: ${newFileOwner}` : ""}\n**PR Author**: ${prOwner}`}
${
authorized
? ""
: `**File Owner**: ${oldFileOwner}${
newFileOwner ? `**New File Owner**: ${newFileOwner}` : ""
}\n**PR Author**: ${prOwner}`
}
<details>
<summary><h3>📸 Screenshot</h3></summary>
Expand All @@ -87,34 +114,36 @@ ${authorized ? "" : `**File Owner**: ${oldFileOwner}${newFileOwner ? `**New File
</details>
`;

// Post the comment to the GitHub pull request
await context.octokit.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: commentMessage,
});
} else if (file.status === "added") {
// Get the actual file content
const fileContent = await utils.getRawFileContent(file.raw_url);

let url;

if ("CNAME" in fileContent.record) {
url = `http://${fileContent.record.CNAME}`;
} else if ("URL" in fileContent.record) {
url = fileContent.record.URL;
} else {
continue;
}

// Capture a screenshot of the content as if it were a webpage (assuming HTML content)
const screenshot = await utils.screenshotUrl(url);
const imgbb = await utils.uploadImageToImgbb(screenshot.toString("base64"));
const imageUrl = imgbb.data.url;
const description = fileContent.description || "N/A";
const repository = fileContent.repo || "N/A";
const commentMessage = `
// Post the comment to the GitHub pull request
await context.octokit.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: commentMessage,
});
} else if (file.status === "added") {
// Get the actual file content
const fileContent = await utils.getRawFileContent(file.raw_url);

let url;

if ("CNAME" in fileContent.record) {
url = `http://${fileContent.record.CNAME}`;
} else if ("URL" in fileContent.record) {
url = fileContent.record.URL;
} else {
continue;
}

// Capture a screenshot of the content as if it were a webpage (assuming HTML content)
const screenshot = await utils.screenshotUrl(url);
const imgbb = await utils.uploadImageToImgbb(
screenshot.toString("base64"),
);
const imageUrl = imgbb.data.url;
const description = fileContent.description || "N/A";
const repository = fileContent.repo || "N/A";
const commentMessage = `
# 🔍 ReviewMate Analysis
File: [${file.filename}](${file.blob_url})
Content URL: ${url}
Expand All @@ -128,44 +157,49 @@ Repository: ${repository}
</details>
`;

// Post the comment to the GitHub pull request
await context.octokit.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: commentMessage,
});
} else {
const oldRawFileUrl = file.raw_url.replace(/\/[0-9a-f]{40}\//, "/main/");
const oldFile = await utils.getRawFileContent(oldRawFileUrl);
const oldFileOwner = oldFile.owner.username;
const prOwner = context.payload.pull_request.user.login;

let authorized = false;

if (oldFileOwner === prOwner) {
authorized = true;
}

const commentMessage = `
// Post the comment to the GitHub pull request
await context.octokit.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: commentMessage,
});
} else {
const oldRawFileUrl = file.raw_url.replace(
/\/[0-9a-f]{40}\//,
"/main/",
);
const oldFile = await utils.getRawFileContent(oldRawFileUrl);
const oldFileOwner = oldFile.owner.username;
const prOwner = context.payload.pull_request.user.login;

let authorized = false;

if (oldFileOwner === prOwner) {
authorized = true;
}

const commentMessage = `
# 🔍 ReviewMate Analysis
🗑️ **File Deleted**: [${file.filename}](${file.blob_url})
### 🔒 Authorized: ${authorized ? "✅" : "❌"}
${authorized ? "" : `**File Owner**: ${oldFileOwner}\n**PR Author**: ${prOwner}`}
${
authorized ? "" : `**File Owner**: ${oldFileOwner}\n**PR Author**: ${prOwner}`
}
`;

// Post the comment to the GitHub pull request
await context.octokit.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: commentMessage,
});

await new Promise((r) => setTimeout(r, WAIT_TIME_AFTER_EACH_FILE)); // For Screenshotlayer API ratelimit (2 req / minute)
}
}
// Post the comment to the GitHub pull request
await context.octokit.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: commentMessage,
});

await new Promise((r) => setTimeout(r, WAIT_TIME_AFTER_EACH_FILE)); // For Screenshotlayer API ratelimit (2 req / minute)
}
);
}
},
);
};
Loading

0 comments on commit 12ff0e9

Please sign in to comment.