commit-bridge 0.1.0

Seamless workflow dispatch for remote git dependencies.
Documentation
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! SQLite implementation of the repository.

use crate::domain::{BranchName, EventType, RepoUrl, TargetRepo};
use crate::model::{
    Branch, CreateSubscription, Subscription, SubscriptionWithBranch, TriggerQueueItem,
    UpdateSubscription,
};
use crate::repository::{
    RepositoryError,
    branch::BranchRepository,
    subscription::SubscriptionRepository,
    trigger::{TriggerRepository, UpdateRetryStatus},
};
use async_trait::async_trait;
use futures::future::BoxFuture;
use sqlx::{SqliteConnection, SqlitePool};

#[derive(Debug)]
/// Access point of the repository using a SQLite connection pool.
pub struct SqliteRepository {
    /// The SQLite connection pool to the database.
    pool: SqlitePool,
}

impl SqliteRepository {
    /// Creates a new [`SqliteRepository`] from a [`SqlitePool`].
    pub fn new(pool: SqlitePool) -> Self {
        Self { pool }
    }

    /// Returns the stored [`SqlitePool`].
    pub fn get_pool(&self) -> &SqlitePool {
        &self.pool
    }

    /// Runs a closure within a transaction.
    pub async fn run_in_transaction<'a, F, T, E>(&self, f: F) -> Result<T, E>
    where
        F: for<'b> FnOnce(&'b mut SqliteConnection) -> BoxFuture<'b, Result<T, E>> + Send + 'a,
        E: From<sqlx::Error> + Send + 'a,
        T: Send + 'a,
    {
        let mut tx = self.pool.begin().await?;
        let result = f(&mut tx).await?;
        tx.commit().await?;
        Ok(result)
    }
}

#[async_trait]
impl BranchRepository for SqliteRepository {
    async fn get_all(&self) -> Result<Vec<Branch>, RepositoryError> {
        sqlx::query_as::<_, Branch>("SELECT * FROM branches")
            .fetch_all(&self.pool)
            .await
            .map_err(RepositoryError::Database)
    }

    async fn find_by_id(&self, id: i64) -> Result<Option<Branch>, RepositoryError> {
        sqlx::query_as::<_, Branch>("SELECT * FROM branches WHERE id = ?")
            .bind(id)
            .fetch_optional(&self.pool)
            .await
            .map_err(RepositoryError::Database)
    }

    async fn delete_by_id(&self, id: i64) -> Result<(), RepositoryError> {
        let result = sqlx::query!("DELETE FROM branches WHERE id = ?", id)
            .execute(&self.pool)
            .await
            .map_err(RepositoryError::Database)?;

        if result.rows_affected() == 0 {
            return Err(RepositoryError::NotFound);
        }
        Ok(())
    }

    async fn update_last_commit_hash(
        &self,
        id: i64,
        hash: &crate::domain::CommitHash,
    ) -> Result<(), RepositoryError> {
        sqlx::query!(
            "UPDATE branches SET last_commit_hash = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?",
            hash,
            id
        )
        .execute(&self.pool)
        .await
        .map_err(RepositoryError::Database)?;
        Ok(())
    }

    async fn update_last_commit_hash_in_tx(
        &self,
        id: i64,
        hash: &crate::domain::CommitHash,
        tx: &mut sqlx::SqliteConnection,
    ) -> Result<(), RepositoryError> {
        sqlx::query!(
            "UPDATE branches SET last_commit_hash = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?",
            hash,
            id
        )
        .execute(tx)
        .await
        .map_err(RepositoryError::Database)?;
        Ok(())
    }
}

#[async_trait]
impl SubscriptionRepository for SqliteRepository {
    async fn create(
        &self,
        subscription_payload: &CreateSubscription,
    ) -> Result<SubscriptionWithBranch, RepositoryError> {
        let mut transaction = self.pool.begin().await.map_err(RepositoryError::Database)?;

        let branch_id = sqlx::query_scalar::<_, i64>(
            "INSERT INTO branches (repo_url, name) VALUES (?, ?) \
             ON CONFLICT(repo_url, name) DO UPDATE SET repo_url=excluded.repo_url \
             RETURNING id",
        )
        .bind(&subscription_payload.source_repo_url)
        .bind(&subscription_payload.source_branch_name)
        .fetch_one(&mut *transaction)
        .await
        .map_err(RepositoryError::Database)?;

        let subscription = sqlx::query_as::<_, Subscription>(
            "INSERT INTO subscriptions (branch_id, target_repo, event_type, gh_app_installation_id) VALUES (?, ?, ?, ?) RETURNING *",
        )
        .bind(branch_id)
        .bind(&subscription_payload.target_repo)
        .bind(&subscription_payload.event_type)
        .bind(subscription_payload.gh_app_installation_id)
        .fetch_one(&mut *transaction)
        .await
        .map_err(RepositoryError::Database)?;

        transaction
            .commit()
            .await
            .map_err(RepositoryError::Database)?;

        Ok(SubscriptionWithBranch {
            subscription,
            source_branch: crate::model::SourceBranchInfo {
                repo_url: subscription_payload.source_repo_url.clone(),
                name: subscription_payload.source_branch_name.clone(),
            },
        })
    }

