-
Notifications
You must be signed in to change notification settings - Fork 600
/
version.rs
256 lines (232 loc) · 8 KB
/
version.rs
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
use std::collections::HashMap;
use chrono::NaiveDateTime;
use diesel::prelude::*;
use crate::util::errors::{cargo_err, AppResult};
use crate::models::{Crate, Dependency, User, VersionOwnerAction};
use crate::schema::*;
use crate::views::{EncodableAuditAction, EncodableVersion, EncodableVersionLinks};
// Queryable has a custom implementation below
#[derive(Clone, Identifiable, Associations, Debug, Queryable, Deserialize, Serialize)]
#[belongs_to(Crate)]
pub struct Version {
pub id: i32,
pub crate_id: i32,
pub num: semver::Version,
pub updated_at: NaiveDateTime,
pub created_at: NaiveDateTime,
pub downloads: i32,
pub features: serde_json::Value,
pub yanked: bool,
pub license: Option<String>,
pub crate_size: Option<i32>,
pub published_by: Option<i32>,
}
#[derive(Insertable, Debug)]
#[table_name = "versions"]
pub struct NewVersion {
crate_id: i32,
num: String,
features: serde_json::Value,
license: Option<String>,
crate_size: Option<i32>,
published_by: i32,
}
/// The highest version (semver order) and the most recently updated version.
/// Typically used for a single crate.
#[derive(Debug, Clone)]
pub struct TopVersions {
pub highest: semver::Version,
pub newest: semver::Version,
}
/// A default semver value, "0.0.0", for use in TopVersions
fn default_semver_version() -> semver::Version {
semver::Version {
major: 0,
minor: 0,
patch: 0,
pre: vec![],
build: vec![],
}
}
impl Version {
pub fn encodable(
self,
crate_name: &str,
published_by: Option<User>,
audit_actions: Vec<(VersionOwnerAction, User)>,
) -> EncodableVersion {
let Version {
id,
num,
updated_at,
created_at,
downloads,
features,
yanked,
license,
crate_size,
..
} = self;
let num = num.to_string();
EncodableVersion {
dl_path: format!("/api/v1/crates/{}/{}/download", crate_name, num),
readme_path: format!("/api/v1/crates/{}/{}/readme", crate_name, num),
num: num.clone(),
id,
krate: crate_name.to_string(),
updated_at,
created_at,
downloads,
features,
yanked,
license,
links: EncodableVersionLinks {
dependencies: format!("/api/v1/crates/{}/{}/dependencies", crate_name, num),
version_downloads: format!("/api/v1/crates/{}/{}/downloads", crate_name, num),
authors: format!("/api/v1/crates/{}/{}/authors", crate_name, num),
},
crate_size,
published_by: published_by.map(User::encodable_public),
audit_actions: audit_actions
.into_iter()
.map(|(audit_action, user)| EncodableAuditAction {
action: audit_action.action.into(),
user: User::encodable_public(user),
time: audit_action.time,
})
.collect(),
}
}
/// Returns (dependency, crate dependency name)
pub fn dependencies(&self, conn: &PgConnection) -> QueryResult<Vec<(Dependency, String)>> {
Dependency::belonging_to(self)
.inner_join(crates::table)
.select((dependencies::all_columns, crates::name))
.order((dependencies::optional, crates::name))
.load(conn)
}
/// Return both the newest (most recently updated) and the
/// highest version (in semver order) for a collection of date/version pairs.
pub fn top<T>(pairs: T) -> TopVersions
where
T: Clone + IntoIterator<Item = (NaiveDateTime, semver::Version)>,
{
TopVersions {
newest: pairs
.clone()
.into_iter()
.max()
.unwrap_or((
NaiveDateTime::from_timestamp(0, 0),
default_semver_version(),
))
.1,
highest: pairs
.into_iter()
.map(|(_, v)| v)
.max()
.unwrap_or_else(default_semver_version),
}
}
pub fn record_readme_rendering(version_id_: i32, conn: &PgConnection) -> QueryResult<usize> {
use crate::schema::readme_renderings::dsl::*;
use diesel::dsl::now;
diesel::insert_into(readme_renderings)
.values(version_id.eq(version_id_))
.on_conflict(version_id)
.do_update()
.set(rendered_at.eq(now))
.execute(conn)
}
/// Gets the User who ran `cargo publish` for this version, if recorded.
/// Not for use when you have a group of versions you need the publishers for.
pub fn published_by(&self, conn: &PgConnection) -> Option<User> {
match self.published_by {
Some(pb) => users::table.find(pb).first(conn).ok(),
None => None,
}
}
}
impl NewVersion {
pub fn new(
crate_id: i32,
num: &semver::Version,
features: &HashMap<String, Vec<String>>,
license: Option<String>,
license_file: Option<&str>,
crate_size: i32,
published_by: i32,
) -> AppResult<Self> {
let features = serde_json::to_value(features)?;
let mut new_version = NewVersion {
crate_id,
num: num.to_string(),
features,
license,
crate_size: Some(crate_size),
published_by,
};
new_version.validate_license(license_file)?;
Ok(new_version)
}
pub fn save(
&self,
conn: &PgConnection,
authors: &[String],
published_by_email: &str,
) -> AppResult<Version> {
use crate::schema::version_authors::{name, version_id};
use crate::schema::versions::dsl::*;
use diesel::dsl::exists;
use diesel::{insert_into, select};
conn.transaction(|| {
let already_uploaded = versions
.filter(crate_id.eq(self.crate_id))
.filter(num.eq(&self.num));
if select(exists(already_uploaded)).get_result(conn)? {
return Err(cargo_err(&format_args!(
"crate version `{}` is already \
uploaded",
self.num
)));
}
let version = insert_into(versions)
.values(self)
.get_result::<Version>(conn)?;
insert_into(versions_published_by::table)
.values((
versions_published_by::version_id.eq(version.id),
versions_published_by::email.eq(published_by_email),
))
.execute(conn)?;
let new_authors = authors
.iter()
.map(|s| (version_id.eq(version.id), name.eq(s)))
.collect::<Vec<_>>();
insert_into(version_authors::table)
.values(&new_authors)
.execute(conn)?;
Ok(version)
})
}
fn validate_license(&mut self, license_file: Option<&str>) -> AppResult<()> {
if let Some(ref license) = self.license {
for part in license.split('/') {
license_exprs::validate_license_expr(part).map_err(|e| {
cargo_err(&format_args!(
"{}; see http://opensource.org/licenses \
for options, and http://spdx.org/licenses/ \
for their identifiers",
e
))
})?;
}
} else if license_file.is_some() {
// If no license is given, but a license file is given, flag this
// crate as having a nonstandard license. Note that we don't
// actually do anything else with license_file currently.
self.license = Some(String::from("non-standard"));
}
Ok(())
}
}