glim-tui 0.2.0

A TUI for monitoring GitLab CI/CD pipelines and projects
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
//! Core HTTP client for GitLab API

use std::sync::RwLock;

use chrono::Local;
use compact_str::{format_compact, CompactString};
use reqwest::{Client, RequestBuilder, Response};
use serde::Deserialize;
use tracing::{debug, instrument, warn};

use super::{
    config::{ClientConfig, PipelineQuery, ProjectQuery},
    error::{ClientError, Result},
};
use crate::{
    domain::{JobDto, PipelineDto, ProjectDto},
    id::{JobId, PipelineId, ProjectId},
};

/// Pure HTTP client for GitLab API
#[derive(Debug)]
pub struct GitlabApi {
    client: RwLock<Client>,
    config: RwLock<ClientConfig>,
}

/// GitLab API error response formats
#[derive(Debug, Deserialize)]
struct GitlabApiError {
    error: CompactString,
    error_description: Option<CompactString>,
}

#[derive(Debug, Deserialize)]
struct GitlabApiError2 {
    message: CompactString,
}

impl GitlabApi {
    pub fn force_new(config: ClientConfig) -> Result<Self> {
        let client = Client::builder()
            .timeout(config.request.timeout)
            .build()
            .map_err(ClientError::Http)?;

        Ok(Self {
            client: RwLock::new(client),
            config: RwLock::new(config),
        })
    }

    /// Get projects from GitLab API
    #[instrument(skip(self), fields(per_page = %query.per_page))]
    pub async fn get_projects(&self, query: &ProjectQuery) -> Result<Vec<ProjectDto>> {
        let url = self.build_projects_url(query);
        self.get_json(&url).await
    }

    /// Get pipelines for a project
    #[instrument(skip(self), fields(project_id = %project_id, per_page = %query.per_page))]
    pub async fn get_pipelines(
        &self,
        project_id: ProjectId,
        query: &PipelineQuery,
    ) -> Result<Vec<PipelineDto>> {
        let url = self.build_pipelines_url(project_id, query);
        self.get_json(&url).await
    }

    /// Get jobs for a pipeline
    #[instrument(skip(self), fields(project_id = %project_id, pipeline_id = %pipeline_id))]
    pub async fn get_jobs(
        &self,
        project_id: ProjectId,
        pipeline_id: PipelineId,
    ) -> Result<Vec<JobDto>> {
        let base_url = {
            let config = self.config.read().unwrap();
            format_compact!(
                "{}/projects/{}/pipelines/{}",
                config.base_url,
                project_id,
                pipeline_id
            )
        };

        // Fetch both regular jobs and trigger jobs concurrently
        let jobs_url = format_compact!("{}/jobs", base_url);
        let bridges_url = format_compact!("{}/bridges", base_url);

        let (jobs_result, bridges_result) = tokio::try_join!(
            self.get_json::<Vec<JobDto>>(&jobs_url),
            self.get_json::<Vec<JobDto>>(&bridges_url)
        )?;

        // Combine and sort by ID
        let mut all_jobs = jobs_result;
        all_jobs.extend(bridges_result);
        all_jobs.sort_by_key(|job| job.id);

        debug!(job_count = all_jobs.len(), "Successfully fetched jobs");
        Ok(all_jobs)
    }

    /// Get job trace/log
    #[instrument(skip(self), fields(project_id = %project_id, job_id = %job_id))]
    pub async fn get_job_trace(
        &self,
        project_id: ProjectId,
        job_id: JobId,
    ) -> Result<CompactString> {
        let url = {
            let config = self.config.read().unwrap();
            format_compact!(
                "{}/projects/{}/jobs/{}/trace",
                config.base_url,
                project_id,
                job_id
            )
        };

        let response = self.authenticated_request(&url).send().await?;
        let body = response.text().await?;
        Ok(body.into())
    }

    /// Update configuration
    pub fn update_config(&self, config: ClientConfig) -> Result<()> {
        config.validate()?;

        // Create new client with updated timeout
        let client = Client::builder()
            .timeout(config.request.timeout)
            .build()
            .map_err(ClientError::Http)?;

        // Update both config and client atomically
        *self.config.write().unwrap() = config;
        *self.client.write().unwrap() = client;

        Ok(())
    }

    /// Get current configuration
    pub fn config(&self) -> ClientConfig {
        self.config.read().unwrap().clone()
    }

    pub fn is_configured(&self) -> bool {
        self.config
            .read()
            .map(|c| c.validate().is_ok())
            .unwrap_or(false)
    }

    // Private helper methods

