Skip to content

Commit

Permalink
fix: use env variable to set slash command based on environment (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m authored Jul 25, 2024
1 parent 90c0ee9 commit aa726f7
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ npm run scripts/register-github-app.js
1. Open the "OAuth & Permissions" page
1. Click on "Install to Workspace" and grant the requested permissions
1. Copy the "Bot User OAuth Token" and set it as the value for `SLACK_BOT_TOKEN` in your `.env` file.
1.

### Verify Credentials

Expand Down
5 changes: 3 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ Example: \`/hello-github subscribe monalisa/smile\``;
* @param {object} options
* @param {import("@slack/bolt").App} options.boltApp
* @param {import("octokit").App} options.octokitApp
* @param {{ slackCommand: string }} options.settings
*/
export default async function main({ octokitApp, boltApp }) {
export default async function main({ octokitApp, boltApp, settings }) {
octokitApp.webhooks.on("issues.opened", async ({ id, payload, octokit }) => {
const owner = payload.repository.owner.login;
const repo = payload.repository.name;
Expand Down Expand Up @@ -141,7 +142,7 @@ export default async function main({ octokitApp, boltApp }) {
});

boltApp.command(
"/hello-github",
settings.slackCommand,
async ({ command, ack, respond, logger, context }) => {
const [subcommand, repository] = command.text.split(/[+ ]+/g);

Expand Down
9 changes: 8 additions & 1 deletion netlify/functions/github-webhooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const env = cleanEnv(process.env, {
// Slack App credentials
SLACK_BOT_TOKEN: str(),
SLACK_SIGNING_SECRET: str(),

// app settings
SLACK_COMMAND: str({ default: "/hello-github-local" }),
});

// export as object for testing
Expand Down Expand Up @@ -81,7 +84,11 @@ export async function setupApp() {
});

octokitApp.log.info("Register Octokit and Bolt handlers");
await state.main({ octokitApp, boltApp });
await state.main({
octokitApp,
boltApp,
settings: { slackCommand: env.SLACK_COMMAND },
});

state.octokitApp = octokitApp;
} catch (error) {
Expand Down
5 changes: 4 additions & 1 deletion netlify/functions/slack-events/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const env = cleanEnv(process.env, {
// Slack App credentials
SLACK_BOT_TOKEN: str(),
SLACK_SIGNING_SECRET: str(),

// app settings
SLACK_COMMAND: str({ default: "/hello-github-local" }),
});

const slackEventsLog = pino().child({ function: "slack-events" });
Expand Down Expand Up @@ -65,7 +68,7 @@ const octokitApp = new OctokitApp({
},
});

main({ boltApp, octokitApp });
main({ boltApp, octokitApp, settings: { slackCommand: env.SLACK_COMMAND } });

export async function handler(event, context) {
let payload;
Expand Down
10 changes: 5 additions & 5 deletions test/integration/github-webhooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ test("issues.opened event", async (t) => {
};
};

main({ octokitApp, boltApp });
main({ octokitApp, boltApp, settings: { slackCommand: "/hello-github" } });

// Act
await octokitApp.webhooks.receive({
Expand Down Expand Up @@ -165,7 +165,7 @@ test("issues.opened event - subscription not found", async (t) => {
},
});

main({ octokitApp, boltApp });
main({ octokitApp, boltApp, settings: { slackCommand: "/hello-github" } });

// Act
await octokitApp.webhooks.receive({
Expand Down Expand Up @@ -263,7 +263,7 @@ test("issues.opened event - subscription found but not for app", async (t) => {
};
};

main({ octokitApp, boltApp });
main({ octokitApp, boltApp, settings: { slackCommand: "/hello-github" } });

// Act
await octokitApp.webhooks.receive({
Expand Down Expand Up @@ -361,7 +361,7 @@ test("issues.opened event - subscription found but not for installation", async
};
};

main({ octokitApp, boltApp });
main({ octokitApp, boltApp, settings: { slackCommand: "/hello-github" } });

// Act
await octokitApp.webhooks.receive({
Expand Down Expand Up @@ -434,7 +434,7 @@ test("error in event handler", async (t) => {
throw new Error("error from event handler");
});

main({ octokitApp, boltApp });
main({ octokitApp, boltApp, settings: { slackCommand: "/hello-github" } });

// Act
await t.throwsAsync(() =>
Expand Down
14 changes: 7 additions & 7 deletions test/integration/slack-events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test("app_home_opened event", async (t) => {
is_ext_shared_channel: false,
};

main({ octokitApp, boltApp });
main({ octokitApp, boltApp, settings: { slackCommand: "/hello-github" } });

// Act
const slackEvent = {
Expand Down Expand Up @@ -168,7 +168,7 @@ test("/hello-github unknown", async (t) => {
trigger_id: "7461069880567.2367503921381.9506161dae0667c99560d2c7b9a58f16",
};

main({ octokitApp, boltApp });
main({ octokitApp, boltApp, settings: { slackCommand: "/hello-github" } });

// Act
const slackEvent = {
Expand Down Expand Up @@ -254,7 +254,7 @@ test("/hello-github help", async (t) => {
trigger_id: "7461069880567.2367503921381.9506161dae0667c99560d2c7b9a58f16",
};

main({ octokitApp, boltApp });
main({ octokitApp, boltApp, settings: { slackCommand: "/hello-github" } });

// Act
const slackEvent = {
Expand Down Expand Up @@ -340,7 +340,7 @@ test("/hello-github subscribe not-a-full-repo-name", async (t) => {
trigger_id: "7461069880567.2367503921381.9506161dae0667c99560d2c7b9a58f16",
};

main({ octokitApp, boltApp });
main({ octokitApp, boltApp, settings: { slackCommand: "/hello-github" } });

// Act
const slackEvent = {
Expand Down Expand Up @@ -430,7 +430,7 @@ test("/hello-github subscribe monalisa/smile - no installation", async (t) => {
trigger_id: "7461069880567.2367503921381.9506161dae0667c99560d2c7b9a58f16",
};

main({ octokitApp, boltApp });
main({ octokitApp, boltApp, settings: { slackCommand: "/hello-github" } });

// Act
const slackEvent = {
Expand Down Expand Up @@ -534,7 +534,7 @@ test("/hello-github subscribe monalisa/smile - no variable", async (t) => {
trigger_id: "7461069880567.2367503921381.9506161dae0667c99560d2c7b9a58f16",
};

main({ octokitApp, boltApp });
main({ octokitApp, boltApp, settings: { slackCommand: "/hello-github" } });

// Act
const slackEvent = {
Expand Down Expand Up @@ -649,7 +649,7 @@ test("/hello-github subscribe monalisa/smile - existing variable", async (t) =>
trigger_id: "7461069880567.2367503921381.9506161dae0667c99560d2c7b9a58f16",
};

main({ octokitApp, boltApp });
main({ octokitApp, boltApp, settings: { slackCommand: "/hello-github" } });

// Act
const slackEvent = {
Expand Down

0 comments on commit aa726f7

Please sign in to comment.