Skip to main content

fakecloud_codecommit/
state.rs

1//! Account-partitioned, serializable state for AWS CodeCommit.
2//!
3//! A repository owns a content-addressed object store: `blobs` maps a 40-char
4//! SHA-1 blob id to its (base64) bytes, and `trees` maps a commit id to the
5//! full flat path -> [`FileEntry`] snapshot of that commit's working tree (a
6//! materialized tree, so `GetFile`/`GetFolder`/`GetDifferences` are simple
7//! lookups). `commits` maps a commit id to its stored `Commit`-shaped JSON, and
8//! `branches` maps a branch name to the commit id at its tip. Pull requests,
9//! comments, approval-rule templates and their associations, triggers, and tags
10//! are stored as response-shaped JSON so a read returns exactly what was
11//! written.
12
13use std::collections::BTreeMap;
14use std::sync::Arc;
15
16use parking_lot::RwLock;
17use serde::{Deserialize, Serialize};
18use serde_json::Value;
19
20use fakecloud_core::multi_account::{AccountState, MultiAccountState};
21
22pub const CODECOMMIT_SNAPSHOT_SCHEMA_VERSION: u32 = 1;
23
24/// One entry in a commit's materialized working tree.
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct FileEntry {
27    /// The 40-char SHA-1 blob id of the file content.
28    pub blob_id: String,
29    /// The git file mode: `100644` (NORMAL), `100755` (EXECUTABLE), or
30    /// `120000` (SYMLINK).
31    pub mode: String,
32}
33
34/// A single git repository and its content-addressed object store.
35#[derive(Debug, Clone, Default, Serialize, Deserialize)]
36pub struct Repo {
37    /// `RepositoryMetadata`-shaped JSON (id, arn, clone urls, dates, kms key).
38    pub metadata: Value,
39    /// Branch name -> tip commit id.
40    #[serde(default)]
41    pub branches: BTreeMap<String, String>,
42    /// Commit id -> `Commit`-shaped JSON.
43    #[serde(default)]
44    pub commits: BTreeMap<String, Value>,
45    /// Blob id -> base64-encoded content bytes.
46    #[serde(default)]
47    pub blobs: BTreeMap<String, String>,
48    /// Commit id -> materialized working tree (path -> entry).
49    #[serde(default)]
50    pub trees: BTreeMap<String, BTreeMap<String, FileEntry>>,
51    /// Configured repository triggers (`RepositoryTrigger`-shaped JSON list).
52    #[serde(default)]
53    pub triggers: Vec<Value>,
54    /// The current triggers configuration id (a UUID), empty when never set.
55    #[serde(default)]
56    pub triggers_config_id: String,
57    /// Approval-rule-template names associated with this repository.
58    #[serde(default)]
59    pub associated_templates: Vec<String>,
60}
61
62/// The account-scoped CodeCommit state for one AWS account.
63#[derive(Debug, Clone, Default, Serialize, Deserialize)]
64pub struct CodeCommitState {
65    /// Repositories keyed by repository name.
66    #[serde(default)]
67    pub repositories: BTreeMap<String, Repo>,
68    /// Insertion order of repository names (most-recent last).
69    #[serde(default)]
70    pub repository_order: Vec<String>,
71    /// Approval-rule templates keyed by template name; value is an
72    /// `ApprovalRuleTemplate`-shaped JSON.
73    #[serde(default)]
74    pub templates: BTreeMap<String, Value>,
75    /// Insertion order of template names.
76    #[serde(default)]
77    pub template_order: Vec<String>,
78    /// Pull requests keyed by pull-request id; value is a `PullRequest`-shaped
79    /// JSON carrying its targets, status, revision id, and approval rules.
80    #[serde(default)]
81    pub pull_requests: BTreeMap<String, Value>,
82    /// Insertion order of pull-request ids.
83    #[serde(default)]
84    pub pull_request_order: Vec<String>,
85    /// Monotonic counter for minting pull-request ids.
86    #[serde(default)]
87    pub pull_request_counter: u64,
88    /// Pull-request event log keyed by pull-request id.
89    #[serde(default)]
90    pub pull_request_events: BTreeMap<String, Vec<Value>>,
91    /// Per-pull-request approval state, keyed by pull-request id then revision
92    /// id then user ARN; value is `APPROVE` or `REVOKE`.
93    #[serde(default)]
94    pub pull_request_approvals: BTreeMap<String, BTreeMap<String, BTreeMap<String, String>>>,
95    /// Per-pull-request override state, keyed by pull-request id then revision
96    /// id; value is the overrider ARN (present means overridden).
97    #[serde(default)]
98    pub pull_request_overrides: BTreeMap<String, BTreeMap<String, String>>,
99    /// Comments keyed by comment id; value is a `Comment`-shaped JSON with the
100    /// thread context (`repositoryName`, before/after commit, `pullRequestId`,
101    /// `location`) stored alongside under private `_ctx*` members that are
102    /// stripped before a comment is returned to the wire.
103    #[serde(default)]
104    pub comments: BTreeMap<String, Value>,
105    /// Insertion order of comment ids.
106    #[serde(default)]
107    pub comment_order: Vec<String>,
108    /// Resource tags keyed by resource ARN; value is a `key -> value` map.
109    #[serde(default)]
110    pub tags: BTreeMap<String, BTreeMap<String, String>>,
111}
112
113impl AccountState for CodeCommitState {
114    fn new_for_account(_account_id: &str, _region: &str, _endpoint: &str) -> Self {
115        Self::default()
116    }
117}
118
119pub type SharedCodeCommitState = Arc<RwLock<MultiAccountState<CodeCommitState>>>;
120
121#[derive(Debug, Serialize, Deserialize)]
122pub struct CodeCommitSnapshot {
123    pub schema_version: u32,
124    pub accounts: MultiAccountState<CodeCommitState>,
125}