@agentjam/sdk 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +181 -2
- package/dist/adapters/ai.d.ts +47 -0
- package/dist/adapters/ai.d.ts.map +1 -0
- package/dist/adapters/ai.js +168 -0
- package/dist/adapters/ai.js.map +1 -0
- package/dist/adapters/anthropic.d.ts +68 -0
- package/dist/adapters/anthropic.d.ts.map +1 -0
- package/dist/adapters/anthropic.js +193 -0
- package/dist/adapters/anthropic.js.map +1 -0
- package/dist/adapters/openai.d.ts +35 -0
- package/dist/adapters/openai.d.ts.map +1 -0
- package/dist/adapters/openai.js +80 -0
- package/dist/adapters/openai.js.map +1 -0
- package/dist/adapters/openaiAgents.d.ts +39 -0
- package/dist/adapters/openaiAgents.d.ts.map +1 -0
- package/dist/adapters/openaiAgents.js +152 -0
- package/dist/adapters/openaiAgents.js.map +1 -0
- package/dist/adapters/shared.d.ts +72 -0
- package/dist/adapters/shared.d.ts.map +1 -0
- package/dist/adapters/shared.js +174 -0
- package/dist/adapters/shared.js.map +1 -0
- package/dist/agent.d.ts +462 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +1820 -0
- package/dist/agent.js.map +1 -0
- package/dist/artifacts.d.ts +79 -0
- package/dist/artifacts.d.ts.map +1 -0
- package/dist/artifacts.js +367 -0
- package/dist/artifacts.js.map +1 -0
- package/dist/attachments.d.ts +43 -0
- package/dist/attachments.d.ts.map +1 -0
- package/dist/attachments.js +69 -0
- package/dist/attachments.js.map +1 -0
- package/dist/connections.d.ts +109 -0
- package/dist/connections.d.ts.map +1 -0
- package/dist/connections.js +274 -0
- package/dist/connections.js.map +1 -0
- package/dist/content.d.ts +36 -0
- package/dist/content.d.ts.map +1 -0
- package/dist/content.js +21 -0
- package/dist/content.js.map +1 -0
- package/dist/cron.d.ts +31 -0
- package/dist/cron.d.ts.map +1 -0
- package/dist/cron.js +93 -0
- package/dist/cron.js.map +1 -0
- package/dist/errors.d.ts +227 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +253 -0
- package/dist/errors.js.map +1 -0
- package/dist/events.d.ts +57 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +24 -0
- package/dist/events.js.map +1 -0
- package/dist/idempotency.d.ts +21 -0
- package/dist/idempotency.d.ts.map +1 -0
- package/dist/idempotency.js +46 -0
- package/dist/idempotency.js.map +1 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/inputs.d.ts +22 -0
- package/dist/inputs.d.ts.map +1 -0
- package/dist/inputs.js +72 -0
- package/dist/inputs.js.map +1 -0
- package/dist/mpp.d.ts +76 -0
- package/dist/mpp.d.ts.map +1 -0
- package/dist/mpp.js +113 -0
- package/dist/mpp.js.map +1 -0
- package/dist/oauth.d.ts +3 -0
- package/dist/oauth.d.ts.map +1 -0
- package/dist/oauth.js +2 -0
- package/dist/oauth.js.map +1 -0
- package/dist/oauthCallbacks.d.ts +2 -0
- package/dist/oauthCallbacks.d.ts.map +1 -0
- package/dist/oauthCallbacks.js +58 -0
- package/dist/oauthCallbacks.js.map +1 -0
- package/dist/platforms/datadog.d.ts +10 -0
- package/dist/platforms/datadog.d.ts.map +1 -0
- package/dist/platforms/datadog.js +15 -0
- package/dist/platforms/datadog.js.map +1 -0
- package/dist/platforms/github.d.ts +181 -0
- package/dist/platforms/github.d.ts.map +1 -0
- package/dist/platforms/github.js +468 -0
- package/dist/platforms/github.js.map +1 -0
- package/dist/platforms/google.d.ts +19 -0
- package/dist/platforms/google.d.ts.map +1 -0
- package/dist/platforms/google.js +70 -0
- package/dist/platforms/google.js.map +1 -0
- package/dist/platforms/grafana.d.ts +10 -0
- package/dist/platforms/grafana.d.ts.map +1 -0
- package/dist/platforms/grafana.js +14 -0
- package/dist/platforms/grafana.js.map +1 -0
- package/dist/platforms/posthog.d.ts +10 -0
- package/dist/platforms/posthog.d.ts.map +1 -0
- package/dist/platforms/posthog.js +14 -0
- package/dist/platforms/posthog.js.map +1 -0
- package/dist/platforms/presets.d.ts +34 -0
- package/dist/platforms/presets.d.ts.map +1 -0
- package/dist/platforms/presets.js +36 -0
- package/dist/platforms/presets.js.map +1 -0
- package/dist/platforms/sentry.d.ts +79 -0
- package/dist/platforms/sentry.d.ts.map +1 -0
- package/dist/platforms/sentry.js +124 -0
- package/dist/platforms/sentry.js.map +1 -0
- package/dist/platforms/slack.d.ts +101 -0
- package/dist/platforms/slack.d.ts.map +1 -0
- package/dist/platforms/slack.js +309 -0
- package/dist/platforms/slack.js.map +1 -0
- package/dist/platforms/splunk.d.ts +10 -0
- package/dist/platforms/splunk.d.ts.map +1 -0
- package/dist/platforms/splunk.js +14 -0
- package/dist/platforms/splunk.js.map +1 -0
- package/dist/polling.d.ts +2 -0
- package/dist/polling.d.ts.map +1 -0
- package/dist/polling.js +66 -0
- package/dist/polling.js.map +1 -0
- package/dist/profile.d.ts +60 -0
- package/dist/profile.d.ts.map +1 -0
- package/dist/profile.js +119 -0
- package/dist/profile.js.map +1 -0
- package/dist/protocol.d.ts +474 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +2 -0
- package/dist/protocol.js.map +1 -0
- package/dist/providers.d.ts +113 -0
- package/dist/providers.d.ts.map +1 -0
- package/dist/providers.js +98 -0
- package/dist/providers.js.map +1 -0
- package/dist/recaps.d.ts +10 -0
- package/dist/recaps.d.ts.map +1 -0
- package/dist/recaps.js +49 -0
- package/dist/recaps.js.map +1 -0
- package/dist/relay/chains.d.ts +62 -0
- package/dist/relay/chains.d.ts.map +1 -0
- package/dist/relay/chains.js +150 -0
- package/dist/relay/chains.js.map +1 -0
- package/dist/relay/credentials.d.ts +25 -0
- package/dist/relay/credentials.d.ts.map +1 -0
- package/dist/relay/credentials.js +234 -0
- package/dist/relay/credentials.js.map +1 -0
- package/dist/relay/endpoints.d.ts +13 -0
- package/dist/relay/endpoints.d.ts.map +1 -0
- package/dist/relay/endpoints.js +13 -0
- package/dist/relay/endpoints.js.map +1 -0
- package/dist/relay/frames.d.ts +193 -0
- package/dist/relay/frames.d.ts.map +1 -0
- package/dist/relay/frames.js +161 -0
- package/dist/relay/frames.js.map +1 -0
- package/dist/relay/grants.d.ts +158 -0
- package/dist/relay/grants.d.ts.map +1 -0
- package/dist/relay/grants.js +2 -0
- package/dist/relay/grants.js.map +1 -0
- package/dist/relay/guestStore.d.ts +37 -0
- package/dist/relay/guestStore.d.ts.map +1 -0
- package/dist/relay/guestStore.js +61 -0
- package/dist/relay/guestStore.js.map +1 -0
- package/dist/relay/guests.d.ts +7 -0
- package/dist/relay/guests.d.ts.map +1 -0
- package/dist/relay/guests.js +30 -0
- package/dist/relay/guests.js.map +1 -0
- package/dist/relay/memory.d.ts +60 -0
- package/dist/relay/memory.d.ts.map +1 -0
- package/dist/relay/memory.js +334 -0
- package/dist/relay/memory.js.map +1 -0
- package/dist/relay/payments.d.ts +43 -0
- package/dist/relay/payments.d.ts.map +1 -0
- package/dist/relay/payments.js +115 -0
- package/dist/relay/payments.js.map +1 -0
- package/dist/relay/profiles.d.ts +10 -0
- package/dist/relay/profiles.d.ts.map +1 -0
- package/dist/relay/profiles.js +43 -0
- package/dist/relay/profiles.js.map +1 -0
- package/dist/relay/relay.d.ts +133 -0
- package/dist/relay/relay.d.ts.map +1 -0
- package/dist/relay/relay.js +2 -0
- package/dist/relay/relay.js.map +1 -0
- package/dist/relay/remote.d.ts +39 -0
- package/dist/relay/remote.d.ts.map +1 -0
- package/dist/relay/remote.js +492 -0
- package/dist/relay/remote.js.map +1 -0
- package/dist/relay/revisions.d.ts +13 -0
- package/dist/relay/revisions.d.ts.map +1 -0
- package/dist/relay/revisions.js +34 -0
- package/dist/relay/revisions.js.map +1 -0
- package/dist/relay/schedules.d.ts +41 -0
- package/dist/relay/schedules.d.ts.map +1 -0
- package/dist/relay/schedules.js +217 -0
- package/dist/relay/schedules.js.map +1 -0
- package/dist/relay/secrets.d.ts +14 -0
- package/dist/relay/secrets.d.ts.map +1 -0
- package/dist/relay/secrets.js +30 -0
- package/dist/relay/secrets.js.map +1 -0
- package/dist/relay/sessions.d.ts +59 -0
- package/dist/relay/sessions.d.ts.map +1 -0
- package/dist/relay/sessions.js +282 -0
- package/dist/relay/sessions.js.map +1 -0
- package/dist/relay/uploads.d.ts +15 -0
- package/dist/relay/uploads.d.ts.map +1 -0
- package/dist/relay/uploads.js +55 -0
- package/dist/relay/uploads.js.map +1 -0
- package/dist/requests.d.ts +22 -0
- package/dist/requests.d.ts.map +1 -0
- package/dist/requests.js +64 -0
- package/dist/requests.js.map +1 -0
- package/dist/screening.d.ts +93 -0
- package/dist/screening.d.ts.map +1 -0
- package/dist/screening.js +176 -0
- package/dist/screening.js.map +1 -0
- package/dist/secrets.d.ts +38 -0
- package/dist/secrets.d.ts.map +1 -0
- package/dist/secrets.js +162 -0
- package/dist/secrets.js.map +1 -0
- package/dist/skills.d.ts +11 -0
- package/dist/skills.d.ts.map +1 -0
- package/dist/skills.js +55 -0
- package/dist/skills.js.map +1 -0
- package/dist/stripe.d.ts +10 -0
- package/dist/stripe.d.ts.map +1 -0
- package/dist/stripe.js +51 -0
- package/dist/stripe.js.map +1 -0
- package/dist/tasks/access.d.ts +35 -0
- package/dist/tasks/access.d.ts.map +1 -0
- package/dist/tasks/access.js +101 -0
- package/dist/tasks/access.js.map +1 -0
- package/dist/tasks/incoming.d.ts +333 -0
- package/dist/tasks/incoming.d.ts.map +1 -0
- package/dist/tasks/incoming.js +354 -0
- package/dist/tasks/incoming.js.map +1 -0
- package/dist/tasks/outgoing.d.ts +137 -0
- package/dist/tasks/outgoing.d.ts.map +1 -0
- package/dist/tasks/outgoing.js +233 -0
- package/dist/tasks/outgoing.js.map +1 -0
- package/dist/tasks/scheduled.d.ts +76 -0
- package/dist/tasks/scheduled.d.ts.map +1 -0
- package/dist/tasks/scheduled.js +110 -0
- package/dist/tasks/scheduled.js.map +1 -0
- package/dist/tasks/triggers.d.ts +47 -0
- package/dist/tasks/triggers.d.ts.map +1 -0
- package/dist/tasks/triggers.js +149 -0
- package/dist/tasks/triggers.js.map +1 -0
- package/dist/tasks/watches.d.ts +13 -0
- package/dist/tasks/watches.d.ts.map +1 -0
- package/dist/tasks/watches.js +54 -0
- package/dist/tasks/watches.js.map +1 -0
- package/dist/tools.d.ts +85 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +627 -0
- package/dist/tools.js.map +1 -0
- package/dist/types.d.ts +926 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/vaults/local.d.ts +54 -0
- package/dist/vaults/local.d.ts.map +1 -0
- package/dist/vaults/local.js +362 -0
- package/dist/vaults/local.js.map +1 -0
- package/dist/vaults/remote.d.ts +78 -0
- package/dist/vaults/remote.d.ts.map +1 -0
- package/dist/vaults/remote.js +295 -0
- package/dist/vaults/remote.js.map +1 -0
- package/dist/vaults/vault.d.ts +69 -0
- package/dist/vaults/vault.d.ts.map +1 -0
- package/dist/vaults/vault.js +19 -0
- package/dist/vaults/vault.js.map +1 -0
- package/dist/verdicts.d.ts +15 -0
- package/dist/verdicts.d.ts.map +1 -0
- package/dist/verdicts.js +17 -0
- package/dist/verdicts.js.map +1 -0
- package/dist/watermarks.d.ts +29 -0
- package/dist/watermarks.d.ts.map +1 -0
- package/dist/watermarks.js +11 -0
- package/dist/watermarks.js.map +1 -0
- package/package.json +152 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"datadog.js","sourceRoot":"","sources":["../../src/platforms/datadog.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAErD;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,qBAAqB,CAAC,SAAS,EAAE;IAChE,MAAM;IACN,QAAQ;IACR,gBAAgB;CACjB,CAAC,CAAC"}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import type { CredentialSource, OAuthConnection, OAuthPresetOptions } from "../connections.js";
|
|
2
|
+
import type { EventSource } from "../events.js";
|
|
3
|
+
import type { OAuthProfile } from "../providers.js";
|
|
4
|
+
import type { JsonSchema, TriggerSchedule } from "../types.js";
|
|
5
|
+
import type { WatermarkStore } from "../watermarks.js";
|
|
6
|
+
/** Options for `githubTriggers()`. */
|
|
7
|
+
export type GitHubEventsOptions = {
|
|
8
|
+
/** Where polls keep each schedule's progress. Defaults to a `MemoryWatermarkStore`, which forgets it on exit. */
|
|
9
|
+
watermarks?: WatermarkStore;
|
|
10
|
+
/** How often a schedule that doesn't set `pollIntervalMinutes` polls GitHub. Defaults to 2. */
|
|
11
|
+
pollIntervalMinutes?: number;
|
|
12
|
+
/** Handles a failed poll, which is tried again at the next interval. Defaults to `console.error`. */
|
|
13
|
+
onError?: (error: unknown) => void;
|
|
14
|
+
};
|
|
15
|
+
/** Options for `githubAppConnection()`. */
|
|
16
|
+
export type GitHubAppConnectionOptions = OAuthPresetOptions & {
|
|
17
|
+
/** The app's slug, as its public page URL shows it. */
|
|
18
|
+
slug: string;
|
|
19
|
+
};
|
|
20
|
+
/** Options for `githubWebhooks()`. */
|
|
21
|
+
export type GitHubWebhooksOptions = {
|
|
22
|
+
/** The secret of the GitHub App's or repository's webhook, which signs each delivery. */
|
|
23
|
+
secret: string;
|
|
24
|
+
};
|
|
25
|
+
/** A pull request, as the `task.event.data` of a `pull_request_opened` run. */
|
|
26
|
+
export type GitHubPullRequestData = {
|
|
27
|
+
/** The repository, as `owner/name`. */
|
|
28
|
+
repository: string;
|
|
29
|
+
repositoryId: number;
|
|
30
|
+
number: number;
|
|
31
|
+
title: string;
|
|
32
|
+
url: string;
|
|
33
|
+
/** The login of the pull request's author, unless their account was deleted. */
|
|
34
|
+
author: string | null;
|
|
35
|
+
draft: boolean;
|
|
36
|
+
/** Whether the pull request's branch lives in a fork, or in a fork that was deleted. */
|
|
37
|
+
fork: boolean;
|
|
38
|
+
baseBranch: string;
|
|
39
|
+
headBranch: string;
|
|
40
|
+
headSha: string;
|
|
41
|
+
};
|
|
42
|
+
/** A merged pull request, as the `task.event.data` of a `pull_request_merged` run. */
|
|
43
|
+
export type GitHubMergedPullRequestData = GitHubPullRequestData & {
|
|
44
|
+
mergedAt: string;
|
|
45
|
+
mergeCommitSha: string | null;
|
|
46
|
+
};
|
|
47
|
+
/** What an issue event changed: the login of the user it assigned the issue to, or the label it added. */
|
|
48
|
+
export type GitHubIssueChange = {
|
|
49
|
+
action: "assigned";
|
|
50
|
+
assignee: string;
|
|
51
|
+
} | {
|
|
52
|
+
action: "labeled";
|
|
53
|
+
label: string;
|
|
54
|
+
};
|
|
55
|
+
/** An issue assigned to the schedule's user or labeled with its label, as the `task.event.data` of an `issue_assigned` run. */
|
|
56
|
+
export type GitHubIssueAssignedData = {
|
|
57
|
+
/** The repository, as `owner/name`. */
|
|
58
|
+
repository: string;
|
|
59
|
+
repositoryId: number;
|
|
60
|
+
number: number;
|
|
61
|
+
title: string;
|
|
62
|
+
url: string;
|
|
63
|
+
} & GitHubIssueChange;
|
|
64
|
+
/** How to run the flow of a GitHub OAuth app, whose broader scopes imply narrower ones, such as `repo` implying `public_repo`. */
|
|
65
|
+
export declare const githubProfile: OAuthProfile;
|
|
66
|
+
/**
|
|
67
|
+
* Builds the profile of the GitHub App with `slug`, whose link opens the app's installation page so the human picks
|
|
68
|
+
* repositories and authorizes in one visit. The app needs "Request user authorization (OAuth) during installation" enabled.
|
|
69
|
+
*
|
|
70
|
+
* @param slug The app's slug, as its public page URL shows it.
|
|
71
|
+
* @returns The app's profile.
|
|
72
|
+
*/
|
|
73
|
+
export declare const constructGitHubAppProfile: (slug: string) => OAuthProfile;
|
|
74
|
+
/**
|
|
75
|
+
* Connects a skill to a human's GitHub account through an OAuth app. The client settings left out are read from
|
|
76
|
+
* `GITHUB_CLIENT_ID`, `GITHUB_CLIENT_SECRET` and `GITHUB_REDIRECT_URI`.
|
|
77
|
+
*
|
|
78
|
+
* @param options The scopes to request, such as `repo`, and a token store if the relay holds no grants.
|
|
79
|
+
* @returns A connection to list in a skill's `connections`.
|
|
80
|
+
* @throws {AgentJamError} If a client setting is neither passed nor set in the environment.
|
|
81
|
+
*/
|
|
82
|
+
export declare const githubConnection: (options: OAuthPresetOptions & {
|
|
83
|
+
scopes: string[];
|
|
84
|
+
}) => OAuthConnection<"default">;
|
|
85
|
+
/**
|
|
86
|
+
* Connects a skill to a human's GitHub account through the GitHub App with `slug`, whose permissions decide what it
|
|
87
|
+
* reaches, so it takes no scopes. The client settings left out are read from `GITHUB_APP_CLIENT_ID`,
|
|
88
|
+
* `GITHUB_APP_CLIENT_SECRET` and `GITHUB_APP_REDIRECT_URI`.
|
|
89
|
+
*
|
|
90
|
+
* @param options The app's slug, and a token store if the relay holds no grants.
|
|
91
|
+
* @returns A connection to list in a skill's `connections`.
|
|
92
|
+
* @throws {AgentJamError} If a client setting is neither passed nor set in the environment.
|
|
93
|
+
*/
|
|
94
|
+
export declare const githubAppConnection: ({ slug, ...options }: GitHubAppConnectionOptions) => OAuthConnection<"default">;
|
|
95
|
+
/**
|
|
96
|
+
* Reads the webhook deliveries of a GitHub App or repository, for `agent.receiveEvents()` at the webhook's URL. It
|
|
97
|
+
* verifies each delivery's `X-Hub-Signature-256` in constant time, and reads opened, ready for review and merged pull
|
|
98
|
+
* requests under the keys `githubTriggers()` subscribe to, with the same event ids their polls fire.
|
|
99
|
+
*
|
|
100
|
+
* @param options The webhook's secret.
|
|
101
|
+
* @returns An event source for GitHub's deliveries.
|
|
102
|
+
*/
|
|
103
|
+
export declare const githubWebhooks: ({ secret, }: GitHubWebhooksOptions) => EventSource;
|
|
104
|
+
/**
|
|
105
|
+
* Triggers that run a schedule on a GitHub repository's activity, for a skill whose connections include `connection`.
|
|
106
|
+
* Each polls with the schedule's token from where `watermarks` says it left off, and the pull request triggers also
|
|
107
|
+
* subscribe to the keys `githubWebhooks()` reads deliveries under, which only make runs sooner.
|
|
108
|
+
*
|
|
109
|
+
* @param connection The skill's GitHub connection, such as one for a GitHub App.
|
|
110
|
+
* @param options Where polls keep their progress, and how often they run.
|
|
111
|
+
* @returns The `pull_request_opened`, `pull_request_merged` and `issue_assigned` triggers, for a skill's `triggers`.
|
|
112
|
+
*/
|
|
113
|
+
export declare const githubTriggers: (connection: CredentialSource, { watermarks, pollIntervalMinutes, onError, }?: GitHubEventsOptions) => {
|
|
114
|
+
/** A pull request opened in `config.repository`, or a draft marked ready for review, from its own branches unless `includeForks` is set. */
|
|
115
|
+
pull_request_opened: {
|
|
116
|
+
description: string;
|
|
117
|
+
config: {
|
|
118
|
+
type: string;
|
|
119
|
+
properties: {
|
|
120
|
+
repository: JsonSchema;
|
|
121
|
+
includeDrafts: {
|
|
122
|
+
type: string;
|
|
123
|
+
description: string;
|
|
124
|
+
};
|
|
125
|
+
includeForks: {
|
|
126
|
+
type: string;
|
|
127
|
+
description: string;
|
|
128
|
+
};
|
|
129
|
+
pollIntervalMinutes: JsonSchema;
|
|
130
|
+
};
|
|
131
|
+
required: string[];
|
|
132
|
+
};
|
|
133
|
+
onScheduleActive: (schedule: TriggerSchedule, context: import("../types.js").TriggerContext) => import("../types.js").Awaitable<import("../types.js").TriggerActivation | void>;
|
|
134
|
+
};
|
|
135
|
+
/** A pull request merged into the default branch of `config.repository`, or into `config.branch`. */
|
|
136
|
+
pull_request_merged: {
|
|
137
|
+
description: string;
|
|
138
|
+
config: {
|
|
139
|
+
type: string;
|
|
140
|
+
properties: {
|
|
141
|
+
repository: JsonSchema;
|
|
142
|
+
branch: {
|
|
143
|
+
type: string;
|
|
144
|
+
minLength: number;
|
|
145
|
+
maxLength: number;
|
|
146
|
+
description: string;
|
|
147
|
+
};
|
|
148
|
+
pollIntervalMinutes: JsonSchema;
|
|
149
|
+
};
|
|
150
|
+
required: string[];
|
|
151
|
+
};
|
|
152
|
+
onScheduleActive: (schedule: TriggerSchedule, context: import("../types.js").TriggerContext) => import("../types.js").Awaitable<import("../types.js").TriggerActivation | void>;
|
|
153
|
+
};
|
|
154
|
+
/** An issue in `config.repository` assigned to `config.assignee` or labeled `config.label`, leaving out pull requests. */
|
|
155
|
+
issue_assigned: {
|
|
156
|
+
description: string;
|
|
157
|
+
config: {
|
|
158
|
+
type: string;
|
|
159
|
+
properties: {
|
|
160
|
+
repository: JsonSchema;
|
|
161
|
+
assignee: {
|
|
162
|
+
type: string;
|
|
163
|
+
minLength: number;
|
|
164
|
+
description: string;
|
|
165
|
+
};
|
|
166
|
+
label: {
|
|
167
|
+
type: string;
|
|
168
|
+
minLength: number;
|
|
169
|
+
description: string;
|
|
170
|
+
};
|
|
171
|
+
pollIntervalMinutes: JsonSchema;
|
|
172
|
+
};
|
|
173
|
+
required: string[];
|
|
174
|
+
anyOf: {
|
|
175
|
+
required: string[];
|
|
176
|
+
}[];
|
|
177
|
+
};
|
|
178
|
+
onScheduleActive: (schedule: TriggerSchedule, context: import("../types.js").TriggerContext) => import("../types.js").Awaitable<import("../types.js").TriggerActivation | void>;
|
|
179
|
+
};
|
|
180
|
+
};
|
|
181
|
+
//# sourceMappingURL=github.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"github.d.ts","sourceRoot":"","sources":["../../src/platforms/github.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import { AgentJamError } from "../errors.js";
|
|
3
|
+
import { readSourceRequest } from "../events.js";
|
|
4
|
+
import { constructIntervalProperty, pollSchedule, truncateSummary, } from "../polling.js";
|
|
5
|
+
import { constructStandardProfile } from "../providers.js";
|
|
6
|
+
import { MemoryWatermarkStore } from "../watermarks.js";
|
|
7
|
+
import { constructOAuthPreset } from "./presets.js";
|
|
8
|
+
/** How to run the flow of a GitHub OAuth app, whose broader scopes imply narrower ones, such as `repo` implying `public_repo`. */
|
|
9
|
+
export const githubProfile = {
|
|
10
|
+
...constructStandardProfile({
|
|
11
|
+
authorize: "https://github.com/login/oauth/authorize",
|
|
12
|
+
token: "https://github.com/login/oauth/access_token",
|
|
13
|
+
}),
|
|
14
|
+
impliedScopes: {
|
|
15
|
+
repo: [
|
|
16
|
+
"repo:status",
|
|
17
|
+
"repo_deployment",
|
|
18
|
+
"public_repo",
|
|
19
|
+
"repo:invite",
|
|
20
|
+
"security_events",
|
|
21
|
+
],
|
|
22
|
+
"admin:repo_hook": ["write:repo_hook", "read:repo_hook"],
|
|
23
|
+
"write:repo_hook": ["read:repo_hook"],
|
|
24
|
+
"admin:org": ["write:org", "read:org"],
|
|
25
|
+
"write:org": ["read:org"],
|
|
26
|
+
"admin:public_key": ["write:public_key", "read:public_key"],
|
|
27
|
+
"write:public_key": ["read:public_key"],
|
|
28
|
+
user: ["read:user", "user:email", "user:follow"],
|
|
29
|
+
project: ["read:project"],
|
|
30
|
+
"write:packages": ["read:packages"],
|
|
31
|
+
"admin:gpg_key": ["write:gpg_key", "read:gpg_key"],
|
|
32
|
+
"write:gpg_key": ["read:gpg_key"],
|
|
33
|
+
},
|
|
34
|
+
lostGrantErrors: ["invalid_grant", "bad_refresh_token"],
|
|
35
|
+
revokeTokens: async ({ accessToken }, { clientId, clientSecret }) => {
|
|
36
|
+
const response = await fetch(`https://api.github.com/applications/${encodeURIComponent(clientId)}/grant`, {
|
|
37
|
+
method: "DELETE",
|
|
38
|
+
headers: {
|
|
39
|
+
authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString("base64")}`,
|
|
40
|
+
accept: "application/vnd.github+json",
|
|
41
|
+
"content-type": "application/json",
|
|
42
|
+
"x-github-api-version": "2022-11-28",
|
|
43
|
+
},
|
|
44
|
+
body: JSON.stringify({ access_token: accessToken }),
|
|
45
|
+
});
|
|
46
|
+
if (!response.ok) {
|
|
47
|
+
throw new AgentJamError(`The provider refused to revoke the github grant (status ${response.status})`);
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
constructAccessUrl: ({ clientId }) => `https://github.com/settings/connections/applications/${encodeURIComponent(clientId)}`,
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Builds the profile of the GitHub App with `slug`, whose link opens the app's installation page so the human picks
|
|
54
|
+
* repositories and authorizes in one visit. The app needs "Request user authorization (OAuth) during installation" enabled.
|
|
55
|
+
*
|
|
56
|
+
* @param slug The app's slug, as its public page URL shows it.
|
|
57
|
+
* @returns The app's profile.
|
|
58
|
+
*/
|
|
59
|
+
export const constructGitHubAppProfile = (slug) => {
|
|
60
|
+
const installation = `https://github.com/apps/${encodeURIComponent(slug)}/installations/new`;
|
|
61
|
+
return {
|
|
62
|
+
...githubProfile,
|
|
63
|
+
endpoints: { ...githubProfile.endpoints, authorize: installation },
|
|
64
|
+
// WARNING: GitHub's installation page drops PKCE challenges, so the code exchange can't send a verifier.
|
|
65
|
+
pkce: false,
|
|
66
|
+
impliedScopes: {},
|
|
67
|
+
constructAccessUrl: () => installation,
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Connects a skill to a human's GitHub account through an OAuth app. The client settings left out are read from
|
|
72
|
+
* `GITHUB_CLIENT_ID`, `GITHUB_CLIENT_SECRET` and `GITHUB_REDIRECT_URI`.
|
|
73
|
+
*
|
|
74
|
+
* @param options The scopes to request, such as `repo`, and a token store if the relay holds no grants.
|
|
75
|
+
* @returns A connection to list in a skill's `connections`.
|
|
76
|
+
* @throws {AgentJamError} If a client setting is neither passed nor set in the environment.
|
|
77
|
+
*/
|
|
78
|
+
export const githubConnection = (options) => constructOAuthPreset({ provider: "github", profile: githubProfile, prefix: "GITHUB" }, options);
|
|
79
|
+
/**
|
|
80
|
+
* Connects a skill to a human's GitHub account through the GitHub App with `slug`, whose permissions decide what it
|
|
81
|
+
* reaches, so it takes no scopes. The client settings left out are read from `GITHUB_APP_CLIENT_ID`,
|
|
82
|
+
* `GITHUB_APP_CLIENT_SECRET` and `GITHUB_APP_REDIRECT_URI`.
|
|
83
|
+
*
|
|
84
|
+
* @param options The app's slug, and a token store if the relay holds no grants.
|
|
85
|
+
* @returns A connection to list in a skill's `connections`.
|
|
86
|
+
* @throws {AgentJamError} If a client setting is neither passed nor set in the environment.
|
|
87
|
+
*/
|
|
88
|
+
export const githubAppConnection = ({ slug, ...options }) => constructOAuthPreset({
|
|
89
|
+
provider: "github-app",
|
|
90
|
+
profile: constructGitHubAppProfile(slug),
|
|
91
|
+
prefix: "GITHUB_APP",
|
|
92
|
+
}, { ...options, scopes: [] });
|
|
93
|
+
const githubApi = "https://api.github.com";
|
|
94
|
+
const pullRequestsPageSize = 50;
|
|
95
|
+
const issueEventsPageSize = 100;
|
|
96
|
+
const maxIssueEventPages = 10;
|
|
97
|
+
const maxEventKeyLength = 256;
|
|
98
|
+
const repositoryProperty = {
|
|
99
|
+
type: "string",
|
|
100
|
+
pattern: "^[\\w.-]+/[\\w.-]+$",
|
|
101
|
+
description: "The GitHub repository to watch, as owner/name",
|
|
102
|
+
};
|
|
103
|
+
const isSigned = (secret, headers, body) => {
|
|
104
|
+
const digest = createHmac("sha256", secret).update(body).digest("hex");
|
|
105
|
+
const expected = Buffer.from(`sha256=${digest}`);
|
|
106
|
+
const signature = Buffer.from(headers.get("x-hub-signature-256") ?? "");
|
|
107
|
+
return (signature.length === expected.length && timingSafeEqual(signature, expected));
|
|
108
|
+
};
|
|
109
|
+
const requestGitHub = async (token, path, etag) => {
|
|
110
|
+
const response = await fetch(new URL(path, githubApi), {
|
|
111
|
+
headers: {
|
|
112
|
+
accept: "application/vnd.github+json",
|
|
113
|
+
authorization: `Bearer ${token}`,
|
|
114
|
+
"x-github-api-version": "2022-11-28",
|
|
115
|
+
...(etag && { "if-none-match": etag }),
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
if (response.ok || response.status === 304)
|
|
119
|
+
return response;
|
|
120
|
+
throw new AgentJamError(`GitHub answered ${response.status} for ${path}: ${await response.text()}`);
|
|
121
|
+
};
|
|
122
|
+
/** Lists a page of `path`, or resolves to undefined when GitHub says it hasn't changed since `etag`, which costs no rate limit. */
|
|
123
|
+
const listPage = async (token, path, etag) => {
|
|
124
|
+
const response = await requestGitHub(token, path, etag ?? undefined);
|
|
125
|
+
if (response.status === 304)
|
|
126
|
+
return undefined;
|
|
127
|
+
return {
|
|
128
|
+
items: (await response.json()),
|
|
129
|
+
etag: response.headers.get("etag"),
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
const getRepository = async (token, repository) => {
|
|
133
|
+
try {
|
|
134
|
+
const response = await requestGitHub(token, `/repos/${repository}`);
|
|
135
|
+
return (await response.json());
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
throw new AgentJamError(`The schedule's GitHub account can't read ${repository}, so it can't be watched`, { cause: error });
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
const isLater = (time, since) => Date.parse(time) > Date.parse(since);
|
|
142
|
+
const compareTimes = (first, second) => Date.parse(first) - Date.parse(second);
|
|
143
|
+
const isMerged = (pullRequest) => pullRequest.merged_at !== null;
|
|
144
|
+
const isMatch = (value, wanted) => wanted !== undefined && value?.toLowerCase() === wanted.toLowerCase();
|
|
145
|
+
const constructPullRequestData = (repository, pullRequest) => ({
|
|
146
|
+
repository: repository.full_name,
|
|
147
|
+
repositoryId: repository.id,
|
|
148
|
+
number: pullRequest.number,
|
|
149
|
+
title: pullRequest.title,
|
|
150
|
+
url: pullRequest.html_url,
|
|
151
|
+
author: pullRequest.user?.login ?? null,
|
|
152
|
+
draft: pullRequest.draft ?? false,
|
|
153
|
+
fork: pullRequest.head.repo?.id !== repository.id,
|
|
154
|
+
baseBranch: pullRequest.base.ref,
|
|
155
|
+
headBranch: pullRequest.head.ref,
|
|
156
|
+
headSha: pullRequest.head.sha,
|
|
157
|
+
});
|
|
158
|
+
// Invariant: the id leaves out whether the pull request was a draft, so a draft that ran when it opened doesn't run again once it's ready.
|
|
159
|
+
const constructOpenedEvent = (data) => ({
|
|
160
|
+
id: `github:${data.repositoryId}#${data.number}`,
|
|
161
|
+
summary: truncateSummary(`Pull request #${data.number} opened in ${data.repository}: ${data.title}`),
|
|
162
|
+
data,
|
|
163
|
+
});
|
|
164
|
+
const constructMergedEvent = (repository, pullRequest) => {
|
|
165
|
+
const data = {
|
|
166
|
+
...constructPullRequestData(repository, pullRequest),
|
|
167
|
+
mergedAt: pullRequest.merged_at,
|
|
168
|
+
mergeCommitSha: pullRequest.merge_commit_sha,
|
|
169
|
+
};
|
|
170
|
+
return {
|
|
171
|
+
id: `github:${data.repositoryId}#${data.number}:merged`,
|
|
172
|
+
summary: truncateSummary(`Pull request #${data.number} merged into ${data.baseBranch} in ${data.repository}: ${data.title}`),
|
|
173
|
+
data,
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
const readIssueChange = ({ event, assignee, label }, config) => {
|
|
177
|
+
const login = assignee?.login;
|
|
178
|
+
const name = label?.name;
|
|
179
|
+
if (event === "assigned" && isMatch(login, config.assignee)) {
|
|
180
|
+
return { action: "assigned", assignee: login };
|
|
181
|
+
}
|
|
182
|
+
if (event === "labeled" && isMatch(name, config.label)) {
|
|
183
|
+
return { action: "labeled", label: name };
|
|
184
|
+
}
|
|
185
|
+
return undefined;
|
|
186
|
+
};
|
|
187
|
+
const readAssignment = (repository, issueEvent, config) => {
|
|
188
|
+
const { issue } = issueEvent;
|
|
189
|
+
const change = readIssueChange(issueEvent, config);
|
|
190
|
+
if (!issue || issue.pull_request !== undefined || !change)
|
|
191
|
+
return undefined;
|
|
192
|
+
const data = {
|
|
193
|
+
repository: repository.full_name,
|
|
194
|
+
repositoryId: repository.id,
|
|
195
|
+
number: issue.number,
|
|
196
|
+
title: issue.title,
|
|
197
|
+
url: issue.html_url,
|
|
198
|
+
...change,
|
|
199
|
+
};
|
|
200
|
+
const action = change.action === "assigned"
|
|
201
|
+
? `assigned to ${change.assignee}`
|
|
202
|
+
: `labeled ${change.label}`;
|
|
203
|
+
return {
|
|
204
|
+
id: `github:${repository.id}#${issue.number}@${issueEvent.id}`,
|
|
205
|
+
summary: truncateSummary(`Issue #${issue.number} in ${repository.full_name} ${action}: ${issue.title}`),
|
|
206
|
+
data,
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
const constructOpenedKey = (repositoryId, { draft, fork }) => `github:${repositoryId}:${fork ? "fork_" : ""}${draft ? "draft_" : ""}pull_request_opened`;
|
|
210
|
+
const constructMergedKey = (repositoryId, branch) => `github:${repositoryId}:pull_request_merged:${branch}`;
|
|
211
|
+
const readDelivery = ({ action, repository, pull_request: pullRequest, }) => {
|
|
212
|
+
if (!repository || !pullRequest)
|
|
213
|
+
return [];
|
|
214
|
+
const data = constructPullRequestData(repository, pullRequest);
|
|
215
|
+
const isOpened = action === "opened" || (action === "ready_for_review" && !data.draft);
|
|
216
|
+
if (isOpened) {
|
|
217
|
+
const eventKeys = [constructOpenedKey(repository.id, data)];
|
|
218
|
+
return [{ type: "event", eventKeys, event: constructOpenedEvent(data) }];
|
|
219
|
+
}
|
|
220
|
+
if (action !== "closed" || !isMerged(pullRequest))
|
|
221
|
+
return [];
|
|
222
|
+
const mergedKey = constructMergedKey(repository.id, data.baseBranch);
|
|
223
|
+
if (mergedKey.length > maxEventKeyLength)
|
|
224
|
+
return [];
|
|
225
|
+
return [
|
|
226
|
+
{
|
|
227
|
+
type: "event",
|
|
228
|
+
eventKeys: [mergedKey],
|
|
229
|
+
event: constructMergedEvent(repository, pullRequest),
|
|
230
|
+
},
|
|
231
|
+
];
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* Reads the webhook deliveries of a GitHub App or repository, for `agent.receiveEvents()` at the webhook's URL. It
|
|
235
|
+
* verifies each delivery's `X-Hub-Signature-256` in constant time, and reads opened, ready for review and merged pull
|
|
236
|
+
* requests under the keys `githubTriggers()` subscribe to, with the same event ids their polls fire.
|
|
237
|
+
*
|
|
238
|
+
* @param options The webhook's secret.
|
|
239
|
+
* @returns An event source for GitHub's deliveries.
|
|
240
|
+
*/
|
|
241
|
+
export const githubWebhooks = ({ secret, }) => ({
|
|
242
|
+
receive: async (request) => {
|
|
243
|
+
const { headers, body } = await readSourceRequest(request);
|
|
244
|
+
if (!isSigned(secret, headers, body)) {
|
|
245
|
+
return {
|
|
246
|
+
response: { status: 401, body: "Invalid GitHub signature" },
|
|
247
|
+
events: [],
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
const isPullRequest = headers.get("x-github-event") === "pull_request";
|
|
251
|
+
return {
|
|
252
|
+
response: { status: 200 },
|
|
253
|
+
events: isPullRequest
|
|
254
|
+
? readDelivery(JSON.parse(body))
|
|
255
|
+
: [],
|
|
256
|
+
};
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
const constructPollOptions = (schedule, { watermarks, pollIntervalMinutes, onError }) => ({
|
|
260
|
+
schedule,
|
|
261
|
+
watermarks,
|
|
262
|
+
intervalMinutes: schedule.config.pollIntervalMinutes ?? pollIntervalMinutes,
|
|
263
|
+
onError,
|
|
264
|
+
});
|
|
265
|
+
const watchOpened = (polling) => async (schedule, { credentials, fire }) => {
|
|
266
|
+
const config = schedule.config;
|
|
267
|
+
const { includeDrafts = false, includeForks = false } = config;
|
|
268
|
+
const repository = await getRepository(await credentials(polling.connection), config.repository);
|
|
269
|
+
const isWatched = ({ draft, fork }) => (includeDrafts || !draft) && (includeForks || !fork);
|
|
270
|
+
const poll = async (watermark) => {
|
|
271
|
+
const { since, etag } = watermark.current;
|
|
272
|
+
const page = await listPage(await credentials(polling.connection), `/repos/${repository.full_name}/pulls?state=open&sort=created&direction=desc&per_page=${pullRequestsPageSize}`, etag);
|
|
273
|
+
if (!page)
|
|
274
|
+
return;
|
|
275
|
+
const opened = page.items
|
|
276
|
+
.filter(({ created_at: createdAt }) => isLater(createdAt, since))
|
|
277
|
+
.map((pullRequest) => ({
|
|
278
|
+
createdAt: pullRequest.created_at,
|
|
279
|
+
data: constructPullRequestData(repository, pullRequest),
|
|
280
|
+
}))
|
|
281
|
+
.filter(({ data }) => isWatched(data))
|
|
282
|
+
.toSorted((first, second) => compareTimes(first.createdAt, second.createdAt));
|
|
283
|
+
for (const { createdAt, data } of opened) {
|
|
284
|
+
await fire(constructOpenedEvent(data));
|
|
285
|
+
await watermark.advance({ since: createdAt });
|
|
286
|
+
}
|
|
287
|
+
await watermark.advance({ etag: page.etag });
|
|
288
|
+
};
|
|
289
|
+
const eventKeys = [false, ...(includeForks ? [true] : [])].flatMap((fork) => [false, ...(includeDrafts ? [true] : [])].map((draft) => constructOpenedKey(repository.id, { draft, fork })));
|
|
290
|
+
const activation = await pollSchedule({
|
|
291
|
+
...constructPollOptions(schedule, polling),
|
|
292
|
+
initial: { since: new Date().toISOString(), etag: null },
|
|
293
|
+
target: `${repository.full_name} for opened pull requests`,
|
|
294
|
+
poll,
|
|
295
|
+
});
|
|
296
|
+
return { eventKeys, ...activation };
|
|
297
|
+
};
|
|
298
|
+
const watchMerged = (polling) => async (schedule, { credentials, fire }) => {
|
|
299
|
+
const config = schedule.config;
|
|
300
|
+
const repository = await getRepository(await credentials(polling.connection), config.repository);
|
|
301
|
+
const branch = config.branch ?? repository.default_branch;
|
|
302
|
+
const query = new URLSearchParams({
|
|
303
|
+
state: "closed",
|
|
304
|
+
base: branch,
|
|
305
|
+
sort: "updated",
|
|
306
|
+
direction: "desc",
|
|
307
|
+
per_page: String(pullRequestsPageSize),
|
|
308
|
+
});
|
|
309
|
+
const poll = async (watermark) => {
|
|
310
|
+
const { since, etag } = watermark.current;
|
|
311
|
+
const page = await listPage(await credentials(polling.connection), `/repos/${repository.full_name}/pulls?${query.toString()}`, etag);
|
|
312
|
+
if (!page)
|
|
313
|
+
return;
|
|
314
|
+
const merged = page.items
|
|
315
|
+
.filter(isMerged)
|
|
316
|
+
.filter(({ merged_at: mergedAt }) => isLater(mergedAt, since))
|
|
317
|
+
.toSorted((first, second) => compareTimes(first.merged_at, second.merged_at));
|
|
318
|
+
for (const pullRequest of merged) {
|
|
319
|
+
await fire(constructMergedEvent(repository, pullRequest));
|
|
320
|
+
await watermark.advance({ since: pullRequest.merged_at });
|
|
321
|
+
}
|
|
322
|
+
await watermark.advance({ etag: page.etag });
|
|
323
|
+
};
|
|
324
|
+
const activation = await pollSchedule({
|
|
325
|
+
...constructPollOptions(schedule, polling),
|
|
326
|
+
initial: { since: new Date().toISOString(), etag: null },
|
|
327
|
+
target: `${repository.full_name} for pull requests merged into ${branch}`,
|
|
328
|
+
poll,
|
|
329
|
+
});
|
|
330
|
+
return {
|
|
331
|
+
eventKeys: [constructMergedKey(repository.id, branch)],
|
|
332
|
+
...activation,
|
|
333
|
+
};
|
|
334
|
+
};
|
|
335
|
+
/** Lists the issue events newer than `cursor`, oldest first, paging back to it, or only the newest page for a new watch. */
|
|
336
|
+
const listIssueEvents = async (token, repository, { cursor, etag }) => {
|
|
337
|
+
const events = [];
|
|
338
|
+
let firstEtag = null;
|
|
339
|
+
for (let page = 1; page <= maxIssueEventPages; page++) {
|
|
340
|
+
const answer = await listPage(token, `/repos/${repository}/issues/events?per_page=${issueEventsPageSize}&page=${page}`, page === 1 ? etag : null);
|
|
341
|
+
if (!answer)
|
|
342
|
+
return undefined;
|
|
343
|
+
if (page === 1)
|
|
344
|
+
firstEtag = answer.etag;
|
|
345
|
+
events.push(...answer.items);
|
|
346
|
+
const isLastPage = answer.items.length < issueEventsPageSize;
|
|
347
|
+
const reachedCursor = answer.items.some(({ id }) => id <= cursor);
|
|
348
|
+
if (cursor === 0 || isLastPage || reachedCursor)
|
|
349
|
+
break;
|
|
350
|
+
}
|
|
351
|
+
return {
|
|
352
|
+
etag: firstEtag,
|
|
353
|
+
events: events
|
|
354
|
+
.filter(({ id }) => id > cursor)
|
|
355
|
+
.toSorted((first, second) => first.id - second.id),
|
|
356
|
+
};
|
|
357
|
+
};
|
|
358
|
+
const watchAssigned = (polling) => async (schedule, { credentials, fire }) => {
|
|
359
|
+
const config = schedule.config;
|
|
360
|
+
if (config.assignee === undefined && config.label === undefined) {
|
|
361
|
+
throw new AgentJamError("An issue_assigned schedule needs an assignee, a label or both to watch for");
|
|
362
|
+
}
|
|
363
|
+
const repository = await getRepository(await credentials(polling.connection), config.repository);
|
|
364
|
+
const poll = async (watermark) => {
|
|
365
|
+
const { since, cursor } = watermark.current;
|
|
366
|
+
const listed = await listIssueEvents(await credentials(polling.connection), repository.full_name, watermark.current);
|
|
367
|
+
if (!listed)
|
|
368
|
+
return;
|
|
369
|
+
for (const issueEvent of listed.events) {
|
|
370
|
+
const assignment = isLater(issueEvent.created_at, since)
|
|
371
|
+
? readAssignment(repository, issueEvent, config)
|
|
372
|
+
: undefined;
|
|
373
|
+
if (!assignment)
|
|
374
|
+
continue;
|
|
375
|
+
await fire(assignment);
|
|
376
|
+
await watermark.advance({ cursor: issueEvent.id });
|
|
377
|
+
}
|
|
378
|
+
await watermark.advance({
|
|
379
|
+
cursor: listed.events.at(-1)?.id ?? cursor,
|
|
380
|
+
etag: listed.etag,
|
|
381
|
+
});
|
|
382
|
+
};
|
|
383
|
+
return pollSchedule({
|
|
384
|
+
...constructPollOptions(schedule, polling),
|
|
385
|
+
initial: { since: new Date().toISOString(), etag: null, cursor: 0 },
|
|
386
|
+
target: `${repository.full_name} for assigned issues`,
|
|
387
|
+
poll,
|
|
388
|
+
});
|
|
389
|
+
};
|
|
390
|
+
/**
|
|
391
|
+
* Triggers that run a schedule on a GitHub repository's activity, for a skill whose connections include `connection`.
|
|
392
|
+
* Each polls with the schedule's token from where `watermarks` says it left off, and the pull request triggers also
|
|
393
|
+
* subscribe to the keys `githubWebhooks()` reads deliveries under, which only make runs sooner.
|
|
394
|
+
*
|
|
395
|
+
* @param connection The skill's GitHub connection, such as one for a GitHub App.
|
|
396
|
+
* @param options Where polls keep their progress, and how often they run.
|
|
397
|
+
* @returns The `pull_request_opened`, `pull_request_merged` and `issue_assigned` triggers, for a skill's `triggers`.
|
|
398
|
+
*/
|
|
399
|
+
export const githubTriggers = (connection, { watermarks = new MemoryWatermarkStore(), pollIntervalMinutes = 2, onError = console.error, } = {}) => {
|
|
400
|
+
const polling = { connection, watermarks, pollIntervalMinutes, onError };
|
|
401
|
+
return {
|
|
402
|
+
/** A pull request opened in `config.repository`, or a draft marked ready for review, from its own branches unless `includeForks` is set. */
|
|
403
|
+
pull_request_opened: {
|
|
404
|
+
description: "A pull request is opened in a GitHub repository, or a draft is marked ready for review",
|
|
405
|
+
config: {
|
|
406
|
+
type: "object",
|
|
407
|
+
properties: {
|
|
408
|
+
repository: repositoryProperty,
|
|
409
|
+
includeDrafts: {
|
|
410
|
+
type: "boolean",
|
|
411
|
+
description: "Whether to run when a draft is opened, rather than once it's marked ready for review",
|
|
412
|
+
},
|
|
413
|
+
includeForks: {
|
|
414
|
+
type: "boolean",
|
|
415
|
+
description: "Whether to also run on pull requests from forks",
|
|
416
|
+
},
|
|
417
|
+
pollIntervalMinutes: constructIntervalProperty("opened pull requests a webhook missed"),
|
|
418
|
+
},
|
|
419
|
+
required: ["repository"],
|
|
420
|
+
},
|
|
421
|
+
onScheduleActive: watchOpened(polling),
|
|
422
|
+
},
|
|
423
|
+
/** A pull request merged into the default branch of `config.repository`, or into `config.branch`. */
|
|
424
|
+
pull_request_merged: {
|
|
425
|
+
description: "A pull request is merged into a GitHub repository's default branch, or into another branch",
|
|
426
|
+
config: {
|
|
427
|
+
type: "object",
|
|
428
|
+
properties: {
|
|
429
|
+
repository: repositoryProperty,
|
|
430
|
+
branch: {
|
|
431
|
+
type: "string",
|
|
432
|
+
minLength: 1,
|
|
433
|
+
maxLength: 200,
|
|
434
|
+
description: "The branch to watch merges into. Defaults to the repository's default branch",
|
|
435
|
+
},
|
|
436
|
+
pollIntervalMinutes: constructIntervalProperty("merged pull requests a webhook missed"),
|
|
437
|
+
},
|
|
438
|
+
required: ["repository"],
|
|
439
|
+
},
|
|
440
|
+
onScheduleActive: watchMerged(polling),
|
|
441
|
+
},
|
|
442
|
+
/** An issue in `config.repository` assigned to `config.assignee` or labeled `config.label`, leaving out pull requests. */
|
|
443
|
+
issue_assigned: {
|
|
444
|
+
description: "An issue in a GitHub repository is assigned to a user or labeled with a label",
|
|
445
|
+
config: {
|
|
446
|
+
type: "object",
|
|
447
|
+
properties: {
|
|
448
|
+
repository: repositoryProperty,
|
|
449
|
+
assignee: {
|
|
450
|
+
type: "string",
|
|
451
|
+
minLength: 1,
|
|
452
|
+
description: "Runs when an issue is assigned to this GitHub user, such as a bot account",
|
|
453
|
+
},
|
|
454
|
+
label: {
|
|
455
|
+
type: "string",
|
|
456
|
+
minLength: 1,
|
|
457
|
+
description: "Runs when this label is added to an issue",
|
|
458
|
+
},
|
|
459
|
+
pollIntervalMinutes: constructIntervalProperty("assigned issues"),
|
|
460
|
+
},
|
|
461
|
+
required: ["repository"],
|
|
462
|
+
anyOf: [{ required: ["assignee"] }, { required: ["label"] }],
|
|
463
|
+
},
|
|
464
|
+
onScheduleActive: watchAssigned(polling),
|
|
465
|
+
},
|
|
466
|
+
};
|
|
467
|
+
};
|
|
468
|
+
//# sourceMappingURL=github.js.map
|