@defrex/autobuild 1.4.2 → 1.6.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 +1 -1
- package/SPEC.md +512 -101
- package/bin/ab-dispatch-kernel.ts +1 -0
- package/docs/accounts.md +9 -0
- package/docs/agent-runtimes.md +1 -0
- package/docs/answering-blocked-builds.md +7 -3
- package/docs/architecture.md +313 -130
- package/docs/assets/headline-wide.png +0 -0
- package/docs/build-store.md +33 -3
- package/docs/cli.md +2 -1
- package/docs/configuration-overview.md +9 -0
- package/docs/configuration.md +217 -106
- package/docs/dispatcher-hosting.md +19 -3
- package/docs/dispatcher.md +11 -6
- package/docs/glossary.md +1 -1
- package/docs/integration-overview.md +2 -2
- package/docs/integration-tree.md +91 -28
- package/docs/merge-policy.md +28 -9
- package/docs/operating.md +1 -1
- package/docs/operator-api.md +57 -20
- package/docs/operator-surfaces.md +1 -1
- package/docs/plugin-authoring.md +89 -8
- package/docs/remote-store-protocol.md +39 -11
- package/docs/remote-ticket-protocol.md +126 -12
- package/docs/restacking.md +24 -6
- package/docs/seams.md +2 -2
- package/docs/setup.md +19 -13
- package/docs/spec-standard.md +2 -1
- package/docs/ticket-dependencies.md +26 -8
- package/docs/ticket-migration.md +70 -7
- package/docs/ticket-sources.md +82 -5
- package/docs/tickets.md +34 -2
- package/docs/troubleshooting.md +3 -3
- package/docs/verification.md +5 -5
- package/docs/website.md +8 -0
- package/docs/workspaces.md +1 -1
- package/package.json +4 -3
- package/packages/core/src/accounts/hosted-readiness.ts +21 -0
- package/packages/core/src/cli/attention.ts +3 -0
- package/packages/core/src/cli/build-control.ts +36 -12
- package/packages/core/src/cli/connect.ts +9 -18
- package/packages/core/src/cli/context.ts +44 -0
- package/packages/core/src/cli/dashboard/configuration.ts +11 -0
- package/packages/core/src/cli/dashboard/model.ts +51 -11
- package/packages/core/src/cli/dashboard/poll.ts +14 -0
- package/packages/core/src/cli/dashboard/queue.ts +21 -0
- package/packages/core/src/cli/dashboard/render.ts +43 -22
- package/packages/core/src/cli/dispatch-frontend.ts +63 -30
- package/packages/core/src/cli/dispatch-mode.ts +35 -16
- package/packages/core/src/cli/dispatch-preflight.ts +4 -1
- package/packages/core/src/cli/dispatch-repository-feed.ts +40 -11
- package/packages/core/src/cli/dispatch-tickets.ts +47 -0
- package/packages/core/src/cli/dispatch-upgrade-controller.ts +3 -0
- package/packages/core/src/cli/dispatch.ts +522 -145
- package/packages/core/src/cli/help.ts +63 -24
- package/packages/core/src/cli/init-hosted-accounts.ts +54 -0
- package/packages/core/src/cli/init-readiness-remote.ts +16 -3
- package/packages/core/src/cli/init-validation.ts +144 -42
- package/packages/core/src/cli/issue-links.ts +67 -0
- package/packages/core/src/cli/main.ts +32 -15
- package/packages/core/src/cli/pr-summary-publication.ts +38 -7
- package/packages/core/src/cli/publication-settlement.ts +36 -24
- package/packages/core/src/cli/repo-lease.ts +225 -0
- package/packages/core/src/cli/repository-status.ts +203 -49
- package/packages/core/src/cli/status.ts +42 -4
- package/packages/core/src/cli/terminals.ts +37 -2
- package/packages/core/src/cli/ticket-editor.ts +20 -6
- package/packages/core/src/cli/ticket.ts +264 -46
- package/packages/core/src/cli/triage-count-sampler.ts +59 -0
- package/packages/core/src/cli/wait.ts +4 -1
- package/packages/core/src/cli/watch.ts +19 -4
- package/packages/core/src/config/live.ts +45 -4
- package/packages/core/src/config/load.ts +9 -0
- package/packages/core/src/config/pipeline-source.ts +6 -2
- package/packages/core/src/config/ready-assignee.ts +86 -0
- package/packages/core/src/config/schema.ts +202 -38
- package/packages/core/src/distribution-packaging.ts +1 -1
- package/packages/core/src/events/catalog.ts +7 -0
- package/packages/core/src/events/compatibility.ts +2 -1
- package/packages/core/src/events/payloads.ts +85 -0
- package/packages/core/src/events/policy.ts +10 -0
- package/packages/core/src/events/repository.ts +93 -28
- package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +237 -230
- package/packages/core/src/kernel/build-identity.ts +12 -0
- package/packages/core/src/kernel/change-presence.ts +139 -6
- package/packages/core/src/kernel/dispatch-capacity.ts +189 -19
- package/packages/core/src/kernel/dispatch-status.ts +10 -1
- package/packages/core/src/kernel/dispatch-upgrade.ts +104 -5
- package/packages/core/src/kernel/engine.ts +17 -8
- package/packages/core/src/kernel/finalize-pr-rejection.ts +95 -0
- package/packages/core/src/kernel/generators/build-log.ts +9 -0
- package/packages/core/src/kernel/generators/repository-journals.ts +15 -1
- package/packages/core/src/kernel/harvest.ts +4 -1
- package/packages/core/src/kernel/incremental-fixtures.ts +1 -0
- package/packages/core/src/kernel/integration-completeness.ts +104 -17
- package/packages/core/src/kernel/integration-landing.ts +17 -0
- package/packages/core/src/kernel/integration-state.ts +17 -0
- package/packages/core/src/kernel/issue-keywords.ts +202 -0
- package/packages/core/src/kernel/janitor-failure.ts +96 -5
- package/packages/core/src/kernel/kernel-git.ts +87 -0
- package/packages/core/src/kernel/landing-readiness.ts +25 -17
- package/packages/core/src/kernel/leaf-restack-state.ts +4 -2
- package/packages/core/src/kernel/log-index.ts +1 -1
- package/packages/core/src/kernel/node-issue-links.ts +142 -0
- package/packages/core/src/kernel/node-landing.ts +2 -1
- package/packages/core/src/kernel/node-parent.ts +6 -0
- package/packages/core/src/kernel/node-restack-transaction.ts +2 -0
- package/packages/core/src/kernel/pr-summary.ts +24 -0
- package/packages/core/src/kernel/read-deferrals.ts +75 -12
- package/packages/core/src/kernel/read-facts.ts +32 -1
- package/packages/core/src/kernel/reconcile-restack.ts +1 -1
- package/packages/core/src/kernel/reducer-registry.ts +37 -2
- package/packages/core/src/kernel/reducer.ts +147 -5
- package/packages/core/src/kernel/ticket-preparation.ts +16 -0
- package/packages/core/src/kernel/verify-budget.ts +43 -0
- package/packages/core/src/operator/index.ts +3 -1
- package/packages/core/src/operator/presentation.ts +3 -0
- package/packages/core/src/operator/protocol.ts +38 -2
- package/packages/core/src/operator/query.ts +69 -29
- package/packages/core/src/operator/registry.ts +47 -26
- package/packages/core/src/operator/tickets.ts +77 -10
- package/packages/core/src/plugin-sdk/index.ts +13 -0
- package/packages/core/src/plugins/manifest.ts +1 -1
- package/packages/core/src/plugins/registry.ts +1 -0
- package/packages/core/src/ports/forge/contract.ts +69 -0
- package/packages/core/src/ports/forge/create.ts +5 -0
- package/packages/core/src/ports/forge/fake.ts +184 -3
- package/packages/core/src/ports/forge/git-rewrite.ts +4 -2
- package/packages/core/src/ports/forge/github.ts +175 -38
- package/packages/core/src/ports/forge/local-git.ts +14 -0
- package/packages/core/src/ports/forge/readiness.ts +10 -0
- package/packages/core/src/ports/forge-failure.ts +43 -0
- package/packages/core/src/ports/runner/claude.ts +7 -1
- package/packages/core/src/ports/runner/codex.ts +7 -2
- package/packages/core/src/ports/runner/native-cli-fixture.ts +2 -1
- package/packages/core/src/ports/runner/pi.ts +7 -1
- package/packages/core/src/ports/runner/runtime.ts +3 -0
- package/packages/core/src/ports/runner/start-prompt.ts +37 -0
- package/packages/core/src/ports/tickets/contract.ts +320 -7
- package/packages/core/src/ports/tickets/create.ts +89 -20
- package/packages/core/src/ports/tickets/fake.ts +18 -0
- package/packages/core/src/ports/tickets/file-codec.ts +20 -0
- package/packages/core/src/ports/tickets/file.ts +43 -1
- package/packages/core/src/ports/tickets/github-fake.ts +1123 -0
- package/packages/core/src/ports/tickets/github-issues.ts +431 -0
- package/packages/core/src/ports/tickets/github-validators.ts +194 -0
- package/packages/core/src/ports/tickets/github.ts +2029 -0
- package/packages/core/src/ports/tickets/hosted-index.ts +34 -3
- package/packages/core/src/ports/tickets/issue-links.ts +86 -0
- package/packages/core/src/ports/tickets/lifecycle.ts +60 -4
- package/packages/core/src/ports/tickets/linear.ts +66 -6
- package/packages/core/src/ports/tickets/migration.ts +290 -68
- package/packages/core/src/ports/tickets/operations.ts +98 -12
- package/packages/core/src/ports/tickets/priority.ts +55 -0
- package/packages/core/src/ports/tickets/remote-protocol.ts +195 -29
- package/packages/core/src/ports/tickets/remote.ts +188 -13
- package/packages/core/src/ports/tickets/revision.ts +22 -1
- package/packages/core/src/ports/tickets/states.ts +43 -0
- package/packages/core/src/ports/tickets/tree.ts +58 -12
- package/packages/core/src/ports/tickets/update.ts +12 -2
- package/packages/core/src/ports/types.ts +194 -9
- package/packages/core/src/ports/workspace/commit-preservation.ts +163 -25
- package/packages/core/src/ports/workspace/git-worktree.ts +6 -2
- package/packages/core/src/ports/workspace/harvest-execution.ts +4 -2
- package/packages/core/src/ports/workspace/vercel-sandbox.ts +121 -27
- package/packages/core/src/processes/build-runner.ts +101 -22
- package/packages/core/src/processes/dispatcher.ts +1046 -372
- package/packages/core/src/processes/execution-no-progress.ts +7 -0
- package/packages/core/src/processes/harvest-child.ts +4 -2
- package/packages/core/src/processes/harvest-execution-state.ts +104 -0
- package/packages/core/src/processes/harvest-runner.ts +263 -25
- package/packages/core/src/processes/harvest.ts +5 -8
- package/packages/core/src/processes/tick-publication.ts +12 -2
- package/packages/core/src/processes/ticket-scan-schedule.ts +78 -0
- package/packages/core/src/processes/workspace-preservation.ts +36 -1
- package/packages/core/src/runtime-identity.ts +10 -2
- package/packages/core/src/scenarios/harness.ts +149 -35
- package/packages/core/src/store/adapter.ts +7 -1
- package/packages/core/src/store/contract.ts +83 -2
- package/packages/core/src/store/digest.ts +32 -1
- package/packages/core/src/store/reduced-reads.ts +10 -0
- package/packages/core/src/store/reducer-value-shapes.json +4471 -3427
- package/packages/core/src/store/remote/protocol.ts +2 -0
- package/packages/core/src/store/remote/released-clients.json +22 -0
- package/packages/core/src/store/remote/released-reducer-values/1.5.0.json +26924 -0
- package/packages/core/src/store/remote/released-reducer-values/1.6.0.json +27173 -0
- package/packages/core/src/store/remote/released-reducer-values/index.ts +4 -0
- package/packages/core/src/store/repo-state-events.ts +2 -0
- package/packages/core/src/store/types.ts +5 -0
- package/packages/core/src/testing/dispatch-read-budget.ts +729 -123
- package/packages/core/src/testing/index.ts +18 -1
- package/packages/core/src/testing/presence-history.ts +79 -0
- package/packages/core/src/testing/reduced-read-budget.ts +1424 -33
- package/packages/core/src/testing/upgrade-generations.ts +77 -0
- package/skills/code-review/SKILL.md +4 -3
- package/skills/finalize/SKILL.md +18 -2
- package/skills/guide/SKILL.md +417 -143
- package/skills/guide/references/accounts.md +9 -0
- package/skills/guide/references/integration-tree.md +91 -28
- package/skills/guide/references/merge-policy.md +28 -9
- package/skills/guide/references/operator-api.md +57 -20
- package/skills/guide/references/plugin-authoring.md +89 -8
- package/skills/guide/references/remote-store-protocol.md +39 -11
- package/skills/guide/references/remote-ticket-protocol.md +126 -12
- package/skills/guide/references/restacking.md +24 -6
- package/skills/guide/references/setup.md +19 -13
- package/skills/guide/references/spec-standard.md +2 -1
- package/skills/guide/references/ticket-dependencies.md +26 -8
- package/skills/guide/references/ticket-migration.md +70 -7
- package/skills/harvest-review/SKILL.md +4 -3
- package/skills/implement/SKILL.md +20 -1
- package/skills/operate/SKILL.md +149 -35
- package/skills/plan/SKILL.md +1 -1
- package/skills/reconcile/SKILL.md +5 -4
- package/skills/spec/SKILL.md +15 -2
- package/skills/tickets/SKILL.md +116 -37
- package/templates/autobuild.toml +5 -0
- package/templates/hosted-service/package.json +2 -2
|
@@ -0,0 +1,1123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory GitHub Issues API for the `github` ticket source's seam tests.
|
|
3
|
+
* Like `fake.ts`, it is a fixture module rather than a test file, so scenario
|
|
4
|
+
* tests can drive a real `GitHubTicketSource` with no network.
|
|
5
|
+
*
|
|
6
|
+
* It serves the REST routes and the GraphQL queries the adapter sends, with
|
|
7
|
+
* ETags (a hash of the response) and `If-None-Match` → 304, and counts every
|
|
8
|
+
* request by route so budget tests can assert exact costs. Sub-issues are
|
|
9
|
+
* served as GitHub does: `parent_issue_url` and `sub_issues_summary` on
|
|
10
|
+
* every issue, the paginated `GET /issues/{n}/sub_issues`, `POST` to add one
|
|
11
|
+
* and `DELETE /issues/{n}/sub_issue` to remove one. It deliberately
|
|
12
|
+
* serves the comment-history route (`comments-list`) for explicit migration reads,
|
|
13
|
+
* but no REST issue-events or REST search route: any
|
|
14
|
+
* adapter read of those fails a test with a 404.
|
|
15
|
+
* Identity routes answer for the requesting credential: `GET user` returns
|
|
16
|
+
* its login (or 403 for `transportAs(null)`), and the assignee-check route
|
|
17
|
+
* answers 204 for `assignable` logins.
|
|
18
|
+
*
|
|
19
|
+
* UI-style mutators simulate what people do on GitHub outside the adapter.
|
|
20
|
+
*/
|
|
21
|
+
import { createHash } from 'node:crypto'
|
|
22
|
+
import { GitHubApiError, type GitHubRequest, type GitHubResponse } from '../forge/github-transport'
|
|
23
|
+
|
|
24
|
+
interface FakeEvent {
|
|
25
|
+
type: 'labeled' | 'unlabeled' | 'closed' | 'reopened'
|
|
26
|
+
label?: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface FakeIssue {
|
|
30
|
+
id: number
|
|
31
|
+
number: number
|
|
32
|
+
title: string
|
|
33
|
+
body: string | null
|
|
34
|
+
state: 'open' | 'closed'
|
|
35
|
+
state_reason: string | null
|
|
36
|
+
labels: string[]
|
|
37
|
+
/** Assignee logins. */
|
|
38
|
+
assignees: string[]
|
|
39
|
+
created_at: string
|
|
40
|
+
updated_at: string
|
|
41
|
+
pull_request: boolean
|
|
42
|
+
gone?: 'deleted' | 'transferred'
|
|
43
|
+
/** Where a transferred issue went, as an `owner/repo#number` key. */
|
|
44
|
+
movedTo?: string
|
|
45
|
+
comments: number
|
|
46
|
+
events: FakeEvent[]
|
|
47
|
+
/** Blockers as `owner/repo#number` keys. */
|
|
48
|
+
blockedBy: string[]
|
|
49
|
+
/** The parent issue as an `owner/repo#number` key. */
|
|
50
|
+
parent?: string
|
|
51
|
+
/** State changes for the lagging search index: newest last. */
|
|
52
|
+
history: Array<{ at: number; state: 'open' | 'closed'; state_reason: string | null }>
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface FakeComment {
|
|
56
|
+
id: number
|
|
57
|
+
body: string
|
|
58
|
+
created_at: string
|
|
59
|
+
user: { login: string } | null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
interface FakeLabel {
|
|
63
|
+
name: string
|
|
64
|
+
description: string | null
|
|
65
|
+
color: string
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export type FakeGitHubRequestKind =
|
|
69
|
+
| 'repo'
|
|
70
|
+
| 'list-issues'
|
|
71
|
+
| 'create-issue'
|
|
72
|
+
| 'get-issue'
|
|
73
|
+
| 'patch-issue'
|
|
74
|
+
| 'comment'
|
|
75
|
+
| 'comments-list'
|
|
76
|
+
| 'blocked-by-list'
|
|
77
|
+
| 'blocked-by-add'
|
|
78
|
+
| 'blocked-by-remove'
|
|
79
|
+
| 'sub-issues-list'
|
|
80
|
+
| 'sub-issue-add'
|
|
81
|
+
| 'sub-issue-remove'
|
|
82
|
+
| 'labels-list'
|
|
83
|
+
| 'label-get'
|
|
84
|
+
| 'label-create'
|
|
85
|
+
| 'count'
|
|
86
|
+
| 'timeline-reopen'
|
|
87
|
+
| 'timeline-closed'
|
|
88
|
+
| 'user'
|
|
89
|
+
| 'assignee-check'
|
|
90
|
+
| 'unknown'
|
|
91
|
+
|
|
92
|
+
export interface FakeGitHubRequestRecord {
|
|
93
|
+
method: string
|
|
94
|
+
path: string
|
|
95
|
+
kind: FakeGitHubRequestKind
|
|
96
|
+
status: number
|
|
97
|
+
/** REST query parameters, as sent. */
|
|
98
|
+
params?: Record<string, string>
|
|
99
|
+
/** GraphQL only: the count query's requested fields. */
|
|
100
|
+
fields?: string[]
|
|
101
|
+
/** GraphQL only: the query text. */
|
|
102
|
+
query?: string
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface FakeGitHubOptions {
|
|
106
|
+
owner?: string
|
|
107
|
+
name?: string
|
|
108
|
+
/** The fake's clock, for `created_at`, `updated_at` and search lag. */
|
|
109
|
+
now?: () => number
|
|
110
|
+
/** How far the `reason:completed` search index lags state changes. */
|
|
111
|
+
searchLagMs?: number
|
|
112
|
+
/** First issue number; 1001 keeps the contract's link numbers free. */
|
|
113
|
+
firstNumber?: number
|
|
114
|
+
/** Clamp `per_page` on sub-issue lists, so tests can prove `Link` is followed. */
|
|
115
|
+
subIssuePageCap?: number
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** GitHub's documented limit on a parent's direct sub-issues. */
|
|
119
|
+
const SUB_ISSUE_LIMIT = 100
|
|
120
|
+
|
|
121
|
+
const API = 'https://api.github.com'
|
|
122
|
+
|
|
123
|
+
function etagOf(json: unknown, link: string | undefined): string {
|
|
124
|
+
return `"${createHash('sha1')
|
|
125
|
+
.update(JSON.stringify(json) + (link ?? ''))
|
|
126
|
+
.digest('hex')}"`
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export class FakeGitHubIssues {
|
|
130
|
+
readonly owner: string
|
|
131
|
+
readonly name: string
|
|
132
|
+
readonly requests: FakeGitHubRequestRecord[] = []
|
|
133
|
+
/** Issue comments posted through the API or the UI, by issue number. */
|
|
134
|
+
readonly comments = new Map<number, string[]>()
|
|
135
|
+
/** The same comments as the history route serves them, oldest first. */
|
|
136
|
+
private readonly commentRecords = new Map<number, FakeComment[]>()
|
|
137
|
+
hasIssues = true
|
|
138
|
+
permissions: Record<string, boolean> = { pull: true, push: true }
|
|
139
|
+
/** When false, the token lacks Issues write: every REST write is refused. */
|
|
140
|
+
issueWrites = true
|
|
141
|
+
/** Simulates a token without push access: GitHub silently drops label
|
|
142
|
+
* changes on issue create and edit. */
|
|
143
|
+
dropLabels = false
|
|
144
|
+
/** When false, UI labelling leaves `updated_at` untouched. */
|
|
145
|
+
labelsBumpUpdatedAt = true
|
|
146
|
+
searchLagMs: number
|
|
147
|
+
/** The login `transport`'s credential belongs to; null has no user (an App
|
|
148
|
+
* installation token). */
|
|
149
|
+
viewer: string | null = 'fake-user'
|
|
150
|
+
/** Logins (lowercased) that can be assigned issues in the repository. */
|
|
151
|
+
readonly assignable = new Set<string>()
|
|
152
|
+
subIssuePageCap: number | undefined
|
|
153
|
+
|
|
154
|
+
private readonly issues = new Map<number, FakeIssue>()
|
|
155
|
+
private readonly foreign = new Map<string, FakeIssue & { repo: string }>()
|
|
156
|
+
private readonly labels = new Map<string, FakeLabel>()
|
|
157
|
+
/** Children by parent key, rebuilt lazily after any change. */
|
|
158
|
+
private childIndex: Map<string, FakeIssue[]> | undefined
|
|
159
|
+
private readonly clock: () => number
|
|
160
|
+
private nextNumber: number
|
|
161
|
+
private nextId = 900_000
|
|
162
|
+
private failures: Array<{
|
|
163
|
+
status: number
|
|
164
|
+
headers: Record<string, string>
|
|
165
|
+
body: unknown
|
|
166
|
+
match?: (method: string, path: string) => boolean
|
|
167
|
+
}> = []
|
|
168
|
+
|
|
169
|
+
constructor(opts: FakeGitHubOptions = {}) {
|
|
170
|
+
this.owner = opts.owner ?? 'acme'
|
|
171
|
+
this.name = opts.name ?? 'widgets'
|
|
172
|
+
this.clock = opts.now ?? Date.now
|
|
173
|
+
this.searchLagMs = opts.searchLagMs ?? 0
|
|
174
|
+
this.nextNumber = opts.firstNumber ?? 1001
|
|
175
|
+
this.subIssuePageCap = opts.subIssuePageCap
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
get repository(): { owner: string; name: string } {
|
|
179
|
+
return { owner: this.owner, name: this.name }
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private get fullName(): string {
|
|
183
|
+
return `${this.owner}/${this.name}`
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// ── Assertions ────────────────────────────────────────────────────────────
|
|
187
|
+
|
|
188
|
+
/** Requests that cost primary budget (everything but 304s). */
|
|
189
|
+
get primary(): number {
|
|
190
|
+
return this.requests.filter((request) => request.status !== 304).length
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
get notModified(): number {
|
|
194
|
+
return this.requests.filter((request) => request.status === 304).length
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
count(kind: FakeGitHubRequestKind, filter?: (r: FakeGitHubRequestRecord) => boolean): number {
|
|
198
|
+
return this.requests.filter((r) => r.kind === kind && (filter?.(r) ?? true)).length
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
graphqlRequests(): FakeGitHubRequestRecord[] {
|
|
202
|
+
return this.requests.filter((r) => r.path === 'graphql')
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
writes(): FakeGitHubRequestRecord[] {
|
|
206
|
+
return this.requests.filter((r) => r.method !== 'GET' && r.path !== 'graphql')
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
resetRequests(): void {
|
|
210
|
+
this.requests.length = 0
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
issue(number: number): Readonly<FakeIssue> | undefined {
|
|
214
|
+
return this.issues.get(number)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
issueCount(): number {
|
|
218
|
+
return [...this.issues.values()].filter((issue) => !issue.pull_request && !issue.gone).length
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
labelNames(): string[] {
|
|
222
|
+
return [...this.labels.values()].map((label) => label.name)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
label(name: string): FakeLabel | undefined {
|
|
226
|
+
return this.labels.get(name.toLowerCase())
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// ── Fault injection ───────────────────────────────────────────────────────
|
|
230
|
+
|
|
231
|
+
/** Fail the next matching request (any request when `match` is absent). */
|
|
232
|
+
failNext(
|
|
233
|
+
status: number,
|
|
234
|
+
headers: Record<string, string> = {},
|
|
235
|
+
body: unknown = { message: `fake failure ${status}` },
|
|
236
|
+
match?: (method: string, path: string) => boolean,
|
|
237
|
+
): void {
|
|
238
|
+
this.failures.push({ status, headers, body, ...(match ? { match } : {}) })
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// ── UI-style mutators (people acting on GitHub directly) ───────────────────
|
|
242
|
+
|
|
243
|
+
/** File an issue the way a person does in the GitHub UI. */
|
|
244
|
+
fileIssue(
|
|
245
|
+
opts: {
|
|
246
|
+
title?: string
|
|
247
|
+
body?: string
|
|
248
|
+
labels?: string[]
|
|
249
|
+
state?: 'open' | 'closed'
|
|
250
|
+
state_reason?: string | null
|
|
251
|
+
assignees?: string[]
|
|
252
|
+
} = {},
|
|
253
|
+
): number {
|
|
254
|
+
const issue = this.insert(opts.title ?? 'Filed in the UI', opts.body ?? 'From the UI.', [])
|
|
255
|
+
issue.assignees = [...(opts.assignees ?? [])]
|
|
256
|
+
for (const label of opts.labels ?? []) this.uiAddLabel(issue.number, label)
|
|
257
|
+
if (opts.state === 'closed')
|
|
258
|
+
this.uiClose(issue.number, opts.state_reason === undefined ? 'completed' : opts.state_reason)
|
|
259
|
+
return issue.number
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
openPullRequest(title = 'A pull request'): number {
|
|
263
|
+
const issue = this.insert(title, 'PR body', [])
|
|
264
|
+
issue.pull_request = true
|
|
265
|
+
return issue.number
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
uiClose(number: number, reason: string | null = 'completed'): void {
|
|
269
|
+
const issue = this.require(number)
|
|
270
|
+
issue.state = 'closed'
|
|
271
|
+
issue.state_reason = reason
|
|
272
|
+
issue.events.push({ type: 'closed' })
|
|
273
|
+
this.changed(issue, true)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
uiReopen(number: number): void {
|
|
277
|
+
const issue = this.require(number)
|
|
278
|
+
issue.state = 'open'
|
|
279
|
+
issue.state_reason = 'reopened'
|
|
280
|
+
issue.events.push({ type: 'reopened' })
|
|
281
|
+
this.changed(issue, true)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
uiAddLabel(number: number, name: string): void {
|
|
285
|
+
const issue = this.require(number)
|
|
286
|
+
const label = this.ensureLabel(name)
|
|
287
|
+
if (issue.labels.some((held) => held.toLowerCase() === label.name.toLowerCase())) return
|
|
288
|
+
issue.labels.push(label.name)
|
|
289
|
+
issue.events.push({ type: 'labeled', label: label.name })
|
|
290
|
+
this.changed(issue, this.labelsBumpUpdatedAt)
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
uiRemoveLabel(number: number, name: string): void {
|
|
294
|
+
const issue = this.require(number)
|
|
295
|
+
const before = issue.labels.length
|
|
296
|
+
issue.labels = issue.labels.filter((held) => held.toLowerCase() !== name.toLowerCase())
|
|
297
|
+
if (issue.labels.length === before) return
|
|
298
|
+
issue.events.push({ type: 'unlabeled', label: name })
|
|
299
|
+
this.changed(issue, this.labelsBumpUpdatedAt)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Replace the issue's assignees. */
|
|
303
|
+
uiAssign(number: number, ...logins: string[]): void {
|
|
304
|
+
const issue = this.require(number)
|
|
305
|
+
issue.assignees = [...logins]
|
|
306
|
+
this.changed(issue, true)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
uiEditTitle(number: number, title: string): void {
|
|
310
|
+
const issue = this.require(number)
|
|
311
|
+
issue.title = title
|
|
312
|
+
this.changed(issue, true)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
uiEditBody(number: number, body: string): void {
|
|
316
|
+
const issue = this.require(number)
|
|
317
|
+
issue.body = body
|
|
318
|
+
this.changed(issue, true)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
uiComment(number: number, body: string, count = 1, author: string | null = 'octocat'): void {
|
|
322
|
+
const issue = this.require(number)
|
|
323
|
+
for (let i = 0; i < count; i++) this.addComment(issue, body, author)
|
|
324
|
+
this.changed(issue, true)
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** Grow an issue's label history with `count` additions of other labels. */
|
|
328
|
+
addLabelEvents(number: number, count: number): void {
|
|
329
|
+
const issue = this.require(number)
|
|
330
|
+
for (let i = 0; i < count; i++) issue.events.push({ type: 'labeled', label: `noise-${i}` })
|
|
331
|
+
this.changed(issue, true)
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
uiAddDependency(number: number, blocker: number): void {
|
|
335
|
+
const issue = this.require(number)
|
|
336
|
+
const key = `${this.fullName}#${blocker}`
|
|
337
|
+
if (!issue.blockedBy.includes(key)) issue.blockedBy.push(key)
|
|
338
|
+
this.changed(issue, true)
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** A blocker in another repository; returns its key. */
|
|
342
|
+
addForeignDependency(
|
|
343
|
+
number: number,
|
|
344
|
+
repo: string,
|
|
345
|
+
blocker: number,
|
|
346
|
+
state: 'open' | 'closed' = 'closed',
|
|
347
|
+
): string {
|
|
348
|
+
const issue = this.require(number)
|
|
349
|
+
const key = `${repo}#${blocker}`
|
|
350
|
+
if (!this.foreign.has(key)) {
|
|
351
|
+
const at = new Date(this.clock()).toISOString()
|
|
352
|
+
this.foreign.set(key, {
|
|
353
|
+
repo,
|
|
354
|
+
id: this.nextId++,
|
|
355
|
+
number: blocker,
|
|
356
|
+
title: `Foreign ${key}`,
|
|
357
|
+
body: '',
|
|
358
|
+
state,
|
|
359
|
+
state_reason: state === 'closed' ? 'completed' : null,
|
|
360
|
+
labels: [],
|
|
361
|
+
assignees: [],
|
|
362
|
+
created_at: at,
|
|
363
|
+
updated_at: at,
|
|
364
|
+
pull_request: false,
|
|
365
|
+
comments: 0,
|
|
366
|
+
events: [],
|
|
367
|
+
blockedBy: [],
|
|
368
|
+
history: [],
|
|
369
|
+
})
|
|
370
|
+
}
|
|
371
|
+
issue.blockedBy.push(key)
|
|
372
|
+
this.changed(issue, true)
|
|
373
|
+
return key
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** Make `child` a sub-issue of `parent` in the UI, replacing any parent. */
|
|
377
|
+
uiAddSubIssue(parent: number, child: number): void {
|
|
378
|
+
this.require(parent)
|
|
379
|
+
const issue = this.require(child)
|
|
380
|
+
issue.parent = `${this.fullName}#${parent}`
|
|
381
|
+
this.changed(issue, true)
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
uiRemoveSubIssue(child: number): void {
|
|
385
|
+
const issue = this.require(child)
|
|
386
|
+
delete issue.parent
|
|
387
|
+
this.changed(issue, true)
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/** Give `child` a parent in another repository. */
|
|
391
|
+
setForeignParent(child: number, repo: string, parent: number): void {
|
|
392
|
+
const issue = this.require(child)
|
|
393
|
+
issue.parent = `${repo}#${parent}`
|
|
394
|
+
this.changed(issue, true)
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** A sub-issue of `parent` that lives in another repository; returns its key. */
|
|
398
|
+
addForeignSubIssue(parent: number, repo: string, number: number): string {
|
|
399
|
+
this.require(parent)
|
|
400
|
+
const key = `${repo}#${number}`
|
|
401
|
+
const at = new Date(this.clock()).toISOString()
|
|
402
|
+
this.foreign.set(key, {
|
|
403
|
+
repo,
|
|
404
|
+
id: this.nextId++,
|
|
405
|
+
number,
|
|
406
|
+
assignees: [],
|
|
407
|
+
title: `Foreign ${key}`,
|
|
408
|
+
body: '',
|
|
409
|
+
state: 'open',
|
|
410
|
+
state_reason: null,
|
|
411
|
+
labels: [],
|
|
412
|
+
created_at: at,
|
|
413
|
+
updated_at: at,
|
|
414
|
+
pull_request: false,
|
|
415
|
+
comments: 0,
|
|
416
|
+
events: [],
|
|
417
|
+
blockedBy: [],
|
|
418
|
+
history: [],
|
|
419
|
+
parent: `${this.fullName}#${parent}`,
|
|
420
|
+
})
|
|
421
|
+
this.childIndex = undefined
|
|
422
|
+
return key
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** The local sub-issues of `parent`, in insertion order. */
|
|
426
|
+
subIssues(parent: number): number[] {
|
|
427
|
+
return this.childrenOf(`${this.fullName}#${parent}`)
|
|
428
|
+
.filter((issue) => (issue as { repo?: string }).repo === undefined)
|
|
429
|
+
.map((issue) => issue.number)
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
deleteIssue(number: number): void {
|
|
433
|
+
this.require(number).gone = 'deleted'
|
|
434
|
+
this.childIndex = undefined
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Transfer issue `number` to `to` (an `owner/repo#number` key). GitHub
|
|
439
|
+
* answers a GET of the old number with a 301 the transport follows, so
|
|
440
|
+
* the fake serves the moved issue's body there; every other route 404s.
|
|
441
|
+
*/
|
|
442
|
+
transferIssue(number: number, to = `${this.owner}/elsewhere#${number + 500}`): string {
|
|
443
|
+
const issue = this.require(number)
|
|
444
|
+
const [repo, moved] = to.split('#')
|
|
445
|
+
issue.gone = 'transferred'
|
|
446
|
+
issue.movedTo = to
|
|
447
|
+
this.childIndex = undefined
|
|
448
|
+
const { gone: _gone, movedTo: _movedTo, ...copy } = issue
|
|
449
|
+
this.foreign.set(to, {
|
|
450
|
+
...copy,
|
|
451
|
+
labels: [...issue.labels],
|
|
452
|
+
blockedBy: [...issue.blockedBy],
|
|
453
|
+
repo: repo!,
|
|
454
|
+
number: Number(moved),
|
|
455
|
+
})
|
|
456
|
+
return to
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
deleteLabel(name: string): void {
|
|
460
|
+
const label = this.labels.get(name.toLowerCase())
|
|
461
|
+
if (!label) return
|
|
462
|
+
this.labels.delete(name.toLowerCase())
|
|
463
|
+
for (const issue of this.issues.values())
|
|
464
|
+
issue.labels = issue.labels.filter((held) => held.toLowerCase() !== name.toLowerCase())
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/** Bulk-create plain issues with no events (budget tests). */
|
|
468
|
+
seedIssues(
|
|
469
|
+
count: number,
|
|
470
|
+
opts: { labels?: string[]; state?: 'open' | 'closed'; state_reason?: string | null } = {},
|
|
471
|
+
): number[] {
|
|
472
|
+
const numbers: number[] = []
|
|
473
|
+
for (let i = 0; i < count; i++) {
|
|
474
|
+
const issue = this.insert(`Seeded ${i}`, 'Seeded body.', [])
|
|
475
|
+
for (const name of opts.labels ?? []) issue.labels.push(this.ensureLabel(name).name)
|
|
476
|
+
if (opts.state === 'closed') {
|
|
477
|
+
issue.state = 'closed'
|
|
478
|
+
issue.state_reason = opts.state_reason === undefined ? 'completed' : opts.state_reason
|
|
479
|
+
issue.history.push({
|
|
480
|
+
at: this.clock(),
|
|
481
|
+
state: 'closed',
|
|
482
|
+
state_reason: issue.state_reason,
|
|
483
|
+
})
|
|
484
|
+
}
|
|
485
|
+
numbers.push(issue.number)
|
|
486
|
+
}
|
|
487
|
+
return numbers
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// ── Transport ─────────────────────────────────────────────────────────────
|
|
491
|
+
|
|
492
|
+
readonly transport: GitHubRequest = (method, rawPath, opts) =>
|
|
493
|
+
this.serve(this.viewer, method, rawPath, opts)
|
|
494
|
+
|
|
495
|
+
/** A transport authenticated as `login` (null: a credential with no user),
|
|
496
|
+
* over the same repository state. */
|
|
497
|
+
transportAs(login: string | null): GitHubRequest {
|
|
498
|
+
return (method, rawPath, opts) => this.serve(login, method, rawPath, opts)
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
private async serve(
|
|
502
|
+
identity: string | null,
|
|
503
|
+
method: string,
|
|
504
|
+
rawPath: string,
|
|
505
|
+
opts: Parameters<GitHubRequest>[2] = {},
|
|
506
|
+
): Promise<GitHubResponse> {
|
|
507
|
+
const path = rawPath.replace(/^https:\/\/api\.github\.com\//, '').replace(/^\/+/, '')
|
|
508
|
+
const record: FakeGitHubRequestRecord = {
|
|
509
|
+
method,
|
|
510
|
+
path,
|
|
511
|
+
kind: 'unknown',
|
|
512
|
+
status: 0,
|
|
513
|
+
...(opts.query !== undefined ? { params: { ...opts.query } } : {}),
|
|
514
|
+
}
|
|
515
|
+
this.requests.push(record)
|
|
516
|
+
this.childIndex = undefined
|
|
517
|
+
const failure = this.failures.findIndex((f) => f.match?.(method, path) ?? true)
|
|
518
|
+
if (failure >= 0) {
|
|
519
|
+
const [hit] = this.failures.splice(failure, 1)
|
|
520
|
+
record.kind = this.classify(method, path, opts.body)
|
|
521
|
+
record.status = hit!.status
|
|
522
|
+
throw new GitHubApiError(
|
|
523
|
+
hit!.status,
|
|
524
|
+
(hit!.body as { message?: string })?.message ?? `fake failure ${hit!.status}`,
|
|
525
|
+
hit!.body,
|
|
526
|
+
hit!.headers,
|
|
527
|
+
)
|
|
528
|
+
}
|
|
529
|
+
try {
|
|
530
|
+
const result = this.route(identity, method, path, opts.query ?? {}, opts.body, record)
|
|
531
|
+
if (method === 'GET' && result.status === 204) {
|
|
532
|
+
record.status = 204
|
|
533
|
+
return { status: 204, headers: {} }
|
|
534
|
+
}
|
|
535
|
+
if (method === 'GET') {
|
|
536
|
+
const etag = etagOf(result.json, result.link)
|
|
537
|
+
const sent = opts.headers?.['If-None-Match'] ?? opts.headers?.['if-none-match']
|
|
538
|
+
if (sent === etag) {
|
|
539
|
+
record.status = 304
|
|
540
|
+
return { status: 304, headers: { etag } }
|
|
541
|
+
}
|
|
542
|
+
record.status = 200
|
|
543
|
+
return {
|
|
544
|
+
status: 200,
|
|
545
|
+
headers: { etag, ...(result.link ? { link: result.link } : {}) },
|
|
546
|
+
json: result.json,
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
record.status = result.status ?? 200
|
|
550
|
+
return { status: record.status, headers: {}, json: result.json }
|
|
551
|
+
} catch (error) {
|
|
552
|
+
if (error instanceof GitHubApiError) record.status = error.status
|
|
553
|
+
throw error
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
private classify(method: string, path: string, body: unknown): FakeGitHubRequestKind {
|
|
558
|
+
if (path === 'graphql') {
|
|
559
|
+
const query = (body as { query?: string })?.query ?? ''
|
|
560
|
+
if (query.includes('REOPENED_EVENT')) return 'timeline-reopen'
|
|
561
|
+
if (query.includes('CLOSED_EVENT')) return 'timeline-closed'
|
|
562
|
+
return 'count'
|
|
563
|
+
}
|
|
564
|
+
const prefix = `repos/${this.fullName}`
|
|
565
|
+
const rest = path.startsWith(prefix) ? path.slice(prefix.length) : path
|
|
566
|
+
if (path === 'user') return 'user'
|
|
567
|
+
if (rest === '') return 'repo'
|
|
568
|
+
if (/^\/assignees\/[^/]+$/.test(rest)) return 'assignee-check'
|
|
569
|
+
if (rest === '/issues') return method === 'POST' ? 'create-issue' : 'list-issues'
|
|
570
|
+
if (/^\/issues\/\d+$/.test(rest)) return method === 'PATCH' ? 'patch-issue' : 'get-issue'
|
|
571
|
+
if (/^\/issues\/\d+\/comments$/.test(rest))
|
|
572
|
+
return method === 'POST' ? 'comment' : 'comments-list'
|
|
573
|
+
if (/^\/issues\/\d+\/dependencies\/blocked_by$/.test(rest))
|
|
574
|
+
return method === 'POST' ? 'blocked-by-add' : 'blocked-by-list'
|
|
575
|
+
if (/^\/issues\/\d+\/dependencies\/blocked_by\/\d+$/.test(rest)) return 'blocked-by-remove'
|
|
576
|
+
if (/^\/issues\/\d+\/sub_issues$/.test(rest))
|
|
577
|
+
return method === 'POST' ? 'sub-issue-add' : 'sub-issues-list'
|
|
578
|
+
if (/^\/issues\/\d+\/sub_issue$/.test(rest)) return 'sub-issue-remove'
|
|
579
|
+
if (rest === '/labels') return method === 'POST' ? 'label-create' : 'labels-list'
|
|
580
|
+
if (/^\/labels\/[^/]+$/.test(rest)) return 'label-get'
|
|
581
|
+
return 'unknown'
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
private route(
|
|
585
|
+
identity: string | null,
|
|
586
|
+
method: string,
|
|
587
|
+
path: string,
|
|
588
|
+
query: Record<string, string>,
|
|
589
|
+
body: unknown,
|
|
590
|
+
record: FakeGitHubRequestRecord,
|
|
591
|
+
): { json: unknown; link?: string; status?: number } {
|
|
592
|
+
record.kind = this.classify(method, path, body)
|
|
593
|
+
if (path === 'graphql' && method === 'POST') return this.graphql(body, record)
|
|
594
|
+
if (path === 'user' && method === 'GET') {
|
|
595
|
+
if (identity === null) throw new GitHubApiError(403, 'Resource not accessible by integration')
|
|
596
|
+
return { json: { login: identity } }
|
|
597
|
+
}
|
|
598
|
+
const prefix = `repos/${this.fullName}`
|
|
599
|
+
if (!path.startsWith(prefix)) throw this.notFound()
|
|
600
|
+
const rest = path.slice(prefix.length)
|
|
601
|
+
const assigneePath = /^\/assignees\/([^/]+)$/.exec(rest)
|
|
602
|
+
if (assigneePath && method === 'GET') {
|
|
603
|
+
if (!this.assignable.has(decodeURIComponent(assigneePath[1]!).toLowerCase()))
|
|
604
|
+
throw this.notFound()
|
|
605
|
+
return { json: undefined, status: 204 }
|
|
606
|
+
}
|
|
607
|
+
if (!this.issueWrites && method !== 'GET')
|
|
608
|
+
throw new GitHubApiError(403, 'Resource not accessible by personal access token')
|
|
609
|
+
if (rest === '' && method === 'GET')
|
|
610
|
+
return {
|
|
611
|
+
json: {
|
|
612
|
+
full_name: this.fullName,
|
|
613
|
+
has_issues: this.hasIssues,
|
|
614
|
+
permissions: this.permissions,
|
|
615
|
+
},
|
|
616
|
+
}
|
|
617
|
+
if (!this.hasIssues && rest.startsWith('/issues'))
|
|
618
|
+
throw new GitHubApiError(410, 'Issues are disabled for this repo', {
|
|
619
|
+
message: 'Issues are disabled for this repo',
|
|
620
|
+
})
|
|
621
|
+
if (rest === '/issues' && method === 'GET') return this.listIssues(query)
|
|
622
|
+
if (rest === '/issues' && method === 'POST') return this.createIssue(body)
|
|
623
|
+
if (rest === '/labels' && method === 'GET')
|
|
624
|
+
return this.page([...this.labels.values()], query, rest)
|
|
625
|
+
if (rest === '/labels' && method === 'POST') return this.createLabel(body)
|
|
626
|
+
const labelPath = /^\/labels\/([^/]+)$/.exec(rest)
|
|
627
|
+
if (labelPath && method === 'GET') {
|
|
628
|
+
// GitHub matches the name case-insensitively.
|
|
629
|
+
const label = this.labels.get(decodeURIComponent(labelPath[1]!).toLowerCase())
|
|
630
|
+
if (label === undefined) throw this.notFound()
|
|
631
|
+
return { json: label }
|
|
632
|
+
}
|
|
633
|
+
let match = /^\/issues\/(\d+)$/.exec(rest)
|
|
634
|
+
if (match) {
|
|
635
|
+
const moved = this.issues.get(Number(match[1]))?.movedTo
|
|
636
|
+
if (moved !== undefined && method === 'GET')
|
|
637
|
+
return { json: this.json(this.foreign.get(moved)!) }
|
|
638
|
+
const issue = this.live(Number(match[1]))
|
|
639
|
+
if (method === 'GET') return { json: this.json(issue) }
|
|
640
|
+
if (method === 'PATCH') return { json: this.patchIssue(issue, body) }
|
|
641
|
+
}
|
|
642
|
+
match = /^\/issues\/(\d+)\/comments$/.exec(rest)
|
|
643
|
+
if (match && method === 'POST') {
|
|
644
|
+
const issue = this.live(Number(match[1]))
|
|
645
|
+
const text = (body as { body?: string })?.body ?? ''
|
|
646
|
+
const comment = this.addComment(issue, text, 'autobuild[bot]')
|
|
647
|
+
this.changed(issue, true)
|
|
648
|
+
return { json: comment, status: 201 }
|
|
649
|
+
}
|
|
650
|
+
if (match && method === 'GET') {
|
|
651
|
+
const issue = this.live(Number(match[1]))
|
|
652
|
+
return this.page(this.commentRecords.get(issue.number) ?? [], query, rest)
|
|
653
|
+
}
|
|
654
|
+
match = /^\/issues\/(\d+)\/dependencies\/blocked_by$/.exec(rest)
|
|
655
|
+
if (match) {
|
|
656
|
+
const issue = this.live(Number(match[1]))
|
|
657
|
+
if (method === 'GET')
|
|
658
|
+
return this.page(
|
|
659
|
+
issue.blockedBy.map((key) => this.json(this.byKey(key)!)),
|
|
660
|
+
query,
|
|
661
|
+
rest,
|
|
662
|
+
true,
|
|
663
|
+
)
|
|
664
|
+
if (method === 'POST') {
|
|
665
|
+
const id = (body as { issue_id?: number })?.issue_id
|
|
666
|
+
const blocker = this.byId(id)
|
|
667
|
+
if (!blocker) throw this.notFound()
|
|
668
|
+
const key = this.keyOf(blocker)
|
|
669
|
+
if (issue.blockedBy.includes(key))
|
|
670
|
+
throw new GitHubApiError(422, 'Validation Failed', {
|
|
671
|
+
message: 'Validation Failed',
|
|
672
|
+
errors: [{ message: 'Issue dependency already exists' }],
|
|
673
|
+
})
|
|
674
|
+
issue.blockedBy.push(key)
|
|
675
|
+
this.changed(issue, true)
|
|
676
|
+
return { json: this.json(blocker), status: 201 }
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
match = /^\/issues\/(\d+)\/dependencies\/blocked_by\/(\d+)$/.exec(rest)
|
|
680
|
+
if (match && method === 'DELETE') {
|
|
681
|
+
const issue = this.live(Number(match[1]))
|
|
682
|
+
const blocker = this.byId(Number(match[2]))
|
|
683
|
+
if (!blocker || !issue.blockedBy.includes(this.keyOf(blocker))) throw this.notFound()
|
|
684
|
+
issue.blockedBy = issue.blockedBy.filter((key) => key !== this.keyOf(blocker))
|
|
685
|
+
this.changed(issue, true)
|
|
686
|
+
return { json: this.json(blocker) }
|
|
687
|
+
}
|
|
688
|
+
match = /^\/issues\/(\d+)\/sub_issues$/.exec(rest)
|
|
689
|
+
if (match) {
|
|
690
|
+
const parent = this.live(Number(match[1]))
|
|
691
|
+
const key = this.keyOf(parent)
|
|
692
|
+
if (method === 'GET') {
|
|
693
|
+
const cap = this.subIssuePageCap
|
|
694
|
+
const capped =
|
|
695
|
+
cap === undefined
|
|
696
|
+
? query
|
|
697
|
+
: { ...query, per_page: String(Math.min(Number(query.per_page ?? 30), cap)) }
|
|
698
|
+
return this.page(
|
|
699
|
+
this.childrenOf(key).map((child) => this.json(child)),
|
|
700
|
+
capped,
|
|
701
|
+
rest,
|
|
702
|
+
)
|
|
703
|
+
}
|
|
704
|
+
if (method === 'POST') {
|
|
705
|
+
const input = body as { sub_issue_id?: number; replace_parent?: boolean }
|
|
706
|
+
const child = this.byId(input?.sub_issue_id)
|
|
707
|
+
if (!child) throw this.notFound()
|
|
708
|
+
const invalid = (message: string) =>
|
|
709
|
+
new GitHubApiError(422, 'Validation Failed', {
|
|
710
|
+
message: 'Validation Failed',
|
|
711
|
+
errors: [{ message }],
|
|
712
|
+
})
|
|
713
|
+
if (child.parent === key) throw invalid('Issue is already a sub-issue of this issue')
|
|
714
|
+
if (child.parent !== undefined && input.replace_parent !== true)
|
|
715
|
+
throw invalid('Issue may not have more than one parent')
|
|
716
|
+
if (this.childrenOf(key).length >= SUB_ISSUE_LIMIT)
|
|
717
|
+
throw invalid(`Parent may not have more than ${SUB_ISSUE_LIMIT} sub-issues`)
|
|
718
|
+
const childKey = this.keyOf(child)
|
|
719
|
+
for (let up: FakeIssue | undefined = parent; up !== undefined; ) {
|
|
720
|
+
if (this.keyOf(up) === childKey) throw invalid('Sub-issue would create a cycle')
|
|
721
|
+
up = up.parent === undefined ? undefined : this.byKey(up.parent)
|
|
722
|
+
}
|
|
723
|
+
child.parent = key
|
|
724
|
+
this.changed(child, true)
|
|
725
|
+
this.changed(parent, true)
|
|
726
|
+
return { json: this.json(child), status: 201 }
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
match = /^\/issues\/(\d+)\/sub_issue$/.exec(rest)
|
|
730
|
+
if (match && method === 'DELETE') {
|
|
731
|
+
const parent = this.live(Number(match[1]))
|
|
732
|
+
const child = this.byId((body as { sub_issue_id?: number })?.sub_issue_id)
|
|
733
|
+
if (!child || child.parent !== this.keyOf(parent)) throw this.notFound()
|
|
734
|
+
delete child.parent
|
|
735
|
+
this.changed(child, true)
|
|
736
|
+
this.changed(parent, true)
|
|
737
|
+
return { json: this.json(child) }
|
|
738
|
+
}
|
|
739
|
+
throw this.notFound()
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/** Live issues (local first, then foreign) whose parent is `key`. */
|
|
743
|
+
private childrenOf(key: string): FakeIssue[] {
|
|
744
|
+
if (this.childIndex === undefined) {
|
|
745
|
+
this.childIndex = new Map()
|
|
746
|
+
for (const issue of [...this.issues.values(), ...this.foreign.values()]) {
|
|
747
|
+
if (issue.parent === undefined || issue.gone || issue.pull_request) continue
|
|
748
|
+
this.childIndex.set(issue.parent, [...(this.childIndex.get(issue.parent) ?? []), issue])
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
return this.childIndex.get(key) ?? []
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
// ── REST helpers ──────────────────────────────────────────────────────────
|
|
755
|
+
|
|
756
|
+
private listIssues(query: Record<string, string>): { json: unknown; link?: string } {
|
|
757
|
+
const state = query.state ?? 'open'
|
|
758
|
+
const wanted = (query.labels ?? '')
|
|
759
|
+
.split(',')
|
|
760
|
+
.map((label) => label.trim().toLowerCase())
|
|
761
|
+
.filter((label) => label !== '')
|
|
762
|
+
const assignee = query.assignee?.toLowerCase()
|
|
763
|
+
const assigned = (issue: FakeIssue) =>
|
|
764
|
+
assignee === undefined ||
|
|
765
|
+
(assignee === 'none'
|
|
766
|
+
? issue.assignees.length === 0
|
|
767
|
+
: assignee === '*'
|
|
768
|
+
? issue.assignees.length > 0
|
|
769
|
+
: issue.assignees.some((login) => login.toLowerCase() === assignee))
|
|
770
|
+
let issues = [...this.issues.values()].filter(
|
|
771
|
+
(issue) =>
|
|
772
|
+
!issue.gone &&
|
|
773
|
+
(state === 'all' || issue.state === state) &&
|
|
774
|
+
wanted.every((label) => issue.labels.some((held) => held.toLowerCase() === label)) &&
|
|
775
|
+
assigned(issue),
|
|
776
|
+
)
|
|
777
|
+
const sort = query.sort ?? 'created'
|
|
778
|
+
const key = (issue: FakeIssue) =>
|
|
779
|
+
sort === 'updated' ? `${issue.updated_at}#${issue.number}` : issue.number
|
|
780
|
+
issues.sort((a, b) => (key(a) < key(b) ? -1 : key(a) > key(b) ? 1 : 0))
|
|
781
|
+
if ((query.direction ?? 'desc') === 'desc') issues = issues.reverse()
|
|
782
|
+
return this.page(
|
|
783
|
+
issues.map((issue) => this.json(issue)),
|
|
784
|
+
query,
|
|
785
|
+
'/issues',
|
|
786
|
+
)
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
private page<T>(
|
|
790
|
+
items: T[],
|
|
791
|
+
query: Record<string, string>,
|
|
792
|
+
rest: string,
|
|
793
|
+
_dependencies = false,
|
|
794
|
+
): { json: T[]; link?: string } {
|
|
795
|
+
const perPage = Math.min(Number(query.per_page ?? 30), 100)
|
|
796
|
+
const page = Number(query.page ?? 1)
|
|
797
|
+
const slice = items.slice((page - 1) * perPage, page * perPage)
|
|
798
|
+
if (page * perPage >= items.length) return { json: slice }
|
|
799
|
+
const next = new URLSearchParams({ ...query, page: String(page + 1) })
|
|
800
|
+
return {
|
|
801
|
+
json: slice,
|
|
802
|
+
link: `<${API}/repos/${this.fullName}${rest}?${next.toString()}>; rel="next"`,
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
private createIssue(body: unknown): { json: unknown; status: number } {
|
|
807
|
+
const input = body as { title?: string; body?: string; labels?: string[] }
|
|
808
|
+
if (!input?.title) throw new GitHubApiError(422, 'Validation Failed', { message: 'title' })
|
|
809
|
+
const issue = this.insert(input.title, input.body ?? null, [])
|
|
810
|
+
if (!this.dropLabels)
|
|
811
|
+
for (const name of input.labels ?? []) {
|
|
812
|
+
const label = this.ensureLabel(name)
|
|
813
|
+
if (!issue.labels.includes(label.name)) {
|
|
814
|
+
issue.labels.push(label.name)
|
|
815
|
+
issue.events.push({ type: 'labeled', label: label.name })
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
return { json: this.json(issue), status: 201 }
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
private patchIssue(issue: FakeIssue, body: unknown): unknown {
|
|
822
|
+
const input = body as {
|
|
823
|
+
title?: string
|
|
824
|
+
body?: string
|
|
825
|
+
labels?: string[]
|
|
826
|
+
state?: 'open' | 'closed'
|
|
827
|
+
state_reason?: string | null
|
|
828
|
+
}
|
|
829
|
+
if (input.title !== undefined) issue.title = input.title
|
|
830
|
+
if (input.body !== undefined) issue.body = input.body
|
|
831
|
+
if (input.labels !== undefined && !this.dropLabels) {
|
|
832
|
+
const next = input.labels.map((name) => this.ensureLabel(name).name)
|
|
833
|
+
for (const name of issue.labels)
|
|
834
|
+
if (!next.includes(name)) issue.events.push({ type: 'unlabeled', label: name })
|
|
835
|
+
for (const name of next)
|
|
836
|
+
if (!issue.labels.includes(name)) issue.events.push({ type: 'labeled', label: name })
|
|
837
|
+
issue.labels = [...new Set(next)]
|
|
838
|
+
}
|
|
839
|
+
let stateChanged = false
|
|
840
|
+
if (input.state !== undefined && input.state !== issue.state) {
|
|
841
|
+
stateChanged = true
|
|
842
|
+
issue.state = input.state
|
|
843
|
+
if (input.state === 'closed') {
|
|
844
|
+
issue.state_reason = input.state_reason ?? 'completed'
|
|
845
|
+
issue.events.push({ type: 'closed' })
|
|
846
|
+
} else {
|
|
847
|
+
issue.state_reason = 'reopened'
|
|
848
|
+
issue.events.push({ type: 'reopened' })
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
// GitHub documents state_reason as ignored unless the state changes.
|
|
852
|
+
this.changed(issue, true, stateChanged)
|
|
853
|
+
return this.json(issue)
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
private createLabel(body: unknown): { json: unknown; status: number } {
|
|
857
|
+
const input = body as { name?: string; description?: string; color?: string }
|
|
858
|
+
if (!this.permissions.push)
|
|
859
|
+
throw new GitHubApiError(403, 'Resource not accessible by integration')
|
|
860
|
+
if (!input?.name) throw new GitHubApiError(422, 'Validation Failed', { message: 'name' })
|
|
861
|
+
if (input.name.length > 50)
|
|
862
|
+
throw new GitHubApiError(422, 'Validation Failed', { message: 'name is too long' })
|
|
863
|
+
if (this.labels.has(input.name.toLowerCase()))
|
|
864
|
+
throw new GitHubApiError(422, 'Validation Failed', {
|
|
865
|
+
message: 'Validation Failed',
|
|
866
|
+
errors: [{ resource: 'Label', code: 'already_exists', field: 'name' }],
|
|
867
|
+
})
|
|
868
|
+
const label = {
|
|
869
|
+
name: input.name,
|
|
870
|
+
description: input.description ?? null,
|
|
871
|
+
color: input.color ?? 'ededed',
|
|
872
|
+
}
|
|
873
|
+
this.labels.set(input.name.toLowerCase(), label)
|
|
874
|
+
return { json: label, status: 201 }
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
// ── GraphQL ───────────────────────────────────────────────────────────────
|
|
878
|
+
|
|
879
|
+
private graphql(body: unknown, record: FakeGitHubRequestRecord): { json: unknown } {
|
|
880
|
+
const { query = '', variables = {} } = (body ?? {}) as {
|
|
881
|
+
query?: string
|
|
882
|
+
variables?: Record<string, unknown>
|
|
883
|
+
}
|
|
884
|
+
record.query = query
|
|
885
|
+
if (variables.owner !== this.owner || variables.name !== this.name)
|
|
886
|
+
return { json: { data: { repository: null } } }
|
|
887
|
+
if (record.kind === 'timeline-reopen' || record.kind === 'timeline-closed') {
|
|
888
|
+
const issue = this.issues.get(Number(variables.number))
|
|
889
|
+
if (!issue || issue.gone) return { json: { data: { repository: { issue: null } } } }
|
|
890
|
+
if (record.kind === 'timeline-closed')
|
|
891
|
+
return {
|
|
892
|
+
json: {
|
|
893
|
+
data: {
|
|
894
|
+
repository: {
|
|
895
|
+
issue: {
|
|
896
|
+
timelineItems: {
|
|
897
|
+
totalCount: issue.events.filter((e) => e.type === 'closed').length,
|
|
898
|
+
},
|
|
899
|
+
},
|
|
900
|
+
},
|
|
901
|
+
},
|
|
902
|
+
},
|
|
903
|
+
}
|
|
904
|
+
const last = Number(/last:\s*(\d+)/.exec(query)?.[1] ?? 100)
|
|
905
|
+
const nodes = issue.events
|
|
906
|
+
.filter((e) => e.type === 'labeled' || e.type === 'reopened')
|
|
907
|
+
.slice(-last)
|
|
908
|
+
.map((e) =>
|
|
909
|
+
e.type === 'reopened'
|
|
910
|
+
? { __typename: 'ReopenedEvent' }
|
|
911
|
+
: { __typename: 'LabeledEvent', label: { name: e.label } },
|
|
912
|
+
)
|
|
913
|
+
return { json: { data: { repository: { issue: { timelineItems: { nodes } } } } } }
|
|
914
|
+
}
|
|
915
|
+
// State counts: database-backed connection totals plus one search.
|
|
916
|
+
const fields: string[] = []
|
|
917
|
+
const repository: Record<string, unknown> = {}
|
|
918
|
+
const data: Record<string, unknown> = { repository }
|
|
919
|
+
const live = [...this.issues.values()].filter((issue) => !issue.pull_request && !issue.gone)
|
|
920
|
+
if (/open:\s*issues\(states:\s*OPEN\)/.test(query)) {
|
|
921
|
+
fields.push('open')
|
|
922
|
+
repository.open = { totalCount: live.filter((issue) => issue.state === 'open').length }
|
|
923
|
+
}
|
|
924
|
+
if (/closed:\s*issues\(states:\s*CLOSED\)/.test(query)) {
|
|
925
|
+
fields.push('closed')
|
|
926
|
+
repository.closed = { totalCount: live.filter((issue) => issue.state === 'closed').length }
|
|
927
|
+
}
|
|
928
|
+
if (/labels\(query:/.test(query)) {
|
|
929
|
+
fields.push('labels')
|
|
930
|
+
const search = String(/labels\(query:\s*"([^"]*)"/.exec(query)?.[1] ?? '').toLowerCase()
|
|
931
|
+
const matching = [...this.labels.values()].filter((label) =>
|
|
932
|
+
label.name.toLowerCase().includes(search),
|
|
933
|
+
)
|
|
934
|
+
const after =
|
|
935
|
+
variables.labelsAfter === null || variables.labelsAfter === undefined
|
|
936
|
+
? 0
|
|
937
|
+
: Number(variables.labelsAfter)
|
|
938
|
+
const slice = matching.slice(after, after + 100)
|
|
939
|
+
repository.labels = {
|
|
940
|
+
nodes: slice.map((label) => ({
|
|
941
|
+
name: label.name,
|
|
942
|
+
description: label.description,
|
|
943
|
+
issues: {
|
|
944
|
+
totalCount: live.filter(
|
|
945
|
+
(issue) =>
|
|
946
|
+
issue.state === 'open' &&
|
|
947
|
+
issue.labels.some((held) => held.toLowerCase() === label.name.toLowerCase()),
|
|
948
|
+
).length,
|
|
949
|
+
},
|
|
950
|
+
})),
|
|
951
|
+
pageInfo: {
|
|
952
|
+
hasNextPage: after + 100 < matching.length,
|
|
953
|
+
endCursor: String(after + slice.length),
|
|
954
|
+
},
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
if (/completed:\s*search\(/.test(query)) {
|
|
958
|
+
fields.push('search')
|
|
959
|
+
const searchQuery = String(variables.search ?? '')
|
|
960
|
+
if (
|
|
961
|
+
!/\breason:completed\b/.test(searchQuery) ||
|
|
962
|
+
/reason:(duplicate|"not planned")/.test(searchQuery)
|
|
963
|
+
)
|
|
964
|
+
throw new GitHubApiError(422, `fake: unsupported search ${searchQuery}`)
|
|
965
|
+
const asOf = this.clock() - this.searchLagMs
|
|
966
|
+
data.completed = {
|
|
967
|
+
issueCount: live.filter((issue) => {
|
|
968
|
+
const indexed = [...issue.history].reverse().find((entry) => entry.at <= asOf)
|
|
969
|
+
// The fake follows the conservative assumption that a null
|
|
970
|
+
// (legacy) close reason does not match `reason:completed`.
|
|
971
|
+
return indexed?.state === 'closed' && indexed.state_reason === 'completed'
|
|
972
|
+
}).length,
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
record.fields = fields
|
|
976
|
+
return { json: { data } }
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
// ── Internals ─────────────────────────────────────────────────────────────
|
|
980
|
+
|
|
981
|
+
private insert(title: string, body: string | null, labels: string[]): FakeIssue {
|
|
982
|
+
const at = new Date(this.clock()).toISOString()
|
|
983
|
+
const issue: FakeIssue = {
|
|
984
|
+
id: this.nextId++,
|
|
985
|
+
number: this.nextNumber++,
|
|
986
|
+
title,
|
|
987
|
+
body,
|
|
988
|
+
state: 'open',
|
|
989
|
+
state_reason: null,
|
|
990
|
+
labels,
|
|
991
|
+
assignees: [],
|
|
992
|
+
created_at: at,
|
|
993
|
+
updated_at: at,
|
|
994
|
+
pull_request: false,
|
|
995
|
+
comments: 0,
|
|
996
|
+
events: [],
|
|
997
|
+
blockedBy: [],
|
|
998
|
+
history: [{ at: this.clock(), state: 'open', state_reason: null }],
|
|
999
|
+
}
|
|
1000
|
+
this.issues.set(issue.number, issue)
|
|
1001
|
+
return issue
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
private addComment(issue: FakeIssue, body: string, author: string | null): FakeComment {
|
|
1005
|
+
const comment: FakeComment = {
|
|
1006
|
+
id: this.nextId++,
|
|
1007
|
+
body,
|
|
1008
|
+
created_at: new Date(this.clock()).toISOString(),
|
|
1009
|
+
user: author === null ? null : { login: author },
|
|
1010
|
+
}
|
|
1011
|
+
issue.comments++
|
|
1012
|
+
this.comments.set(issue.number, [...(this.comments.get(issue.number) ?? []), body])
|
|
1013
|
+
this.commentRecords.set(issue.number, [
|
|
1014
|
+
...(this.commentRecords.get(issue.number) ?? []),
|
|
1015
|
+
comment,
|
|
1016
|
+
])
|
|
1017
|
+
return comment
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
private ensureLabel(name: string): FakeLabel {
|
|
1021
|
+
const known = this.labels.get(name.toLowerCase())
|
|
1022
|
+
if (known) return known
|
|
1023
|
+
const label = { name, description: null, color: 'ededed' }
|
|
1024
|
+
this.labels.set(name.toLowerCase(), label)
|
|
1025
|
+
return label
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
private changed(issue: FakeIssue, bump: boolean, stateChanged = true): void {
|
|
1029
|
+
this.childIndex = undefined
|
|
1030
|
+
if (bump) issue.updated_at = new Date(this.clock()).toISOString()
|
|
1031
|
+
if (stateChanged) {
|
|
1032
|
+
const last = issue.history.at(-1)
|
|
1033
|
+
if (last?.state !== issue.state || last.state_reason !== issue.state_reason)
|
|
1034
|
+
issue.history.push({
|
|
1035
|
+
at: this.clock(),
|
|
1036
|
+
state: issue.state,
|
|
1037
|
+
state_reason: issue.state_reason,
|
|
1038
|
+
})
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
private require(number: number): FakeIssue {
|
|
1043
|
+
const issue = this.issues.get(number)
|
|
1044
|
+
if (!issue) throw new Error(`fake github: no issue #${number}`)
|
|
1045
|
+
return issue
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
private live(number: number): FakeIssue {
|
|
1049
|
+
const issue = this.issues.get(number)
|
|
1050
|
+
if (!issue || issue.gone === 'transferred') throw this.notFound()
|
|
1051
|
+
if (issue.gone === 'deleted') throw new GitHubApiError(410, 'This issue was deleted')
|
|
1052
|
+
return issue
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
private keyOf(issue: FakeIssue): string {
|
|
1056
|
+
const repo = (issue as { repo?: string }).repo ?? this.fullName
|
|
1057
|
+
return `${repo}#${issue.number}`
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
private byKey(key: string): FakeIssue | undefined {
|
|
1061
|
+
if (this.foreign.has(key)) return this.foreign.get(key)
|
|
1062
|
+
const [repo, number] = key.split('#')
|
|
1063
|
+
return repo === this.fullName ? this.issues.get(Number(number)) : undefined
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
private byId(id: number | undefined): FakeIssue | undefined {
|
|
1067
|
+
for (const issue of this.issues.values()) if (issue.id === id && !issue.gone) return issue
|
|
1068
|
+
for (const issue of this.foreign.values()) if (issue.id === id) return issue
|
|
1069
|
+
return undefined
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
private json(issue: FakeIssue): Record<string, unknown> {
|
|
1073
|
+
const repo = (issue as { repo?: string }).repo ?? this.fullName
|
|
1074
|
+
return {
|
|
1075
|
+
id: issue.id,
|
|
1076
|
+
number: issue.number,
|
|
1077
|
+
title: issue.title,
|
|
1078
|
+
body: issue.body,
|
|
1079
|
+
html_url: `https://github.com/${repo}/${issue.pull_request ? 'pull' : 'issues'}/${issue.number}`,
|
|
1080
|
+
repository_url: `${API}/repos/${repo}`,
|
|
1081
|
+
state: issue.state,
|
|
1082
|
+
state_reason: issue.state_reason,
|
|
1083
|
+
labels: issue.labels.map((name) => ({ name })),
|
|
1084
|
+
assignees: issue.assignees.map((login) => ({ login })),
|
|
1085
|
+
comments: issue.comments,
|
|
1086
|
+
created_at: issue.created_at,
|
|
1087
|
+
updated_at: issue.updated_at,
|
|
1088
|
+
...(issue.pull_request
|
|
1089
|
+
? { pull_request: { url: `${API}/repos/${repo}/pulls/${issue.number}` } }
|
|
1090
|
+
: {}),
|
|
1091
|
+
parent_issue_url:
|
|
1092
|
+
issue.parent === undefined
|
|
1093
|
+
? null
|
|
1094
|
+
: `${API}/repos/${issue.parent.split('#')[0]}/issues/${issue.parent.split('#')[1]}`,
|
|
1095
|
+
sub_issues_summary: this.subIssueSummary(this.keyOf(issue)),
|
|
1096
|
+
issue_dependencies_summary: {
|
|
1097
|
+
blocked_by: issue.blockedBy.length,
|
|
1098
|
+
blocking: 0,
|
|
1099
|
+
total_blocked_by: issue.blockedBy.length,
|
|
1100
|
+
total_blocking: 0,
|
|
1101
|
+
},
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
private subIssueSummary(key: string): {
|
|
1106
|
+
total: number
|
|
1107
|
+
completed: number
|
|
1108
|
+
percent_completed: number
|
|
1109
|
+
} {
|
|
1110
|
+
const children = this.childrenOf(key)
|
|
1111
|
+
const completed = children.filter((child) => child.state === 'closed').length
|
|
1112
|
+
return {
|
|
1113
|
+
total: children.length,
|
|
1114
|
+
completed,
|
|
1115
|
+
percent_completed:
|
|
1116
|
+
children.length === 0 ? 0 : Math.floor((completed * 100) / children.length),
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
private notFound(): GitHubApiError {
|
|
1121
|
+
return new GitHubApiError(404, 'Not Found', { message: 'Not Found' })
|
|
1122
|
+
}
|
|
1123
|
+
}
|