pub struct GitLab { /* private fields */ }Expand description
GitLab API client for retrieving user activity and commit information.
This client handles authentication and data retrieval from GitLab instances, specifically focusing on user events and commit details that can be transformed into task entries for time tracking purposes.
The client is stateless and thread-safe, making it suitable for concurrent operations and long-running applications.
Implementationsยง
Sourceยงimpl GitLab
impl GitLab
Sourcepub fn new(config: &GitLabConfig) -> Self
pub fn new(config: &GitLabConfig) -> Self
Creates a new GitLab API client instance.
Initializes the HTTP client with default settings suitable for GitLab API interactions. The client is configured for JSON responses and includes reasonable timeout settings.
ยงArguments
config- GitLab configuration containing API endpoint and authentication token
ยงExample
let config = GitLabConfig {
access_token: "glpat-xxxxxxxxxxxxxxxxxxxx".to_string(),
api_url: "https://gitlab.example.com".to_string(),
};
let client = GitLab::new(&config);Sourcepub async fn get_user_id(&self) -> Result<u32>
pub async fn get_user_id(&self) -> Result<u32>
Retrieves the current userโs GitLab ID.
Makes a request to GitLabโs /user endpoint to fetch the authenticated userโs
information. The user ID is required for subsequent calls to the events API.
ยงReturns
Result<u32>- The numeric user ID on success
ยงErrors
Returns an error if:
- Network request fails
- Authentication token is invalid
- GitLab returns an unexpected response format
ยงAPI Endpoint
GET /api/v4/user - Requires read_user scope
Sourcepub async fn get_today_commits(&self) -> Result<Vec<CommitInfo>>
pub async fn get_today_commits(&self) -> Result<Vec<CommitInfo>>
Fetches all commits made by the authenticated user today.
This is the primary method for discovering development activity that can be converted into time tracking tasks. It retrieves user events from yesterday to tomorrow (to handle timezone issues) and filters for push events containing commit information.
ยงProcess Flow
- Date Range Calculation: Creates a date range around today to handle timezone differences
- User ID Retrieval: Gets the authenticated userโs ID for events API
- Events Fetching: Retrieves user events within the date range
- Event Filtering: Processes only โpushed toโ events with commit data
- Commit Details: Fetches detailed commit information for each push
- Message Processing: Extracts and cleans commit messages for task names
ยงError Resilience
This method is designed to be fault-tolerant in production environments:
- Network failures return empty results instead of errors
- Individual commit fetch failures are skipped
- API parsing errors are logged and donโt interrupt processing
- Missing or malformed data is handled gracefully
ยงReturns
Result<Vec<CommitInfo>>- List of todayโs commits, or empty vector on any error
ยงExample
let commits = gitlab_client.get_today_commits().await?;
for commit in commits {
println!("Commit {}: {}", commit.sha, commit.message);
}