1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//! GitHub issue-filing client with fingerprint-based deduplication.
//!
//! Why: Phase 3 of the bug-reporting system files or increments GitHub issues
//! in `bobmatnyc/trusty-tools` using a shared bot token (or later a GitHub
//! App installation token). The [`GithubApi`] trait decouples the real
//! reqwest implementation from a mock used in tests, so all filing logic
//! can be exercised without network access.
//!
//! ## Authentication
//!
//! Tokens are resolved at call time from, in order:
//! 1. An explicit `token` argument supplied by the caller.
//! 2. The `TRUSTY_BUGREPORT_GITHUB_TOKEN` environment variable.
//! 3. A file at `~/.config/trusty-mpm/bugreport-token` (or the path in
//! `TRUSTY_BUGREPORT_TOKEN_FILE`).
//!
//! If no token is found, `file_issue` returns
//! `Err(GithubFilingError::NoToken)` — nothing is filed, and the caller
//! surfaces an actionable error message to the user.
//!
//! ## GitHub App (Phase 4)
//!
//! The [`TokenProvider`] trait (and `EnvFileTokenProvider`) are defined in
//! [`super::token`]. Phase 4 adds `GithubAppTokenProvider` there. The filing
//! logic here accepts any `dyn TokenProvider` without change.
//!
//! ## Deduplication
//!
//! Before creating a new issue the client searches GitHub for an open issue
//! whose body contains the hidden marker
//! `<!-- trusty-bug-fingerprint: <fp> -->`. If found, it posts a
//! "+1 occurrence" comment on the existing issue. If not found, it creates
//! a new issue with the marker embedded in the body.
//!
//! ## Rate-limit guard
//!
//! [`github_client::RealGithubClient`] refuses to file more than
//! [`MAX_ISSUES_PER_CALL`] issues in a single `file_issue` invocation
//! (currently 1) to prevent accidental spam if the caller loops.
//!
//! ## Module layout
//!
//! - This file (`github.rs`): [`GithubApi`] trait, error type, shared structs,
//! and the orchestration / dedup logic ([`file_issue`], [`file_issue_with`],
//! [`extract_fingerprint`]).
//! - [`super::github_client`]: `RealGithubClient` — the `reqwest::blocking`
//! transport that implements [`GithubApi`].
//! - [`super::github_tests`]: unit tests (mock-based, no network).
//!
//! Test: `tests::token_resolution_*`, `tests::label_mapping_*`,
//! `tests::dedup_marker_*`, `tests::mock_create_path`,
//! `tests::mock_comment_path`.
use IssuePreview;
use TokenProvider;
use FilingResult;
use crateRealGithubClient;
/// Maximum issues created in a single `file_issue` call (anti-spam).
///
/// Why: a defensive upper bound prevents accidental bulk filing if a caller
/// loops. Phase 4 will add a richer rate-limit with a local stamp file.
/// What: hardcoded to 1 — one `report_bug` call files at most one issue.
const MAX_ISSUES_PER_CALL: usize = 1;
// ── Error type ────────────────────────────────────────────────────────────────
/// Errors returned by the GitHub filing client.
///
/// Why: typed errors let the MCP tool and HTTP handler produce targeted,
/// actionable messages rather than opaque strings.
/// What: each variant represents a distinct failure mode with enough context
/// to form a user-facing message.
/// Test: `tests::no_token_yields_no_token_error`.
// ── GithubApi trait ───────────────────────────────────────────────────────────
/// Minimal GitHub REST API surface needed by the filing client.
///
/// Why: a trait boundary allows unit tests to inject a mock that records calls
/// and returns canned responses without any network access, satisfying the
/// hard requirement "NO real GitHub calls in tests".
/// What: two methods — `search_open_issues` (find by fingerprint marker) and
/// `create_issue` / `add_comment` (the create vs. increment paths). The
/// real implementation uses `reqwest`; the mock used in tests uses
/// in-memory `Vec`s.
/// Test: `tests::mock_create_path`, `tests::mock_comment_path`.
// ── Shared result types ───────────────────────────────────────────────────────
/// A found open issue from a GitHub search result.
///
/// Why: the dedup logic needs the URL and number of any pre-existing issue.
/// What: carries the HTML URL and numeric issue ID returned by the search API.
/// Test: constructed by the mock impl in tests.
/// The result of creating a new GitHub issue.
///
/// Why: the filing client must return the URL and number of the created issue
/// so the caller can surface them in the MCP response.
/// What: carries the HTML URL and numeric issue ID returned by the create API.
/// Test: constructed by the mock impl in tests.
// ── Top-level filing function ─────────────────────────────────────────────────
/// File or increment a GitHub issue for the given preview.
///
/// Why: this is the single call that wires token resolution, dedup search,
/// create-vs-comment decision, and anti-spam guard together. Both the
/// MCP `report_bug` confirm path and the HTTP `POST /api/v1/report-bug`
/// confirm path call this function.
/// What:
/// 1. Resolves the bearer token via `provider.token()`. Returns
/// `Err(GithubFilingError::NoToken)` immediately if absent.
/// 2. Constructs a `RealGithubClient` and calls [`file_issue_with`].
///
/// Test: `tests::no_token_yields_no_token_error` (pure-logic, no network).
/// File or increment a GitHub issue using the supplied [`GithubApi`] impl.
///
/// Why: the trait indirection is the seam that lets tests inject a mock without
/// touching `file_issue` (which does the token resolution).
/// What: applies the anti-spam guard (refuses > [`MAX_ISSUES_PER_CALL`] issues
/// per call, which is 1), searches for an existing open issue matching the
/// fingerprint, then either posts a "+1" comment (dedup path) or creates
/// a new issue (create path).
/// Test: `tests::mock_create_path`, `tests::mock_comment_path`.
/// Extract the fingerprint from an issue body that contains the hidden marker.
///
/// Why: the dedup search returns issue bodies; this helper lets callers verify
/// or extract fingerprints from bodies — useful for logging and in tests.
/// What: scans `body` for `<!-- trusty-bug-fingerprint: <fp> -->` and returns
/// the 64-character fingerprint string, or `None` if the marker is absent.
/// Test: `tests::dedup_marker_extraction`.
// ── Tests ─────────────────────────────────────────────────────────────────────