Skip to main content

jira_cli/api/
client.rs

1use base64::Engine;
2use base64::engine::general_purpose::STANDARD as BASE64;
3use reqwest::header::{AUTHORIZATION, HeaderMap, HeaderValue};
4use serde::de::DeserializeOwned;
5use std::collections::BTreeMap;
6
7use super::ApiError;
8use super::AuthType;
9use super::types::*;
10
11pub struct JiraClient {
12    http: reqwest::Client,
13    base_url: String,
14    agile_base_url: String,
15    site_url: String,
16    host: String,
17    api_version: u8,
18}
19
20const SEARCH_FIELDS: [&str; 7] = [
21    "summary",
22    "status",
23    "assignee",
24    "priority",
25    "issuetype",
26    "created",
27    "updated",
28];
29const SEARCH_GET_JQL_LIMIT: usize = 1500;
30
31/// Max issues per page the Jira Cloud `/search/jql` endpoint will return when
32/// any non-ID fields are requested. The server silently caps larger values,
33/// so we paginate internally to fulfil larger caller-requested limits.
34const SEARCH_JQL_MAX_PAGE: usize = 100;
35
36/// Page size used when walking the cursor forward to simulate an offset on
37/// Jira Cloud. Requests only `id` to stay cheap (allows up to 5000/page).
38const SEARCH_JQL_SKIP_PAGE: usize = 1000;
39
40impl JiraClient {
41    pub fn new(
42        host: &str,
43        email: &str,
44        token: &str,
45        auth_type: AuthType,
46        api_version: u8,
47    ) -> Result<Self, ApiError> {
48        // Determine the scheme. An explicit `http://` prefix is preserved as-is
49        // (useful for local testing); everything else defaults to HTTPS.
50        let (scheme, domain) = if host.starts_with("http://") {
51            (
52                "http",
53                host.trim_start_matches("http://").trim_end_matches('/'),
54            )
55        } else {
56            (
57                "https",
58                host.trim_start_matches("https://").trim_end_matches('/'),
59            )
60        };
61
62        if domain.is_empty() {
63            return Err(ApiError::Other("Host cannot be empty".into()));
64        }
65
66        let auth_value = match auth_type {
67            AuthType::Basic => {
68                let credentials = BASE64.encode(format!("{email}:{token}"));
69                format!("Basic {credentials}")
70            }
71            AuthType::Pat => format!("Bearer {token}"),
72        };
73
74        let mut headers = HeaderMap::new();
75        headers.insert(
76            AUTHORIZATION,
77            HeaderValue::from_str(&auth_value).map_err(|e| ApiError::Other(e.to_string()))?,
78        );
79
80        let http = reqwest::Client::builder()
81            .default_headers(headers)
82            .timeout(std::time::Duration::from_secs(30))
83            .build()
84            .map_err(ApiError::Http)?;
85
86        let site_url = format!("{scheme}://{domain}");
87        let base_url = format!("{site_url}/rest/api/{api_version}");
88        let agile_base_url = format!("{site_url}/rest/agile/1.0");
89
90        Ok(Self {
91            http,
92            base_url,
93            agile_base_url,
94            site_url,
95            host: domain.to_string(),
96            api_version,
97        })
98    }
99
100    pub fn host(&self) -> &str {
101        &self.host
102    }
103
104    pub fn api_version(&self) -> u8 {
105        self.api_version
106    }
107
108    pub fn browse_base_url(&self) -> &str {
109        &self.site_url
110    }
111
112    pub fn browse_url(&self, issue_key: &str) -> String {
113        format!("{}/browse/{issue_key}", self.browse_base_url())
114    }
115
116    fn map_status(status: u16, body: String) -> ApiError {
117        let message = summarize_error_body(status, &body);
118        match status {
119            401 | 403 => ApiError::Auth(message),
120            404 => ApiError::NotFound(message),
121            429 => ApiError::RateLimit,
122            _ => ApiError::Api { status, message },
123        }
124    }
125
126    async fn get<T: DeserializeOwned>(&self, path: &str) -> Result<T, ApiError> {
127        let url = format!("{}/{path}", self.base_url);
128        let resp = self.http.get(&url).send().await?;
129        let status = resp.status();
130        if !status.is_success() {
131            let body = resp.text().await.unwrap_or_default();
132            return Err(Self::map_status(status.as_u16(), body));
133        }
134        resp.json::<T>().await.map_err(ApiError::Http)
135    }
136
137    async fn agile_get<T: DeserializeOwned>(&self, path: &str) -> Result<T, ApiError> {
138        let url = format!("{}/{path}", self.agile_base_url);
139        let resp = self.http.get(&url).send().await?;
140        let status = resp.status();
141        if !status.is_success() {
142            let body = resp.text().await.unwrap_or_default();
143            return Err(Self::map_status(status.as_u16(), body));
144        }
145        resp.json::<T>().await.map_err(ApiError::Http)
146    }
147
148    async fn post<T: DeserializeOwned>(
149        &self,
150        path: &str,
151        body: &serde_json::Value,
152    ) -> Result<T, ApiError> {
153        let url = format!("{}/{path}", self.base_url);
154        let resp = self.http.post(&url).json(body).send().await?;
155        let status = resp.status();
156        if !status.is_success() {
157            let body_text = resp.text().await.unwrap_or_default();
158            return Err(Self::map_status(status.as_u16(), body_text));
159        }
160        resp.json::<T>().await.map_err(ApiError::Http)
161    }
162
163    async fn post_empty_response(
164        &self,
165        path: &str,
166        body: &serde_json::Value,
167    ) -> Result<(), ApiError> {
168        let url = format!("{}/{path}", self.base_url);
169        let resp = self.http.post(&url).json(body).send().await?;
170        let status = resp.status();
171        if !status.is_success() {
172            let body_text = resp.text().await.unwrap_or_default();
173            return Err(Self::map_status(status.as_u16(), body_text));
174        }
175        Ok(())
176    }
177
178    async fn put_empty_response(
179        &self,
180        path: &str,
181        body: &serde_json::Value,
182    ) -> Result<(), ApiError> {
183        let url = format!("{}/{path}", self.base_url);
184        let resp = self.http.put(&url).json(body).send().await?;
185        let status = resp.status();
186        if !status.is_success() {
187            let body_text = resp.text().await.unwrap_or_default();
188            return Err(Self::map_status(status.as_u16(), body_text));
189        }
190        Ok(())
191    }
192
193    // ── Issues ────────────────────────────────────────────────────────────────
194
195    /// Search issues using JQL.
196    ///
197    /// On API v2 (Jira Data Center / Server) this uses the classic
198    /// `/rest/api/2/search` endpoint with offset-based pagination.
199    ///
200    /// On API v3 (Jira Cloud) this uses the replacement
201    /// `/rest/api/3/search/jql` endpoint — the original `/search` was retired
202    /// on 2025-10-31 and returns 410 Gone. The new endpoint only supports
203    /// cursor-based pagination and does not return an exact total, so we
204    /// simulate the `start_at` offset by walking the cursor forward.
205    pub async fn search(
206        &self,
207        jql: &str,
208        max_results: usize,
209        start_at: usize,
210    ) -> Result<SearchResponse, ApiError> {
211        if self.api_version >= 3 {
212            self.search_jql_v3(jql, max_results, start_at).await
213        } else {
214            self.search_v2(jql, max_results, start_at).await
215        }
216    }
217
218    async fn search_v2(
219        &self,
220        jql: &str,
221        max_results: usize,
222        start_at: usize,
223    ) -> Result<SearchResponse, ApiError> {
224        let fields = SEARCH_FIELDS.join(",");
225        let encoded_jql = percent_encode(jql);
226        #[derive(serde::Deserialize)]
227        struct RawV2 {
228            issues: Vec<Issue>,
229            #[serde(default)]
230            total: usize,
231            #[serde(rename = "startAt", default)]
232            start_at: usize,
233            #[serde(rename = "maxResults", default)]
234            max_results: usize,
235        }
236        let raw: RawV2 = if encoded_jql.len() <= SEARCH_GET_JQL_LIMIT {
237            let path = format!(
238                "search?jql={encoded_jql}&maxResults={max_results}&startAt={start_at}&fields={fields}"
239            );
240            self.get(&path).await?
241        } else {
242            self.post(
243                "search",
244                &serde_json::json!({
245                    "jql": jql,
246                    "maxResults": max_results,
247                    "startAt": start_at,
248                    "fields": SEARCH_FIELDS,
249                }),
250            )
251            .await?
252        };
253        let is_last = raw.start_at + raw.issues.len() >= raw.total;
254        Ok(SearchResponse {
255            issues: raw.issues,
256            total: Some(raw.total),
257            start_at: raw.start_at,
258            max_results: raw.max_results,
259            is_last,
260        })
261    }
262
263    /// Fetch a single page from the Jira Cloud `/search/jql` endpoint with
264    /// the full field list populated on each issue.
265    ///
266    /// Always uses POST: it handles long JQL without URL-length limits and
267    /// accepts `fields` as a JSON array (GET requires repeated query params).
268    async fn search_jql_page(
269        &self,
270        jql: &str,
271        page_size: usize,
272        next_token: Option<&str>,
273    ) -> Result<SearchJqlPage, ApiError> {
274        let mut body = serde_json::json!({
275            "jql": jql,
276            "maxResults": page_size,
277            "fields": SEARCH_FIELDS,
278        });
279        if let Some(t) = next_token {
280            body["nextPageToken"] = serde_json::Value::String(t.to_string());
281        }
282        self.post("search/jql", &body).await
283    }
284
285    /// Fetch a `/search/jql` page requesting only the `id` field.
286    ///
287    /// Used to cheaply walk the cursor forward when simulating an offset.
288    /// Issues in the response lack a `fields` sub-object, so they are
289    /// deserialized as raw JSON values rather than full `Issue`s.
290    async fn search_jql_skip_page(
291        &self,
292        jql: &str,
293        page_size: usize,
294        next_token: Option<&str>,
295    ) -> Result<SearchJqlSkipPage, ApiError> {
296        let mut body = serde_json::json!({
297            "jql": jql,
298            "maxResults": page_size,
299            "fields": ["id"],
300        });
301        if let Some(t) = next_token {
302            body["nextPageToken"] = serde_json::Value::String(t.to_string());
303        }
304        self.post("search/jql", &body).await
305    }
306
307    async fn search_jql_v3(
308        &self,
309        jql: &str,
310        max_results: usize,
311        start_at: usize,
312    ) -> Result<SearchResponse, ApiError> {
313        // Walk the cursor forward to simulate `start_at`. The `/search/jql`
314        // endpoint only supports sequential cursor pagination, so arbitrary
315        // offsets require fetching and discarding earlier pages. Request
316        // `id`-only to keep skip-pages cheap.
317        let mut next_token: Option<String> = None;
318        let mut skipped = 0usize;
319        while skipped < start_at {
320            let want = (start_at - skipped).min(SEARCH_JQL_SKIP_PAGE);
321            let page = self
322                .search_jql_skip_page(jql, want, next_token.as_deref())
323                .await?;
324            let got = page.issues.len();
325            skipped += got;
326            if got == 0 || page.is_last {
327                // Offset is past the end of the result set.
328                return Ok(SearchResponse {
329                    issues: Vec::new(),
330                    total: None,
331                    start_at,
332                    max_results: 0,
333                    is_last: true,
334                });
335            }
336            next_token = page.next_page_token;
337            if next_token.is_none() {
338                // Server reported more pages but returned no cursor; treat as end
339                // rather than silently restarting from page 0 on the next iteration.
340                return Ok(SearchResponse {
341                    issues: Vec::new(),
342                    total: None,
343                    start_at,
344                    max_results: 0,
345                    is_last: true,
346                });
347            }
348        }
349
350        // Collect up to `max_results` issues, paging internally to honour
351        // the server's per-page cap when fields are requested.
352        let mut collected: Vec<Issue> = Vec::new();
353        let mut is_last = false;
354        while collected.len() < max_results {
355            let remaining = max_results - collected.len();
356            let want = remaining.min(SEARCH_JQL_MAX_PAGE);
357            let page = self
358                .search_jql_page(jql, want, next_token.as_deref())
359                .await?;
360            let got = page.issues.len();
361            collected.extend(page.issues);
362            if page.is_last || got == 0 {
363                is_last = true;
364                break;
365            }
366            next_token = page.next_page_token;
367            if next_token.is_none() {
368                is_last = true;
369                break;
370            }
371        }
372
373        let returned = collected.len();
374        Ok(SearchResponse {
375            issues: collected,
376            // Cloud `/search/jql` does not return an exact total.
377            total: None,
378            start_at,
379            max_results: returned,
380            is_last,
381        })
382    }
383
384    /// Fetch a single issue by key (e.g. `PROJ-123`), including all comments.
385    ///
386    /// Jira embeds only the first page of comments in the issue response. When
387    /// the embedded page is incomplete, additional requests are made to fetch
388    /// the remaining comments.
389    pub async fn get_issue(&self, key: &str) -> Result<Issue, ApiError> {
390        validate_issue_key(key)?;
391        let fields = "summary,status,assignee,reporter,priority,issuetype,description,labels,created,updated,comment,issuelinks";
392        let path = format!("issue/{key}?fields={fields}");
393        let mut issue: Issue = self.get(&path).await?;
394
395        // Fetch remaining comment pages if the embedded page is incomplete
396        if let Some(ref mut comment_list) = issue.fields.comment
397            && comment_list.total > comment_list.comments.len()
398        {
399            let mut start_at = comment_list.comments.len();
400            while comment_list.comments.len() < comment_list.total {
401                let page: CommentList = self
402                    .get(&format!(
403                        "issue/{key}/comment?startAt={start_at}&maxResults=100"
404                    ))
405                    .await?;
406                if page.comments.is_empty() {
407                    break;
408                }
409                start_at += page.comments.len();
410                comment_list.comments.extend(page.comments);
411            }
412        }
413
414        Ok(issue)
415    }
416
417    /// Create a new issue.
418    #[allow(clippy::too_many_arguments)]
419    pub async fn create_issue(
420        &self,
421        project_key: &str,
422        issue_type: &str,
423        summary: &str,
424        description: Option<&str>,
425        priority: Option<&str>,
426        labels: Option<&[&str]>,
427        assignee: Option<&str>,
428        parent: Option<&str>,
429        custom_fields: &[(String, serde_json::Value)],
430    ) -> Result<CreateIssueResponse, ApiError> {
431        let mut fields = serde_json::json!({
432            "project": { "key": project_key },
433            "issuetype": { "name": issue_type },
434            "summary": summary,
435        });
436
437        if let Some(desc) = description {
438            fields["description"] = self.make_body(desc);
439        }
440        if let Some(p) = priority {
441            fields["priority"] = serde_json::json!({ "name": p });
442        }
443        if let Some(lbls) = labels
444            && !lbls.is_empty()
445        {
446            fields["labels"] = serde_json::json!(lbls);
447        }
448        if let Some(id) = assignee {
449            fields["assignee"] = self.assignee_payload(id);
450        }
451        if let Some(parent_key) = parent {
452            fields["parent"] = serde_json::json!({ "key": parent_key });
453        }
454        for (key, value) in custom_fields {
455            fields[key] = value.clone();
456        }
457
458        self.post("issue", &serde_json::json!({ "fields": fields }))
459            .await
460    }
461
462    /// Log work on an issue.
463    ///
464    /// `time_spent` uses Jira duration format (e.g. `2h 30m`, `1d`, `30m`).
465    /// `started` is an ISO-8601 datetime string; when `None` the server uses now.
466    pub async fn log_work(
467        &self,
468        key: &str,
469        time_spent: &str,
470        comment: Option<&str>,
471        started: Option<&str>,
472    ) -> Result<WorklogEntry, ApiError> {
473        validate_issue_key(key)?;
474        let mut payload = serde_json::json!({ "timeSpent": time_spent });
475        if let Some(c) = comment {
476            payload["comment"] = self.make_body(c);
477        }
478        if let Some(s) = started {
479            payload["started"] = serde_json::Value::String(s.to_string());
480        }
481        self.post(&format!("issue/{key}/worklog"), &payload).await
482    }
483
484    /// Add a comment to an issue.
485    pub async fn add_comment(&self, key: &str, body: &str) -> Result<Comment, ApiError> {
486        validate_issue_key(key)?;
487        let payload = serde_json::json!({ "body": self.make_body(body) });
488        self.post(&format!("issue/{key}/comment"), &payload).await
489    }
490
491    /// List available transitions for an issue.
492    pub async fn get_transitions(&self, key: &str) -> Result<Vec<Transition>, ApiError> {
493        validate_issue_key(key)?;
494        let resp: TransitionsResponse = self.get(&format!("issue/{key}/transitions")).await?;
495        Ok(resp.transitions)
496    }
497
498    /// Execute a transition by transition ID.
499    pub async fn do_transition(&self, key: &str, transition_id: &str) -> Result<(), ApiError> {
500        validate_issue_key(key)?;
501        let payload = serde_json::json!({ "transition": { "id": transition_id } });
502        self.post_empty_response(&format!("issue/{key}/transitions"), &payload)
503            .await
504    }
505
506    /// Assign an issue to a user, or unassign with `None`.
507    ///
508    /// API v3 (Jira Cloud) identifies users by `accountId`.
509    /// API v2 (Jira Data Center / Server) identifies users by `name` (username).
510    pub async fn assign_issue(&self, key: &str, account_id: Option<&str>) -> Result<(), ApiError> {
511        validate_issue_key(key)?;
512        let payload = match account_id {
513            Some(id) => self.assignee_payload(id),
514            None => {
515                if self.api_version >= 3 {
516                    serde_json::json!({ "accountId": null })
517                } else {
518                    serde_json::json!({ "name": null })
519                }
520            }
521        };
522        self.put_empty_response(&format!("issue/{key}/assignee"), &payload)
523            .await
524    }
525
526    /// Build the assignee payload for the current API version.
527    ///
528    /// API v3 uses `accountId`; API v2 uses `name` (username).
529    fn assignee_payload(&self, id: &str) -> serde_json::Value {
530        if self.api_version >= 3 {
531            serde_json::json!({ "accountId": id })
532        } else {
533            serde_json::json!({ "name": id })
534        }
535    }
536
537    /// Get the currently authenticated user.
538    pub async fn get_myself(&self) -> Result<Myself, ApiError> {
539        self.get("myself").await
540    }
541
542    /// Update issue fields (summary, description, priority, or any custom field).
543    pub async fn update_issue(
544        &self,
545        key: &str,
546        summary: Option<&str>,
547        description: Option<&str>,
548        priority: Option<&str>,
549        custom_fields: &[(String, serde_json::Value)],
550    ) -> Result<(), ApiError> {
551        validate_issue_key(key)?;
552        let mut fields = serde_json::Map::new();
553        if let Some(s) = summary {
554            fields.insert("summary".into(), serde_json::Value::String(s.into()));
555        }
556        if let Some(d) = description {
557            fields.insert("description".into(), self.make_body(d));
558        }
559        if let Some(p) = priority {
560            fields.insert("priority".into(), serde_json::json!({ "name": p }));
561        }
562        for (k, value) in custom_fields {
563            fields.insert(k.clone(), value.clone());
564        }
565        if fields.is_empty() {
566            return Err(ApiError::InvalidInput(
567                "At least one field (--summary, --description, --priority, or --field) is required"
568                    .into(),
569            ));
570        }
571        self.put_empty_response(
572            &format!("issue/{key}"),
573            &serde_json::json!({ "fields": fields }),
574        )
575        .await
576    }
577
578    /// Build the appropriate body value for a description or comment field.
579    ///
580    /// API v3 (Jira Cloud) requires Atlassian Document Format (ADF). API v2
581    /// (Jira Data Center / Server) accepts plain strings.
582    fn make_body(&self, text: &str) -> serde_json::Value {
583        if self.api_version >= 3 {
584            text_to_adf(text)
585        } else {
586            serde_json::Value::String(text.to_string())
587        }
588    }
589
590    // ── Users ─────────────────────────────────────────────────────────────────
591
592    /// Search for users matching a query string.
593    ///
594    /// API v2: uses `username` parameter. API v3: uses `query` parameter.
595    pub async fn search_users(&self, query: &str) -> Result<Vec<User>, ApiError> {
596        let encoded = percent_encode(query);
597        let param = if self.api_version >= 3 {
598            "query"
599        } else {
600            "username"
601        };
602        let path = format!("user/search?{param}={encoded}&maxResults=50");
603        self.get::<Vec<User>>(&path).await
604    }
605
606    // ── Issue links ───────────────────────────────────────────────────────────
607
608    /// List available issue link types.
609    pub async fn get_link_types(&self) -> Result<Vec<IssueLinkType>, ApiError> {
610        #[derive(serde::Deserialize)]
611        struct Wrapper {
612            #[serde(rename = "issueLinkTypes")]
613            types: Vec<IssueLinkType>,
614        }
615        let w: Wrapper = self.get("issueLinkType").await?;
616        Ok(w.types)
617    }
618
619    /// Link two issues.
620    ///
621    /// `link_type` is the name of the link type (e.g. "Blocks", "Duplicate").
622    /// The direction follows the link type's `outward` description:
623    /// `from_key` outward-links to `to_key`.
624    pub async fn link_issues(
625        &self,
626        from_key: &str,
627        to_key: &str,
628        link_type: &str,
629    ) -> Result<(), ApiError> {
630        validate_issue_key(from_key)?;
631        validate_issue_key(to_key)?;
632        let payload = serde_json::json!({
633            "type": { "name": link_type },
634            "inwardIssue": { "key": from_key },
635            "outwardIssue": { "key": to_key },
636        });
637        let url = format!("{}/issueLink", self.base_url);
638        let resp = self.http.post(&url).json(&payload).send().await?;
639        let status = resp.status();
640        if !status.is_success() {
641            let body = resp.text().await.unwrap_or_default();
642            return Err(Self::map_status(status.as_u16(), body));
643        }
644        Ok(())
645    }
646
647    /// Remove an issue link by its ID.
648    pub async fn unlink_issues(&self, link_id: &str) -> Result<(), ApiError> {
649        let url = format!("{}/issueLink/{link_id}", self.base_url);
650        let resp = self.http.delete(&url).send().await?;
651        let status = resp.status();
652        if !status.is_success() {
653            let body = resp.text().await.unwrap_or_default();
654            return Err(Self::map_status(status.as_u16(), body));
655        }
656        Ok(())
657    }
658
659    // ── Boards & Sprints ──────────────────────────────────────────────────────
660
661    /// List all boards, fetching all pages.
662    pub async fn list_boards(&self) -> Result<Vec<Board>, ApiError> {
663        let mut all = Vec::new();
664        let mut start_at = 0usize;
665        const PAGE: usize = 50;
666        loop {
667            let path = format!("board?startAt={start_at}&maxResults={PAGE}");
668            let page: BoardSearchResponse = self.agile_get(&path).await?;
669            let received = page.values.len();
670            all.extend(page.values);
671            if page.is_last || received == 0 {
672                break;
673            }
674            start_at += received;
675        }
676        Ok(all)
677    }
678
679    /// List sprints for a board, optionally filtered by state.
680    ///
681    /// `state` can be "active", "closed", "future", or `None` for all.
682    pub async fn list_sprints(
683        &self,
684        board_id: u64,
685        state: Option<&str>,
686    ) -> Result<Vec<Sprint>, ApiError> {
687        let mut all = Vec::new();
688        let mut start_at = 0usize;
689        const PAGE: usize = 50;
690        loop {
691            let state_param = state.map(|s| format!("&state={s}")).unwrap_or_default();
692            let path = format!(
693                "board/{board_id}/sprint?startAt={start_at}&maxResults={PAGE}{state_param}"
694            );
695            let page: SprintSearchResponse = self.agile_get(&path).await?;
696            let received = page.values.len();
697            all.extend(page.values);
698            if page.is_last || received == 0 {
699                break;
700            }
701            start_at += received;
702        }
703        Ok(all)
704    }
705
706    // ── Projects ──────────────────────────────────────────────────────────────
707
708    /// List all accessible projects.
709    ///
710    /// API v3 (Jira Cloud) uses the paginated `project/search` endpoint.
711    /// API v2 (Jira Data Center / Server) uses the simpler `project` endpoint
712    /// that returns all results in a single flat array.
713    pub async fn list_projects(&self) -> Result<Vec<Project>, ApiError> {
714        if self.api_version < 3 {
715            return self.get::<Vec<Project>>("project").await;
716        }
717
718        let mut all: Vec<Project> = Vec::new();
719        let mut start_at: usize = 0;
720        const PAGE: usize = 50;
721
722        loop {
723            let path = format!("project/search?startAt={start_at}&maxResults={PAGE}&orderBy=key");
724            let page: ProjectSearchResponse = self.get(&path).await?;
725            let page_start = page.start_at;
726            let received = page.values.len();
727            let total = page.total;
728            all.extend(page.values);
729
730            if page.is_last || all.len() >= total {
731                break;
732            }
733
734            if received == 0 {
735                return Err(ApiError::Other(
736                    "Project pagination returned an empty non-terminal page".into(),
737                ));
738            }
739
740            start_at = page_start.saturating_add(received);
741        }
742
743        Ok(all)
744    }
745
746    /// Fetch a single project by key.
747    pub async fn get_project(&self, key: &str) -> Result<Project, ApiError> {
748        self.get(&format!("project/{key}")).await
749    }
750
751    // ── Fields ────────────────────────────────────────────────────────────────
752
753    /// List all available fields (system and custom).
754    pub async fn list_fields(&self) -> Result<Vec<Field>, ApiError> {
755        self.get::<Vec<Field>>("field").await
756    }
757
758    /// Move an issue to a sprint.
759    ///
760    /// Uses the Agile REST API which is version-independent.
761    pub async fn move_issue_to_sprint(
762        &self,
763        issue_key: &str,
764        sprint_id: u64,
765    ) -> Result<(), ApiError> {
766        validate_issue_key(issue_key)?;
767        let url = format!("{}/sprint/{sprint_id}/issue", self.agile_base_url);
768        let payload = serde_json::json!({ "issues": [issue_key] });
769        let resp = self.http.post(&url).json(&payload).send().await?;
770        let status = resp.status();
771        if !status.is_success() {
772            let body = resp.text().await.unwrap_or_default();
773            return Err(Self::map_status(status.as_u16(), body));
774        }
775        Ok(())
776    }
777
778    /// Fetch a single sprint by numeric ID.
779    pub async fn get_sprint(&self, sprint_id: u64) -> Result<Sprint, ApiError> {
780        self.agile_get::<Sprint>(&format!("sprint/{sprint_id}"))
781            .await
782    }
783
784    /// Resolve a sprint specifier to a `Sprint`.
785    ///
786    /// Accepts:
787    /// - A numeric string: fetches the sprint by ID to confirm it exists and get the name
788    /// - `"active"`: returns the first active sprint found across all boards
789    /// - Any other string: matched case-insensitively as a substring of sprint names
790    pub async fn resolve_sprint(&self, specifier: &str) -> Result<Sprint, ApiError> {
791        if let Ok(id) = specifier.parse::<u64>() {
792            return self.get_sprint(id).await;
793        }
794
795        let boards = self.list_boards().await?;
796        if boards.is_empty() {
797            return Err(ApiError::NotFound("No boards found".into()));
798        }
799
800        let target_state = if specifier.eq_ignore_ascii_case("active") {
801            Some("active")
802        } else {
803            None
804        };
805
806        for board in &boards {
807            let sprints = self.list_sprints(board.id, target_state).await?;
808            for sprint in sprints {
809                if specifier.eq_ignore_ascii_case("active") {
810                    if sprint.state == "active" {
811                        return Ok(sprint);
812                    }
813                } else if sprint
814                    .name
815                    .to_lowercase()
816                    .contains(&specifier.to_lowercase())
817                {
818                    return Ok(sprint);
819                }
820            }
821        }
822
823        Err(ApiError::NotFound(format!(
824            "No sprint found matching '{specifier}'"
825        )))
826    }
827
828    /// Resolve a sprint specifier to its numeric ID.
829    ///
830    /// See [`resolve_sprint`] for accepted specifier formats.
831    pub async fn resolve_sprint_id(&self, specifier: &str) -> Result<u64, ApiError> {
832        if let Ok(id) = specifier.parse::<u64>() {
833            return Ok(id);
834        }
835        self.resolve_sprint(specifier).await.map(|s| s.id)
836    }
837}
838
839/// Validate that a key matches the `[A-Z][A-Z0-9]*-[0-9]+` format
840/// before using it in a URL path.
841///
842/// Jira project keys start with an uppercase letter and may contain further
843/// uppercase letters or digits (e.g. `ABC2-123` is valid).
844fn validate_issue_key(key: &str) -> Result<(), ApiError> {
845    let mut parts = key.splitn(2, '-');
846    let project = parts.next().unwrap_or("");
847    let number = parts.next().unwrap_or("");
848
849    let valid = !project.is_empty()
850        && !number.is_empty()
851        && project
852            .chars()
853            .next()
854            .is_some_and(|c| c.is_ascii_uppercase())
855        && project
856            .chars()
857            .all(|c| c.is_ascii_uppercase() || c.is_ascii_digit())
858        && number.chars().all(|c| c.is_ascii_digit());
859
860    if valid {
861        Ok(())
862    } else {
863        Err(ApiError::InvalidInput(format!(
864            "Invalid issue key '{key}'. Expected format: PROJECT-123"
865        )))
866    }
867}
868
869/// Percent-encode a string for use in a URL query parameter.
870///
871/// Uses `%20` for spaces (not `+`) per standard URL encoding.
872fn percent_encode(s: &str) -> String {
873    let mut encoded = String::with_capacity(s.len() * 2);
874    for byte in s.bytes() {
875        match byte {
876            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
877                encoded.push(byte as char)
878            }
879            b => encoded.push_str(&format!("%{b:02X}")),
880        }
881    }
882    encoded
883}
884
885/// Truncate an API error body when explicitly debugging HTTP failures.
886fn truncate_error_body(body: &str) -> String {
887    const MAX: usize = 200;
888    if body.chars().count() <= MAX {
889        body.to_string()
890    } else {
891        let truncated: String = body.chars().take(MAX).collect();
892        format!("{truncated}… (truncated)")
893    }
894}
895
896fn summarize_error_body(status: u16, body: &str) -> String {
897    if should_include_raw_error_body() && !body.trim().is_empty() {
898        return truncate_error_body(body);
899    }
900
901    if let Some(message) = summarize_json_error_body(body) {
902        return message;
903    }
904
905    default_status_message(status)
906}
907
908fn summarize_json_error_body(body: &str) -> Option<String> {
909    let parsed: JiraErrorPayload = serde_json::from_str(body).ok()?;
910    let mut parts = Vec::new();
911
912    if !parsed.error_messages.is_empty() {
913        parts.push(format_error_messages(&parsed.error_messages));
914    }
915
916    if !parsed.errors.is_empty() {
917        let fields = parsed.errors.keys().take(5).cloned().collect::<Vec<_>>();
918        parts.push(format!(
919            "validation errors for fields: {}",
920            fields.join(", ")
921        ));
922    }
923
924    if parts.is_empty() {
925        None
926    } else {
927        Some(parts.join("; "))
928    }
929}
930
931/// Maximum number of Jira `errorMessages` entries to surface inline before
932/// collapsing the remainder into a `(+N more)` suffix.
933const MAX_ERROR_MESSAGES_SHOWN: usize = 3;
934
935/// Maximum character length of each individual message, so a single
936/// pathological Jira response cannot dominate the user-visible error line.
937const MAX_ERROR_MESSAGE_LEN: usize = 240;
938
939fn format_error_messages(messages: &[String]) -> String {
940    let shown: Vec<String> = messages
941        .iter()
942        .take(MAX_ERROR_MESSAGES_SHOWN)
943        .map(|m| truncate_message(m.trim()))
944        .collect();
945    let joined = shown.join(" | ");
946    let remaining = messages.len().saturating_sub(MAX_ERROR_MESSAGES_SHOWN);
947    if remaining > 0 {
948        format!("{joined} (+{remaining} more)")
949    } else {
950        joined
951    }
952}
953
954fn truncate_message(msg: &str) -> String {
955    if msg.chars().count() <= MAX_ERROR_MESSAGE_LEN {
956        msg.to_string()
957    } else {
958        let truncated: String = msg.chars().take(MAX_ERROR_MESSAGE_LEN).collect();
959        format!("{truncated}…")
960    }
961}
962
963fn default_status_message(status: u16) -> String {
964    match status {
965        401 | 403 => "request unauthorized".into(),
966        404 => "resource not found".into(),
967        429 => "rate limited by Jira".into(),
968        400..=499 => format!("request failed with status {status}"),
969        _ => format!("Jira request failed with status {status}"),
970    }
971}
972
973fn should_include_raw_error_body() -> bool {
974    matches!(
975        std::env::var("JIRA_DEBUG_HTTP").ok().as_deref(),
976        Some("1" | "true" | "TRUE" | "yes" | "YES")
977    )
978}
979
980#[derive(Debug, serde::Deserialize)]
981#[serde(rename_all = "camelCase")]
982struct JiraErrorPayload {
983    #[serde(default)]
984    error_messages: Vec<String>,
985    #[serde(default)]
986    errors: BTreeMap<String, String>,
987}
988
989#[cfg(test)]
990mod tests {
991    use super::*;
992
993    #[test]
994    fn percent_encode_spaces_use_percent_20() {
995        assert_eq!(percent_encode("project = FOO"), "project%20%3D%20FOO");
996    }
997
998    #[test]
999    fn percent_encode_complex_jql() {
1000        let jql = r#"project = "MY PROJECT""#;
1001        let encoded = percent_encode(jql);
1002        assert!(encoded.contains("project"));
1003        assert!(!encoded.contains('"'));
1004        assert!(!encoded.contains(' '));
1005    }
1006
1007    #[test]
1008    fn validate_issue_key_valid() {
1009        assert!(validate_issue_key("PROJ-123").is_ok());
1010        assert!(validate_issue_key("ABC-1").is_ok());
1011        assert!(validate_issue_key("MYPROJECT-9999").is_ok());
1012        // Digits are allowed in the project key after the initial letter
1013        assert!(validate_issue_key("ABC2-123").is_ok());
1014        assert!(validate_issue_key("P1-1").is_ok());
1015    }
1016
1017    #[test]
1018    fn validate_issue_key_invalid() {
1019        assert!(validate_issue_key("proj-123").is_err()); // lowercase
1020        assert!(validate_issue_key("PROJ123").is_err()); // no dash
1021        assert!(validate_issue_key("PROJ-abc").is_err()); // non-numeric suffix
1022        assert!(validate_issue_key("../etc/passwd").is_err());
1023        assert!(validate_issue_key("").is_err());
1024        assert!(validate_issue_key("1PROJ-123").is_err()); // starts with digit
1025    }
1026
1027    #[test]
1028    fn truncate_error_body_short() {
1029        let body = "short error";
1030        assert_eq!(truncate_error_body(body), body);
1031    }
1032
1033    #[test]
1034    fn truncate_error_body_long() {
1035        let body = "x".repeat(300);
1036        let result = truncate_error_body(&body);
1037        assert!(result.len() < body.len());
1038        assert!(result.ends_with("(truncated)"));
1039    }
1040
1041    #[test]
1042    fn summarize_json_error_body_surfaces_messages_and_redacts_field_values() {
1043        let body = serde_json::json!({
1044            "errorMessages": ["JQL validation failed"],
1045            "errors": {
1046                "summary": "Summary must not contain secret project name",
1047                "description": "Description cannot include api token"
1048            }
1049        })
1050        .to_string();
1051
1052        let message = summarize_error_body(400, &body);
1053        // errorMessages are server-provided strings, safe to surface in full.
1054        assert!(message.contains("JQL validation failed"));
1055        // `errors` keys (field names) are safe; their values may echo user
1056        // input and must stay redacted.
1057        assert!(message.contains("summary"));
1058        assert!(message.contains("description"));
1059        assert!(!message.contains("secret project name"));
1060        assert!(!message.contains("api token"));
1061    }
1062
1063    #[test]
1064    fn summarize_json_error_body_reports_retired_api() {
1065        // Real payload shape returned by Atlassian after CHANGE-2046.
1066        let body = serde_json::json!({
1067            "errorMessages": [
1068                "The requested API has been removed. Please migrate to the /rest/api/3/search/jql API."
1069            ],
1070            "errors": {}
1071        })
1072        .to_string();
1073
1074        let message = summarize_error_body(410, &body);
1075        assert!(message.contains("The requested API has been removed"));
1076        assert!(message.contains("/rest/api/3/search/jql"));
1077    }
1078
1079    #[test]
1080    fn summarize_json_error_body_joins_multiple_messages() {
1081        let body = serde_json::json!({
1082            "errorMessages": ["first problem", "second problem"],
1083            "errors": {}
1084        })
1085        .to_string();
1086
1087        let message = summarize_error_body(400, &body);
1088        assert!(message.contains("first problem"));
1089        assert!(message.contains("second problem"));
1090        assert!(message.contains(" | "));
1091    }
1092
1093    #[test]
1094    fn summarize_json_error_body_collapses_overflow_messages() {
1095        let body = serde_json::json!({
1096            "errorMessages": ["a", "b", "c", "d", "e"],
1097            "errors": {}
1098        })
1099        .to_string();
1100
1101        let message = summarize_error_body(400, &body);
1102        assert!(message.contains("(+2 more)"));
1103    }
1104
1105    #[test]
1106    fn summarize_json_error_body_truncates_oversized_message() {
1107        let huge = "x".repeat(1000);
1108        let body = serde_json::json!({
1109            "errorMessages": [huge],
1110            "errors": {}
1111        })
1112        .to_string();
1113
1114        let message = summarize_error_body(400, &body);
1115        assert!(message.chars().count() < 500);
1116        assert!(message.contains('…'));
1117    }
1118
1119    #[test]
1120    fn browse_url_preserves_explicit_http_hosts() {
1121        let client = JiraClient::new(
1122            "http://localhost:8080",
1123            "me@example.com",
1124            "token",
1125            AuthType::Basic,
1126            3,
1127        )
1128        .unwrap();
1129        assert_eq!(
1130            client.browse_url("PROJ-1"),
1131            "http://localhost:8080/browse/PROJ-1"
1132        );
1133    }
1134
1135    #[test]
1136    fn new_with_pat_auth_does_not_require_email() {
1137        let client = JiraClient::new(
1138            "https://jira.example.com",
1139            "",
1140            "my-pat-token",
1141            AuthType::Pat,
1142            3,
1143        );
1144        assert!(client.is_ok());
1145    }
1146
1147    #[test]
1148    fn new_with_api_v2_uses_v2_base_url() {
1149        let client = JiraClient::new(
1150            "https://jira.example.com",
1151            "me@example.com",
1152            "token",
1153            AuthType::Basic,
1154            2,
1155        )
1156        .unwrap();
1157        assert_eq!(client.api_version(), 2);
1158    }
1159}