@leitwerk-dev/github 0.0.0-bootstrap.0 → 0.2.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 +113 -1
- package/dist/LICENSE +13 -0
- package/dist/authorization.d.ts +12 -0
- package/dist/authorization.d.ts.map +1 -0
- package/dist/binding.d.ts +4 -0
- package/dist/binding.d.ts.map +1 -0
- package/dist/capability.d.ts +8 -0
- package/dist/capability.d.ts.map +1 -0
- package/dist/chunk-PVE52UFH.js +318 -0
- package/dist/chunk-PVE52UFH.js.map +1 -0
- package/dist/client.d.ts +111 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/external.d.ts +85 -0
- package/dist/external.d.ts.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +809 -0
- package/dist/index.js.map +1 -0
- package/dist/issue-provider.d.ts +8 -0
- package/dist/issue-provider.d.ts.map +1 -0
- package/dist/issue-watcher.d.ts +19 -0
- package/dist/issue-watcher.d.ts.map +1 -0
- package/dist/poll-report.d.ts +22 -0
- package/dist/poll-report.d.ts.map +1 -0
- package/dist/provider.d.ts +6 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/testing.d.ts +81 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +297 -0
- package/dist/testing.js.map +1 -0
- package/dist/tools.d.ts +5 -0
- package/dist/tools.d.ts.map +1 -0
- package/package.json +55 -14
- package/src/authorization.ts +54 -0
- package/src/binding.ts +6 -0
- package/src/capability.ts +13 -0
- package/src/client.ts +421 -0
- package/src/external.ts +139 -0
- package/src/index.ts +45 -0
- package/src/issue-provider.ts +278 -0
- package/src/issue-watcher.ts +30 -0
- package/src/poll-report.ts +47 -0
- package/src/provider.ts +216 -0
- package/src/testing.ts +384 -0
- package/src/tools.ts +304 -0
package/src/external.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineExternalActionSource,
|
|
3
|
+
type ExternalEventDescription,
|
|
4
|
+
} from "@leitwerk-dev/process-sdk";
|
|
5
|
+
import { describeConflict } from "@leitwerk-dev/repository-rebase";
|
|
6
|
+
|
|
7
|
+
export const GITHUB_PR_STATE_KIND = "@leitwerk-private/github.pr_state";
|
|
8
|
+
|
|
9
|
+
export interface GitHubPullRequestSourceConfig {
|
|
10
|
+
profile: string;
|
|
11
|
+
owner: string;
|
|
12
|
+
repo: string;
|
|
13
|
+
prNumber: number;
|
|
14
|
+
headSha: string;
|
|
15
|
+
feedbackCursor: number;
|
|
16
|
+
lastConflictKey?: string | null;
|
|
17
|
+
pollInterval?: string;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
eventKinds?: Array<"checks" | "feedback" | "merged" | "closed" | "merge_conflict">;
|
|
20
|
+
checkStatuses?: Array<"pending" | "success" | "failure">;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const GITHUB_RELEASE_KIND = "@leitwerk-private/github.release";
|
|
24
|
+
|
|
25
|
+
export interface GitHubReleaseSourceConfig {
|
|
26
|
+
profile: string;
|
|
27
|
+
owner: string;
|
|
28
|
+
repo: string;
|
|
29
|
+
mergeSha: string;
|
|
30
|
+
pollInterval?: string;
|
|
31
|
+
disabled?: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function describeGitHubEvent(event: unknown): ExternalEventDescription {
|
|
35
|
+
const value = event as {
|
|
36
|
+
kind?: string;
|
|
37
|
+
checks?: {
|
|
38
|
+
headSha: string;
|
|
39
|
+
status: string;
|
|
40
|
+
failed: Array<{ name: string; url: string | null }>;
|
|
41
|
+
};
|
|
42
|
+
pullRequest?: { number: number; merged?: boolean; state?: string };
|
|
43
|
+
};
|
|
44
|
+
if (value.kind === "merge_conflict") return describeConflict(event);
|
|
45
|
+
if (value.checks) {
|
|
46
|
+
const checks = value.checks;
|
|
47
|
+
return {
|
|
48
|
+
summary: `Checks ${checks.status} on ${checks.headSha}${checks.failed.length ? `: ${checks.failed.map((check) => check.name).join(", ")}` : ""}`,
|
|
49
|
+
links: checks.failed.flatMap((check, index) =>
|
|
50
|
+
check.url
|
|
51
|
+
? [{ id: `check-${index}`, label: check.name, url: check.url, kind: "pipeline" as const }]
|
|
52
|
+
: [],
|
|
53
|
+
),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
summary:
|
|
58
|
+
value.kind === "merged" || value.kind === "closed" || value.pullRequest
|
|
59
|
+
? `PR #${value.pullRequest?.number} ${value.kind ?? (value.pullRequest?.merged ? "merged" : "closed")}`
|
|
60
|
+
: value.kind === "feedback"
|
|
61
|
+
? "Pull request feedback received"
|
|
62
|
+
: "GitHub event received",
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const GITHUB_PR_TERMINAL_KIND = "@leitwerk-public/github.pr_terminal";
|
|
67
|
+
export const GITHUB_PR_FEEDBACK_KIND = "@leitwerk-public/github.pr_feedback";
|
|
68
|
+
export const GITHUB_ISSUE_CANCELLED_KIND = "@leitwerk-public/github.issue_cancelled";
|
|
69
|
+
|
|
70
|
+
export interface GitHubPullRequestTerminalSourceConfig {
|
|
71
|
+
profile: string;
|
|
72
|
+
owner: string;
|
|
73
|
+
repo: string;
|
|
74
|
+
prNumber: number;
|
|
75
|
+
pollInterval?: string;
|
|
76
|
+
terminalOutcome?: "merged" | "closed";
|
|
77
|
+
disabled?: boolean;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface GitHubFeedbackSourceConfig extends GitHubPullRequestTerminalSourceConfig {
|
|
81
|
+
conversationCursor: number;
|
|
82
|
+
reviewCursor: number;
|
|
83
|
+
inlineCursor: number;
|
|
84
|
+
quietPeriodMs: number;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface GitHubIssueCancelledSourceConfig {
|
|
88
|
+
profile: string;
|
|
89
|
+
owner: string;
|
|
90
|
+
repo: string;
|
|
91
|
+
issueNumber: number;
|
|
92
|
+
triggerLabel: string;
|
|
93
|
+
pollInterval?: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export const GITHUB_CHECKS_KIND = "@leitwerk-public/github.checks";
|
|
97
|
+
|
|
98
|
+
export const githubExternal = {
|
|
99
|
+
checks: defineExternalActionSource<{
|
|
100
|
+
profile: string;
|
|
101
|
+
owner: string;
|
|
102
|
+
repo: string;
|
|
103
|
+
prNumber: number;
|
|
104
|
+
headSha: string;
|
|
105
|
+
}>({
|
|
106
|
+
kind: GITHUB_CHECKS_KIND,
|
|
107
|
+
describeEvent: describeGitHubEvent,
|
|
108
|
+
label: "GitHub Actions failures",
|
|
109
|
+
}),
|
|
110
|
+
issueCancelled: defineExternalActionSource<GitHubIssueCancelledSourceConfig>({
|
|
111
|
+
kind: GITHUB_ISSUE_CANCELLED_KIND,
|
|
112
|
+
describeEvent: () => ({ summary: "Source issue cancelled" }),
|
|
113
|
+
label: "GitHub source issue cancelled",
|
|
114
|
+
description: "Fires when the source issue closes or loses its trigger label",
|
|
115
|
+
}),
|
|
116
|
+
pullRequestTerminal: defineExternalActionSource<GitHubPullRequestTerminalSourceConfig>({
|
|
117
|
+
kind: GITHUB_PR_TERMINAL_KIND,
|
|
118
|
+
describeEvent: describeGitHubEvent,
|
|
119
|
+
label: "GitHub pull request merged or closed",
|
|
120
|
+
description: "Fires when the tracked pull request reaches a terminal state",
|
|
121
|
+
}),
|
|
122
|
+
pullRequestFeedback: defineExternalActionSource<GitHubFeedbackSourceConfig>({
|
|
123
|
+
kind: GITHUB_PR_FEEDBACK_KIND,
|
|
124
|
+
describeEvent: () => ({ summary: "Pull request feedback received" }),
|
|
125
|
+
label: "GitHub pull request feedback",
|
|
126
|
+
description: "Fires after unseen human feedback has been quiet long enough to batch",
|
|
127
|
+
}),
|
|
128
|
+
releaseContaining: defineExternalActionSource<GitHubReleaseSourceConfig>({
|
|
129
|
+
kind: GITHUB_RELEASE_KIND,
|
|
130
|
+
label: "Published GitHub release",
|
|
131
|
+
description: "Fires when a published release contains the tracked merge commit",
|
|
132
|
+
}),
|
|
133
|
+
pullRequestState: defineExternalActionSource<GitHubPullRequestSourceConfig>({
|
|
134
|
+
kind: GITHUB_PR_STATE_KIND,
|
|
135
|
+
describeEvent: describeGitHubEvent,
|
|
136
|
+
label: "GitHub pull request state",
|
|
137
|
+
description: "Fires for failed or successful checks, review feedback, and terminal PR state",
|
|
138
|
+
}),
|
|
139
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { ExternalWriteLogRepoLike } from "@leitwerk-dev/external-writes";
|
|
2
|
+
import { coreHostCapabilities, type LeitwerkExtensionModule } from "@leitwerk-dev/process-sdk";
|
|
3
|
+
import { type GitHubIntegration, githubIntegration } from "./capability.js";
|
|
4
|
+
import { GitHubClient, parseGitHubProfiles } from "./client.js";
|
|
5
|
+
import { createGitHubProvider } from "./provider.js";
|
|
6
|
+
import { registerGitHubTools } from "./tools.js";
|
|
7
|
+
|
|
8
|
+
export const manifest = { id: "github", version: "0.1.9" } as const;
|
|
9
|
+
|
|
10
|
+
const extension: LeitwerkExtensionModule = {
|
|
11
|
+
manifest,
|
|
12
|
+
setupServer(api, config) {
|
|
13
|
+
const profiles = parseGitHubProfiles(config);
|
|
14
|
+
const integration: GitHubIntegration = {
|
|
15
|
+
client(profile) {
|
|
16
|
+
const value = profiles.get(profile);
|
|
17
|
+
if (!value) throw new Error(`Unknown GitHub profile '${profile}'`);
|
|
18
|
+
return new GitHubClient(value);
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
setupGitHubIntegration(api, integration);
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export * from "./capability.js";
|
|
26
|
+
export * from "./client.js";
|
|
27
|
+
export * from "./external.js";
|
|
28
|
+
export default extension;
|
|
29
|
+
|
|
30
|
+
/** Register shared tools and polling with an explicit integration. */
|
|
31
|
+
export function setupGitHubIntegration(
|
|
32
|
+
api: Parameters<NonNullable<LeitwerkExtensionModule["setupServer"]>>[0],
|
|
33
|
+
integration: GitHubIntegration,
|
|
34
|
+
options: { now?: () => number } = {},
|
|
35
|
+
) {
|
|
36
|
+
api.provide(githubIntegration, integration);
|
|
37
|
+
const deps = api.get(coreHostCapabilities.serverSetup);
|
|
38
|
+
if (!deps || Array.isArray(deps)) return;
|
|
39
|
+
registerGitHubTools(api, integration, deps.externalWrites as ExternalWriteLogRepoLike);
|
|
40
|
+
return createGitHubProvider(deps, integration, options);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export * from "./binding.js";
|
|
44
|
+
|
|
45
|
+
export * from "./issue-watcher.js";
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { asUnknownRecord } from "@leitwerk-dev/domain";
|
|
2
|
+
import {
|
|
3
|
+
type CoreServerSetupDeps,
|
|
4
|
+
type ExternalSourceArmingLike,
|
|
5
|
+
matchesRepository as matchesConfiguredRepository,
|
|
6
|
+
parseRepositoryFeedbackConfig as parseFeedbackConfig,
|
|
7
|
+
parseRepositoryIssueCancelledConfig as parseIssueCancelledConfig,
|
|
8
|
+
parseRepositoryPullRequestConfig as parsePrConfig,
|
|
9
|
+
type RegisteredProcessWatcherLike,
|
|
10
|
+
repositoryFeedbackBatch,
|
|
11
|
+
} from "@leitwerk-dev/process-sdk";
|
|
12
|
+
import { sameSubscription } from "@leitwerk-dev/repository-rebase";
|
|
13
|
+
import { createPollSchedule, type emptyPollResult } from "@leitwerk-dev/watcher-utils";
|
|
14
|
+
import type { GitHubIntegration } from "./capability.js";
|
|
15
|
+
import type { GitHubIssue, GitHubRepository } from "./client.js";
|
|
16
|
+
import {
|
|
17
|
+
describeGitHubEvent,
|
|
18
|
+
GITHUB_CHECKS_KIND,
|
|
19
|
+
GITHUB_ISSUE_CANCELLED_KIND,
|
|
20
|
+
GITHUB_PR_FEEDBACK_KIND,
|
|
21
|
+
GITHUB_PR_TERMINAL_KIND,
|
|
22
|
+
} from "./external.js";
|
|
23
|
+
import {
|
|
24
|
+
type GitHubIssueWatcherConfig,
|
|
25
|
+
type GitHubIssueWatcherEvent,
|
|
26
|
+
githubIssueWatcherSource,
|
|
27
|
+
} from "./issue-watcher.js";
|
|
28
|
+
import { githubPollReporter } from "./poll-report.js";
|
|
29
|
+
|
|
30
|
+
function hasLabel(issue: GitHubIssue, label: string): boolean {
|
|
31
|
+
return issue.labels.some((candidate) => candidate.name === label);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function externalId(repo: GitHubRepository, issue: GitHubIssue): string {
|
|
35
|
+
return `github:${repo.full_name}#${issue.number}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { matchesConfiguredRepository };
|
|
39
|
+
|
|
40
|
+
export function createGitHubIssuePolling(
|
|
41
|
+
deps: CoreServerSetupDeps,
|
|
42
|
+
integration: GitHubIntegration,
|
|
43
|
+
options: { now?: () => number } = {},
|
|
44
|
+
) {
|
|
45
|
+
const now = options.now ?? Date.now;
|
|
46
|
+
const due = createPollSchedule(now);
|
|
47
|
+
|
|
48
|
+
async function discover(
|
|
49
|
+
watcher: RegisteredProcessWatcherLike<GitHubIssueWatcherConfig, GitHubIssueWatcherEvent>,
|
|
50
|
+
result: ReturnType<typeof emptyPollResult>,
|
|
51
|
+
) {
|
|
52
|
+
const config = watcher.config;
|
|
53
|
+
const client = integration.client(config.profile);
|
|
54
|
+
for (const repo of await client.listRepositories()) {
|
|
55
|
+
if (!matchesConfiguredRepository(config, repo)) continue;
|
|
56
|
+
for (const issue of await client.listOpenIssues(repo.owner.login, repo.name)) {
|
|
57
|
+
if (!hasLabel(issue, config.labels.trigger) || hasLabel(issue, config.labels.done))
|
|
58
|
+
continue;
|
|
59
|
+
const id = externalId(repo, issue);
|
|
60
|
+
try {
|
|
61
|
+
if (deps.processes.listAll().some((process) => process.externalId === id)) {
|
|
62
|
+
result.skipped.push(id);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const authorized = await client.authorizedTrigger(
|
|
66
|
+
repo.owner.login,
|
|
67
|
+
repo.name,
|
|
68
|
+
issue.number,
|
|
69
|
+
config.labels.trigger,
|
|
70
|
+
config.labels.done,
|
|
71
|
+
);
|
|
72
|
+
if (!authorized) {
|
|
73
|
+
result.skipped.push(`${id}:unauthorized`);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const event: GitHubIssueWatcherEvent = {
|
|
77
|
+
profile: config.profile,
|
|
78
|
+
repository: repo,
|
|
79
|
+
issue: authorized.issue,
|
|
80
|
+
authorization: {
|
|
81
|
+
actor: authorized.actor,
|
|
82
|
+
eventId: authorized.eventId,
|
|
83
|
+
},
|
|
84
|
+
labels: config.labels,
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const guardedWatcher = {
|
|
88
|
+
...watcher,
|
|
89
|
+
async resolveLaunchAttempt(...args: Parameters<typeof watcher.resolveLaunchAttempt>) {
|
|
90
|
+
const attempt = await watcher.resolveLaunchAttempt(...args);
|
|
91
|
+
if (!attempt) return null;
|
|
92
|
+
return {
|
|
93
|
+
...attempt,
|
|
94
|
+
preparationChecks: [
|
|
95
|
+
...attempt.preparationChecks,
|
|
96
|
+
{
|
|
97
|
+
id: "github-trigger-authorization",
|
|
98
|
+
label: "Check GitHub trigger authorization",
|
|
99
|
+
async run() {
|
|
100
|
+
const rechecked = await client.authorizedTrigger(
|
|
101
|
+
repo.owner.login,
|
|
102
|
+
repo.name,
|
|
103
|
+
issue.number,
|
|
104
|
+
config.labels.trigger,
|
|
105
|
+
config.labels.done,
|
|
106
|
+
);
|
|
107
|
+
if (
|
|
108
|
+
!rechecked ||
|
|
109
|
+
rechecked.eventId !== authorized.eventId ||
|
|
110
|
+
rechecked.actor !== authorized.actor
|
|
111
|
+
)
|
|
112
|
+
throw new Error("GitHub trigger authorization changed before launch");
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
};
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
const launched = await deps.launchRuns.startWatcher(guardedWatcher, event, {
|
|
120
|
+
idempotencyKey: id,
|
|
121
|
+
});
|
|
122
|
+
if (launched.error) result.errors.push(`${id}:launch_failed:${launched.error}`);
|
|
123
|
+
else if (launched.process) result.created.push(id);
|
|
124
|
+
else result.skipped.push(id);
|
|
125
|
+
} catch (error) {
|
|
126
|
+
result.errors.push(
|
|
127
|
+
`${id}:${error instanceof Error ? error.message : "discovery_failed"}`,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function pollExternal(result: ReturnType<typeof emptyPollResult>) {
|
|
135
|
+
const report = githubPollReporter(deps.externalSources, result);
|
|
136
|
+
const terminalArmings = deps.externalSources.listArmed(GITHUB_PR_TERMINAL_KIND);
|
|
137
|
+
const terminalInstances = new Set<string>();
|
|
138
|
+
async function poll<C extends { pollInterval?: string; disabled?: boolean }>(
|
|
139
|
+
kind: string,
|
|
140
|
+
parse: (value: unknown) => C | null,
|
|
141
|
+
read: (config: C, armed: ExternalSourceArmingLike) => Promise<void>,
|
|
142
|
+
) {
|
|
143
|
+
const armings =
|
|
144
|
+
kind === GITHUB_PR_TERMINAL_KIND ? terminalArmings : deps.externalSources.listArmed(kind);
|
|
145
|
+
for (const armed of armings) {
|
|
146
|
+
if (kind !== GITHUB_PR_TERMINAL_KIND && terminalInstances.has(armed.instanceId)) continue;
|
|
147
|
+
const config = parse(armed.resolved);
|
|
148
|
+
const key = `${armed.instanceId}:${armed.id}:${armed.generation ?? ""}`;
|
|
149
|
+
if (!config || !due(key, config.pollInterval ?? "30s") || config.disabled) continue;
|
|
150
|
+
try {
|
|
151
|
+
await read(config, armed);
|
|
152
|
+
} catch (error) {
|
|
153
|
+
await report.observe(armed, {
|
|
154
|
+
refreshError: error instanceof Error ? error.message : "GitHub refresh failed",
|
|
155
|
+
});
|
|
156
|
+
const fallback = kind === GITHUB_CHECKS_KIND ? "checks_failed" : "poll_failed";
|
|
157
|
+
result.errors.push(`${armed.id}:${error instanceof Error ? error.message : fallback}`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
await poll(GITHUB_PR_TERMINAL_KIND, parsePrConfig, async (config, armed) => {
|
|
162
|
+
const pr = await integration
|
|
163
|
+
.client(config.profile)
|
|
164
|
+
.getPullRequest(config.owner, config.repo, config.prNumber);
|
|
165
|
+
if (!pr.merged && pr.state !== "closed") return;
|
|
166
|
+
if (
|
|
167
|
+
!deps.externalSources
|
|
168
|
+
.listArmed(GITHUB_PR_TERMINAL_KIND)
|
|
169
|
+
.some((current) => sameSubscription(armed, current))
|
|
170
|
+
)
|
|
171
|
+
return;
|
|
172
|
+
terminalInstances.add(armed.instanceId);
|
|
173
|
+
const outcome = pr.merged ? "merged" : "closed";
|
|
174
|
+
if (config.terminalOutcome && config.terminalOutcome !== outcome) return;
|
|
175
|
+
await report.fire(armed, { pullRequest: pr }, `${pr.number}:${outcome}`);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
await poll(
|
|
179
|
+
GITHUB_CHECKS_KIND,
|
|
180
|
+
(value) => ({ pollInterval: "30s", config: asUnknownRecord(value) ?? {} }),
|
|
181
|
+
async ({ config }, armed) => {
|
|
182
|
+
const client = integration.client(String(config.profile));
|
|
183
|
+
const pr = await client.getPullRequest(
|
|
184
|
+
String(config.owner),
|
|
185
|
+
String(config.repo),
|
|
186
|
+
Number(config.prNumber),
|
|
187
|
+
);
|
|
188
|
+
if (pr.state !== "open" || pr.head.sha !== config.headSha) return;
|
|
189
|
+
const checks = await client.getCheckSummary(
|
|
190
|
+
String(config.owner),
|
|
191
|
+
String(config.repo),
|
|
192
|
+
String(config.headSha),
|
|
193
|
+
);
|
|
194
|
+
if (checks.headSha !== config.headSha) return;
|
|
195
|
+
const refreshed = await client.getPullRequest(
|
|
196
|
+
String(config.owner),
|
|
197
|
+
String(config.repo),
|
|
198
|
+
Number(config.prNumber),
|
|
199
|
+
);
|
|
200
|
+
if (refreshed.state !== "open" || refreshed.merged || refreshed.head.sha !== checks.headSha)
|
|
201
|
+
return;
|
|
202
|
+
await report.observe(armed, {
|
|
203
|
+
observation: {
|
|
204
|
+
...describeGitHubEvent({ checks }),
|
|
205
|
+
observedAt: new Date(now()).toISOString(),
|
|
206
|
+
subject: `${config.owner}/${config.repo}#${config.prNumber}`,
|
|
207
|
+
revision: checks.headSha,
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
if (checks.status !== "failure") return;
|
|
211
|
+
await report.fire(
|
|
212
|
+
armed,
|
|
213
|
+
{ checks },
|
|
214
|
+
`${checks.headSha}:${checks.failed.map((run) => run.url).join(":")}`,
|
|
215
|
+
);
|
|
216
|
+
},
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
await poll(GITHUB_ISSUE_CANCELLED_KIND, parseIssueCancelledConfig, async (config, armed) => {
|
|
220
|
+
const issue = await integration
|
|
221
|
+
.client(config.profile)
|
|
222
|
+
.getIssue(config.owner, config.repo, config.issueNumber);
|
|
223
|
+
const reason =
|
|
224
|
+
issue.state === "closed"
|
|
225
|
+
? "issue_closed"
|
|
226
|
+
: hasLabel(issue, config.triggerLabel)
|
|
227
|
+
? null
|
|
228
|
+
: "trigger_label_removed";
|
|
229
|
+
if (!reason) return;
|
|
230
|
+
// A merge may close the issue between terminal polls. Preserve terminal
|
|
231
|
+
// reconciliation instead of treating that close as cancellation.
|
|
232
|
+
const terminal = terminalArmings
|
|
233
|
+
.filter((candidate) => candidate.instanceId === armed.instanceId)
|
|
234
|
+
.map((candidate) => parsePrConfig(candidate.resolved))
|
|
235
|
+
.find((candidate) => candidate && !candidate.disabled);
|
|
236
|
+
if (terminal) {
|
|
237
|
+
const pr = await integration
|
|
238
|
+
.client(terminal.profile)
|
|
239
|
+
.getPullRequest(terminal.owner, terminal.repo, terminal.prNumber);
|
|
240
|
+
if (pr.merged || pr.state === "closed") {
|
|
241
|
+
terminalInstances.add(armed.instanceId);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
await report.fire(armed, { issue, reason }, `${issue.number}:${reason}`);
|
|
246
|
+
});
|
|
247
|
+
await poll(GITHUB_PR_FEEDBACK_KIND, parseFeedbackConfig, async (config, armed) => {
|
|
248
|
+
const client = integration.client(config.profile);
|
|
249
|
+
const pr = await client.getPullRequest(config.owner, config.repo, config.prNumber);
|
|
250
|
+
if (pr.merged || pr.state !== "open") return;
|
|
251
|
+
const unseen = (
|
|
252
|
+
await client.listActionablePullRequestFeedback(config.owner, config.repo, config.prNumber)
|
|
253
|
+
).filter((item) => item.id > config[`${item.kind}Cursor`]);
|
|
254
|
+
|
|
255
|
+
const batch = repositoryFeedbackBatch(unseen, config, now());
|
|
256
|
+
if (batch) await report.fire(armed, batch.event, batch.mergeKey);
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return async (result: ReturnType<typeof emptyPollResult>) => {
|
|
261
|
+
const watchers = deps.processWatchers?.listBySource(githubIssueWatcherSource) ?? [];
|
|
262
|
+
for (const watcher of watchers) {
|
|
263
|
+
if (
|
|
264
|
+
!watcher.enabled ||
|
|
265
|
+
!due(`${watcher.processId}:${watcher.watcherId}`, watcher.config.pollInterval)
|
|
266
|
+
)
|
|
267
|
+
continue;
|
|
268
|
+
try {
|
|
269
|
+
await discover(watcher, result);
|
|
270
|
+
} catch (error) {
|
|
271
|
+
result.errors.push(
|
|
272
|
+
`${watcher.processId}:${watcher.watcherId}:${error instanceof Error ? error.message : "discovery_failed"}`,
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
await pollExternal(result);
|
|
277
|
+
};
|
|
278
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineProcessWatcherSource,
|
|
3
|
+
parseRepositoryIssueWatcherConfig,
|
|
4
|
+
presentRepositoryIssueWatcherConfig,
|
|
5
|
+
type RepositoryIssueWatcherConfig,
|
|
6
|
+
} from "@leitwerk-dev/process-sdk";
|
|
7
|
+
import type { GitHubIssue, GitHubRepository } from "./client.js";
|
|
8
|
+
|
|
9
|
+
export interface GitHubIssueWatcherConfig extends RepositoryIssueWatcherConfig {}
|
|
10
|
+
|
|
11
|
+
export interface GitHubIssueWatcherEvent {
|
|
12
|
+
authorization: { actor: string; eventId: number };
|
|
13
|
+
profile: string;
|
|
14
|
+
repository: GitHubRepository;
|
|
15
|
+
issue: GitHubIssue;
|
|
16
|
+
labels: {
|
|
17
|
+
trigger: string;
|
|
18
|
+
done: string;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const githubIssueWatcherSource = defineProcessWatcherSource<
|
|
23
|
+
GitHubIssueWatcherConfig,
|
|
24
|
+
GitHubIssueWatcherEvent
|
|
25
|
+
>({
|
|
26
|
+
id: "@leitwerk-public/github.issue",
|
|
27
|
+
label: "GitHub issue",
|
|
28
|
+
parseConfig: (raw) => parseRepositoryIssueWatcherConfig(raw, "github_issue", true),
|
|
29
|
+
presentConfig: presentRepositoryIssueWatcherConfig,
|
|
30
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createExternalSourcePollReporter,
|
|
3
|
+
type ExternalSourceArmingLike,
|
|
4
|
+
type ExternalSourceServiceLike,
|
|
5
|
+
} from "@leitwerk-dev/process-sdk";
|
|
6
|
+
import { sameSubscription } from "@leitwerk-dev/repository-rebase";
|
|
7
|
+
import {
|
|
8
|
+
GITHUB_CHECKS_KIND,
|
|
9
|
+
GITHUB_ISSUE_CANCELLED_KIND,
|
|
10
|
+
GITHUB_PR_FEEDBACK_KIND,
|
|
11
|
+
GITHUB_PR_STATE_KIND,
|
|
12
|
+
GITHUB_PR_TERMINAL_KIND,
|
|
13
|
+
GITHUB_RELEASE_KIND,
|
|
14
|
+
} from "./external.js";
|
|
15
|
+
|
|
16
|
+
export function githubPollReporter(
|
|
17
|
+
sources: ExternalSourceServiceLike,
|
|
18
|
+
result: { created: string[]; errors: string[] },
|
|
19
|
+
) {
|
|
20
|
+
const report = createExternalSourcePollReporter(sources, result, { forwardGeneration: true });
|
|
21
|
+
const current = (armed: ExternalSourceArmingLike) =>
|
|
22
|
+
[
|
|
23
|
+
GITHUB_CHECKS_KIND,
|
|
24
|
+
GITHUB_PR_STATE_KIND,
|
|
25
|
+
GITHUB_PR_TERMINAL_KIND,
|
|
26
|
+
GITHUB_PR_FEEDBACK_KIND,
|
|
27
|
+
GITHUB_ISSUE_CANCELLED_KIND,
|
|
28
|
+
GITHUB_RELEASE_KIND,
|
|
29
|
+
].some((kind) => sources.listArmed(kind).some((value) => sameSubscription(armed, value)));
|
|
30
|
+
return {
|
|
31
|
+
...report,
|
|
32
|
+
async fire(
|
|
33
|
+
armed: Parameters<typeof report.fire>[0],
|
|
34
|
+
event: Record<string, unknown>,
|
|
35
|
+
mergeKey: string,
|
|
36
|
+
) {
|
|
37
|
+
if (!current(armed as ExternalSourceArmingLike)) return false;
|
|
38
|
+
return report.fire(armed, event, mergeKey);
|
|
39
|
+
},
|
|
40
|
+
observe(
|
|
41
|
+
armed: Parameters<typeof report.observe>[0],
|
|
42
|
+
input: Parameters<typeof report.observe>[1],
|
|
43
|
+
) {
|
|
44
|
+
if (current(armed as ExternalSourceArmingLike)) return report.observe(armed, input);
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|