    /// Perform authenticated GET request and deserialize JSON response
    async fn get_json<T>(&self, url: &str) -> Result<T>
    where
        T: for<'de> Deserialize<'de>,
    {
        let response = self.authenticated_request(url).send().await?;
        self.handle_response(response).await
    }

    /// Create authenticated request builder
    fn authenticated_request(&self, url: &str) -> RequestBuilder {
        let client = self.client.read().unwrap();
        let private_token = self.config.read().unwrap().private_token.clone();
        client
            .get(url)
            .header("PRIVATE-TOKEN", private_token.as_str())
    }

    /// Handle HTTP response and deserialize JSON
    async fn handle_response<T>(&self, response: Response) -> Result<T>
    where
        T: for<'de> Deserialize<'de>,
    {
        let url_path = response.url().path().to_string();
        let status = response.status();
        let body = response.text().await?;

        // Log response if debug is enabled
        {
            let config = self.config.read().unwrap();
            if config.debug.log_responses {
                self.log_response_to_file(&url_path, &body, &config);
            }
        }

        if status.is_success() {
            serde_json::from_str(&body)
                .map_err(|e| ClientError::json_parse(url_path, "Failed to parse response", e))
        } else {
            self.handle_error_response(status.as_u16(), &body)
        }
    }

    /// Handle error responses from GitLab API
    fn handle_error_response<T>(&self, status: u16, body: &str) -> Result<T> {
        match status {
            401 => {
                // Try to parse GitLab API error to distinguish between invalid and expired tokens
                if let Ok(api_error) = serde_json::from_str::<GitlabApiError>(body) {
                    match api_error.error.as_str() {
                        "invalid_token" => Err(ClientError::InvalidToken),
                        "expired_token" => Err(ClientError::ExpiredToken),
                        _ => {
                            // Check error description for expiration indicators
                            if let Some(description) = &api_error.error_description {
                                if description.contains("expired") || description.contains("expiry")
                                {
                                    return Err(ClientError::ExpiredToken);
                                }
                            }
                            Err(ClientError::Authentication)
                        },
                    }
                } else {
                    // Fallback to generic authentication error
                    Err(ClientError::Authentication)
                }
            },
            404 => Err(ClientError::not_found("Resource")),
            429 => {
                // Try to extract retry-after header info if available
                Err(ClientError::rate_limit(None))
            },
            _ => {
                // Try to parse GitLab API error formats
                if let Ok(api_error) = serde_json::from_str::<GitlabApiError>(body) {
                    Err(ClientError::gitlab_api(format_compact!(
                        "HTTP {}: {} {}",
                        status,
                        api_error.error,
                        api_error.error_description.unwrap_or_default()
                    )))
                } else if let Ok(api_error2) = serde_json::from_str::<GitlabApiError2>(body) {
                    Err(ClientError::gitlab_api(format_compact!(
                        "HTTP {}: {}",
                        status,
                        api_error2.message
                    )))
                } else {
                    Err(ClientError::gitlab_api(format_compact!(
                        "HTTP {}: {}",
                        status,
                        body
                    )))
                }
            },
        }
    }

    /// Build URL for projects endpoint
    fn build_projects_url(&self, query: &ProjectQuery) -> CompactString {
        let config = self.config.read().unwrap();
        let mut url = format_compact!("{}/projects?", config.base_url);

        // Add query parameters
        url.push_str("search_namespaces=true");

        if let Some(filter) = &query.search_filter {
            url.push_str(&format_compact!("&search={}", filter));
        }

        if let Some(updated_after) = query.updated_after {
            url.push_str(&format_compact!(
                "&last_activity_after={}",
                updated_after.to_rfc3339()
            ));
        }

        if query.include_statistics {
            url.push_str("&statistics=true");
        }

        if !query.archived {
            url.push_str("&archived=false");
        }

        if query.membership {
            url.push_str("&membership=true");
        }

        url.push_str(&format_compact!("&per_page={}", query.per_page));

        url
    }

    /// Build URL for pipelines endpoint
    fn build_pipelines_url(&self, project_id: ProjectId, query: &PipelineQuery) -> CompactString {
        let config = self.config.read().unwrap();
        let mut url = format_compact!(
            "{}/projects/{}/pipelines?per_page={}",
            config.base_url,
            project_id,
            query.per_page
        );

        if let Some(updated_after) = query.updated_after {
            url.push_str(&format_compact!(
                "&updated_after={}",
                updated_after.to_rfc3339()
            ));
        }

        url
    }

