tedi 0.16.3

Personal productivity CLI for task tracking, time management, and GitHub issue integration
Documentation
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
use std::sync::Arc;

use async_trait::async_trait;
use reqwest::Client;
use serde::Deserialize;
use v_utils::macros::wrap_err;

pub use crate::RepoInfo;

pub type BoxedGithubClient = Arc<dyn GithubClient>;
use color_eyre::eyre::{Result, bail, eyre};

/// Trait defining all Github API operations.
/// This allows for both real API calls and mock implementations for testing.
#[async_trait]
pub trait GithubClient: Send + Sync {
	/// Fetch the authenticated user's login name
	async fn fetch_authenticated_user(&self) -> Result<String, GithubError>;

	/// Fetch a single issue by number
	async fn fetch_issue(&self, repo: RepoInfo, issue_number: u64) -> Result<GithubIssue, GithubError>;

	/// Fetch all comments on an issue
	async fn fetch_comments(&self, repo: RepoInfo, issue_number: u64) -> Result<Vec<GithubComment>, GithubError>;

	/// Fetch all sub-issues of an issue
	async fn fetch_sub_issues(&self, repo: RepoInfo, issue_number: u64) -> Result<Vec<GithubIssue>, GithubError>;

	/// Update an issue's body
	async fn update_issue_body(&self, repo: RepoInfo, issue_number: u64, body: &str) -> Result<(), GithubError>;

	/// Update an issue's state (open/closed)
	async fn update_issue_state(&self, repo: RepoInfo, issue_number: u64, state: &str) -> Result<(), GithubError>;

	/// Update a comment's body
	async fn update_comment(&self, repo: RepoInfo, comment_id: u64, body: &str) -> Result<(), GithubError>;

	/// Create a new comment on an issue
	async fn create_comment(&self, repo: RepoInfo, issue_number: u64, body: &str) -> Result<(), GithubError>;

	/// Delete a comment
	async fn delete_comment(&self, repo: RepoInfo, comment_id: u64) -> Result<(), GithubError>;

	/// Create a new issue
	async fn create_issue(&self, repo: RepoInfo, title: &str, body: &str) -> Result<CreatedIssue, GithubError>;

	/// Add a sub-issue to a parent issue
	/// Note: `child_issue_id` is the resource ID (not the issue number)
	async fn add_sub_issue(&self, repo: RepoInfo, parent_issue_number: u64, child_issue_id: u64) -> Result<(), GithubError>;

	/// Find an issue by exact title match
	#[allow(dead_code)]
	async fn find_issue_by_title(&self, repo: RepoInfo, title: &str) -> Result<Option<u64>, GithubError>;

	/// Check if an issue exists by number
	#[allow(dead_code)]
	async fn issue_exists(&self, repo: RepoInfo, issue_number: u64) -> Result<bool, GithubError>;

	/// Fetch the parent issue of a sub-issue (returns None if issue has no parent)
	async fn fetch_parent_issue(&self, repo: RepoInfo, issue_number: u64) -> Result<Option<GithubIssue>, GithubError>;

	/// Fetch timestamps from GraphQL timeline API for title, description, and label changes.
	/// All fields are optional because GitHub only retains timeline events for 90 days.
	/// Note: Comment timestamps should be extracted from GithubComment's created_at/updated_at fields.
	async fn fetch_timeline_timestamps(&self, repo: RepoInfo, issue_number: u64) -> Result<GraphqlTimelineTimestamps, GithubError>;

	/// Replace all labels on an issue.
	async fn set_labels(&self, repo: RepoInfo, issue_number: u64, labels: &[String]) -> Result<(), GithubError>;

	/// Set or clear the milestone on an issue.
	/// Pass `Some(number)` to assign, `None` to unassign.
	async fn set_issue_milestone(&self, repo: RepoInfo, issue_number: u64, milestone: Option<u64>) -> Result<(), GithubError>;

