-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
310 lines (257 loc) · 8.73 KB
/
app.js
File metadata and controls
310 lines (257 loc) · 8.73 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
require('dotenv').config();
const express = require('express');
const crypto = require('crypto');
const { Octokit } = require('@octokit/rest');
const { createAppAuth } = require('@octokit/auth-app');
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
// Create Express app
const app = express();
app.use(express.json());
// GitHub App credentials
const APP_ID = process.env.APP_ID;
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET;
// Verify GitHub webhook signature
function verifySignature(req) {
const signature = req.headers['x-hub-signature-256'];
if (!signature) {
throw new Error('No X-Hub-Signature-256 found on request');
}
const hmac = crypto.createHmac('sha256', WEBHOOK_SECRET);
const digest = 'sha256=' + hmac.update(JSON.stringify(req.body)).digest('hex');
return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(digest));
}
// Create a new branch from main
async function createBranch(octokit, owner, repo, issueNumber) {
// Get the SHA of the latest commit on the main branch
const { data: refData } = await octokit.git.getRef({
owner,
repo,
ref: 'heads/main',
});
const mainSha = refData.object.sha;
// Create a new branch
const branchName = `uwuify-issue-${issueNumber}`;
await octokit.git.createRef({
owner,
repo,
ref: `refs/heads/${branchName}`,
sha: mainSha,
});
return branchName;
}
// Post an immediate reply comment
async function postReplyComment(octokit, owner, repo, issueNumber) {
try {
// Post the reply comment
await octokit.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: "see you, uwuing..."
});
console.log(`Posted immediate reply to issue #${issueNumber}`);
} catch (error) {
console.error('Error posting reply comment:', error);
}
}
// Add uwuify script to the repository
async function addUwuifyScript(octokit, owner, repo, branch) {
// Content of the uwuify script
const scriptContent = `#!/usr/bin/env python3
import os
import re
import sys
from pathlib import Path
try:
import uwuify
except ImportError:
os.system('pip install uwuify')
import uwuify
def uwuify_markdown_file(file_path):
"""Uwuify the content of a markdown file."""
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Preserve code blocks
code_blocks = []
def save_code_block(match):
code_blocks.append(match.group(0))
return f"CODE_BLOCK_{len(code_blocks) - 1}"
# Save code blocks
content_without_code = re.sub(r'\`\`\`.*?\`\`\`', save_code_block, content, flags=re.DOTALL)
content_without_code = re.sub(r'\`.*?\`', save_code_block, content_without_code)
# Uwuify the text
uwuified_content = uwuify.uwuify(content_without_code)
# Restore code blocks
for i, block in enumerate(code_blocks):
uwuified_content = uwuified_content.replace(f"CODE_BLOCK_{i}", block)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(uwuified_content)
def main():
"""Find and uwuify all markdown files in the repository."""
# Get the repository root directory
repo_root = os.getcwd()
# Find all markdown files
markdown_files = list(Path(repo_root).rglob('*.md'))
print(f"Found {len(markdown_files)} markdown files")
# Uwuify each markdown file
for file_path in markdown_files:
print(f"Uwuifying {file_path}")
uwuify_markdown_file(file_path)
print("All markdown files have been uwuified!")
if __name__ == "__main__":
main()
`;
// Create or update the uwuify_repo.py file
await octokit.repos.createOrUpdateFileContents({
owner,
repo,
path: 'uwuify_repo.py',
message: 'Add uwuify script',
content: Buffer.from(scriptContent).toString('base64'),
branch,
});
}
// Run the uwuify script and commit changes
async function runUwuifyScript(octokit, owner, repo, branch) {
// Get the tree of the branch
const { data: refData } = await octokit.git.getRef({
owner,
repo,
ref: `heads/${branch}`,
});
const commitSha = refData.object.sha;
// Get the commit to get the tree
const { data: commitData } = await octokit.git.getCommit({
owner,
repo,
commit_sha: commitSha,
});
const treeSha = commitData.tree.sha;
// Get all markdown files in the repository
const { data: treeData } = await octokit.git.getTree({
owner,
repo,
tree_sha: treeSha,
recursive: 1,
});
// Find all markdown files
const markdownFiles = treeData.tree.filter(item =>
item.path.endsWith('.md') && item.type === 'blob'
);
// Process each markdown file
for (const file of markdownFiles) {
// Get the content of the file
const { data: fileData } = await octokit.repos.getContent({
owner,
repo,
path: file.path,
ref: branch,
});
// Decode the content
const content = Buffer.from(fileData.content, 'base64').toString();
// Uwuify the content
const uwuifiedContent = require('uwuify').uwuify(content);
// Update the file with uwuified content
await octokit.repos.createOrUpdateFileContents({
owner,
repo,
path: file.path,
message: `Uwuify ${file.path}`,
content: Buffer.from(uwuifiedContent).toString('base64'),
sha: fileData.sha,
branch,
});
}
// Delete the uwuify script
const { data: scriptFile } = await octokit.repos.getContent({
owner,
repo,
path: 'uwuify_repo.py',
ref: branch,
});
await octokit.repos.deleteFile({
owner,
repo,
path: 'uwuify_repo.py',
message: 'Remove uwuify script',
sha: scriptFile.sha,
branch,
});
}
// Create a pull request
async function createPullRequest(octokit, owner, repo, branch, issueNumber, requester) {
const { data: pullRequest } = await octokit.pulls.create({
owner,
repo,
title: `Uwuify markdown files (requested in #${issueNumber})`,
body: `This PR uwuifies all markdown files in the repository as requested by @${requester} in issue #${issueNumber}.`,
head: branch,
base: 'main',
});
// Add a comment to the issue mentioning the requester
await octokit.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: `@${requester} I've created a pull request with uwuified markdown files: ${pullRequest.html_url}`,
});
return pullRequest.number;
}
// Webhook endpoint
app.post('/webhook', async (req, res) => {
try {
// Verify webhook signature
if (!verifySignature(req)) {
return res.status(401).send('Invalid signature');
}
const event = req.headers['x-github-event'];
const payload = req.body;
// Only process issue_comment events
if (event === 'issue_comment' && payload.action === 'created') {
const comment = payload.comment.body;
const issueNumber = payload.issue.number;
const requester = payload.comment.user.login;
const repo = payload.repository.name;
const owner = payload.repository.owner.login;
// Check if the comment mentions @uwularpy
if (comment.includes('@uwularpy')) {
console.log(`Mention detected in issue #${issueNumber} by ${requester}`);
// Create an authenticated Octokit instance
const octokit = new Octokit({
authStrategy: createAppAuth,
auth: {
appId: APP_ID,
privateKey: PRIVATE_KEY,
installationId: payload.installation.id,
},
});
// Post an immediate reply comment for validation
await postReplyComment(octokit, owner, repo, issueNumber);
// Create a new branch
const branch = await createBranch(octokit, owner, repo, issueNumber);
console.log(`Created branch: ${branch}`);
// Add uwuify script to the repository
await addUwuifyScript(octokit, owner, repo, branch);
console.log('Added uwuify script to the repository');
// Run the uwuify script and commit changes
await runUwuifyScript(octokit, owner, repo, branch);
console.log('Ran uwuify script and committed changes');
// Create a pull request
const prNumber = await createPullRequest(octokit, owner, repo, branch, issueNumber, requester);
console.log(`Created pull request #${prNumber}`);
}
}
res.status(200).send('Webhook processed');
} catch (error) {
console.error('Error processing webhook:', error);
res.status(500).send('Error processing webhook');
}
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server is running on port ${PORT}`);
});