    async fn get_by_id(&self, id: i64) -> Result<Option<Subscription>, RepositoryError> {
        sqlx::query_as::<_, Subscription>("SELECT * FROM subscriptions WHERE id = ?")
            .bind(id)
            .fetch_optional(&self.pool)
            .await
            .map_err(RepositoryError::Database)
    }

    async fn get_by_id_with_branch(
        &self,
        id: i64,
    ) -> Result<Option<SubscriptionWithBranch>, RepositoryError> {
        let row = sqlx::query!(
            "SELECT s.*, b.repo_url as branch_repo_url, b.name as branch_name \
             FROM subscriptions s \
             JOIN branches b ON s.branch_id = b.id \
             WHERE s.id = ?",
            id
        )
        .fetch_optional(&self.pool)
        .await
        .map_err(RepositoryError::Database)?;

        match row {
            Some(row) => Ok(Some(SubscriptionWithBranch {
                subscription: Subscription {
                    id: row.id,
                    branch_id: row.branch_id,
                    target_repo: crate::domain::TargetRepo::new(row.target_repo)
                        .map_err(|e| RepositoryError::Mapping(e.to_string()))?,
                    event_type: crate::domain::EventType::new(row.event_type)
                        .map_err(|e| RepositoryError::Mapping(e.to_string()))?,
                    gh_app_installation_id: row.gh_app_installation_id,
                    created_at: row.created_at.and_utc(),
                    updated_at: row.updated_at.and_utc(),
                },
                source_branch: crate::model::SourceBranchInfo {
                    repo_url: crate::domain::RepoUrl::new(row.branch_repo_url)
                        .map_err(|e| RepositoryError::Mapping(e.to_string()))?,
                    name: crate::domain::BranchName::new(row.branch_name)
                        .map_err(|e| RepositoryError::Mapping(e.to_string()))?,
                },
            })),
            None => Ok(None),
        }
    }

    async fn get_by_keys_with_branch(
        &self,
        branch_id: i64,
        target_repo: &TargetRepo,
        event_type: &EventType,
    ) -> Result<Option<SubscriptionWithBranch>, RepositoryError> {
        let row = sqlx::query!(
            "SELECT s.*, b.repo_url as branch_repo_url, b.name as branch_name \
             FROM subscriptions s \
             JOIN branches b ON s.branch_id = b.id \
             WHERE s.branch_id = ? AND s.target_repo = ? AND s.event_type = ?",
            branch_id,
            target_repo,
            event_type
        )
        .fetch_optional(&self.pool)
        .await
        .map_err(RepositoryError::Database)?;

        match row {
            Some(row) => Ok(Some(SubscriptionWithBranch {
                subscription: Subscription {
                    id: row.id,
                    branch_id: row.branch_id,
                    target_repo: crate::domain::TargetRepo::new(row.target_repo)
                        .map_err(|e| RepositoryError::Mapping(e.to_string()))?,
                    event_type: crate::domain::EventType::new(row.event_type)
                        .map_err(|e| RepositoryError::Mapping(e.to_string()))?,
                    gh_app_installation_id: row.gh_app_installation_id,
                    created_at: row.created_at.and_utc(),
                    updated_at: row.updated_at.and_utc(),
                },
                source_branch: crate::model::SourceBranchInfo {
                    repo_url: crate::domain::RepoUrl::new(row.branch_repo_url)
                        .map_err(|e| RepositoryError::Mapping(e.to_string()))?,
                    name: crate::domain::BranchName::new(row.branch_name)
                        .map_err(|e| RepositoryError::Mapping(e.to_string()))?,
                },
            })),
            None => Ok(None),
        }
    }

    async fn list_paginated(
        &self,
        last_id: i64,
        limit: i64,
    ) -> Result<Vec<Subscription>, RepositoryError> {
        sqlx::query_as::<_, Subscription>(
            "SELECT * FROM subscriptions WHERE id > ? ORDER BY id ASC LIMIT ?",
        )
        .bind(last_id)
        .bind(limit)
        .fetch_all(&self.pool)
        .await
        .map_err(RepositoryError::Database)
    }

