-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
141 lines (114 loc) · 3.88 KB
/
index.js
File metadata and controls
141 lines (114 loc) · 3.88 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
const express = require("express");
const bodyParser = require("body-parser");
const _ = require("lodash");
const config = require("./config");
const github = require("./github");
const { getFileName, getPostFileContent } = require("./utils");
const app = express();
app.use(bodyParser.json());
// possible event_types in the webhook body
const EVENT_TYPES = {
CREATE: "post_create",
UPDATE: "post_update",
DELETE: "post_delete",
};
// listening for post requests for able
app.post("/", async (request, response) => {
const { body } = request;
try {
// get the able token from the Authorization header
const authHeader = request.headers.authorization;
const token = authHeader && authHeader.split(" ").pop();
// check if it matches the secret token you have generated in your Able webhook settings
if (token !== config.ableToken) {
// if it doesn't match then the request is not from Able and can be disregarded
response.status(401).json({ details: "Invalid Request" });
return;
}
// get the event type of the webhook request from the metadata
const eventType = body.event;
// get the slug of the post from the metadata
const slug = _.get(body, "metadata.slug_id");
// if the event type is DELETE
if (eventType === EVENT_TYPES.DELETE) {
// get the post from Github repo
const post = await github.getPost(slug);
// if the post exists in Github repo
if (post) {
// delete the post from the repo
await github.deletePost({
name: post.name,
message: `delete: ${post.name}`,
sha: post.sha,
});
}
response.status(200).json({ details: `Post Deleted: ${post.name}` });
}
// get the content properties from the request body
const title = _.get(body, "content.title");
const contentBody = _.get(body, "content.body");
const description = _.get(body, "content.subtitle");
const tags = _.get(body, "content.tags");
// get slugified file name
const fileName = getFileName({ title, slug });
if (eventType === EVENT_TYPES.CREATE) {
const createdAt = _.get(body, "metadata.created_at");
// encode the file content to be committed
const content = getPostFileContent({
title,
body: contentBody,
description,
tags,
date: createdAt,
});
// create the file in the repo
await github.createPost({
name: fileName,
content,
message: `create: ${fileName}`,
});
response.status(200).json({ details: `Post Created: ${fileName}` });
return;
}
if (eventType === EVENT_TYPES.UPDATE) {
const updatedAt = _.get(body, "metadata.updated_at");
// encode the file content to be committed
const content = getPostFileContent({
title,
body: contentBody,
description,
tags,
date: updatedAt,
});
const post = await github.getPost(slug);
// if post is present in github already, let's update its content
if (post) {
await github.updatePost({
name: post.name,
content,
sha: post.sha,
message: `update: ${post.name}`,
});
} else {
// if post is not already in github, let's create it
await github.createPost({
name: fileName,
content,
message: `create: ${fileName}`,
});
}
response.status(200).json({ details: `Post Updated: ${fileName}` });
return;
}
} catch (err) {
response.status(500).json({ details: "Failed to push changes to Github" });
}
});
// a catch all route
app.all("*", (request, response) => {
response.status(400).json({ details: "Invalid Request" });
});
// listening for requests coming in
const listener = app.listen(config.port, () => {
console.log("Server is listening on port " + listener.address().port);
});