-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook.js
More file actions
54 lines (41 loc) · 1.4 KB
/
webhook.js
File metadata and controls
54 lines (41 loc) · 1.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
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(bodyParser.json());
app.post('/webhook', (req, res) => {
const { title, description } = req.body;
const fileName = `${title.replace(/\s+/g, '-').toLowerCase()}-${Date.now()}.md`;
const markdownContent = `---
title: ${title}
date: ${new Date().toISOString()}
description: ${description}
---
# ${title}
${description}
`;
const filePath = path.join(__dirname, 'questions', fileName);
fs.writeFile(filePath, markdownContent, (err) => {
if (err) {
console.error('Error writing file', err);
return res.status(500).send('Error writing file');
}
console.log(`Markdown file generated: ${fileName}`);
// Git 自动提交并推送
exec('git add . && git commit -m "Auto add new question" && git push', (error, stdout, stderr) => {
if (error) {
console.error(`Error during git push: ${error}`);
return res.status(500).send('Error during git push');
}
console.log(`Git push result: ${stdout}`);
res.status(200).send('Markdown file created and pushed successfully');
});
});
});
app.listen(PORT, () => {
console.log(`Webhook server running on port ${PORT}`);
});
// 测试一下。。。。。