From 10cb597165bc994e8078ca28d3a8daa371a20b35 Mon Sep 17 00:00:00 2001 From: William Bergamin Date: Fri, 23 May 2025 14:59:56 -0400 Subject: [PATCH] chore: update to latests Deno and CLI standards --- .github/dependabot.yml | 6 +++ .github/workflows/deno.yml | 8 ++-- .gitignore | 1 - .slack/.gitignore | 2 + slack.json => .slack/hooks.json | 0 README.md | 9 ++--- deno.jsonc | 11 ++++-- functions/sample_function.ts | 6 +-- functions/sample_function_test.ts | 64 ++++++++++++++++++------------- import_map.json | 8 ---- triggers/sample_trigger.ts | 2 +- 11 files changed, 67 insertions(+), 50 deletions(-) create mode 100644 .github/dependabot.yml create mode 100644 .slack/.gitignore rename slack.json => .slack/hooks.json (100%) delete mode 100644 import_map.json diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..8ac6b8c --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "monthly" diff --git a/.github/workflows/deno.yml b/.github/workflows/deno.yml index f5a5020..ba20c7f 100644 --- a/.github/workflows/deno.yml +++ b/.github/workflows/deno.yml @@ -2,9 +2,11 @@ name: Deno app build and testing on: push: - branches: [ main ] + branches: + - main pull_request: - branches: [ main ] + branches: + - main jobs: deno: @@ -18,7 +20,7 @@ jobs: - name: Setup Deno uses: denoland/setup-deno@v1 with: - deno-version: v1.x + deno-version: v2.x - name: Verify formatting run: deno fmt --check diff --git a/.gitignore b/.gitignore index 68dfaa9..1ca605b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ dist package .DS_Store -.slack/apps.dev.json diff --git a/.slack/.gitignore b/.slack/.gitignore new file mode 100644 index 0000000..973ba60 --- /dev/null +++ b/.slack/.gitignore @@ -0,0 +1,2 @@ +apps.dev.json +cache/ diff --git a/slack.json b/.slack/hooks.json similarity index 100% rename from slack.json rename to .slack/hooks.json diff --git a/README.md b/README.md index 7e20676..a0c88a6 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,10 @@ $ slack activity --tail Contains `apps.dev.json` and `apps.json`, which include installation details for development and deployed apps. +Contains `hooks.json` used by the CLI to interact with the project's SDK +dependencies. It contains script hooks that are executed by the CLI and +implemented by the SDK. + ### `datastores/` [Datastores](https://api.slack.com/automation/datastores) securely store data @@ -185,11 +189,6 @@ continuing to the next step. The [app manifest](https://api.slack.com/automation/manifest) contains the app's configuration. This file defines attributes like app name and description. -### `slack.json` - -Used by the CLI to interact with the project's SDK dependencies. It contains -script hooks that are executed by the CLI and implemented by the SDK. - ## Resources To learn more about developing automations on Slack, visit the following: diff --git a/deno.jsonc b/deno.jsonc index 56029c4..535414b 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -1,5 +1,5 @@ { - "$schema": "https://deno.land/x/deno/cli/schemas/config-file.v1.json", + "$schema": "https://raw.githubusercontent.com/denoland/deno/main/cli/schemas/config-file.v1.json", "fmt": { "include": [ "README.md", @@ -13,7 +13,6 @@ "workflows" ] }, - "importMap": "import_map.json", "lint": { "include": [ "datastores", @@ -28,6 +27,12 @@ }, "lock": false, "tasks": { - "test": "deno fmt --check && deno lint && deno test --allow-read --allow-none" + "test": "deno fmt --check && deno lint && deno test --allow-read" + }, + "imports": { + "@std/assert": "jsr:@std/assert@^1.0.13", + "@std/testing": "jsr:@std/testing@^1.0.12", + "deno-slack-sdk/": "https://deno.land/x/deno_slack_sdk@2.15.0/", + "deno-slack-api/": "https://deno.land/x/deno_slack_api@2.8.0/" } } diff --git a/functions/sample_function.ts b/functions/sample_function.ts index 4b1324b..d50394f 100644 --- a/functions/sample_function.ts +++ b/functions/sample_function.ts @@ -1,5 +1,5 @@ import { DefineFunction, Schema, SlackFunction } from "deno-slack-sdk/mod.ts"; -import SampleObjectDatastore from "../datastores/sample_datastore.ts"; +import type SampleObjectDatastore from "../datastores/sample_datastore.ts"; /** * Functions are reusable building blocks of automation that accept @@ -49,8 +49,8 @@ export default SlackFunction( // inputs.user is set from the interactivity_context defined in sample_trigger.ts // https://api.slack.com/automation/forms#add-interactivity - const updatedMsg = `:wave: ` + `<@${inputs.user}>` + - ` submitted the following message: \n\n>${inputs.message}`; + const updatedMsg = + `:wave: <@${inputs.user}> submitted the following message: \n\n>${inputs.message}`; const sampleObject = { original_msg: inputs.message, diff --git a/functions/sample_function_test.ts b/functions/sample_function_test.ts index 65c2669..3644cae 100644 --- a/functions/sample_function_test.ts +++ b/functions/sample_function_test.ts @@ -1,32 +1,33 @@ import { SlackFunctionTester } from "deno-slack-sdk/mod.ts"; -import { - assertEquals, - assertExists, - assertStringIncludes, -} from "std/testing/asserts.ts"; +import { assertEquals, assertExists, assertStringIncludes } from "@std/assert"; +import { stub } from "@std/testing/mock"; import SampleFunction from "./sample_function.ts"; -import * as mf from "mock-fetch/mod.ts"; const { createContext } = SlackFunctionTester("sample_function"); -// Replaces globalThis.fetch with the mocked copy -mf.install(); +Deno.test("Sample function test", async () => { + // Replaces globalThis.fetch with the mocked copy + using _stubFetch = stub( + globalThis, + "fetch", + async (url: string | URL | Request, options?: RequestInit) => { + const request = url instanceof Request ? url : new Request(url, options); + + assertEquals(request.method, "POST"); + assertEquals(request.url, "https://slack.com/api/apps.datastore.put"); -// Shared mocks can be defined at the top level of tests -mf.mock("POST@/api/apps.datastore.put", async (args) => { - const body = await args.formData(); - const datastore = body.get("datastore"); - const item = body.get("item"); + const body = await request.formData(); + const datastore = body.get("datastore"); + const item = body.get("item"); - return new Response( - `{"ok": true, "datastore": "${datastore}", "item": ${item}}`, - { - status: 200, + return new Response( + `{"ok": true, "datastore": "${datastore}", "item": ${item}}`, + { + status: 200, + }, + ); }, ); -}); - -Deno.test("Sample function test", async () => { const inputs = { message: "Hello, World!", user: "U01234567" }; const { outputs, error } = await SampleFunction(createContext({ inputs })); @@ -38,12 +39,23 @@ Deno.test("Sample function test", async () => { }); Deno.test("Sample function datastore error handling", async () => { - // Mocks specific to a test can be overriden within a test - mf.mock("POST@/api/apps.datastore.put", () => { - return new Response(`{"ok": false, "error": "datastore_error"}`, { - status: 200, - }); - }); + // Replaces globalThis.fetch with the mocked copy + using _stubFetch = stub( + globalThis, + "fetch", + (url: string | URL | Request, options?: RequestInit) => { + const request = url instanceof Request ? url : new Request(url, options); + + assertEquals(request.method, "POST"); + assertEquals(request.url, "https://slack.com/api/apps.datastore.put"); + + return Promise.resolve( + new Response(`{"ok": false, "error": "datastore_error"}`, { + status: 200, + }), + ); + }, + ); const inputs = { message: "Hello, World!", user: "U01234567" }; const { outputs, error } = await SampleFunction(createContext({ inputs })); diff --git a/import_map.json b/import_map.json deleted file mode 100644 index 8144976..0000000 --- a/import_map.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "imports": { - "deno-slack-sdk/": "https://deno.land/x/deno_slack_sdk@2.15.0/", - "deno-slack-api/": "https://deno.land/x/deno_slack_api@2.8.0/", - "std/": "https://deno.land/std@0.224.0/", - "mock-fetch/": "https://deno.land/x/mock_fetch@0.3.0/" - } -} diff --git a/triggers/sample_trigger.ts b/triggers/sample_trigger.ts index 2190023..205e2f1 100644 --- a/triggers/sample_trigger.ts +++ b/triggers/sample_trigger.ts @@ -1,4 +1,4 @@ -import { Trigger } from "deno-slack-sdk/types.ts"; +import type { Trigger } from "deno-slack-sdk/types.ts"; import { TriggerContextData, TriggerTypes } from "deno-slack-api/mod.ts"; import SampleWorkflow from "../workflows/sample_workflow.ts"; /**