slackapi / deno-slack-api

Slack API Client for Deno Run on Slack projects
https://api.slack.com/automation
MIT License
36 stars 17 forks source link

Export trigger types and data #69

Closed filmaj closed 1 year ago

filmaj commented 1 year ago

The idea in this PR is to provide some string literal types for:

  1. trigger names
  2. trigger contextual data

Today, a typical trigger definition looks like:

import { Trigger } from "deno-slack-sdk/types.ts";
import PromptSurveyWorkflow from "../workflows/prompt_survey.ts";

const promptSurveyTrigger: Trigger<typeof PromptSurveyWorkflow.definition> = {
  type: "event",
  name: "Survey reacji added",
  description: "Initiate survey creation by adding a clipboard reacji",
  workflow: `#/workflows/${PromptSurveyWorkflow.definition.callback_id}`,
  event: {
    event_type: "slack#/events/reaction_added",
    channel_ids: [""], // Channel IDs are added by the configurator workflow
    filter: {
      version: 1,
      root: {
        operator: "AND",
        inputs: [{
          statement: "{{data.reaction}} == clipboard",
        }, {
          // User IDs are configured by the configurator workflow
          operator: "OR",
          inputs: [{ statement: "{{data.user_id}} == USLACKBOT" }],
        }],
      },
    },
  },
  inputs: {
    channel_id: { value: "{{data.channel_id}}" },
    parent_ts: { value: "{{data.message_ts}}" },
    reactor_id: { value: "{{data.user_id}}" },
  },
};

After this PR gets merged and released, this could instead look like:

import { Trigger } from "deno-slack-sdk/types.ts";
import { TriggerContextData, TriggerEventTypes, TriggerTypes } from "deno-slack-api/mod.ts"; // <--- NEW!
import PromptSurveyWorkflow from "../workflows/prompt_survey.ts";

const promptSurveyTrigger: Trigger<typeof PromptSurveyWorkflow.definition> = {
  type: TriggerTypes.Event, // <-- CHANGED!
  name: "Survey reacji added",
  description: "Initiate survey creation by adding a clipboard reacji",
  workflow: `#/workflows/${PromptSurveyWorkflow.definition.callback_id}`,
  event: {
    event_type: TriggerEventTypes.ReactionAdded, // <--- CHANGED!
    channel_ids: [""], // Channel IDs are added by the configurator workflow
    filter: {
      version: 1,
      root: {
        operator: "AND",
        inputs: [{
          statement: "{{data.reaction}} == clipboard",
        }, {
          // User IDs are configured by the configurator workflow
          operator: "OR",
          inputs: [{ statement: "{{data.user_id}} == USLACKBOT" }],
        }],
      },
    },
  },
  inputs: {
    channel_id: { value: TriggerContextData.Event.ReactionAdded.channel_id }, // <--- CHANGED!
    parent_ts: { value: TriggerContextData.Event.ReactionAdded.message_ts }, // <--- CHANGED!
    reactor_id: { value: TriggerContextData.Event.ReactionAdded.user_id }, // <--- CHANGED!
  },
};

Follow-up tasks post-release:

codecov[bot] commented 1 year ago

Codecov Report

Merging #69 (7a5fdef) into main (2e61dc0) will decrease coverage by 0.85%. The diff coverage is 98.96%.

@@             Coverage Diff             @@
##              main      #69      +/-   ##
===========================================
- Coverage   100.00%   99.15%   -0.85%     
===========================================
  Files            6       35      +29     
  Lines          141     1060     +919     
  Branches         9       10       +1     
===========================================
+ Hits           141     1051     +910     
- Misses           0        9       +9     
Impacted Files Coverage Δ
...d-method-types/workflows/triggers/shortcut-data.ts 95.74% <95.74%> (ø)
...types/workflows/triggers/event-data/dnd_updated.ts 96.00% <96.00%> (ø)
...event-data/common-objects/shared_channel_invite.ts 96.10% <96.10%> (ø)
...ggers/event-data/shared_channel_invite_accepted.ts 98.11% <98.11%> (ø)
...ggers/event-data/shared_channel_invite_approved.ts 98.11% <98.11%> (ø)
...ggers/event-data/shared_channel_invite_declined.ts 98.11% <98.11%> (ø)
src/mod.ts 100.00% <100.00%> (ø)
...pes/workflows/triggers/event-data/app_mentioned.ts 100.00% <100.00%> (ø)
.../workflows/triggers/event-data/channel_archived.ts 100.00% <100.00%> (ø)
...s/workflows/triggers/event-data/channel_created.ts 100.00% <100.00%> (ø)
... and 20 more

... and 3 files with indirect coverage changes

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

filmaj commented 1 year ago

Alright round one of comments addressed! Take another look. A large majority of the changes is splitting event trigger data into separate files.

filmaj commented 1 year ago

As for conflicts, I will resolve those just before merging so that reviewers can use GitHub's "View changes since last review" feature.