    async fn list_paginated_with_branches(
        &self,
        last_id: i64,
        limit: i64,
    ) -> Result<Vec<SubscriptionWithBranch>, RepositoryError> {
        let rows = sqlx::query!(
            "SELECT s.*, b.repo_url as branch_repo_url, b.name as branch_name \
             FROM subscriptions s \
             JOIN branches b ON s.branch_id = b.id \
             WHERE s.id > ? ORDER BY s.id ASC LIMIT ?",
            last_id,
            limit
        )
        .fetch_all(&self.pool)
        .await
        .map_err(RepositoryError::Database)?;

        let subscriptions: Result<Vec<SubscriptionWithBranch>, RepositoryError> = rows
            .into_iter()
            .map(|row| {
                Ok(SubscriptionWithBranch {
                    subscription: Subscription {
                        id: row.id,
                        branch_id: row.branch_id,
                        target_repo: TargetRepo::new(row.target_repo)
                            .map_err(|e| RepositoryError::Mapping(e.to_string()))?,
                        event_type: EventType::new(row.event_type)
                            .map_err(|e| RepositoryError::Mapping(e.to_string()))?,
                        gh_app_installation_id: row.gh_app_installation_id,
                        created_at: row.created_at.and_utc(),
                        updated_at: row.updated_at.and_utc(),
                    },
                    source_branch: crate::model::SourceBranchInfo {
                        repo_url: RepoUrl::new(row.branch_repo_url)
                            .map_err(|e| RepositoryError::Mapping(e.to_string()))?,
                        name: BranchName::new(row.branch_name)
                            .map_err(|e| RepositoryError::Mapping(e.to_string()))?,
                    },
                })
            })
            .collect();
        subscriptions
    }

    async fn count_remaining(&self, last_id: i64) -> Result<i64, RepositoryError> {
        sqlx::query_scalar!("SELECT COUNT(*) FROM subscriptions WHERE id > ?", last_id)
            .fetch_one(&self.pool)
            .await
            .map_err(RepositoryError::Database)
    }

    async fn update(
        &self,
        id: i64,
        subscription: &UpdateSubscription,
    ) -> Result<Subscription, RepositoryError> {
        let mut query_builder = sqlx::QueryBuilder::new("UPDATE subscriptions SET ");
        let mut separated = query_builder.separated(", ");

        if let Some(target_repo) = &subscription.target_repo {
            separated
                .push("target_repo = ")
                .push_bind_unseparated(target_repo);
        }
        if let Some(event_type) = &subscription.event_type {
            separated
                .push("event_type = ")
                .push_bind_unseparated(event_type);
        }
        if let Some(gh_app_installation_id) = subscription.gh_app_installation_id {
            separated
                .push("gh_app_installation_id = ")
                .push_bind_unseparated(gh_app_installation_id);
        }

        separated.push("updated_at = CURRENT_TIMESTAMP");

        query_builder.push(" WHERE id = ");
        query_builder.push_bind(id);
        query_builder.push(" RETURNING *");

        query_builder
            .build_query_as::<Subscription>()
            .fetch_optional(&self.pool)
            .await
            .map_err(RepositoryError::Database)?
            .ok_or(RepositoryError::NotFound)
    }

    async fn delete(&self, id: i64) -> Result<(), RepositoryError> {
        let result = sqlx::query!("DELETE FROM subscriptions WHERE id = ?", id)
            .execute(&self.pool)
            .await
            .map_err(RepositoryError::Database)?;

        if result.rows_affected() == 0 {
            return Err(RepositoryError::NotFound);
        }
        Ok(())
    }

    async fn get_branch_id_by_subscription_id(
        &self,
        id: i64,
    ) -> Result<Option<i64>, RepositoryError> {
        sqlx::query_scalar!("SELECT branch_id FROM subscriptions WHERE id = ?", id)
            .fetch_optional(&self.pool)
            .await
            .map_err(RepositoryError::Database)
    }

    async fn count_subscriptions_by_branch_id(
        &self,
        branch_id: i64,
    ) -> Result<i64, RepositoryError> {
        sqlx::query_scalar!(
            "SELECT COUNT(*) FROM subscriptions WHERE branch_id = ?",
            branch_id
        )
        .fetch_one(&self.pool)
        .await
        .map_err(RepositoryError::Database)
    }

