Skip to main content

commit_bridge/
model.rs

1//! Data structures representing items stored in database.
2//!
3//! The `Create_` `struct`s represent the payload
4//! to create the corresponding row.
5
6// FIXME: Some docstrings have been duplicated.
7// Maybe this problem can be solved by including an external Markdown file.
8// For example:
9//
10// ```rust
11// #[doc = include_str!("docs/my_struct.md")]
12// pub struct MyStruct { ... }
13// ```
14
15use crate::domain::{BranchName, CommitHash, EventType, RepoUrl, TargetRepo};
16use chrono::{DateTime, Utc};
17use rovo::schemars::JsonSchema;
18use serde::{Deserialize, Serialize};
19use sqlx::FromRow;
20
21/// Represents a row in the `branches` table.
22#[derive(Debug, Serialize, Deserialize, FromRow, JsonSchema)]
23pub struct Branch {
24    /// Unique database primary key.
25    pub id: i64,
26
27    /// Full HTTPS URL of the monitored git repository.
28    pub repo_url: RepoUrl,
29
30    /// Name of the git branch to poll.
31    pub name: BranchName,
32
33    /// SHA of the latest commit polled.
34    ///
35    /// `None` if the branch has not been processed.
36    pub last_commit_hash: Option<CommitHash>,
37
38    /// Timestamp when the record was created.
39    pub created_at: DateTime<Utc>,
40
41    /// Timestamp when the record was updated.
42    pub updated_at: DateTime<Utc>,
43}
44
45/// Represents a row in the `subscriptions` table.
46#[derive(Debug, Serialize, Deserialize, FromRow, JsonSchema, Clone)]
47pub struct Subscription {
48    /// Unique database primary key.
49    pub id: i64,
50
51    /// Foreign key to [`Branch::id`].
52    pub branch_id: i64,
53
54    /// The repository whose workflow needs to be triggered.
55    pub target_repo: TargetRepo,
56
57    /// Identifies the specific [`repository_dispatch`] event.
58    ///
59    /// The values must contain at most 100 characters.
60    ///
61    /// <!-- LINKS -->
62    /// [`repository_dispatch`]: https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#repository_dispatch
63    pub event_type: EventType,
64
65    /// Allows authenticating as a [GitHub App installation][gh_app_auth].
66    ///
67    /// <!-- LINKS -->
68    /// [gh_app_auth]: https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation
69    pub gh_app_installation_id: i64,
70
71    /// Timestamp when the record was created.
72    pub created_at: DateTime<Utc>,
73
74    /// Timestamp when the record was updated.
75    pub updated_at: DateTime<Utc>,
76}
77
78/// Represents branch information for a subscription.
79#[derive(Debug, Serialize, JsonSchema, Clone)]
80pub struct SourceBranchInfo {
81    /// Full HTTPS URL of the monitored git repository.
82    pub repo_url: RepoUrl,
83    /// Name of the git branch to poll.
84    pub name: BranchName,
85}
86
87/// HAL links for a subscription page.
88#[derive(Serialize, JsonSchema)]
89pub struct SubscriptionPageLinks {
90    /// Next page link.
91    pub next: Option<HalLink>,
92}
93
94/// Paginated representation of subscriptions.
95#[derive(Serialize, JsonSchema)]
96pub struct SubscriptionPage {
97    /// The subscription data.
98    pub data: Vec<SubscriptionHal>,
99    /// Number of elements remaining after this page.
100    pub remaining_count: i64,
101    /// HAL links.
102    #[serde(rename = "_links")]
103    pub links: SubscriptionPageLinks,
104}
105
106/// HAL link structure.
107#[derive(Serialize, JsonSchema)]
108pub struct HalLink {
109    /// URL of the link.
110    pub href: String,
111}
112
113/// HAL links for a subscription.
114#[derive(Serialize, JsonSchema)]
115pub struct SubscriptionLinks {
116    /// Self link.
117    #[serde(rename = "self")]
118    pub self_link: HalLink,
119    /// Update link.
120    pub update: HalLink,
121    /// Delete link.
122    pub delete: HalLink,
123}
124
125/// Combined subscription and branch information.
126#[derive(Debug, Serialize, JsonSchema)]
127pub struct SubscriptionWithBranch {
128    /// The subscription data.
129    #[serde(flatten)]
130    pub subscription: Subscription,
131    /// The source branch info.
132    pub source_branch: SourceBranchInfo,
133}
134
135/// HAL representation of a subscription.
136#[derive(Serialize, JsonSchema)]
137pub struct SubscriptionHal {
138    /// The subscription data.
139    #[serde(flatten)]
140    pub subscription: Subscription,
141    /// The source repository and branch.
142    pub source_branch: SourceBranchInfo,
143    /// HAL links.
144    #[serde(rename = "_links")]
145    pub links: SubscriptionLinks,
146}
147
148/// Holds payload data for the creation of a [`Subscription`].
149#[derive(Debug, Clone, Deserialize, JsonSchema)]
150pub struct CreateSubscription {
151    /// Full HTTPS URL of the monitored git repository.
152    pub source_repo_url: RepoUrl,
153
154    /// Name of the git branch to poll.
155    pub source_branch_name: BranchName,
156
157    /// The repository whose workflow needs to be triggered.
158    pub target_repo: TargetRepo,
159
160    /// Identifies the specific [`repository_dispatch`] event.
161    ///
162    /// The values must contain at most 100 characters.
163    ///
164    /// <!-- LINKS -->
165    /// [`repository_dispatch`]: https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#repository_dispatch
166    pub event_type: EventType,
167
168    /// Allows authenticating as a [GitHub App installation][gh_app_auth].
169    ///
170    /// <!-- LINKS -->
171    /// [gh_app_auth]: https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation
172    pub gh_app_installation_id: i64,
173}
174
175/// Holds payload data for the update of a [`Subscription`].
176#[derive(Debug, Deserialize, JsonSchema)]
177pub struct UpdateSubscription {
178    /// The repository whose workflow needs to be triggered.
179    pub target_repo: Option<TargetRepo>,
180
181    /// Identifies the specific [`repository_dispatch`] event.
182    ///
183    /// The values must contain at most 100 characters.
184    ///
185    /// <!-- LINKS -->
186    /// [`repository_dispatch`]: https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#repository_dispatch
187    pub event_type: Option<EventType>,
188
189    /// Allows authenticating as a [GitHub App installation][gh_app_auth].
190    ///
191    /// <!-- LINKS -->
192    /// [gh_app_auth]: https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation
193    pub gh_app_installation_id: Option<i64>,
194}
195
196/// Represents a row in the `trigger_queue` table.
197#[derive(Debug, FromRow)]
198pub struct TriggerQueueItem {
199    /// Unique database primary key.
200    pub id: i64,
201
202    /// Foreign key to [`Branch::id`].
203    pub branch_id: i64,
204
205    /// The hash of the latest commit on the branch.
206    pub new_hash: CommitHash,
207
208    /// The repository whose workflow needs to be triggered.
209    pub target_repo: TargetRepo,
210
211    /// Identifies the specific [`repository_dispatch`] event.
212    ///
213    /// <!-- LINKS -->
214    /// [`repository_dispatch`]: https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#repository_dispatch
215    pub event_type: EventType,
216
217    /// Allows authenticating as a [GitHub App installation][gh_app_auth].
218    ///
219    /// <!-- LINKS -->
220    /// [gh_app_auth]: https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation
221    pub gh_app_installation_id: i64,
222
223    /// Number of times the task has been attempted.
224    pub retry_count: i64,
225}