Skip to content

Commit 79e524d

Browse files
author
robot
committed
feat: webhook
1 parent 4848238 commit 79e524d

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

app.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,25 @@ app.use(cors({ credentials: true }));
3131
app.use(json());
3232
app.use(logger());
3333

34+
const loginWhiteList = ["/api/v1/github/content", "/api/v1/github/webhook"];
35+
3436
app.use(fallback.routes(), fallback.allowedMethods());
3537
if (process.env.NODE_ENV === "development") {
36-
app.use(mockUserInfo({ whitelist: ["/api/v1/github/content"] }));
38+
app.use(
39+
mockUserInfo({
40+
whitelist: loginWhiteList,
41+
})
42+
);
3743
} else {
38-
app.use(passport({ whitelist: ["/api/v1/github/content"] }));
44+
app.use(
45+
passport({
46+
whitelist: loginWhiteList,
47+
})
48+
);
3949
}
4050
app.use(
4151
pay({
42-
whitelist: ["/api/v1/user", "/api/v1/logout", "/api/v1/github/content"],
52+
whitelist: ["/api/v1/user", "/api/v1/logout"].concat(loginWhiteList),
4353
})
4454
);
4555
app.use(require("koa-static")(__dirname + "/public"));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"dev": "NODE_ENV=development ./node_modules/.bin/nodemon bin/www",
88
"prd": "pm2 start bin/www",
99
"test": "echo \"Error: no test specified\" && exit 1",
10-
"daily-schedule": "node ./schedule/daily-problem.js && node ./schedule/daily-check.js",
10+
"daily-schedule": "node ./schedule/daily-problem.js",
1111
"generate": "node ./static/users/generate.js && node ./static/solution/generate.js && node ./static/lectures/generate.js"
1212
},
1313
"dependencies": {

routes/github.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const router = require("koa-router")();
2-
const { success, fail } = require("../utils/request");
32
const fetch = require("node-fetch");
3+
const process = require("child_process");
4+
5+
const { getDay } = require("../utils/day");
6+
const { success, fail } = require("../utils/request");
47

58
router.all("/api/v1/github/content", async (ctx) => {
69
const { url, ...params } = ctx.query;
@@ -25,4 +28,25 @@ router.all("/api/v1/github/content", async (ctx) => {
2528
}
2629
});
2730

31+
router.all("/api/v1/github/webhook", async (ctx) => {
32+
const { action, comment } = ctx.body;
33+
if (action === "created" && comment.body.length > 20) {
34+
const mySolutions = require("../static/my/solutions.json");
35+
mySolutions[comment.user.login][getDay() - 1] = {
36+
// title: problem.title,
37+
url: comment.html_url,
38+
body: comment.body,
39+
};
40+
41+
fs.writeFileSync(
42+
path.resolve(__dirname, "../static/my/solutions.json"),
43+
JSON.stringify(mySolutions)
44+
);
45+
46+
process.exec("sh " + path.resolve(__dirname, "../scripts/commit.sh"));
47+
}
48+
49+
ctx.body = success(mySolutions[comment.user.login]);
50+
});
51+
2852
module.exports = router;

schedule/daily-check.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async function run() {
3535
mySolutions[login] = Array(91);
3636
}
3737
mySolutions[login][getDay() - 2] = {
38-
title: problem.title,
38+
// title: problem.title,
3939
url: comment.html_url,
4040
body: comment.body,
4141
}; // getDay() - 1 表示昨天,另外由于索引从 1 开始,因此需要再减去 1。

0 commit comments

Comments
 (0)