Skip to content

Commit 4c7785f

Browse files
committed
controllers/krate/publish: Expose the default_version field
1 parent caf043c commit 4c7785f

File tree

21 files changed

+114
-1
lines changed

21 files changed

+114
-1
lines changed

src/controllers/krate/publish.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
392392
.first(conn)
393393
.optional()?;
394394

395+
let mut default_version = None;
395396
// Upsert the `default_value` determined by the existing `default_value` and the
396397
// published version. Note that this could potentially write an outdated version
397398
// (although this should not happen regularly), as we might be comparing to an
@@ -412,6 +413,8 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
412413
.filter(default_versions::crate_id.eq(krate.id))
413414
.set(default_versions::version_id.eq(version.id))
414415
.execute(conn)?;
416+
} else {
417+
default_version = Some(existing_default_version.num.to_string());
415418
}
416419

417420
// Update the default version asynchronously in a background job
@@ -507,7 +510,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
507510
Ok(Json(GoodCrate {
508511
krate: EncodableCrate::from_minimal(
509512
krate,
510-
None,
513+
default_version.or(Some(version_string)).as_deref(),
511514
Some(&top_versions),
512515
None,
513516
false,

src/tests/krate/publish/basics.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,37 @@ async fn new_krate_twice() {
112112
"###);
113113
}
114114

115+
// This is similar to the `new_krate_twice` case, but the versions are published in reverse order.
116+
// The primary purpose is to verify that the `default_version` we provide is as expected.
117+
#[tokio::test(flavor = "multi_thread")]
118+
async fn new_krate_twice_alt() {
119+
let (app, _, _, token) = TestApp::full().with_token();
120+
121+
let crate_to_publish =
122+
PublishBuilder::new("foo_twice", "2.0.0").description("2.0.0 description");
123+
token.publish_crate(crate_to_publish).await.good();
124+
125+
let crate_to_publish = PublishBuilder::new("foo_twice", "0.99.0");
126+
let response = token.publish_crate(crate_to_publish).await;
127+
assert_eq!(response.status(), StatusCode::OK);
128+
assert_json_snapshot!(response.json(), {
129+
".crate.created_at" => "[datetime]",
130+
".crate.updated_at" => "[datetime]",
131+
});
132+
133+
let crates = app.crates_from_index_head("foo_twice");
134+
assert_json_snapshot!(crates);
135+
136+
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
137+
crates/foo_twice/foo_twice-0.99.0.crate
138+
crates/foo_twice/foo_twice-2.0.0.crate
139+
index/fo/o_/foo_twice
140+
rss/crates.xml
141+
rss/crates/foo_twice.xml
142+
rss/updates.xml
143+
"###);
144+
}
145+
115146
#[tokio::test(flavor = "multi_thread")]
116147
async fn new_krate_duplicate_version() {
117148
let (app, _, user, token) = TestApp::full().with_token();

src/tests/krate/publish/snapshots/crates_io__tests__krate__publish__basics__new_krate.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ expression: response.json()
77
"badges": null,
88
"categories": null,
99
"created_at": "[datetime]",
10+
"default_version": "1.0.0",
1011
"description": "description",
1112
"documentation": null,
1213
"downloads": 0,

src/tests/krate/publish/snapshots/crates_io__tests__krate__publish__basics__new_krate_twice.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ expression: response.json()
77
"badges": null,
88
"categories": null,
99
"created_at": "[datetime]",
10+
"default_version": "2.0.0",
1011
"description": "2.0.0 description",
1112
"documentation": null,
1213
"downloads": 0,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: src/tests/krate/publish/basics.rs
3+
expression: crates
4+
---
5+
[
6+
{
7+
"name": "foo_twice",
8+
"vers": "2.0.0",
9+
"deps": [],
10+
"cksum": "d6e88a7d30b9e5c3d268ede9a9937b62815e45a06fd2c572d602e0705ab6513d",
11+
"features": {},
12+
"yanked": false
13+
},
14+
{
15+
"name": "foo_twice",
16+
"vers": "0.99.0",
17+
"deps": [],
18+
"cksum": "45b0b19cd0280034e07820789d9bb6e4016526eba85c75fc697d49ec99fd2550",
19+
"features": {},
20+
"yanked": false
21+
}
22+
]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
source: src/tests/krate/publish/basics.rs
3+
expression: response.json()
4+
---
5+
{
6+
"crate": {
7+
"badges": null,
8+
"categories": null,
9+
"created_at": "[datetime]",
10+
"default_version": "2.0.0",
11+
"description": "description",
12+
"documentation": null,
13+
"downloads": 0,
14+
"exact_match": false,
15+
"homepage": null,
16+
"id": "foo_twice",
17+
"keywords": null,
18+
"links": {
19+
"owner_team": "/api/v1/crates/foo_twice/owner_team",
20+
"owner_user": "/api/v1/crates/foo_twice/owner_user",
21+
"owners": "/api/v1/crates/foo_twice/owners",
22+
"reverse_dependencies": "/api/v1/crates/foo_twice/reverse_dependencies",
23+
"version_downloads": "/api/v1/crates/foo_twice/downloads",
24+
"versions": "/api/v1/crates/foo_twice/versions"
25+
},
26+
"max_stable_version": "2.0.0",
27+
"max_version": "2.0.0",
28+
"name": "foo_twice",
29+
"newest_version": "0.99.0",
30+
"recent_downloads": null,
31+
"repository": null,
32+
"updated_at": "[datetime]",
33+
"versions": null
34+
},
35+
"warnings": {
36+
"invalid_badges": [],
37+
"invalid_categories": [],
38+
"other": []
39+
}
40+
}

src/tests/krate/publish/snapshots/crates_io__tests__krate__publish__basics__new_krate_weird_version.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ expression: response.json()
77
"badges": null,
88
"categories": null,
99
"created_at": "[datetime]",
10+
"default_version": "0.0.0-pre",
1011
"description": "description",
1112
"documentation": null,
1213
"downloads": 0,

src/tests/krate/publish/snapshots/crates_io__tests__krate__publish__basics__new_krate_with_token.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ expression: response.json()
77
"badges": null,
88
"categories": null,
99
"created_at": "[datetime]",
10+
"default_version": "1.0.0",
1011
"description": "description",
1112
"documentation": null,
1213
"downloads": 0,

src/tests/krate/publish/snapshots/crates_io__tests__krate__publish__build_metadata__version_with_build_metadata@build_metadata_1.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ expression: response.json()
77
"badges": null,
88
"categories": null,
99
"created_at": "[datetime]",
10+
"default_version": "1.0.0+foo",
1011
"description": "description",
1112
"documentation": null,
1213
"downloads": 0,

src/tests/krate/publish/snapshots/crates_io__tests__krate__publish__build_metadata__version_with_build_metadata@build_metadata_2.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ expression: response.json()
77
"badges": null,
88
"categories": null,
99
"created_at": "[datetime]",
10+
"default_version": "1.0.0-beta.1",
1011
"description": "description",
1112
"documentation": null,
1213
"downloads": 0,

0 commit comments

Comments
 (0)