This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
223 lines (190 loc) · 6.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Considers main branch as the absolute truth
const utils = require("./utils.js");
const WAIT_TIME_AFTER_EACH_FILE = 30000; // 30 seconds
const IGNORE_LABELS = ["maintainer"];
const IGNORE_TITLES = ["no-rm", "rm-skip"];
const PR_MERGED_CONTENT_LINK =
"https://raw.githubusercontent.com/is-a-dev/team-docs/main/pr-merged.md";
/**
* This is the main entrypoint to your Probot app
* @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.closed"], async (context) => {
if (context.payload.pull_request.merged !== true) {
return;
}
const { owner, repo, pull_number } = context.pullRequest();
// Send Pull Request Merged Message
const pr_merged_message = await utils.getRawFileContent(
PR_MERGED_CONTENT_LINK,
);
await context.octokit.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: pr_merged_message,
});
});
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;
// Return on 5+ files to avoid comment spam
if (changedFiles.length > 5) 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}
## ${authorized ? "🔒 Authorized" : "🔓 Unauthorized"}
${
authorized
? ""
: `**File Owner**: ${oldFileOwner}\n${
newFileOwner ? `**New File Owner**: ${newFileOwner}` : ""
}\n**PR Author**: ${prOwner}`
}
<details>
<summary><h3>📸 Screenshot</h3></summary>
![${file.filename}](${imageUrl})
</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 = `
## 🔍 ReviewMate Analysis
File: [${file.filename}](${file.blob_url})
Content URL: ${url}
<details>
<summary><h3>📸 Screenshot</h3></summary>
![${file.filename}](${imageUrl})
</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 = `
## 🔍 ReviewMate Analysis
🗑️ **File Deleted**: [${file.filename}](${file.blob_url})
## ${authorized ? "🔒 Authorized" : "🔓 Unauthorized"}
${
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)
}
}
},
);
};