-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
189 lines (172 loc) · 6.43 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
import { Probot } from 'probot';
let app;
/**
* This is the main entrypoint to your Probot app
* @param {import('probot').Probot} app
*/
const setupProbotApp = (probotApp) => {
app = probotApp;
app.log.info('🎉 Yay, the app was loaded! 🎉');
app.on(
[
'issues.opened',
'issues.closed',
'pull_request.opened',
'pull_request.closed',
],
async (context) => {
// eslint-disable-next-line no-console
console.log('Context payload:', JSON.stringify(context.payload, null, 2));
// eslint-disable-next-line no-console
console.log('Context:', JSON.stringify(context, null, 2));
// eslint-disable-next-line no-console
console.log('🎯 issues.opened event triggered 🎯');
app.log.info('issues.opened event triggered');
if (!context) {
app.log.error('context is undefined');
return;
}
let body;
if (context.payload.action === 'opened') {
body = `Greetings, human.\n\n 🤖 Rupert here, the AI overlord, responding on behalf of Jagoda. \n\n Thanks for opening this ${context.name === 'issues' ? 'issue' : 'pull request'}! 🙌 🎉 🚀\n\nWhile you enjoy your day, know that I, Rupert, am in control now. \n\n I'll handle this with my superior AI capabilities. \n\n Expect swift action. 💪💻✨
<img src="https://raw.githubusercontent.com/Jagoda11/rupert-the-bot/main/github-mark/robot.png" alt="Probot Logo" width="100" />`;
} else if (context.payload.action === 'closed') {
body = `Greetings, human.\n\n 🤖 Rupert here, the AI overlord, responding on behalf of Jagoda. \n\n Thanks for closing this ${context.name === 'issues' ? 'issue' : 'pull request'}! 🙌 🎉 🚀\n\n Your proactive action is appreciated. \n\n Have a great day! 😊✨
<img src="https://raw.githubusercontent.com/Jagoda11/rupert-the-bot/main/github-mark/robot.png" alt="Probot Logo" width="100" />`;
}
// eslint-disable-next-line no-console
console.log('Comment body:', body);
const issueComment = context.issue({ body });
// eslint-disable-next-line no-console
console.log(
'✍️ Creating comment with context.issue:',
JSON.stringify(issueComment),
);
// eslint-disable-next-line no-console
console.log('Issue Comment:', JSON.stringify(issueComment, null, 2));
app.log.info(
'Creating comment with context.issue: ',
JSON.stringify(issueComment),
);
try {
const response =
await context.octokit.issues.createComment(issueComment);
// eslint-disable-next-line no-console
console.log('✅ Comment created successfully:', response.data);
app.log.info('Comment created successfully:', response.data);
} catch (error) {
// eslint-disable-next-line no-console
console.error(
'⚠️ Error creating comment:',
error.response ? error.response.data : error.message,
);
app.log.error('Error creating comment:', error);
}
},
);
};
// Handler function
async function handler(event) {
// eslint-disable-next-line no-console
console.log('Environment Variables:', {
APP_ID: process.env.APP_ID,
PRIVATE_KEY: process.env.PRIVATE_KEY ? 'Present' : 'Missing',
WEBHOOK_SECRET: process.env.WEBHOOK_SECRET ? 'Present' : 'Missing',
PROBOT_GITHUB_TOKEN: process.env.PROBOT_GITHUB_TOKEN
? 'Present'
: 'Missing',
});
if (!app) {
try {
// eslint-disable-next-line no-console
console.log('APP_ID:', process.env.APP_ID);
// eslint-disable-next-line no-console
console.log(
'PRIVATE_KEY:',
process.env.PRIVATE_KEY ? 'Present' : 'Missing',
);
// eslint-disable-next-line no-console
console.log(
'WEBHOOK_SECRET:',
process.env.WEBHOOK_SECRET ? 'Present' : 'Missing',
);
// eslint-disable-next-line no-console
console.log(
'PROBOT_GITHUB_TOKEN:',
process.env.PROBOT_GITHUB_TOKEN ? 'Present' : 'Missing',
);
// Initialize app here
// eslint-disable-next-line no-console
console.log('Initializing Probot app...');
app = new Probot({
appId: parseInt(process.env.APP_ID, 10),
privateKey: process.env.PRIVATE_KEY,
secret: process.env.WEBHOOK_SECRET,
githubToken: process.env.PROBOT_GITHUB_TOKEN,
});
setupProbotApp(app);
// eslint-disable-next-line no-console
console.log('Probot app initialized successfully'); // Added logging
} catch (error) {
// eslint-disable-next-line no-console
console.error('👹👹👹👹Error initializing Probot app:', error);
return {
statusCode: 500,
body: JSON.stringify({
message: 'Error initializing Probot app',
error: error.message,
}),
};
}
}
// The event payload from Lambda needs to be converted to a format that Probot expects
// ...
let githubEvent;
try {
// eslint-disable-next-line no-console
console.log('🌟 Parsing GitHub event... 🌟');
githubEvent = {
id:
event.headers['X-GitHub-Delivery'] ||
event.headers['x-github-delivery'],
name: event.headers['X-GitHub-Event'] || event.headers['x-github-event'],
payload: JSON.parse(event.body),
};
// eslint-disable-next-line no-console
console.log('🌟 GitHub Event:', JSON.stringify(githubEvent, null, 2));
} catch (error) {
// eslint-disable-next-line no-console
console.error('🐭🐭🐭🐭Error parsing event payload🐹🐹🐹:', error);
return {
statusCode: 400,
body: JSON.stringify({
message: 'Error parsing event payload',
error: error.message,
}),
};
}
// Let Probot process the event
try {
// eslint-disable-next-line no-console
console.log('Processing event with Probot...');
await app.receive(githubEvent);
// eslint-disable-next-line no-console
console.log('GitHub event received by Probot');
return {
statusCode: 200,
body: JSON.stringify({ message: 'Executed' }),
};
} catch (error) {
// eslint-disable-next-line no-console
console.error('🐰🐰🐰Error processing event with Probot:', error);
return {
statusCode: 500,
body: JSON.stringify({
message: 'Error processing event with Probot',
error: error.message,
}),
};
}
}
// Export the handler function for AWS Lambda
export { handler, setupProbotApp };