	/// Check if a repository exists and is accessible (we have at least read access)
	async fn repo_exists(&self, repo: RepoInfo) -> Result<bool, GithubError>;
}
/// Error type for GitHub API operations.
#[wrap_err]
#[derive(Debug, thiserror::Error)]
pub enum GithubError {
	/// HTTP request failed (network, TLS, timeout, etc.)
	#[foreign]
	Request(reqwest::Error),

	/// GitHub API returned a non-success status code.
	#[leaf]
	#[error("{context}: {status} - {body}")]
	Api { status: reqwest::StatusCode, body: String, context: String },

	/// GraphQL-level errors in the response body.
	#[leaf]
	#[error("GraphQL errors: {msg}")]
	Graphql { msg: String },

	/// Client not initialized.
	#[leaf]
	#[error("GitHub client not initialized. Is the config file missing a github_token?")]
	NotInitialized,

	/// Generic error (for mocks and other non-HTTP contexts).
	#[leaf]
	#[error("{msg}")]
	Other { msg: String },
}

#[derive(Clone, Debug, Deserialize)]
pub struct GithubIssue {
	pub number: u64,
	pub title: String,
	pub body: Option<String>,
	pub labels: Vec<GithubLabel>,
	pub user: GithubUser,
	pub state: String,
	/// Reason for the state (e.g., "completed", "not_planned", "duplicate")
	/// Only present for closed issues.
	pub state_reason: Option<String>,
}

#[derive(Clone, Debug, Deserialize)]
pub struct GithubLabel {
	pub name: String,
}

#[derive(Clone, Debug, Deserialize)]
pub struct GithubUser {
	pub login: String,
}

#[derive(Clone, Debug, Deserialize)]
pub struct GithubComment {
	pub id: u64,
	pub body: Option<String>,
	pub user: GithubUser,
	/// When the comment was created (ISO 8601 format)
	pub created_at: String,
	/// When the comment was last updated (ISO 8601 format)
	pub updated_at: String,
}

/// Response from Github when creating an issue
#[derive(Debug, Deserialize)]
pub struct CreatedIssue {
	pub id: u64,
	pub number: u64,
	pub html_url: String,
}

/// Timestamps from GraphQL timeline API for issue field changes.
/// All fields are optional because GitHub's timeline only retains events for 90 days.
/// Note: Comment timestamps are fetched via REST API (in GithubComment), not here.
#[derive(Clone, Debug, Default)]
pub struct GraphqlTimelineTimestamps {
	/// Most recent title change (from RenamedTitleEvent)
	pub title: Option<jiff::Timestamp>,
	/// Most recent body/description edit (from issue's updatedAt, which is imprecise)
	pub description: Option<jiff::Timestamp>,
	/// Most recent label change (from LabeledEvent/UnlabeledEvent)
	pub labels: Option<jiff::Timestamp>,
	/// Most recent state change (from ClosedEvent/ReopenedEvent)
	pub state: Option<jiff::Timestamp>,
}

//==============================================================================
// Github Client Trait
//==============================================================================

//==============================================================================
// Real Github Client Implementation
//==============================================================================

/// Real Github API client that makes HTTP requests
pub struct RealGithubClient {
	http_client: Client,
	github_token: String,
}

impl RealGithubClient {
	pub fn new(github_token: String) -> Self {
		Self {
			http_client: Client::new(),
			github_token,
		}
	}

	fn request(&self, method: reqwest::Method, url: &str) -> reqwest::RequestBuilder {
		self.http_client
			.request(method, url)
			.header("User-Agent", "Rust Github Client")
			.header("Authorization", format!("token {}", self.github_token))
	}

	fn get(&self, url: &str) -> reqwest::RequestBuilder {
		self.request(reqwest::Method::GET, url)
	}

	fn post(&self, url: &str) -> reqwest::RequestBuilder {
		self.request(reqwest::Method::POST, url)
	}

	fn patch(&self, url: &str) -> reqwest::RequestBuilder {
		self.request(reqwest::Method::PATCH, url)
	}

