//1. create a DynamicTrigger
const dynamicOnIssueOpenedTrigger = client.defineDynamicTrigger({
  id: "github-issue-opened",
  event: events.onIssueOpened,
  source: github.sources.repo,
});

//2. create a Job that is attached to the dynamic trigger
client.defineJob({
  id: "listen-for-dynamic-trigger",
  name: "Listen for dynamic trigger",
  version: "0.1.1",
  trigger: dynamicOnIssueOpenedTrigger,
  integrations: {
    slack,
  },
  run: async (payload, io, ctx) => {
    await io.slack.postMessage("Slack ๐Ÿ“", {
      text: `New Issue opened on repo: ${payload.issue.html_url}. \n\n${JSON.stringify(ctx)}`,
      channel: "C04GWUTDC3W",
    });
  },
});

//3. Register the DynamicTrigger anywhere in your app
async function registerRepo(owner: string, repo: string) {
  //the first param (key) should be unique
  await dynamicOnIssueOpenedTrigger.register(`${owner}-${repo}`, {
    owner,
    repo,
  });
}

//4. Register inside other Jobs
client.defineJob({
  id: "new-repo",
  name: "New repo",
  version: "0.1.1",
  //5. when a new repo is created in your org
  trigger: github.triggers.org({
    event: events.onNewRepository,
    org: "triggerdotdev",
  }),
  run: async (payload, io, ctx) => {
    const owner = payload.repository.owner.login;
    const repo = payload.repository.name;
    //6. Register the dynamic trigger so you get notified when an issue is opened. A task will automatically be created
    await dynamicOnIssueOpenedTrigger.register(`${owner}-${repo}`, {
      owner,
      repo,
    });
  },
});

Sometimes you want to subscribe to a webhook but you donโ€™t know the exact configuration until runtime.

Constructor

DynamicTrigger()

Creates a new DynamicTrigger instance. You should use the TriggerClient.defineDynamicTrigger method instead of calling this directly.

Instance methods

register()

Use this method to register a new schedule with the DynamicTrigger.

unregister()

Use this method to unregister a schedule from the DynamicTrigger, using the id you used when registering it.

Example use cases:

Subscribe to new GitHub issues for all your repos

  1. Create a DynamicTrigger with the onIssueOpened event.
  2. Loop through all of your repos add them to it.
  3. Use an onNewRepository trigger and add them to the DynamicTrigger.
//1. create a DynamicTrigger
const dynamicOnIssueOpenedTrigger = client.defineDynamicTrigger({
  id: "github-issue-opened",
  event: events.onIssueOpened,
  source: github.sources.repo,
});

//2. create a Job that is attached to the dynamic trigger
client.defineJob({
  id: "listen-for-dynamic-trigger",
  name: "Listen for dynamic trigger",
  version: "0.1.1",
  trigger: dynamicOnIssueOpenedTrigger,
  integrations: {
    slack,
  },
  run: async (payload, io, ctx) => {
    await io.slack.postMessage("Slack ๐Ÿ“", {
      text: `New Issue opened on repo: ${payload.issue.html_url}. \n\n${JSON.stringify(ctx)}`,
      channel: "C04GWUTDC3W",
    });
  },
});

//3. Register the DynamicTrigger anywhere in your app
async function registerRepo(owner: string, repo: string) {
  //the first param (key) should be unique
  await dynamicOnIssueOpenedTrigger.register(`${owner}-${repo}`, {
    owner,
    repo,
  });
}

//4. Register inside other Jobs
client.defineJob({
  id: "new-repo",
  name: "New repo",
  version: "0.1.1",
  //5. when a new repo is created in your org
  trigger: github.triggers.org({
    event: events.onNewRepository,
    org: "triggerdotdev",
  }),
  run: async (payload, io, ctx) => {
    const owner = payload.repository.owner.login;
    const repo = payload.repository.name;
    //6. Register the dynamic trigger so you get notified when an issue is opened. A task will automatically be created
    await dynamicOnIssueOpenedTrigger.register(`${owner}-${repo}`, {
      owner,
      repo,
    });
  },
});