Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add makePulumiCallback #5

Merged
merged 1 commit into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions examples/pulumi-slack-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as gcp from '@pulumi/gcp';
import * as pulumi from '@pulumi/pulumi';
import { makePulumiCallback } from 'gcl-slack';

const name = 'slack';

export const topic = new gcp.pubsub.Topic(name, {}, { provider });

const config = new pulumi.Config(name);

topic.onMessagePublished(name, {
region: 'europe-west1',
runtime: 'nodejs14',
environmentVariables: {
SLACK_TOKEN: config.require('token'),
},
callback: makePulumiCallback('api'),
});

const logSink = new gcp.logging.ProjectSink(name, {
filter: 'operation.producer="github.com/bjerkio/nestjs-slack@v1"',
destination: pulumi.interpolate`pubsub.googleapis.com/${topic.id}`,
});

new gcp.pubsub.TopicIAMMember(name, {
topic: topic.name,
role: 'roles/pubsub.publisher',
member: logSink.writerIdentity,
});
16 changes: 2 additions & 14 deletions examples/pulumi.ts → examples/pulumi-slack-webhook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as gcp from '@pulumi/gcp';
import * as pulumi from '@pulumi/pulumi';
import { makeHandler } from 'gcl-slack';
import { handlePubSubMessage } from 'pubsub-http-handler';
import { makePulumiCallback } from 'gcl-slack';

const name = 'slack';

Expand All @@ -15,18 +14,7 @@ topic.onMessagePublished(name, {
environmentVariables: {
WEBHOOK_URL: config.require('webhook-url'),
},
callback: async (message) => {
const handler = makeHandler({
type: 'webhook',
webhookOptions: { url: process.env.WEBHOOK_URL ?? '' },
});

await handlePubSubMessage({
message,
handler,
parseJson: true,
});
},
callback: makePulumiCallback('webhook'),
});

const logSink = new gcp.logging.ProjectSink(name, {
Expand Down
45 changes: 44 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { FastifyPluginAsync } from 'fastify';
import * as phh from 'pubsub-http-handler';
import { handlePubSubMessage } from 'pubsub-http-handler';
import { makeHandler } from './handler';
import { SlackConfig } from './types';
import { SlackConfig, SlackRequestType } from './types';
export { makeHandler } from './handler';

export const makePubSubCloudFunctions = (
Expand All @@ -22,3 +23,45 @@ export const makePubSubServer = (
): phh.CreatePubSubHandlerResponse => {
return phh.createPubSubServer(makeHandler(config));
};

export type PulumiCallbackFun = (message: any) => Promise<any>;

export const makePulumiCallback = (
type: SlackRequestType,
): PulumiCallbackFun => {
if (type === 'api') {
return async (message: any): Promise<void> => {
const token = process.env.SLACK_TOKEN;
if (!token) {
throw new Error('Slack token is missing');
}

await handlePubSubMessage({
message,
handler: makeHandler({
type: 'api',
apiOptions: { token },
}),
parseJson: true,
});
};
} else if (type === 'webhook') {
return async (message: any): Promise<void> => {
const url = process.env.WEBHOOK_URL;
if (!url) {
throw new Error('Slack webhook URL is missing');
}

await handlePubSubMessage({
message,
handler: makeHandler({
type: 'webhook',
webhookOptions: { url },
}),
parseJson: true,
});
};
}

throw new Error('Wrong configuration');
};