	fn delete(&self, url: &str) -> reqwest::RequestBuilder {
		self.request(reqwest::Method::DELETE, url)
	}

	/// Send a PATCH request with JSON body, returning an error on non-success status
	async fn patch_json(&self, url: &str, json: &serde_json::Value, error_context: &str) -> Result<(), GithubError> {
		let res = self.patch(url).json(json).send().await?;

		if !res.status().is_success() {
			let status = res.status();
			let body = res.text().await.unwrap_or_default();
			return Err(GithubError::new_api(status, body, error_context.to_string()));
		}

		Ok(())
	}

	/// Send a POST request with JSON body, returning an error on non-success status
	async fn post_json(&self, url: &str, json: &serde_json::Value, error_context: &str) -> Result<(), GithubError> {
		let res = self.post(url).json(json).send().await?;

		if !res.status().is_success() {
			let status = res.status();
			let body = res.text().await.unwrap_or_default();
			return Err(GithubError::new_api(status, body, error_context.to_string()));
		}

		Ok(())
	}
}

#[async_trait]
impl GithubClient for RealGithubClient {
	async fn fetch_authenticated_user(&self) -> Result<String, GithubError> {
		let res = self.get("https://api.github.com/user").send().await?;

		if !res.status().is_success() {
			let status = res.status();
			let body = res.text().await.unwrap_or_default();
			return Err(GithubError::new_api(status, body, "Failed to fetch authenticated user".to_string()));
		}

		let user = res.json::<GithubUser>().await?;
		Ok(user.login)
	}

	async fn fetch_issue(&self, repo: RepoInfo, issue_number: u64) -> Result<GithubIssue, GithubError> {
		let url = format!("https://api.github.com/repos/{}/{}/issues/{issue_number}", repo.owner(), repo.repo());
		let res = self.get(&url).send().await?;

		if !res.status().is_success() {
			let status = res.status();
			let body = res.text().await.unwrap_or_default();
			return Err(GithubError::new_api(status, body, "Failed to fetch issue".to_string()));
		}

		let issue = res.json::<GithubIssue>().await?;
		Ok(issue)
	}

	async fn fetch_comments(&self, repo: RepoInfo, issue_number: u64) -> Result<Vec<GithubComment>, GithubError> {
		let url = format!("https://api.github.com/repos/{}/{}/issues/{issue_number}/comments", repo.owner(), repo.repo());
		let res = self.get(&url).send().await?;

		if !res.status().is_success() {
			let status = res.status();
			let body = res.text().await.unwrap_or_default();
			return Err(GithubError::new_api(status, body, "Failed to fetch comments".to_string()));
		}

		let comments = res.json::<Vec<GithubComment>>().await?;
		Ok(comments)
	}

	async fn fetch_sub_issues(&self, repo: RepoInfo, issue_number: u64) -> Result<Vec<GithubIssue>, GithubError> {
		let url = format!("https://api.github.com/repos/{}/{}/issues/{issue_number}/sub_issues", repo.owner(), repo.repo());
		let res = self.get(&url).send().await?;

		if !res.status().is_success() {
			// Sub-issues API might not be available or issue has no sub-issues
			// Return empty vec instead of erroring
			return Ok(Vec::new());
		}

		let sub_issues = res.json::<Vec<GithubIssue>>().await?;
		Ok(sub_issues)
	}

	async fn update_issue_body(&self, repo: RepoInfo, issue_number: u64, body: &str) -> Result<(), GithubError> {
		let url = format!("https://api.github.com/repos/{}/{}/issues/{issue_number}", repo.owner(), repo.repo());
		self.patch_json(&url, &serde_json::json!({ "body": body }), "Failed to update issue body").await
	}

	async fn update_issue_state(&self, repo: RepoInfo, issue_number: u64, state: &str) -> Result<(), GithubError> {
		let url = format!("https://api.github.com/repos/{}/{}/issues/{issue_number}", repo.owner(), repo.repo());
		self.patch_json(&url, &serde_json::json!({ "state": state }), "Failed to update issue state").await
	}

