-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
188 lines (158 loc) · 6.03 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
import { Probot } from "probot";
import { exec } from "child_process";
import util from 'util';
import fs from 'fs/promises';
import dotenv from 'dotenv';
dotenv.config();
const execPromise = util.promisify(exec);
async function checkApprovalStatus(context, pull_request, action) {
const { repository } = context.payload;
if (action === 'reopened') {
const status = context.repo({
sha: pull_request.head.sha,
state: 'pending',
context: 'Approval Review',
description: 'Waiting for new approval after reopening'
});
return await context.octokit.repos.createCommitStatus(status);
}
const reviews = await context.octokit.pulls.listReviews({
owner: repository.owner.login,
repo: repository.name,
pull_number: pull_request.number,
});
let hasApproval = false;
if (action === 'synchronize') {
const lastPushDate = new Date(pull_request.head.repo.pushed_at);
hasApproval = reviews.data.some(review =>
review.state === 'APPROVED' && new Date(review.submitted_at) > lastPushDate
);
} else {
hasApproval = reviews.data.some(review => review.state === 'APPROVED');
}
const status = context.repo({
sha: pull_request.head.sha,
state: hasApproval ? 'success' : 'pending',
context: 'Approval Review',
description: hasApproval ? 'Approved by a reviewer' : 'Waiting for approval'
});
await context.octokit.repos.createCommitStatus(status);
}
export default (app) => {
// Event handler for when a pull request is opened
app.on(['pull_request.opened', 'pull_request.synchronize', 'pull_request.reopened'], async (context) => {
const { repository, pull_request, action } = context.payload;
// Post a status update
const status = context.repo({
sha: pull_request.head.sha,
state: 'pending',
context: 'DEPLOY',
description: 'Automated PR deployment bot is running'
});
await context.octokit.repos.createCommitStatus(status);
// Check approval status
await checkApprovalStatus(context, pull_request, action);
try {
// Run the deployment script
const branchName = context.payload.pull_request.head.ref;
const pullRequestNumber = pull_request.number;
const forkedRepoUrl = context.payload.pull_request.head.repo.clone_url;
const { stdout, stderr } = await execPromise(`chmod +x ./deploy.sh && sudo ./deploy.sh ${branchName} ${pullRequestNumber} ${forkedRepoUrl}`);
console.log('Deployment output:', stdout);
const port = await fs.readFile('port.txt', 'utf8');
const deploymentIp = process.env.DEPLOYMENT_IP;
const deploymentUrl = `http://${deploymentIp}:${port.trim()}`;
if (stderr) {
console.error('Deployment errors:', stderr);
}
const currentDate = new Date().toUTCString();
const markdownComment = `
Deployment Successful
| Branch | Status | Preview |Updated (UTC) |
|--------|--------|---------|--------------|
| ${context.payload.pull_request.head.ref} | ✅ Success ([Inspect](${deploymentUrl})) | [Visit Preview](${deploymentUrl}) | ${currentDate} |
`;
// Update comment with success
const comment = context.issue({
body: markdownComment
});
await context.octokit.issues.createComment(comment);
// Post a success status update
const status = context.repo({
sha: pull_request.head.sha,
state: 'success',
context: 'DEPLOY',
description: 'PR deployed successfully',
target_url: `${deploymentUrl}`
});
await context.octokit.repos.createCommitStatus(status);
// Clean up the temporary file
await fs.unlink('port.txt');
} catch (error) {
// Update comment with failure
console.error('Deployment errors:', error);
const currentDate = new Date().toUTCString();
const markdownComment = `
Deployment Failed
| Branch | Status | Preview |Updated (UTC) |
|--------|--------|---------|--------------|
| ${context.payload.pull_request.head.ref} | ❌ Failed | ${currentDate} |
`;
const comment = context.issue({
body: markdownComment
});
await context.octokit.issues.createComment(comment);
// Post a failure status update
const status = context.repo({
sha: pull_request.head.sha,
state: 'failure',
context: 'DEPLOY',
description: 'PR deployment failed'
});
await context.octokit.repos.createCommitStatus(status);
}
});
app.on('pull_request_review.submitted', async (context) => {
const { pull_request } = context.payload;
await checkApprovalStatus(context, pull_request, 'review_submitted');
});
// Event handler for when a pull request is closed
app.on('pull_request.closed', async (context) => {
const { repository, pull_request } = context.payload;
if (pull_request.merged) {
// Post a success status update
const status = context.repo({
sha: pull_request.head.sha,
state: 'success',
context: 'MERGE',
description: 'PR merged successfully'
});
await context.octokit.repos.createCommitStatus(status);
// Post a PR Merged success message
const comment = context.issue({
body: 'The Pull Request has been merged!'
});
await context.octokit.issues.createComment(comment);
}
try {
// Clean up resources
const branchName = context.payload.pull_request.head.ref;
const { stdout, stderr } = await execPromise(`chmod +x ./cleanup.sh && sudo ./cleanup.sh ${branchName}`);
console.log('Cleanup output:', stdout);
if (stderr) {
console.error(`Cleanup errors: ${stderr}`);
}
// Post a cleanup message
const comment = context.issue({
body: 'Resources cleaned up after PR closure.'
});
await context.octokit.issues.createComment(comment);
} catch (error) {
// Update comment with failure
const comment = context.issue({
body: 'Cleanup failed.'
});
await context.octokit.issues.createComment(comment);
}
});
};