    /// Log HTTP response to file for debugging
    fn log_response_to_file(&self, path: &str, body: &str, config: &ClientConfig) {
        if let Some(log_dir) = &config.debug.log_directory {
            if !log_dir.exists() {
                if let Err(e) = std::fs::create_dir_all(log_dir) {
                    warn!("Failed to create log directory: {}", e);
                    return;
                }
            }

            let filename = format!(
                "{}_{}.json",
                Local::now().format("%Y-%m-%d_%H-%M-%S"),
                path.replace('/', "_")
            );

            let log_path = log_dir.join(filename);

            if let Err(e) = std::fs::write(&log_path, body) {
                warn!("Failed to write response log to {:?}: {}", log_path, e);
            } else {
                debug!("Response logged to {:?}", log_path);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use chrono::{DateTime, Utc};

    use super::*;

    impl GitlabApi {
        /// Create a new GitLab API client
        pub fn new(config: ClientConfig) -> Result<Self> {
            config.validate()?;

            let client = Client::builder()
                .timeout(config.request.timeout)
                .build()
                .map_err(ClientError::Http)?;

            Ok(Self {
                client: RwLock::new(client),
                config: RwLock::new(config),
            })
        }
    }

    fn test_config() -> ClientConfig {
        ClientConfig::new("https://gitlab.example.com", "test-token")
    }

    #[test]
    fn test_api_creation() {
        let config = test_config();
        let api = GitlabApi::new(config.clone());
        assert!(api.is_ok());

        let api = api.unwrap();
        assert_eq!(api.config().base_url, config.base_url);
        assert_eq!(api.config().private_token, config.private_token);
    }

    #[test]
    fn test_api_creation_invalid_config() {
        let config = ClientConfig::new("", "test-token");
        let api = GitlabApi::new(config);
        assert!(api.is_err());
    }

    #[test]
    fn test_config_update() {
        let config = test_config();
        let api = GitlabApi::new(config).unwrap();

        let new_config = ClientConfig::new("https://gitlab.new.com", "new-token");
        assert!(api.update_config(new_config.clone()).is_ok());

        let updated_config = api.config();
        assert_eq!(updated_config.base_url, new_config.base_url);
        assert_eq!(updated_config.private_token, new_config.private_token);
    }

    #[test]
    fn test_build_projects_url() {
        let config = test_config().with_search_filter(Some("frontend".into()));
        let api = GitlabApi::new(config).unwrap();

        let mut query = ProjectQuery::default()
            .with_search_filter(Some("frontend".into()))
            .with_per_page(50);
        query.include_statistics = true;
        query.membership = true;
        query.search_namespaces = true;

        let url = api.build_projects_url(&query);

        assert!(url.contains("https://gitlab.example.com/projects?"));
        assert!(url.contains("search_namespaces=true"));
        assert!(url.contains("search=frontend"));
        assert!(url.contains("per_page=50"));
        assert!(url.contains("statistics=true"));
        assert!(url.contains("archived=false"));
        assert!(url.contains("membership=true"));
    }

    #[test]
    fn test_build_pipelines_url() {
        let config = test_config();
        let api = GitlabApi::new(config).unwrap();

        let project_id = ProjectId::new(123);
        let query = PipelineQuery::new().with_per_page(60);

        let url = api.build_pipelines_url(project_id, &query);

        assert_eq!(
            url,
            "https://gitlab.example.com/projects/123/pipelines?per_page=60"
        );
    }

    #[test]
    fn test_build_pipelines_url_with_date() {
        let config = test_config();
        let api = GitlabApi::new(config).unwrap();

        let project_id = ProjectId::new(123);
        let updated_after = DateTime::parse_from_rfc3339("2023-01-01T00:00:00Z")
            .unwrap()
            .with_timezone(&Utc);

        let query = PipelineQuery::new()
            .with_per_page(60)
            .with_updated_after(Some(updated_after));

        let url = api.build_pipelines_url(project_id, &query);

        assert!(url.contains("updated_after=2023-01-01T00:00:00"));
    }

    #[test]
    fn test_error_handling() {
        let api = GitlabApi::new(test_config()).unwrap();

        // Test authentication error
        let error = api.handle_error_response::<()>(401, "");
        assert!(matches!(error, Err(ClientError::Authentication)));

        // Test not found error
        let error = api.handle_error_response::<()>(404, "");
        assert!(matches!(error, Err(ClientError::NotFound { .. })));

        // Test rate limit error
        let error = api.handle_error_response::<()>(429, "");
        assert!(matches!(error, Err(ClientError::RateLimit { .. })));
    }
}