	async fn set_labels(&self, repo: RepoInfo, issue_number: u64, labels: &[String]) -> Result<(), GithubError> {
		let url = format!("https://api.github.com/repos/{}/{}/issues/{issue_number}", repo.owner(), repo.repo());
		self.patch_json(&url, &serde_json::json!({ "labels": labels }), "Failed to set labels").await
	}

	async fn set_issue_milestone(&self, repo: RepoInfo, issue_number: u64, milestone: Option<u64>) -> Result<(), GithubError> {
		let url = format!("https://api.github.com/repos/{}/{}/issues/{issue_number}", repo.owner(), repo.repo());
		let json = serde_json::json!({ "milestone": milestone });
		self.patch_json(&url, &json, "Failed to set issue milestone").await
	}

	async fn update_comment(&self, repo: RepoInfo, comment_id: u64, body: &str) -> Result<(), GithubError> {
		let url = format!("https://api.github.com/repos/{}/{}/issues/comments/{comment_id}", repo.owner(), repo.repo());
		self.patch_json(&url, &serde_json::json!({ "body": body }), "Failed to update comment").await
	}

	async fn create_comment(&self, repo: RepoInfo, issue_number: u64, body: &str) -> Result<(), GithubError> {
		let url = format!("https://api.github.com/repos/{}/{}/issues/{issue_number}/comments", repo.owner(), repo.repo());
		self.post_json(&url, &serde_json::json!({ "body": body }), "Failed to create comment").await
	}

	async fn delete_comment(&self, repo: RepoInfo, comment_id: u64) -> Result<(), GithubError> {
		let url = format!("https://api.github.com/repos/{}/{}/issues/comments/{comment_id}", repo.owner(), repo.repo());
		let res = self.delete(&url).send().await?;

		if !res.status().is_success() {
			let status = res.status();
			let body = res.text().await.unwrap_or_default();
			return Err(GithubError::new_api(status, body, "Failed to delete comment".to_string()));
		}

		Ok(())
	}

	async fn create_issue(&self, repo: RepoInfo, title: &str, body: &str) -> Result<CreatedIssue, GithubError> {
		let url = format!("https://api.github.com/repos/{}/{}/issues", repo.owner(), repo.repo());
		let res = self.post(&url).json(&serde_json::json!({ "title": title, "body": body })).send().await?;

		if !res.status().is_success() {
			let status = res.status();
			let body = res.text().await.unwrap_or_default();
			return Err(GithubError::new_api(status, body, "Failed to create issue".to_string()));
		}

		let issue = res.json::<CreatedIssue>().await?;
		Ok(issue)
	}

	async fn add_sub_issue(&self, repo: RepoInfo, parent_issue_number: u64, child_issue_id: u64) -> Result<(), GithubError> {
		let url = format!("https://api.github.com/repos/{}/{}/issues/{parent_issue_number}/sub_issues", repo.owner(), repo.repo());
		self.post_json(&url, &serde_json::json!({ "sub_issue_id": child_issue_id }), "Failed to add sub-issue").await
	}

	async fn find_issue_by_title(&self, repo: RepoInfo, title: &str) -> Result<Option<u64>, GithubError> {
		// Search for issues with this title (search in open and closed)
		let encoded_title = urlencoding::encode(title);
		let url = format!("https://api.github.com/search/issues?q=repo:{}/{}+in:title+{encoded_title}", repo.owner(), repo.repo());
		let res = self.get(&url).send().await?;

		if !res.status().is_success() {
			return Ok(None);
		}

		#[derive(Deserialize)]
		struct SearchResult {
			items: Vec<SearchItem>,
		}
		#[derive(Deserialize)]
		struct SearchItem {
			number: u64,
			title: String,
		}

		let result: SearchResult = res.json().await?;

		// Find exact title match
		for item in result.items {
			if item.title == title {
				return Ok(Some(item.number));
			}
		}

		Ok(None)
	}

