-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
709 lines (615 loc) · 20.4 KB
/
server.js
File metadata and controls
709 lines (615 loc) · 20.4 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
require("dotenv").config();
const { Octokit } = require("@octokit/rest");
// Environment variables
const tokenA = process.env.TOKEN_A;
const tokenB = process.env.TOKEN_B;
const REPO_OWNER = process.env.REPO_OWNER;
const FORK_OWNER = process.env.FORK_OWNER;
const REPO_NAME = process.env.REPO_NAME;
const BRANCH_NAME = process.env.BRANCH_NAME || "jonny"; // Default to 'jonny' if not specified
// New environment variables for enhanced features
const CREATE_ISSUE = process.env.CREATE_ISSUE === "true";
const ISSUE_TYPE = process.env.ISSUE_TYPE || "enhancement"; // bug, feature, enhancement
const ISSUE_TITLE = process.env.ISSUE_TITLE || "Automated Issue Creation";
const ISSUE_BODY =
process.env.ISSUE_BODY ||
"This issue was created automatically by the pull-merge bot.";
const ENABLE_CODE_REVIEW = process.env.ENABLE_CODE_REVIEW === "true";
const RANDOM_CHANCE_PERCENTAGE =
parseInt(process.env.RANDOM_CHANCE_PERCENTAGE) || 30; // Default to 30%
// Validate environment variables
if (!tokenA || !tokenB || !REPO_OWNER || !FORK_OWNER || !REPO_NAME) {
console.error(
"❌ Missing required environment variables. Please check your .env file."
);
console.error(
"Required: TOKEN_A, TOKEN_B, REPO_OWNER, FORK_OWNER, REPO_NAME"
);
console.error('Optional: BRANCH_NAME (defaults to "jonny")');
console.error("Optional: CREATE_ISSUE (true/false, defaults to false)");
console.error(
"Optional: ISSUE_TYPE (bug/feature/enhancement, defaults to enhancement)"
);
console.error(
'Optional: ISSUE_TITLE (defaults to "Automated Issue Creation")'
);
console.error("Optional: ISSUE_BODY (defaults to generic message)");
console.error("Optional: ENABLE_CODE_REVIEW (true/false, defaults to false)");
console.error("Optional: RANDOM_CHANCE_PERCENTAGE (1-100, defaults to 30)");
process.exit(1);
}
const octokitA = new Octokit({ auth: tokenA });
const octokitB = new Octokit({ auth: tokenB });
console.log("🤖 Pull Merge Bot Starting...");
console.log(`📁 Repository: ${REPO_OWNER}/${REPO_NAME}`);
console.log(`🔗 Account A: ${FORK_OWNER}`);
console.log(`🌿 Branch: ${BRANCH_NAME}`);
console.log(`📝 Create Issue: ${CREATE_ISSUE ? "Yes" : "No"}`);
console.log(`🔍 Code Review: ${ENABLE_CODE_REVIEW ? "Yes" : "No"}`);
console.log(`🎲 Random Chance: ${RANDOM_CHANCE_PERCENTAGE}%`);
// Helper function to get random chance based on environment variable
function getRandomChance() {
const chance = RANDOM_CHANCE_PERCENTAGE / 100;
return Math.random() < chance;
}
async function validateRepositoryAccess() {
try {
// Check if Account A can access the target repository
console.log("🔍 Validating Account A access to repository...");
await octokitA.repos.get({
owner: REPO_OWNER,
repo: REPO_NAME,
});
console.log("✅ Account A can access repository");
// Check if Account B can access the target repository
console.log("🔍 Validating Account B access to repository...");
await octokitB.repos.get({
owner: REPO_OWNER,
repo: REPO_NAME,
});
console.log("✅ Account B can access repository");
return true;
} catch (error) {
console.error("❌ Repository access validation failed:", error.message);
if (error.status === 404) {
console.error(
"💡 This might be a private repository access issue. Check:"
);
console.error(' - Both tokens have "repo" scope');
console.error(" - Account A has been invited to the repository");
}
return false;
}
}
async function getCurrentBranch() {
try {
const repo = await octokitA.repos.get({
owner: REPO_OWNER,
repo: REPO_NAME,
});
return repo.data.default_branch;
} catch (error) {
console.error("Error getting default branch:", error.message);
return "main";
}
}
async function updateBranchFromMain() {
try {
console.log(`🔄 Updating ${BRANCH_NAME} branch from main...`);
// Get the latest commit from main branch
const defaultBranch = await getCurrentBranch();
const mainBranch = await octokitA.repos.getBranch({
owner: REPO_OWNER,
repo: REPO_NAME,
branch: defaultBranch,
});
// Update the branch to point to the latest main
await octokitA.git.updateRef({
owner: REPO_OWNER,
repo: REPO_NAME,
ref: `heads/${BRANCH_NAME}`,
sha: mainBranch.data.commit.sha,
force: true,
});
console.log(`✅ ${BRANCH_NAME} branch updated from main`);
return true;
} catch (error) {
console.error(`❌ Error updating ${BRANCH_NAME} branch:`, error.message);
return false;
}
}
async function createOrUpdateBranch() {
try {
console.log(`🔍 Checking if ${BRANCH_NAME} branch exists...`);
// Try to get the branch
try {
await octokitA.repos.getBranch({
owner: REPO_OWNER,
repo: REPO_NAME,
branch: BRANCH_NAME,
});
console.log(`✅ ${BRANCH_NAME} branch already exists`);
// Update branch from main to avoid conflicts
await updateBranchFromMain();
return true;
} catch (error) {
if (error.status === 404) {
console.log(`📝 Creating ${BRANCH_NAME} branch from main...`);
// Get the latest commit from main branch
const defaultBranch = await getCurrentBranch();
const mainBranch = await octokitA.repos.getBranch({
owner: REPO_OWNER,
repo: REPO_NAME,
branch: defaultBranch,
});
// Create branch from main
await octokitA.git.createRef({
owner: REPO_OWNER,
repo: REPO_NAME,
ref: `refs/heads/${BRANCH_NAME}`,
sha: mainBranch.data.commit.sha,
});
console.log(`✅ ${BRANCH_NAME} branch created successfully`);
return true;
}
throw error;
}
} catch (error) {
console.error(`❌ Error creating ${BRANCH_NAME} branch:`, error.message);
return false;
}
}
async function editAndCommitReadme() {
try {
console.log("📝 Editing README.md...");
// Get current README content from main branch to avoid conflicts
let currentContent = "";
try {
const defaultBranch = await getCurrentBranch();
const readme = await octokitA.repos.getContent({
owner: REPO_OWNER,
repo: REPO_NAME,
path: "README.md",
ref: defaultBranch,
});
currentContent = Buffer.from(readme.data.content, "base64").toString();
} catch (error) {
if (error.status === 404) {
console.log("📄 README.md not found, will create new one");
} else {
throw error;
}
}
// Add timestamp and edit information
const timestamp = new Date().toISOString();
const editContent = `## Last Updated by Bot
This section was automatically updated by the pull-merge bot on ${timestamp}.
### Changes Made:
- Automated README update
- Timestamp: ${timestamp}
- Updated by: ${FORK_OWNER}
- Branch: ${BRANCH_NAME}
${currentContent}`;
// Commit the changes
const commitMessage = `Update README.md - Automated edit by ${FORK_OWNER} on ${BRANCH_NAME}`;
// Get the current tree
const tree = await octokitA.git.getTree({
owner: REPO_OWNER,
repo: REPO_NAME,
tree_sha: (
await octokitA.repos.getBranch({
owner: REPO_OWNER,
repo: REPO_NAME,
branch: BRANCH_NAME,
})
).data.commit.sha,
recursive: true,
});
// Create new tree with updated README
const newTree = await octokitA.git.createTree({
owner: REPO_OWNER,
repo: REPO_NAME,
base_tree: tree.data.sha,
tree: [
{
path: "README.md",
mode: "100644",
type: "blob",
content: editContent,
},
],
});
// Create commit
const commit = await octokitA.git.createCommit({
owner: REPO_OWNER,
repo: REPO_NAME,
message: commitMessage,
tree: newTree.data.sha,
parents: [
(
await octokitA.repos.getBranch({
owner: REPO_OWNER,
repo: REPO_NAME,
branch: BRANCH_NAME,
})
).data.commit.sha,
],
});
// Update the branch
await octokitA.git.updateRef({
owner: REPO_OWNER,
repo: REPO_NAME,
ref: `heads/${BRANCH_NAME}`,
sha: commit.data.sha,
});
console.log("✅ README.md updated and committed successfully");
return true;
} catch (error) {
console.error("❌ Error editing README.md:", error.message);
return false;
}
}
async function checkForExistingPR() {
try {
// Check if there's already an open PR from the branch
const pulls = await octokitA.pulls.list({
owner: REPO_OWNER,
repo: REPO_NAME,
state: "open",
head: BRANCH_NAME,
base: "main",
});
if (pulls.data.length > 0) {
console.log(`⚠️ Found existing open PR: #${pulls.data[0].number}`);
return pulls.data[0];
}
return null;
} catch (error) {
console.error("Error checking for existing PR:", error.message);
return null;
}
}
async function checkForMergeablePR(prNumber) {
try {
// Wait a bit for GitHub to calculate mergeability
await new Promise((resolve) => setTimeout(resolve, 2000));
const pr = await octokitA.pulls.get({
owner: REPO_OWNER,
repo: REPO_NAME,
pull_number: prNumber,
});
if (pr.data.mergeable === false) {
console.log("⚠️ PR has conflicts, cannot merge automatically");
return false;
}
if (pr.data.mergeable === null) {
console.log("⏳ GitHub is still calculating mergeability, waiting...");
await new Promise((resolve) => setTimeout(resolve, 3000));
return await checkForMergeablePR(prNumber);
}
return true;
} catch (error) {
console.error("Error checking PR mergeability:", error.message);
return false;
}
}
async function createIssue() {
try {
console.log("📝 Creating issue...");
const timestamp = new Date().toISOString();
const issueBody = `${ISSUE_BODY}
---
**Issue Details:**
- Created by: ${FORK_OWNER}
- Created on: ${timestamp}
- Issue type: ${ISSUE_TYPE}
- Automated by: Pull-Merge Bot
**Additional Information:**
- Repository: ${REPO_OWNER}/${REPO_NAME}
- Branch: ${BRANCH_NAME}
- Bot version: 2.0.0`;
const issue = await octokitA.issues.create({
owner: REPO_OWNER,
repo: REPO_NAME,
title: `${ISSUE_TITLE} - ${timestamp.split("T")[0]}`,
body: issueBody,
labels: [ISSUE_TYPE, "automated", "bot-created"],
});
console.log(`✅ Issue created successfully: #${issue.data.number}`);
console.log(`🔗 Issue URL: ${issue.data.html_url}`);
return issue.data;
} catch (error) {
console.error("❌ Error creating issue:", error.message);
if (error.response) {
console.error("Response status:", error.response.status);
console.error("Response data:", error.response.data);
}
return null;
}
}
async function addCodeReviewComment(prNumber, comment) {
try {
await octokitA.pulls.createReview({
owner: REPO_OWNER,
repo: REPO_NAME,
pull_number: prNumber,
body: comment,
event: "COMMENT",
});
console.log(
`✅ Code review comment added to PR #${prNumber} by ${FORK_OWNER}`
);
return true;
} catch (error) {
console.error(`❌ Error adding code review comment:`, error.message);
return false;
}
}
async function approvePR(prNumber) {
try {
await octokitB.pulls.createReview({
owner: REPO_OWNER,
repo: REPO_NAME,
pull_number: prNumber,
body: `✅ **Approved by ${REPO_OWNER}** - Code review completed successfully.`,
event: "APPROVE",
});
console.log(`✅ PR #${prNumber} approved by ${REPO_OWNER}`);
return true;
} catch (error) {
console.error(`❌ Error approving PR:`, error.message);
return false;
}
}
async function performCodeReview(prNumber) {
try {
console.log(
`🔍 Performing code review on PR #${prNumber} by ${FORK_OWNER}...`
);
// Get PR details
const pr = await octokitA.pulls.get({
owner: REPO_OWNER,
repo: REPO_NAME,
pull_number: prNumber,
});
// Get PR files
const files = await octokitA.pulls.listFiles({
owner: REPO_OWNER,
repo: REPO_NAME,
pull_number: prNumber,
});
const reviewComments = [];
// Analyze changes and create review comments
for (const file of files.data) {
if (
file.filename.endsWith(".js") ||
file.filename.endsWith(".ts") ||
file.filename.endsWith(".md")
) {
const additions = file.additions;
const deletions = file.deletions;
const changes = file.changes;
if (changes > 0) {
let comment = `📝 **File Review: ${file.filename}**\n\n`;
comment += `- **Changes:** ${changes} lines\n`;
comment += `- **Additions:** ${additions} lines\n`;
comment += `- **Deletions:** ${deletions} lines\n\n`;
if (file.filename.endsWith(".md")) {
comment += `✅ **Documentation Update** - Good to see documentation being maintained!\n`;
comment += `💡 **Suggestion:** Consider adding more context if this is a significant change.\n`;
} else if (
file.filename.endsWith(".js") ||
file.filename.endsWith(".ts")
) {
comment += `🔧 **Code Changes** - Code modifications detected.\n`;
if (additions > 50) {
comment += `⚠️ **Large Change** - This is a substantial modification. Please ensure thorough testing.\n`;
}
comment += `💡 **Suggestion:** Consider adding unit tests for new functionality.\n`;
}
reviewComments.push(comment);
}
}
}
// Add overall review comment
const overallComment = `## 🤖 Automated Code Review by ${FORK_OWNER}
**PR Summary:**
- **Title:** ${pr.data.title}
- **Author:** ${pr.data.user.login}
- **Files Changed:** ${files.data.length}
- **Total Changes:** ${files.data.reduce(
(sum, file) => sum + file.changes,
0
)} lines
**Review Notes:**
${
reviewComments.length > 0
? reviewComments.join("\n\n")
: "- No significant code changes detected"
}
**Recommendations:**
- ✅ Code structure looks good
- ✅ Changes are well-organized
- 💡 Consider adding tests for new functionality
- 💡 Update documentation if needed
**Review Status:** ✅ **Ready for Approval**
---
*This review was performed automatically by the Pull-Merge Bot for ${FORK_OWNER}*`;
await addCodeReviewComment(prNumber, overallComment);
// Approve the PR using Account B (repo owner)
await approvePR(prNumber);
console.log(
`✅ Code review completed by ${FORK_OWNER} and approved by ${REPO_OWNER}`
);
return true;
} catch (error) {
console.error("❌ Error performing code review:", error.message);
return false;
}
}
async function createEnhancedPR() {
try {
console.log("🔁 Creating enhanced pull request...");
const timestamp = new Date().toISOString();
const prBody = `## 🤖 Automated Pull Request
This PR was created automatically by the pull-merge bot.
### 📋 Changes Summary
- **Updated README.md** with timestamp and bot information
- **Automated commit** by ${FORK_OWNER}
- **Branch:** ${BRANCH_NAME}
- **Created:** ${timestamp}
### 🔍 What Changed
- Added timestamp section to README.md
- Updated documentation with bot activity
- Maintained repository documentation standards
### ✅ Quality Checks
- [x] Code follows repository standards
- [x] Documentation updated
- [x] No breaking changes
- [x] Automated testing ready
### 🤖 Bot Information
- **Bot Version:** 2.0.0
- **Created by:** ${FORK_OWNER}
- **Repository:** ${REPO_OWNER}/${REPO_NAME}
- **Branch:** ${BRANCH_NAME}
---
*This PR will be automatically reviewed and merged by the bot.*`;
const pr = await octokitA.pulls.create({
owner: REPO_OWNER,
repo: REPO_NAME,
head: BRANCH_NAME,
base: "main",
title: `🤖 Automated PR: Update README.md from ${BRANCH_NAME} - ${
timestamp.split("T")[0]
}`,
body: prBody,
});
console.log(`🔁 Enhanced PR Created: #${pr.data.number}`);
return pr.data;
} catch (error) {
console.error("❌ Error creating enhanced PR:", error.message);
return null;
}
}
async function createAndMergePR() {
try {
// 1. Validate repository access
const accessValid = await validateRepositoryAccess();
if (!accessValid) {
console.error("❌ Cannot proceed due to repository access issues.");
return;
}
// 2. Create issue if enabled and random chance (30%)
if (CREATE_ISSUE && getRandomChance()) {
console.log("🎲 Random chance triggered: Creating issue...");
const issue = await createIssue();
if (issue) {
console.log(`📝 Issue #${issue.number} created successfully`);
}
} else if (CREATE_ISSUE) {
console.log("🎲 Random chance not triggered: Skipping issue creation");
}
// 3. Create or update branch
const branchCreated = await createOrUpdateBranch();
if (!branchCreated) {
return;
}
// 4. Edit and commit README.md
const readmeUpdated = await editAndCommitReadme();
if (!readmeUpdated) {
return;
}
// 5. Check for existing PR
const existingPR = await checkForExistingPR();
if (existingPR) {
console.log(`🔄 Using existing PR #${existingPR.number}`);
// Perform code review if enabled and random chance (30%)
if (ENABLE_CODE_REVIEW && getRandomChance()) {
console.log("🎲 Random chance triggered: Performing code review...");
await performCodeReview(existingPR.number);
} else if (ENABLE_CODE_REVIEW) {
console.log("🎲 Random chance not triggered: Skipping code review");
}
// Check if PR is mergeable
const isMergeable = await checkForMergeablePR(existingPR.number);
if (!isMergeable) {
console.log(
"❌ Cannot merge PR due to conflicts. Please resolve manually."
);
return;
}
// Merge the existing PR
await octokitB.pulls.merge({
owner: REPO_OWNER,
repo: REPO_NAME,
pull_number: existingPR.number,
commit_title: "Auto-merged by bot with code review",
merge_method: "squash",
});
console.log(`✅ Existing PR #${existingPR.number} merged successfully.`);
return;
}
// 6. Create a new enhanced PR from branch to main (Account A)
const pr = await createEnhancedPR();
if (!pr) {
console.error("❌ Failed to create PR");
return;
}
// 7. Perform code review if enabled and random chance (30%)
if (ENABLE_CODE_REVIEW && getRandomChance()) {
console.log("🎲 Random chance triggered: Performing code review...");
await performCodeReview(pr.number);
} else if (ENABLE_CODE_REVIEW) {
console.log("🎲 Random chance not triggered: Skipping code review");
}
// 8. Check if PR is mergeable
const isMergeable = await checkForMergeablePR(pr.number);
if (!isMergeable) {
console.log(
"❌ Cannot merge PR due to conflicts. Please resolve manually."
);
return;
}
// 9. Merge PR using Account B (owner)
console.log("🔄 Merging pull request...");
await octokitB.pulls.merge({
owner: REPO_OWNER,
repo: REPO_NAME,
pull_number: pr.number,
commit_title: "Auto-merged by bot with code review",
merge_method: "squash",
});
console.log(`✅ PR #${pr.number} merged successfully.`);
// 10. Add a comment to the merged PR
try {
await octokitA.issues.createComment({
owner: REPO_OWNER,
repo: REPO_NAME,
issue_number: pr.number,
body: `## 🎉 PR Successfully Merged!
**Merge Details:**
- ✅ **Status:** Merged successfully
- 📅 **Merged at:** ${new Date().toISOString()}
- 🔄 **Merge method:** Squash merge
- 🤖 **Merged by:** Pull-Merge Bot
**Summary:**
- README.md updated with timestamp
- Code review completed (if enabled and triggered)
- All checks passed
- Repository documentation maintained
---
*This PR was automatically created, reviewed, and merged by the Pull-Merge Bot v2.0.0*`,
});
} catch (error) {
console.log("⚠️ Could not add merge comment:", error.message);
}
} catch (error) {
console.error("❌ Error:", error.message);
if (error.response) {
console.error("Response status:", error.response.status);
console.error("Response data:", error.response.data);
}
}
}
// Run the bot
createAndMergePR().catch(console.error);