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
31impl JiraClient {
32    pub fn new(
33        host: &str,
34        email: &str,
35        token: &str,
36        auth_type: AuthType,
37        api_version: u8,
38    ) -> Result<Self, ApiError> {
39        // Determine the scheme. An explicit `http://` prefix is preserved as-is
40        // (useful for local testing); everything else defaults to HTTPS.
41        let (scheme, domain) = if host.starts_with("http://") {
42            (
43                "http",
44                host.trim_start_matches("http://").trim_end_matches('/'),
45            )
46        } else {
47            (
48                "https",
49                host.trim_start_matches("https://").trim_end_matches('/'),
50            )
51        };
52
53        if domain.is_empty() {
54            return Err(ApiError::Other("Host cannot be empty".into()));
55        }
56
57        let auth_value = match auth_type {
58            AuthType::Basic => {
59                let credentials = BASE64.encode(format!("{email}:{token}"));
60                format!("Basic {credentials}")
61            }
62            AuthType::Pat => format!("Bearer {token}"),
63        };
64
65        let mut headers = HeaderMap::new();
66        headers.insert(
67            AUTHORIZATION,
68            HeaderValue::from_str(&auth_value).map_err(|e| ApiError::Other(e.to_string()))?,
69        );
70
71        let http = reqwest::Client::builder()
72            .default_headers(headers)
73            .timeout(std::time::Duration::from_secs(30))
74            .build()
75            .map_err(ApiError::Http)?;
76
77        let site_url = format!("{scheme}://{domain}");
78        let base_url = format!("{site_url}/rest/api/{api_version}");
79        let agile_base_url = format!("{site_url}/rest/agile/1.0");
80
81        Ok(Self {
82            http,
83            base_url,
84            agile_base_url,
85            site_url,
86            host: domain.to_string(),
87            api_version,
88        })
89    }
90
91    pub fn host(&self) -> &str {
92        &self.host
93    }
94
95    pub fn api_version(&self) -> u8 {
96        self.api_version
97    }
98
99    pub fn browse_base_url(&self) -> &str {
100        &self.site_url
101    }
102
103    pub fn browse_url(&self, issue_key: &str) -> String {
104        format!("{}/browse/{issue_key}", self.browse_base_url())
105    }
106
107    fn map_status(status: u16, body: String) -> ApiError {
108        let message = summarize_error_body(status, &body);
109        match status {
110            401 | 403 => ApiError::Auth(message),
111            404 => ApiError::NotFound(message),
112            429 => ApiError::RateLimit,
113            _ => ApiError::Api { status, message },
114        }
115    }
116
117    async fn get<T: DeserializeOwned>(&self, path: &str) -> Result<T, ApiError> {
118        let url = format!("{}/{path}", self.base_url);
119        let resp = self.http.get(&url).send().await?;
120        let status = resp.status();
121        if !status.is_success() {
122            let body = resp.text().await.unwrap_or_default();
123            return Err(Self::map_status(status.as_u16(), body));
124        }
125        resp.json::<T>().await.map_err(ApiError::Http)
126    }
127
128    async fn agile_get<T: DeserializeOwned>(&self, path: &str) -> Result<T, ApiError> {
129        let url = format!("{}/{path}", self.agile_base_url);
130        let resp = self.http.get(&url).send().await?;
131        let status = resp.status();
132        if !status.is_success() {
133            let body = resp.text().await.unwrap_or_default();
134            return Err(Self::map_status(status.as_u16(), body));
135        }
136        resp.json::<T>().await.map_err(ApiError::Http)
137    }
138
139    async fn post<T: DeserializeOwned>(
140        &self,
141        path: &str,
142        body: &serde_json::Value,
143    ) -> Result<T, ApiError> {
144        let url = format!("{}/{path}", self.base_url);
145        let resp = self.http.post(&url).json(body).send().await?;
146        let status = resp.status();
147        if !status.is_success() {
148            let body_text = resp.text().await.unwrap_or_default();
149            return Err(Self::map_status(status.as_u16(), body_text));
150        }
151        resp.json::<T>().await.map_err(ApiError::Http)
152    }
153
154    async fn post_empty_response(
155        &self,
156        path: &str,
157        body: &serde_json::Value,
158    ) -> Result<(), ApiError> {
159        let url = format!("{}/{path}", self.base_url);
160        let resp = self.http.post(&url).json(body).send().await?;
161        let status = resp.status();
162        if !status.is_success() {
163            let body_text = resp.text().await.unwrap_or_default();
164            return Err(Self::map_status(status.as_u16(), body_text));
165        }
166        Ok(())
167    }
168
169    async fn put_empty_response(
170        &self,
171        path: &str,
172        body: &serde_json::Value,
173    ) -> Result<(), ApiError> {
174        let url = format!("{}/{path}", self.base_url);
175        let resp = self.http.put(&url).json(body).send().await?;
176        let status = resp.status();
177        if !status.is_success() {
178            let body_text = resp.text().await.unwrap_or_default();
179            return Err(Self::map_status(status.as_u16(), body_text));
180        }
181        Ok(())
182    }
183
184    // ── Issues ────────────────────────────────────────────────────────────────
185
186    /// Search issues using JQL.
187    pub async fn search(
188        &self,
189        jql: &str,
190        max_results: usize,
191        start_at: usize,
192    ) -> Result<SearchResponse, ApiError> {
193        let fields = SEARCH_FIELDS.join(",");
194        let encoded_jql = percent_encode(jql);
195        if encoded_jql.len() <= SEARCH_GET_JQL_LIMIT {
196            let path = format!(
197                "search?jql={encoded_jql}&maxResults={max_results}&startAt={start_at}&fields={fields}"
198            );
199            self.get(&path).await
200        } else {
201            self.post(
202                "search",
203                &serde_json::json!({
204                    "jql": jql,
205                    "maxResults": max_results,
206                    "startAt": start_at,
207                    "fields": SEARCH_FIELDS,
208                }),
209            )
210            .await
211        }
212    }
213
214    /// Fetch a single issue by key (e.g. `PROJ-123`), including all comments.
215    ///
216    /// Jira embeds only the first page of comments in the issue response. When
217    /// the embedded page is incomplete, additional requests are made to fetch
218    /// the remaining comments.
219    pub async fn get_issue(&self, key: &str) -> Result<Issue, ApiError> {
220        validate_issue_key(key)?;
221        let fields = "summary,status,assignee,reporter,priority,issuetype,description,labels,created,updated,comment,issuelinks";
222        let path = format!("issue/{key}?fields={fields}");
223        let mut issue: Issue = self.get(&path).await?;
224
225        // Fetch remaining comment pages if the embedded page is incomplete
226        if let Some(ref mut comment_list) = issue.fields.comment
227            && comment_list.total > comment_list.comments.len()
228        {
229            let mut start_at = comment_list.comments.len();
230            while comment_list.comments.len() < comment_list.total {
231                let page: CommentList = self
232                    .get(&format!(
233                        "issue/{key}/comment?startAt={start_at}&maxResults=100"
234                    ))
235                    .await?;
236                if page.comments.is_empty() {
237                    break;
238                }
239                start_at += page.comments.len();
240                comment_list.comments.extend(page.comments);
241            }
242        }
243
244        Ok(issue)
245    }
246
247    /// Create a new issue.
248    #[allow(clippy::too_many_arguments)]
249    pub async fn create_issue(
250        &self,
251        project_key: &str,
252        issue_type: &str,
253        summary: &str,
254        description: Option<&str>,
255        priority: Option<&str>,
256        labels: Option<&[&str]>,
257        assignee: Option<&str>,
258        custom_fields: &[(String, serde_json::Value)],
259    ) -> Result<CreateIssueResponse, ApiError> {
260        let mut fields = serde_json::json!({
261            "project": { "key": project_key },
262            "issuetype": { "name": issue_type },
263            "summary": summary,
264        });
265
266        if let Some(desc) = description {
267            fields["description"] = self.make_body(desc);
268        }
269        if let Some(p) = priority {
270            fields["priority"] = serde_json::json!({ "name": p });
271        }
272        if let Some(lbls) = labels
273            && !lbls.is_empty()
274        {
275            fields["labels"] = serde_json::json!(lbls);
276        }
277        if let Some(id) = assignee {
278            fields["assignee"] = self.assignee_payload(id);
279        }
280        for (key, value) in custom_fields {
281            fields[key] = value.clone();
282        }
283
284        self.post("issue", &serde_json::json!({ "fields": fields }))
285            .await
286    }
287
288    /// Add a comment to an issue.
289    pub async fn add_comment(&self, key: &str, body: &str) -> Result<Comment, ApiError> {
290        validate_issue_key(key)?;
291        let payload = serde_json::json!({ "body": self.make_body(body) });
292        self.post(&format!("issue/{key}/comment"), &payload).await
293    }
294
295    /// List available transitions for an issue.
296    pub async fn get_transitions(&self, key: &str) -> Result<Vec<Transition>, ApiError> {
297        validate_issue_key(key)?;
298        let resp: TransitionsResponse = self.get(&format!("issue/{key}/transitions")).await?;
299        Ok(resp.transitions)
300    }
301
302    /// Execute a transition by transition ID.
303    pub async fn do_transition(&self, key: &str, transition_id: &str) -> Result<(), ApiError> {
304        validate_issue_key(key)?;
305        let payload = serde_json::json!({ "transition": { "id": transition_id } });
306        self.post_empty_response(&format!("issue/{key}/transitions"), &payload)
307            .await
308    }
309
310    /// Assign an issue to a user, or unassign with `None`.
311    ///
312    /// API v3 (Jira Cloud) identifies users by `accountId`.
313    /// API v2 (Jira Data Center / Server) identifies users by `name` (username).
314    pub async fn assign_issue(&self, key: &str, account_id: Option<&str>) -> Result<(), ApiError> {
315        validate_issue_key(key)?;
316        let payload = match account_id {
317            Some(id) => self.assignee_payload(id),
318            None => {
319                if self.api_version >= 3 {
320                    serde_json::json!({ "accountId": null })
321                } else {
322                    serde_json::json!({ "name": null })
323                }
324            }
325        };
326        self.put_empty_response(&format!("issue/{key}/assignee"), &payload)
327            .await
328    }
329
330    /// Build the assignee payload for the current API version.
331    ///
332    /// API v3 uses `accountId`; API v2 uses `name` (username).
333    fn assignee_payload(&self, id: &str) -> serde_json::Value {
334        if self.api_version >= 3 {
335            serde_json::json!({ "accountId": id })
336        } else {
337            serde_json::json!({ "name": id })
338        }
339    }
340
341    /// Get the currently authenticated user.
342    pub async fn get_myself(&self) -> Result<Myself, ApiError> {
343        self.get("myself").await
344    }
345
346    /// Update issue fields (summary, description, priority, or any custom field).
347    pub async fn update_issue(
348        &self,
349        key: &str,
350        summary: Option<&str>,
351        description: Option<&str>,
352        priority: Option<&str>,
353        custom_fields: &[(String, serde_json::Value)],
354    ) -> Result<(), ApiError> {
355        validate_issue_key(key)?;
356        let mut fields = serde_json::Map::new();
357        if let Some(s) = summary {
358            fields.insert("summary".into(), serde_json::Value::String(s.into()));
359        }
360        if let Some(d) = description {
361            fields.insert("description".into(), self.make_body(d));
362        }
363        if let Some(p) = priority {
364            fields.insert("priority".into(), serde_json::json!({ "name": p }));
365        }
366        for (k, value) in custom_fields {
367            fields.insert(k.clone(), value.clone());
368        }
369        if fields.is_empty() {
370            return Err(ApiError::InvalidInput(
371                "At least one field (--summary, --description, --priority, or --field) is required"
372                    .into(),
373            ));
374        }
375        self.put_empty_response(
376            &format!("issue/{key}"),
377            &serde_json::json!({ "fields": fields }),
378        )
379        .await
380    }
381
382    /// Build the appropriate body value for a description or comment field.
383    ///
384    /// API v3 (Jira Cloud) requires Atlassian Document Format (ADF). API v2
385    /// (Jira Data Center / Server) accepts plain strings.
386    fn make_body(&self, text: &str) -> serde_json::Value {
387        if self.api_version >= 3 {
388            text_to_adf(text)
389        } else {
390            serde_json::Value::String(text.to_string())
391        }
392    }
393
394    // ── Users ─────────────────────────────────────────────────────────────────
395
396    /// Search for users matching a query string.
397    ///
398    /// API v2: uses `username` parameter. API v3: uses `query` parameter.
399    pub async fn search_users(&self, query: &str) -> Result<Vec<User>, ApiError> {
400        let encoded = percent_encode(query);
401        let param = if self.api_version >= 3 {
402            "query"
403        } else {
404            "username"
405        };
406        let path = format!("user/search?{param}={encoded}&maxResults=50");
407        self.get::<Vec<User>>(&path).await
408    }
409
410    // ── Issue links ───────────────────────────────────────────────────────────
411
412    /// List available issue link types.
413    pub async fn get_link_types(&self) -> Result<Vec<IssueLinkType>, ApiError> {
414        #[derive(serde::Deserialize)]
415        struct Wrapper {
416            #[serde(rename = "issueLinkTypes")]
417            types: Vec<IssueLinkType>,
418        }
419        let w: Wrapper = self.get("issueLinkType").await?;
420        Ok(w.types)
421    }
422
423    /// Link two issues.
424    ///
425    /// `link_type` is the name of the link type (e.g. "Blocks", "Duplicate").
426    /// The direction follows the link type's `outward` description:
427    /// `from_key` outward-links to `to_key`.
428    pub async fn link_issues(
429        &self,
430        from_key: &str,
431        to_key: &str,
432        link_type: &str,
433    ) -> Result<(), ApiError> {
434        validate_issue_key(from_key)?;
435        validate_issue_key(to_key)?;
436        let payload = serde_json::json!({
437            "type": { "name": link_type },
438            "inwardIssue": { "key": from_key },
439            "outwardIssue": { "key": to_key },
440        });
441        let url = format!("{}/issueLink", self.base_url);
442        let resp = self.http.post(&url).json(&payload).send().await?;
443        let status = resp.status();
444        if !status.is_success() {
445            let body = resp.text().await.unwrap_or_default();
446            return Err(Self::map_status(status.as_u16(), body));
447        }
448        Ok(())
449    }
450
451    /// Remove an issue link by its ID.
452    pub async fn unlink_issues(&self, link_id: &str) -> Result<(), ApiError> {
453        let url = format!("{}/issueLink/{link_id}", self.base_url);
454        let resp = self.http.delete(&url).send().await?;
455        let status = resp.status();
456        if !status.is_success() {
457            let body = resp.text().await.unwrap_or_default();
458            return Err(Self::map_status(status.as_u16(), body));
459        }
460        Ok(())
461    }
462
463    // ── Boards & Sprints ──────────────────────────────────────────────────────
464
465    /// List all boards, fetching all pages.
466    pub async fn list_boards(&self) -> Result<Vec<Board>, ApiError> {
467        let mut all = Vec::new();
468        let mut start_at = 0usize;
469        const PAGE: usize = 50;
470        loop {
471            let path = format!("board?startAt={start_at}&maxResults={PAGE}");
472            let page: BoardSearchResponse = self.agile_get(&path).await?;
473            let received = page.values.len();
474            all.extend(page.values);
475            if page.is_last || received == 0 {
476                break;
477            }
478            start_at += received;
479        }
480        Ok(all)
481    }
482
483    /// List sprints for a board, optionally filtered by state.
484    ///
485    /// `state` can be "active", "closed", "future", or `None` for all.
486    pub async fn list_sprints(
487        &self,
488        board_id: u64,
489        state: Option<&str>,
490    ) -> Result<Vec<Sprint>, ApiError> {
491        let mut all = Vec::new();
492        let mut start_at = 0usize;
493        const PAGE: usize = 50;
494        loop {
495            let state_param = state.map(|s| format!("&state={s}")).unwrap_or_default();
496            let path = format!(
497                "board/{board_id}/sprint?startAt={start_at}&maxResults={PAGE}{state_param}"
498            );
499            let page: SprintSearchResponse = self.agile_get(&path).await?;
500            let received = page.values.len();
501            all.extend(page.values);
502            if page.is_last || received == 0 {
503                break;
504            }
505            start_at += received;
506        }
507        Ok(all)
508    }
509
510    // ── Projects ──────────────────────────────────────────────────────────────
511
512    /// List all accessible projects.
513    ///
514    /// API v3 (Jira Cloud) uses the paginated `project/search` endpoint.
515    /// API v2 (Jira Data Center / Server) uses the simpler `project` endpoint
516    /// that returns all results in a single flat array.
517    pub async fn list_projects(&self) -> Result<Vec<Project>, ApiError> {
518        if self.api_version < 3 {
519            return self.get::<Vec<Project>>("project").await;
520        }
521
522        let mut all: Vec<Project> = Vec::new();
523        let mut start_at: usize = 0;
524        const PAGE: usize = 50;
525
526        loop {
527            let path = format!("project/search?startAt={start_at}&maxResults={PAGE}&orderBy=key");
528            let page: ProjectSearchResponse = self.get(&path).await?;
529            let page_start = page.start_at;
530            let received = page.values.len();
531            let total = page.total;
532            all.extend(page.values);
533
534            if page.is_last || all.len() >= total {
535                break;
536            }
537
538            if received == 0 {
539                return Err(ApiError::Other(
540                    "Project pagination returned an empty non-terminal page".into(),
541                ));
542            }
543
544            start_at = page_start.saturating_add(received);
545        }
546
547        Ok(all)
548    }
549
550    /// Fetch a single project by key.
551    pub async fn get_project(&self, key: &str) -> Result<Project, ApiError> {
552        self.get(&format!("project/{key}")).await
553    }
554
555    // ── Fields ────────────────────────────────────────────────────────────────
556
557    /// List all available fields (system and custom).
558    pub async fn list_fields(&self) -> Result<Vec<Field>, ApiError> {
559        self.get::<Vec<Field>>("field").await
560    }
561
562    /// Move an issue to a sprint.
563    ///
564    /// Uses the Agile REST API which is version-independent.
565    pub async fn move_issue_to_sprint(
566        &self,
567        issue_key: &str,
568        sprint_id: u64,
569    ) -> Result<(), ApiError> {
570        validate_issue_key(issue_key)?;
571        let url = format!("{}/sprint/{sprint_id}/issue", self.agile_base_url);
572        let payload = serde_json::json!({ "issues": [issue_key] });
573        let resp = self.http.post(&url).json(&payload).send().await?;
574        let status = resp.status();
575        if !status.is_success() {
576            let body = resp.text().await.unwrap_or_default();
577            return Err(Self::map_status(status.as_u16(), body));
578        }
579        Ok(())
580    }
581
582    /// Fetch a single sprint by numeric ID.
583    pub async fn get_sprint(&self, sprint_id: u64) -> Result<Sprint, ApiError> {
584        self.agile_get::<Sprint>(&format!("sprint/{sprint_id}"))
585            .await
586    }
587
588    /// Resolve a sprint specifier to a `Sprint`.
589    ///
590    /// Accepts:
591    /// - A numeric string: fetches the sprint by ID to confirm it exists and get the name
592    /// - `"active"`: returns the first active sprint found across all boards
593    /// - Any other string: matched case-insensitively as a substring of sprint names
594    pub async fn resolve_sprint(&self, specifier: &str) -> Result<Sprint, ApiError> {
595        if let Ok(id) = specifier.parse::<u64>() {
596            return self.get_sprint(id).await;
597        }
598
599        let boards = self.list_boards().await?;
600        if boards.is_empty() {
601            return Err(ApiError::NotFound("No boards found".into()));
602        }
603
604        let target_state = if specifier.eq_ignore_ascii_case("active") {
605            Some("active")
606        } else {
607            None
608        };
609
610        for board in &boards {
611            let sprints = self.list_sprints(board.id, target_state).await?;
612            for sprint in sprints {
613                if specifier.eq_ignore_ascii_case("active") {
614                    if sprint.state == "active" {
615                        return Ok(sprint);
616                    }
617                } else if sprint
618                    .name
619                    .to_lowercase()
620                    .contains(&specifier.to_lowercase())
621                {
622                    return Ok(sprint);
623                }
624            }
625        }
626
627        Err(ApiError::NotFound(format!(
628            "No sprint found matching '{specifier}'"
629        )))
630    }
631
632    /// Resolve a sprint specifier to its numeric ID.
633    ///
634    /// See [`resolve_sprint`] for accepted specifier formats.
635    pub async fn resolve_sprint_id(&self, specifier: &str) -> Result<u64, ApiError> {
636        if let Ok(id) = specifier.parse::<u64>() {
637            return Ok(id);
638        }
639        self.resolve_sprint(specifier).await.map(|s| s.id)
640    }
641}
642
643/// Validate that a key matches the `[A-Z][A-Z0-9]*-[0-9]+` format
644/// before using it in a URL path.
645///
646/// Jira project keys start with an uppercase letter and may contain further
647/// uppercase letters or digits (e.g. `ABC2-123` is valid).
648fn validate_issue_key(key: &str) -> Result<(), ApiError> {
649    let mut parts = key.splitn(2, '-');
650    let project = parts.next().unwrap_or("");
651    let number = parts.next().unwrap_or("");
652
653    let valid = !project.is_empty()
654        && !number.is_empty()
655        && project
656            .chars()
657            .next()
658            .is_some_and(|c| c.is_ascii_uppercase())
659        && project
660            .chars()
661            .all(|c| c.is_ascii_uppercase() || c.is_ascii_digit())
662        && number.chars().all(|c| c.is_ascii_digit());
663
664    if valid {
665        Ok(())
666    } else {
667        Err(ApiError::InvalidInput(format!(
668            "Invalid issue key '{key}'. Expected format: PROJECT-123"
669        )))
670    }
671}
672
673/// Percent-encode a string for use in a URL query parameter.
674///
675/// Uses `%20` for spaces (not `+`) per standard URL encoding.
676fn percent_encode(s: &str) -> String {
677    let mut encoded = String::with_capacity(s.len() * 2);
678    for byte in s.bytes() {
679        match byte {
680            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
681                encoded.push(byte as char)
682            }
683            b => encoded.push_str(&format!("%{b:02X}")),
684        }
685    }
686    encoded
687}
688
689/// Truncate an API error body when explicitly debugging HTTP failures.
690fn truncate_error_body(body: &str) -> String {
691    const MAX: usize = 200;
692    if body.chars().count() <= MAX {
693        body.to_string()
694    } else {
695        let truncated: String = body.chars().take(MAX).collect();
696        format!("{truncated}… (truncated)")
697    }
698}
699
700fn summarize_error_body(status: u16, body: &str) -> String {
701    if should_include_raw_error_body() && !body.trim().is_empty() {
702        return truncate_error_body(body);
703    }
704
705    if let Some(message) = summarize_json_error_body(body) {
706        return message;
707    }
708
709    default_status_message(status)
710}
711
712fn summarize_json_error_body(body: &str) -> Option<String> {
713    let parsed: JiraErrorPayload = serde_json::from_str(body).ok()?;
714    let mut parts = Vec::new();
715
716    if !parsed.error_messages.is_empty() {
717        parts.push(format!(
718            "{} Jira error message(s) returned",
719            parsed.error_messages.len()
720        ));
721    }
722
723    if !parsed.errors.is_empty() {
724        let fields = parsed.errors.keys().take(5).cloned().collect::<Vec<_>>();
725        parts.push(format!(
726            "validation errors for fields: {}",
727            fields.join(", ")
728        ));
729    }
730
731    if parts.is_empty() {
732        None
733    } else {
734        Some(parts.join("; "))
735    }
736}
737
738fn default_status_message(status: u16) -> String {
739    match status {
740        401 | 403 => "request unauthorized".into(),
741        404 => "resource not found".into(),
742        429 => "rate limited by Jira".into(),
743        400..=499 => format!("request failed with status {status}"),
744        _ => format!("Jira request failed with status {status}"),
745    }
746}
747
748fn should_include_raw_error_body() -> bool {
749    matches!(
750        std::env::var("JIRA_DEBUG_HTTP").ok().as_deref(),
751        Some("1" | "true" | "TRUE" | "yes" | "YES")
752    )
753}
754
755#[derive(Debug, serde::Deserialize)]
756#[serde(rename_all = "camelCase")]
757struct JiraErrorPayload {
758    #[serde(default)]
759    error_messages: Vec<String>,
760    #[serde(default)]
761    errors: BTreeMap<String, String>,
762}
763
764#[cfg(test)]
765mod tests {
766    use super::*;
767
768    #[test]
769    fn percent_encode_spaces_use_percent_20() {
770        assert_eq!(percent_encode("project = FOO"), "project%20%3D%20FOO");
771    }
772
773    #[test]
774    fn percent_encode_complex_jql() {
775        let jql = r#"project = "MY PROJECT""#;
776        let encoded = percent_encode(jql);
777        assert!(encoded.contains("project"));
778        assert!(!encoded.contains('"'));
779        assert!(!encoded.contains(' '));
780    }
781
782    #[test]
783    fn validate_issue_key_valid() {
784        assert!(validate_issue_key("PROJ-123").is_ok());
785        assert!(validate_issue_key("ABC-1").is_ok());
786        assert!(validate_issue_key("MYPROJECT-9999").is_ok());
787        // Digits are allowed in the project key after the initial letter
788        assert!(validate_issue_key("ABC2-123").is_ok());
789        assert!(validate_issue_key("P1-1").is_ok());
790    }
791
792    #[test]
793    fn validate_issue_key_invalid() {
794        assert!(validate_issue_key("proj-123").is_err()); // lowercase
795        assert!(validate_issue_key("PROJ123").is_err()); // no dash
796        assert!(validate_issue_key("PROJ-abc").is_err()); // non-numeric suffix
797        assert!(validate_issue_key("../etc/passwd").is_err());
798        assert!(validate_issue_key("").is_err());
799        assert!(validate_issue_key("1PROJ-123").is_err()); // starts with digit
800    }
801
802    #[test]
803    fn truncate_error_body_short() {
804        let body = "short error";
805        assert_eq!(truncate_error_body(body), body);
806    }
807
808    #[test]
809    fn truncate_error_body_long() {
810        let body = "x".repeat(300);
811        let result = truncate_error_body(&body);
812        assert!(result.len() < body.len());
813        assert!(result.ends_with("(truncated)"));
814    }
815
816    #[test]
817    fn summarize_json_error_body_redacts_values() {
818        let body = serde_json::json!({
819            "errorMessages": ["JQL validation failed"],
820            "errors": {
821                "summary": "Summary must not contain secret project name",
822                "description": "Description cannot include api token"
823            }
824        })
825        .to_string();
826
827        let message = summarize_error_body(400, &body);
828        assert!(message.contains("1 Jira error message(s) returned"));
829        assert!(message.contains("summary"));
830        assert!(message.contains("description"));
831        assert!(!message.contains("secret project name"));
832        assert!(!message.contains("api token"));
833    }
834
835    #[test]
836    fn browse_url_preserves_explicit_http_hosts() {
837        let client = JiraClient::new(
838            "http://localhost:8080",
839            "me@example.com",
840            "token",
841            AuthType::Basic,
842            3,
843        )
844        .unwrap();
845        assert_eq!(
846            client.browse_url("PROJ-1"),
847            "http://localhost:8080/browse/PROJ-1"
848        );
849    }
850
851    #[test]
852    fn new_with_pat_auth_does_not_require_email() {
853        let client = JiraClient::new(
854            "https://jira.example.com",
855            "",
856            "my-pat-token",
857            AuthType::Pat,
858            3,
859        );
860        assert!(client.is_ok());
861    }
862
863    #[test]
864    fn new_with_api_v2_uses_v2_base_url() {
865        let client = JiraClient::new(
866            "https://jira.example.com",
867            "me@example.com",
868            "token",
869            AuthType::Basic,
870            2,
871        )
872        .unwrap();
873        assert_eq!(client.api_version(), 2);
874    }
875}