	async fn issue_exists(&self, repo: RepoInfo, issue_number: u64) -> Result<bool, GithubError> {
		let url = format!("https://api.github.com/repos/{}/{}/issues/{issue_number}", repo.owner(), repo.repo());
		let res = self.get(&url).send().await?;
		Ok(res.status().is_success())
	}

	async fn fetch_parent_issue(&self, repo: RepoInfo, issue_number: u64) -> Result<Option<GithubIssue>, GithubError> {
		let url = format!("https://api.github.com/repos/{}/{}/issues/{issue_number}/parent", repo.owner(), repo.repo());
		let res = self.get(&url).send().await?;

		if res.status() == reqwest::StatusCode::NOT_FOUND {
			// Issue has no parent
			return Ok(None);
		}

		if !res.status().is_success() {
			let status = res.status();
			let body = res.text().await.unwrap_or_default();
			return Err(GithubError::new_api(status, body, "Failed to fetch parent issue".to_string()));
		}

		let parent = res.json::<GithubIssue>().await?;
		Ok(Some(parent))
	}

	async fn fetch_timeline_timestamps(&self, repo: RepoInfo, issue_number: u64) -> Result<GraphqlTimelineTimestamps, GithubError> {
		// GraphQL query to fetch timeline events for timestamp extraction.
		// We query for:
		// - RenamedTitleEvent: title changes
		// - LabeledEvent/UnlabeledEvent: label changes
		// - ClosedEvent/ReopenedEvent: state changes
		// - The issue body's updatedAt for description changes
		//
		// Note: GitHub timeline only retains events for 90 days, so all timestamps are optional.
		// Comment timestamps are fetched via REST API (GithubComment has created_at/updated_at).
		let query = r#"
			query($owner: String!, $repo: String!, $number: Int!) {
				repository(owner: $owner, name: $repo) {
					issue(number: $number) {
						lastEditedAt
						createdAt
						timelineItems(last: 100, itemTypes: [RENAMED_TITLE_EVENT, LABELED_EVENT, UNLABELED_EVENT, CLOSED_EVENT, REOPENED_EVENT]) {
							nodes {
								__typename
								... on RenamedTitleEvent {
									createdAt
								}
								... on LabeledEvent {
									createdAt
								}
								... on UnlabeledEvent {
									createdAt
								}
								... on ClosedEvent {
									createdAt
								}
								... on ReopenedEvent {
									createdAt
								}
							}
						}
					}
				}
			}
		"#;

		let variables = serde_json::json!({
			"owner": repo.owner(),
			"repo": repo.repo(),
			"number": issue_number as i64
		});

		let body = serde_json::json!({
			"query": query,
			"variables": variables
		});

		let res = self.post("https://api.github.com/graphql").json(&body).send().await?;

		if !res.status().is_success() {
			let status = res.status();
			let body = res.text().await.unwrap_or_default();
			return Err(GithubError::new_api(status, body, "Failed to fetch timeline timestamps via GraphQL".to_string()));
		}

		let response: serde_json::Value = res.json().await?;

		// Check for GraphQL errors
		if let Some(errors) = response.get("errors") {
			return Err(GithubError::new_graphql(errors.to_string()));
		}

		let mut timestamps = GraphqlTimelineTimestamps::default();

		// Extract body edit timestamp (description).
		// `lastEditedAt` is null if the body was never edited after creation, so fall back to `createdAt`.
		let description_ts = response
			.pointer("/data/repository/issue/lastEditedAt")
			.and_then(|v| v.as_str())
			.or_else(|| response.pointer("/data/repository/issue/createdAt").and_then(|v| v.as_str()));
		if let Some(ts_str) = description_ts {
			timestamps.description = ts_str.parse().ok();
		}

		// Process timeline items
		if let Some(nodes) = response.pointer("/data/repository/issue/timelineItems/nodes").and_then(|v| v.as_array()) {
			for node in nodes {
				let Some(typename) = node.get("__typename").and_then(|v| v.as_str()) else {
					tracing::warn!("GraphQL timeline node missing __typename: {node:?}");
					continue;
				};

				match typename {
					"RenamedTitleEvent" =>
						if let Some(created_at) = node.get("createdAt").and_then(|v| v.as_str()) {
							let ts: Option<jiff::Timestamp> = created_at.parse().ok();
							if ts > timestamps.title {
								timestamps.title = ts;
							}
						},
					"LabeledEvent" | "UnlabeledEvent" =>
						if let Some(created_at) = node.get("createdAt").and_then(|v| v.as_str()) {
							let ts: Option<jiff::Timestamp> = created_at.parse().ok();
							if ts > timestamps.labels {
								timestamps.labels = ts;
							}
						},
					"ClosedEvent" | "ReopenedEvent" =>
						if let Some(created_at) = node.get("createdAt").and_then(|v| v.as_str()) {
							let ts: Option<jiff::Timestamp> = created_at.parse().ok();
							if ts > timestamps.state {
								timestamps.state = ts;
							}
						},
					_ => {}
				}
			}
		}

		Ok(timestamps)
	}