    async fn delete_subscription_and_cascade(&self, id: i64) -> Result<(), RepositoryError> {
        self.run_in_transaction(|tx| {
            Box::pin(async move {
                let branch_id = sqlx::query_scalar!(
                    "DELETE FROM subscriptions WHERE id = ? RETURNING branch_id",
                    id
                )
                .fetch_optional(&mut *tx)
                .await
                .map_err(RepositoryError::Database)?
                .ok_or(RepositoryError::NotFound)?;

                let remaining_subscriptions = sqlx::query_scalar!(
                    "SELECT COUNT(*) FROM subscriptions WHERE branch_id = ?",
                    branch_id
                )
                .fetch_one(&mut *tx)
                .await
                .map_err(RepositoryError::Database)?;

                if remaining_subscriptions == 0 {
                    sqlx::query!("DELETE FROM branches WHERE id = ?", branch_id)
                        .execute(&mut *tx)
                        .await
                        .map_err(RepositoryError::Database)?;
                }

                Ok(())
            })
        })
        .await
    }
}

#[async_trait]
impl TriggerRepository for SqliteRepository {
    async fn get_all(&self) -> Result<Vec<TriggerQueueItem>, RepositoryError> {
        sqlx::query_as::<_, TriggerQueueItem>("SELECT * FROM trigger_queue")
            .fetch_all(&self.pool)
            .await
            .map_err(RepositoryError::Database)
    }

    async fn delete_by_id(&self, id: i64) -> Result<(), RepositoryError> {
        sqlx::query!("DELETE FROM trigger_queue WHERE id = ?", id)
            .execute(&self.pool)
            .await
            .map_err(RepositoryError::Database)?;
        Ok(())
    }

    async fn find_oldest_pending_and_mark_processing(
        &self,
    ) -> Result<Option<TriggerQueueItem>, RepositoryError> {
        let trigger = sqlx::query_as::<_, TriggerQueueItem>(
            "UPDATE trigger_queue
             SET status = 'PROCESSING', status_updated_at = CURRENT_TIMESTAMP
             WHERE id = (
                 SELECT id FROM trigger_queue
                 WHERE status IN ('PENDING') AND next_retry_at <= CURRENT_TIMESTAMP
                 ORDER BY next_retry_at ASC LIMIT 1
             )
             RETURNING id, branch_id, new_hash, retry_count, target_repo, event_type, gh_app_installation_id",
        )
        .fetch_optional(&self.pool)
        .await
        .map_err(RepositoryError::Database)?;

        Ok(trigger)
    }

    async fn update_retry_status(&self, params: UpdateRetryStatus) -> Result<(), RepositoryError> {
        let next_retry_count = params.retry_count + 1;

        if next_retry_count as u32 >= params.max_attempts {
            sqlx::query!(
                "UPDATE trigger_queue SET status = 'FAILED', retry_count = ? WHERE id = ?",
                next_retry_count,
                params.id
            )
            .execute(&self.pool)
            .await
            .map_err(RepositoryError::Database)?;
        } else {
            let backoff_secs = (params.backoff_base_secs * (1 << (next_retry_count - 1))) as i64;
            sqlx::query!(
                "UPDATE trigger_queue SET status = 'PENDING', retry_count = ?, next_retry_at = datetime('now', ? || ' seconds') WHERE id = ?",
                next_retry_count,
                backoff_secs,
                params.id
            )
            .execute(&self.pool)
            .await
            .map_err(RepositoryError::Database)?;
        }
        Ok(())
    }

    async fn recover_stuck_tasks(&self, threshold_seconds: u64) -> Result<(), RepositoryError> {
        let threshold_str = format!("-{} seconds", threshold_seconds);

        sqlx::query!(
            "UPDATE trigger_queue
             SET status = 'PENDING', status_updated_at = CURRENT_TIMESTAMP
             WHERE status = 'PROCESSING'
               AND status_updated_at < DATETIME('now', ?)",
            threshold_str
        )
        .execute(&self.pool)
        .await
        .map_err(RepositoryError::Database)?;
        Ok(())
    }

    async fn queue_triggers_for_branch(
        &self,
        branch_id: i64,
        new_hash: &crate::domain::CommitHash,
        executor: &mut sqlx::SqliteConnection,
    ) -> Result<(), RepositoryError> {
        sqlx::query!(
            "INSERT INTO trigger_queue (branch_id, new_hash, target_repo, event_type, gh_app_installation_id)
             SELECT ?, ?, s.target_repo, s.event_type, s.gh_app_installation_id
             FROM subscriptions s
             WHERE s.branch_id = ?
             ON CONFLICT(branch_id, target_repo, event_type) WHERE status = 'PENDING'
             DO UPDATE SET new_hash = excluded.new_hash, status_updated_at = CURRENT_TIMESTAMP",
            branch_id,
            new_hash,
            branch_id
        )
        .execute(executor)
        .await
        .map_err(RepositoryError::Database)?;
        Ok(())
    }
}