Skip to main content

Jira

Struct Jira 

Source
pub struct Jira { /* private fields */ }
Expand description

Jira API client with session management capabilities.

This client handles authentication, session caching, and issue retrieval from Jira instances. It implements the Session trait for automatic credential management and retry logic.

ยงThread Safety

The client is not thread-safe due to mutable retry state. Each thread should use its own client instance for concurrent operations.

ยงSession Lifecycle

  1. Initialization: Client created with configuration
  2. Authentication: Credentials prompted when first needed
  3. Session Caching: Successful sessions stored for reuse
  4. Automatic Retry: Expired sessions trigger re-authentication
  5. Error Handling: Persistent failures return empty results

Implementationsยง

Sourceยง

impl Jira

Source

pub fn new(config: &JiraConfig) -> Self

Creates a new Jira API client instance.

Initializes the HTTP client with default settings suitable for Jira API interactions. The client is configured for JSON requests and includes appropriate timeout and connection settings.

ยงArguments
  • config - Configuration containing Jira URL and login information
ยงExamples
use kasl::api::{Jira, JiraConfig};

let config = JiraConfig {
    login: "username".to_string(),
    api_url: "https://jira.company.com".to_string(),
};
let jira = Jira::new(&config);
Source

pub async fn get_completed_issues( &mut self, date: &NaiveDate, ) -> Result<Vec<JiraIssue>>

Retrieves all issues completed by the current user on a specific date.

This method performs a sophisticated issue search using JQL (Jira Query Language) to find issues that were marked as completed on the specified date. The search includes robust error handling and automatic session management with retry logic.

ยงJQL Query Details

The search uses the following criteria:

  • Status Filter: Issues with status โ€œDoneโ€ or โ€œะ ะตัˆะตะฝะฐโ€ (supports localized Jira)
  • Resolution Date: Issues resolved within the full day range (00:00 to 23:59)
  • Assignee Filter: Only issues assigned to the current user (currentUser())
ยงSession Management

The method implements sophisticated session handling:

  1. Session Retrieval: Get or create a valid session token using the Session trait
  2. API Request: Execute the JQL search with session cookie authentication
  3. Error Handling: Detect HTTP 401 (Unauthorized) responses indicating expired sessions
  4. Automatic Retry: Clear cached session and retry authentication up to the limit
  5. Graceful Degradation: Return empty results on persistent authentication failures
ยงError Recovery Strategy

Unlike other API integrations, Jira errors are allowed to propagate rather than returning empty results silently. This is because Jira data is typically more critical for work tracking, and users should be aware of connection issues.

However, authentication failures are handled gracefully with automatic retry logic and eventual fallback to empty results after exhausting retries.

ยงDate Handling

The method formats the provided date to ensure proper JQL syntax and covers the entire day from midnight to 23:59 to capture all possible resolution times within the target date.

ยงArguments
  • date - The date to search for completed issues (in any timezone)
ยงReturns

Returns a vector of JiraIssue objects representing completed work. Returns an empty vector if:

  • No issues are found matching the criteria
  • Authentication fails persistently after all retries
  • Network errors occur during the request
ยงErrors

May return errors for:

  • JSON parsing failures in API responses
  • Unexpected HTTP response formats
  • Session token formatting errors

Network errors and authentication failures are handled gracefully and result in empty results rather than propagated errors.

ยงExamples
let config = JiraConfig {
    login: "username".to_string(),
    api_url: "https://jira.company.com".to_string(),
};
let mut jira = Jira::new(&config);

let today = chrono::Local::now().date_naive();
let issues = jira.get_completed_issues(&today).await?;

for issue in issues {
    println!("Completed: {} - {}", issue.key, issue.fields.summary);
}

Trait Implementationsยง

Sourceยง

impl Debug for Jira

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Sourceยง

impl Session for Jira

Sourceยง

async fn login(&self) -> Result<String>

Performs session-based authentication with Jira.

This method implements Jiraโ€™s session authentication flow using the REST API. It sends user credentials to the authentication endpoint and receives a session cookie that can be used for subsequent requests.

ยงAuthentication Process
  1. Credential Validation: Ensures credentials are set before proceeding
  2. HTTP Request: POST to the session authentication endpoint with JSON credentials
  3. Response Validation: Checks for successful HTTP status codes
  4. Cookie Extraction: Parses session information from the response
  5. Format Preparation: Creates properly formatted cookie string for headers

The returned session ID is formatted as {cookie_name}={cookie_value} and should be included in the Cookie header of subsequent API requests.

ยงReturns

Returns a formatted session cookie string on successful authentication.

ยงErrors

Returns an error if:

  • No credentials have been set (programming error)
  • HTTP request fails due to network issues
  • Credentials are invalid (401 response)
  • Jira returns an unexpected response format
  • Session parsing fails
Sourceยง

fn set_credentials(&mut self, password: &str) -> Result<()>

Sets user credentials for Jira authentication.

Stores the provided username and password in memory for use during the authentication process. This method is called by the session management system when credentials are needed.

ยงSecurity Notes
  • Credentials are only stored in memory temporarily
  • Password is stored in plain text for authentication
  • No persistence to disk or configuration files
  • Credentials are cleared after successful authentication
ยงArguments
  • password - The userโ€™s Jira password in plain text
ยงReturns

Always returns Ok(()) as this operation cannot fail.

Sourceยง

fn session_id_file(&self) -> &str

Returns the filename for storing Jira session tokens.

The session file is stored in the userโ€™s application data directory and contains the cached session token for automatic login restoration.

Sourceยง

fn secret(&self) -> Secret

Returns a configured Secret instance for secure password prompting.

The Secret manager handles secure password input with hidden characters and optional encrypted caching in the userโ€™s data directory.

ยงReturns

A configured Secret instance with Jira-specific prompts and file names.

Sourceยง

fn retry(&self) -> i32

Returns the current authentication retry count.

Used by the session management system to track failed authentication attempts and implement retry limits.

Sourceยง

fn inc_retry(&mut self)

Increments the authentication retry counter.

Called after each failed authentication attempt to track progress toward the maximum retry limit defined in the session management system.

Sourceยง

fn reset_retry(&mut self)

Resets the authentication retry counter to zero.

Called after successful authentication to ensure future session requests start with a clean slate.

Sourceยง

async fn get_session_id(&mut self) -> Result<String>

Retrieves or establishes a valid session ID. Read more
Sourceยง

fn read_session_id(file_name: &str) -> Result<String>

Reads a session ID from the specified file. Read more
Sourceยง

fn write_session_id(file_name: &str, session_id: &str) -> Result<()>

Writes a session ID to the specified file. Read more
Sourceยง

fn delete_session_id(&self) -> Result<()>

Deletes the cached session file. Read more

Auto Trait Implementationsยง

Blanket Implementationsยง

Sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T> Instrument for T

Sourceยง

fn instrument(self, span: Span) -> Instrumented<Self> โ“˜

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Sourceยง

fn in_current_span(self) -> Instrumented<Self> โ“˜

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Sourceยง

impl<T> PolicyExt for T
where T: ?Sized,

Sourceยง

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Sourceยง

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Sourceยง

impl<T> Same for T

Sourceยง

type Output = T

Should always be Self
Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
Sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Sourceยง

impl<T> WithSubscriber for T

Sourceยง

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> โ“˜
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Sourceยง

fn with_current_subscriber(self) -> WithDispatch<Self> โ“˜

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more