	async fn repo_exists(&self, repo: RepoInfo) -> Result<bool, GithubError> {
		let url = format!("https://api.github.com/repos/{}/{}", repo.owner(), repo.repo());
		let res = self.get(&url).send().await?;
		Ok(res.status().is_success())
	}
}

//==============================================================================
// Convenience type alias for boxed client
//==============================================================================

//==============================================================================
// Global client storage
//==============================================================================

/// Thread-local storage for the GitHub client.
/// Set once at startup, then accessed globally by sink operations.
pub mod client {
	use std::cell::RefCell;

	use super::BoxedGithubClient;

	thread_local! {
		static CLIENT: RefCell<Option<BoxedGithubClient>> = const { RefCell::new(None) };
	}

	/// Set the global GitHub client. Must be called before any sink operations.
	pub fn set(client: BoxedGithubClient) {
		CLIENT.with(|c| *c.borrow_mut() = Some(client));
	}

	/// Get the global GitHub client.
	pub fn get() -> Result<BoxedGithubClient, super::GithubError> {
		CLIENT.with(|c| c.borrow().clone().ok_or_else(|| super::GithubError::new_not_initialized()))
	}
}

//==============================================================================
// Utility functions (URL parsing, etc.) - These don't need the trait
//==============================================================================

/// Parse a Github issue URL and extract owner, repo, and issue number.
/// Supports formats like:
/// - <https://github.com/owner/repo/issues/123>
/// - github.com/owner/repo/issues/123
/// - git@github.com:owner/repo (returns repo info, issue number parsing will fail)
/// - ssh://git@github.com/owner/repo.git (returns repo info, issue number parsing will fail)
pub fn parse_github_issue_url(url: &str) -> Result<(String, String, u64)> {
	let url = url.trim();

	// Try SSH format first: git@github.com:owner/repo.git or git@github.com:owner/repo
	// SSH URLs don't support issue numbers directly, but we parse them for consistency
	if let Some(path) = url.strip_prefix("git@github.com:") {
		// SSH format doesn't have issue numbers - this is an error for issue URLs
		bail!(
			"SSH URL format doesn't support issue numbers. Use HTTPS format: https://github.com/{}/issues/NUMBER",
			path.strip_suffix(".git").unwrap_or(path)
		);
	}

	// Try ssh:// format: ssh://git@github.com/owner/repo.git
	if let Some(path) = url.strip_prefix("ssh://git@github.com/") {
		bail!(
			"SSH URL format doesn't support issue numbers. Use HTTPS format: https://github.com/{}/issues/NUMBER",
			path.strip_suffix(".git").unwrap_or(path)
		);
	}

	// Remove protocol prefix if present (https://, http://)
	let path = url.strip_prefix("https://").or_else(|| url.strip_prefix("http://")).unwrap_or(url);

	// Remove github.com prefix
	let path = path.strip_prefix("github.com/").ok_or_else(|| eyre!("URL must be a Github URL: {url}"))?;

	// Split by /
	let parts: Vec<&str> = path.split('/').collect();

	if parts.len() < 4 || parts[2] != "issues" {
		bail!("Invalid Github issue URL format. Expected: https://github.com/owner/repo/issues/123");
	}

	let owner = parts[0].to_string();
	let repo = parts[1].to_string();
	let issue_number: u64 = parts[3].parse().map_err(|_| eyre!("Invalid issue number: {}", parts[3]))?;

	Ok((owner, repo, issue_number))
}

