+
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 29 additions & 53 deletions WordpressSyncHttpTrigger/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
// @ts-check
const { GitHubTarget } = require("@cagov/wordpress-to-github/common");
const { sleep } = require("@cagov/wordpress-to-github/gitTreeCommon");
const {
slackBotReportError,
slackBotChatPost,
slackBotReplyPost,
slackBotReactionAdd
} = require("../common/slackBot");
const SlackBot = require("@cagov/slack-connector");
const endpoints = require("../WordpressSync/endpoints.json");

const debugChannel = "C02G6PETB9B"; //#wordpress-sync-http-trigger

const gitHubCommitter = {
name: process.env["GITHUB_NAME"],
email: process.env["GITHUB_EMAIL"]
};
const gitHubCredentials = {
token: process.env["GITHUB_TOKEN"]
const slackBotGetToken = () => {
const token = process.env["SLACKBOT_TOKEN"];

if (!token) {
//developers that don't set the creds can still use the rest of the code
console.error(
`You need local.settings.json to contain "SLACKBOT_TOKEN" to use slackbot features.`
);
return;
}

return token;
};

/**
Expand All @@ -31,38 +31,26 @@ const gitHubCredentials = {
* @param {{method:string,headers:{"user-agent":string},query?:{code?:string},params:{},body:{slug?:string,trigger?:string}}} req
*/
module.exports = async function (context, req) {
const appName = context.executionContext?.functionName;
try {
if (req.method !== "POST") {
context.res = {
body: `Service is running, but is expecting a POST.`
};
return;
}

let slackPostTS = "";
if (req.method !== "POST") {
context.res = {
body: `Service is running, but is expecting a POST.`
};
return;
}
const slack = new SlackBot(slackBotGetToken(), debugChannel);

try {
const TriggerName = req.body?.trigger || "(Trigger)";
const SlugName = req.body?.slug || "(slug)";
slackPostTS = (
await (
await slackBotChatPost(
debugChannel,
`Notification received - ${SlugName} - ${TriggerName}`
)
).json()
).ts;
await slack.Chat(`Notification received - ${SlugName} - ${TriggerName}`);

//clean out "code" value display
const redactedOutput = JSON.stringify(req, null, 2).replace(
new RegExp(req.query.code, "g"),
`${req.query?.code?.substring(0, 3)}[...]`
);

await slackBotReplyPost(
debugChannel,
slackPostTS,
`\n\n*Full Details*\n\`\`\`${redactedOutput}\`\`\``
);
await slack.Reply(`\n\n*Full Details*\n\`\`\`${redactedOutput}\`\`\``);

//Find endpoints that match the requestor
const postAgent = req.headers["user-agent"];
Expand All @@ -75,9 +63,7 @@ module.exports = async function (context, req) {

//if you find matches...congrats...report for now
if (activeEndpoints.length) {
await slackBotReplyPost(
debugChannel,
slackPostTS,
await slack.Reply(
`${
activeEndpoints.length
} matching endpoint(s) found ...${activeEndpoints
Expand All @@ -98,27 +84,17 @@ module.exports = async function (context, req) {
null,
activeEndpoints.map(x => x.name)
);
await slackBotReplyPost(debugChannel, slackPostTS, `Done.`);
await slack.Reply(`Done.`);
} else {
await slackBotReplyPost(
debugChannel,
slackPostTS,
`No endpoints found for...${postAgent}`
);
await slackBotReactionAdd(debugChannel, slackPostTS, "no_entry");
await slack.Reply(`No endpoints found for...${postAgent}`);
await slack.ReactionAdd("no_entry");
}

context.res = {
status: 204 //OK - No content
};
} catch (e) {
await slackBotReportError(
debugChannel,
`Error running ${appName}`,
e,
context,
null
);
await slack.Error(e, req);

context.res = {
status: 500,
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载