Skip to content

Commit 25175f5

Browse files
committed
Integrate linecount analysis in publish controller
This modifies the publish endpoint to extract and store linecount statistics by extracting linecount data from tarball processing results and serializing the stats to `JSON` for database storage. The linecount data is then passed to the `NewVersion` builder for persistence. All publish-related test snapshots are updated to include linecount data, demonstrating that the integration works correctly across various publish scenarios. The implementation maintains backward compatibility with null linecount values for any edge cases.
1 parent ea71f12 commit 25175f5

12 files changed

+73
-7
lines changed

src/controllers/krate/publish.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use sha2::{Digest, Sha256};
2626
use std::collections::HashMap;
2727
use tokio::io::{AsyncRead, AsyncReadExt};
2828
use tokio_util::io::StreamReader;
29-
use tracing::{error, instrument};
29+
use tracing::{error, instrument, warn};
3030
use url::Url;
3131

3232
use crate::models::{
@@ -482,6 +482,10 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult<Json<Go
482482
// Read tarball from request
483483
let hex_cksum: String = Sha256::digest(&tarball_bytes).encode_hex();
484484

485+
let linecounts = serde_json::to_value(tarball_info.linecount_stats)
486+
.inspect_err(|err| warn!("Failed to convert linecounts to serde_json::Value: {err}"))
487+
.ok();
488+
485489
// Persist the new version of this crate
486490
let new_version = NewVersion::builder(krate.id, &version_string)
487491
.features(serde_json::to_value(&features)?)
@@ -502,6 +506,7 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult<Json<Go
502506
.maybe_repository(repository.as_deref())
503507
.categories(&categories)
504508
.keywords(&keywords)
509+
.maybe_linecounts(linecounts)
505510
.build();
506511

507512
let version = new_version.save(conn).await.map_err(|error| {

src/tests/krate/publish/snapshots/crates_io__tests__krate__publish__edition__edition_is_saved-4.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ expression: response.json()
3333
"id": "[id]",
3434
"lib_links": null,
3535
"license": "MIT",
36+
"linecounts": {
37+
"languages": {},
38+
"total_code_lines": 0,
39+
"total_comment_lines": 0
40+
},
3641
"links": {
3742
"authors": "/api/v1/crates/foo/1.0.0/authors",
3843
"dependencies": "/api/v1/crates/foo/1.0.0/dependencies",

src/tests/krate/publish/snapshots/crates_io__tests__krate__publish__links__crate_with_links_field-3.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ expression: response.json()
3333
"id": "[id]",
3434
"lib_links": "git2",
3535
"license": "MIT",
36+
"linecounts": {
37+
"languages": {},
38+
"total_code_lines": 0,
39+
"total_comment_lines": 0
40+
},
3641
"links": {
3742
"authors": "/api/v1/crates/foo/1.0.0/authors",
3843
"dependencies": "/api/v1/crates/foo/1.0.0/dependencies",

src/tests/krate/publish/snapshots/crates_io__tests__krate__publish__manifest__boolean_readme-4.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ expression: response.json()
3333
"id": "[id]",
3434
"lib_links": null,
3535
"license": "MIT",
36+
"linecounts": {
37+
"languages": {},
38+
"total_code_lines": 0,
39+
"total_comment_lines": 0
40+
},
3641
"links": {
3742
"authors": "/api/v1/crates/foo/1.0.0/authors",
3843
"dependencies": "/api/v1/crates/foo/1.0.0/dependencies",

src/tests/krate/publish/snapshots/crates_io__tests__krate__publish__manifest__lib_and_bin_crate-4.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ expression: response.json()
3636
"id": "[id]",
3737
"lib_links": null,
3838
"license": "MIT",
39+
"linecounts": {
40+
"languages": {
41+
"Rust": {
42+
"code_lines": 3,
43+
"comment_lines": 0,
44+
"files": 3
45+
}
46+
},
47+
"total_code_lines": 3,
48+
"total_comment_lines": 0
49+
},
3950
"links": {
4051
"authors": "/api/v1/crates/foo/1.0.0/authors",
4152
"dependencies": "/api/v1/crates/foo/1.0.0/dependencies",

src/tests/krate/publish/snapshots/crates_io__tests__krate__publish__trustpub__full_flow-9.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ expression: response.json()
2121
"id": 2,
2222
"lib_links": null,
2323
"license": "MIT",
24+
"linecounts": {
25+
"languages": {},
26+
"total_code_lines": 0,
27+
"total_comment_lines": 0
28+
},
2429
"links": {
2530
"authors": "/api/v1/crates/foo/1.1.0/authors",
2631
"dependencies": "/api/v1/crates/foo/1.1.0/dependencies",

src/tests/krate/snapshots/crates_io__tests__krate__yanking__patch_version_yank_unyank-2.snap

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ expression: json
6262
"description": "description",
6363
"homepage": null,
6464
"documentation": null,
65-
"repository": null
65+
"repository": null,
66+
"linecounts": {
67+
"languages": {},
68+
"total_code_lines": 0,
69+
"total_comment_lines": 0
70+
}
6671
}
6772
}

src/tests/krate/snapshots/crates_io__tests__krate__yanking__patch_version_yank_unyank-3.snap

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ expression: json
7373
"description": "description",
7474
"homepage": null,
7575
"documentation": null,
76-
"repository": null
76+
"repository": null,
77+
"linecounts": {
78+
"languages": {},
79+
"total_code_lines": 0,
80+
"total_comment_lines": 0
81+
}
7782
}
7883
}

src/tests/krate/snapshots/crates_io__tests__krate__yanking__patch_version_yank_unyank-4.snap

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ expression: json
7373
"description": "description",
7474
"homepage": null,
7575
"documentation": null,
76-
"repository": null
76+
"repository": null,
77+
"linecounts": {
78+
"languages": {},
79+
"total_code_lines": 0,
80+
"total_comment_lines": 0
81+
}
7782
}
7883
}

src/tests/krate/snapshots/crates_io__tests__krate__yanking__patch_version_yank_unyank-5.snap

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ expression: json
8484
"description": "description",
8585
"homepage": null,
8686
"documentation": null,
87-
"repository": null
87+
"repository": null,
88+
"linecounts": {
89+
"languages": {},
90+
"total_code_lines": 0,
91+
"total_comment_lines": 0
92+
}
8893
}
8994
}

src/tests/krate/snapshots/crates_io__tests__krate__yanking__patch_version_yank_unyank-6.snap

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ expression: json
8484
"description": "description",
8585
"homepage": null,
8686
"documentation": null,
87-
"repository": null
87+
"repository": null,
88+
"linecounts": {
89+
"languages": {},
90+
"total_code_lines": 0,
91+
"total_comment_lines": 0
92+
}
8893
}
8994
}

src/tests/krate/snapshots/crates_io__tests__krate__yanking__patch_version_yank_unyank.snap

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ expression: json
6262
"description": "description",
6363
"homepage": null,
6464
"documentation": null,
65-
"repository": null
65+
"repository": null,
66+
"linecounts": {
67+
"languages": {},
68+
"total_code_lines": 0,
69+
"total_comment_lines": 0
70+
}
6671
}
6772
}

0 commit comments

Comments
 (0)