/// Check if a string looks like a Github issue URL specifically
pub fn is_github_issue_url(s: &str) -> bool {
	let s = s.trim();
	s.contains("github.com/") && s.contains("/issues/")
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn test_parse_github_issue_url() {
		// Standard HTTPS URL
		let (owner, repo, num) = parse_github_issue_url("https://github.com/owner/repo/issues/123").unwrap();
		assert_eq!(owner, "owner");
		assert_eq!(repo, "repo");
		assert_eq!(num, 123);

		// Without protocol
		let (owner, repo, num) = parse_github_issue_url("github.com/owner/repo/issues/456").unwrap();
		assert_eq!(owner, "owner");
		assert_eq!(repo, "repo");
		assert_eq!(num, 456);

		// HTTP URL
		let (owner, repo, num) = parse_github_issue_url("http://github.com/owner/repo/issues/789").unwrap();
		assert_eq!(owner, "owner");
		assert_eq!(repo, "repo");
		assert_eq!(num, 789);

		// With trailing whitespace
		let (owner, repo, num) = parse_github_issue_url("  https://github.com/owner/repo/issues/123  ").unwrap();
		assert_eq!(owner, "owner");
		assert_eq!(repo, "repo");
		assert_eq!(num, 123);
	}

	#[test]
	fn test_parse_github_issue_url_errors() {
		// Not a Github URL
		assert!(parse_github_issue_url("https://gitlab.com/owner/repo/issues/123").is_err());

		// Not an issues URL
		assert!(parse_github_issue_url("https://github.com/owner/repo/pull/123").is_err());

		// Invalid issue number
		assert!(parse_github_issue_url("https://github.com/owner/repo/issues/abc").is_err());

		// Missing parts
		assert!(parse_github_issue_url("https://github.com/owner").is_err());
	}

	#[test]
	fn test_parse_github_issue_url_ssh_error() {
		// SSH URLs should give a helpful error message
		let result = parse_github_issue_url("git@github.com:owner/repo.git");
		assert!(result.is_err());
		let err = result.unwrap_err().to_string();
		assert!(err.contains("SSH URL format doesn't support issue numbers"));
		assert!(err.contains("owner/repo"));

		// ssh:// format
		let result = parse_github_issue_url("ssh://git@github.com/owner/repo.git");
		assert!(result.is_err());
		let err = result.unwrap_err().to_string();
		assert!(err.contains("SSH URL format doesn't support issue numbers"));
	}

	#[test]
	fn test_is_github_issue_url() {
		// Valid issue URLs
		assert!(is_github_issue_url("https://github.com/owner/repo/issues/123"));
		assert!(is_github_issue_url("github.com/owner/repo/issues/456"));
		assert!(is_github_issue_url("http://github.com/owner/repo/issues/789"));

		// Not issue URLs
		assert!(!is_github_issue_url("https://github.com/owner/repo"));
		assert!(!is_github_issue_url("git@github.com:owner/repo.git"));
		assert!(!is_github_issue_url("https://github.com/owner/repo/pull/123"));
		assert!(!is_github_issue_url("https://gitlab.com/owner/repo/issues/123"));
	}
}