JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Change forwarding

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨2556801

⁨giterated-models/src/messages/repository.rs⁩ - ⁨6114⁩ bytes
Raw
1 use std::collections::HashMap;
2
3 use serde::de::DeserializeOwned;
4 use serde::{Deserialize, Serialize};
5
6 use crate::model::repository::RepositoryVisibility;
7 use crate::model::settings::Setting;
8 use crate::model::{
9 repository::{Commit, Repository, RepositoryTreeEntry},
10 user::User,
11 };
12
13 /// A request to create a repository.
14 ///
15 /// # Authentication
16 /// - Instance Authentication
17 /// - Used to validate User token `issued_for`
18 /// - User Authentication
19 /// - Used to source owning user
20 /// - Used to authorize user token against user's instance
21 /// # Authorization
22 /// - Instance Authorization
23 /// - Used to authorize action using User token requiring a correct `issued_for` and valid issuance from user's instance
24 /// - User Authorization
25 /// - Potential User permissions checks
26 #[derive(Clone, Debug, Serialize, Deserialize)]
27 pub struct RepositoryCreateRequest {
28 pub name: String,
29 pub description: Option<String>,
30 pub visibility: RepositoryVisibility,
31 pub default_branch: String,
32 pub owner: User,
33 }
34
35 #[derive(Clone, Debug, Serialize, Deserialize)]
36 pub struct RepositoryCreateResponse;
37
38 /// A request to inspect the tree of a repository.
39 ///
40 /// # Authentication
41 /// - Instance Authentication
42 /// - Validate request against the `issued_for` public key
43 /// - Validate User token against the user's instance's public key
44 /// # Authorization
45 /// - User Authorization
46 /// - Potential User permissions checks
47 #[derive(Clone, Debug, Serialize, Deserialize)]
48 pub struct RepositoryFileInspectRequest {
49 pub path: RepositoryTreeEntry,
50 }
51
52 #[derive(Clone, Debug, Serialize, Deserialize)]
53 pub enum RepositoryFileInspectionResponse {
54 File {
55 commit_metadata: Commit,
56 },
57 Folder {
58 commit_metadata: Commit,
59 members: Vec<RepositoryTreeEntry>,
60 },
61 Invalid {
62 path: RepositoryTreeEntry,
63 },
64 }
65
66 /// A request to get a repository's information.
67 ///
68 /// # Authentication
69 /// - Instance Authentication
70 /// - Validate request against the `issued_for` public key
71 /// - Validate User token against the user's instance's public key
72 /// # Authorization
73 /// - User Authorization
74 /// - Potential User permissions checks
75 #[derive(Clone, Debug, Serialize, Deserialize)]
76 pub struct RepositoryIssuesCountRequest;
77
78 #[derive(Clone, Debug, Serialize, Deserialize)]
79 pub struct RepositoryIssuesCountResponse {
80 pub count: u64,
81 }
82
83 /// A request to get a repository's issues count.
84 ///
85 /// # Authentication
86 /// - Instance Authentication
87 /// - Validate request against the `issued_for` public key
88 /// - Validate User token against the user's instance's public key
89 /// # Authorization
90 /// - User Authorization
91 /// - Potential User permissions checks
92 #[derive(Clone, Debug, Serialize, Deserialize)]
93 pub struct RepositoryIssueLabelsRequest;
94
95 #[derive(Clone, Debug, Serialize, Deserialize)]
96 pub struct RepositoryIssueLabelsResponse {
97 pub labels: Vec<IssueLabel>,
98 }
99
100 #[derive(Clone, Debug, Serialize, Deserialize)]
101 pub struct IssueLabel {
102 pub name: String,
103 pub color: String,
104 }
105
106 /// A request to get a repository's issue labels.
107 ///
108 /// # Authentication
109 /// - Instance Authentication
110 /// - Validate request against the `issued_for` public key
111 /// - Validate User token against the user's instance's public key
112 /// # Authorization
113 /// - User Authorization
114 /// - Potential User permissions checks
115 #[derive(Clone, Debug, Serialize, Deserialize)]
116 pub struct RepositoryIssuesRequest;
117
118 #[derive(Clone, Debug, Serialize, Deserialize)]
119 pub struct RepositoryIssuesResponse {
120 pub issues: Vec<RepositoryIssue>,
121 }
122
123 /// A request to get a repository's issues.
124 ///
125 /// # Authentication
126 /// - Instance Authentication
127 /// - Validate request against the `issued_for` public key
128 /// - Validate User token against the user's instance's public key
129 /// # Authorization
130 /// - User Authorization
131 /// - Potential User permissions checks
132 #[derive(Clone, Debug, Serialize, Deserialize)]
133 pub struct RepositoryIssue {
134 pub author: User,
135 pub id: u64,
136 pub title: String,
137 pub contents: String,
138 pub labels: Vec<IssueLabel>,
139 }
140
141 #[derive(Clone, Debug, Serialize, Deserialize)]
142 pub struct RepositoryInfoRequest {
143 pub repository: Repository,
144 /// Whether to fetch extra metadata like the last commit made to file or size
145 pub extra_metadata: bool,
146 /// Rev (branch) being requested
147 pub rev: Option<String>,
148 /// Tree path being requested
149 pub path: Option<String>,
150 }
151
152 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
153 pub struct RepositorySettingsRequest {
154 pub repository: Repository,
155 }
156
157 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Default, Deserialize)]
158 pub struct RepositorySettingsResponse {
159 pub settings: HashMap<String, serde_json::Value>,
160 }
161
162 impl RepositorySettingsResponse {
163 pub fn try_get<S: Setting + DeserializeOwned>(&self) -> Option<S> {
164 let setting_member = self
165 .settings
166 .iter()
167 .filter(|(key, _)| key.as_str() == S::name())
168 .next()?;
169
170 serde_json::from_value(setting_member.1.clone()).ok()
171 }
172 }
173
174 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
175 pub struct RepositoryWriteSettingsRequest {
176 pub repository: Repository,
177 pub settings: Vec<(String, String)>,
178 }
179
180 impl RepositoryWriteSettingsRequest {
181 pub fn new(repository: impl ToOwned<Owned = Repository>) -> Self {
182 Self {
183 repository: repository.to_owned(),
184 settings: Default::default(),
185 }
186 }
187
188 pub fn set<S: Setting>(&mut self, setting: S) {
189 self.settings.push((
190 S::name().to_string(),
191 serde_json::to_string(&setting).unwrap(),
192 ));
193 }
194 pub fn try_get<S: Setting + DeserializeOwned>(&self) -> Option<S> {
195 let setting_member = self
196 .settings
197 .iter()
198 .filter(|(key, _)| key.as_str() == S::name())
199 .next()?;
200
201 serde_json::from_str(&setting_member.1).ok()
202 }
203 }
204
205 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
206 pub struct UserWriteSettingsResponse {
207 // IDK?
208 }
209