Skip to content

Commit da6c66d

Browse files
authored
Merge pull request #12365 from Turbo87/trustpub-only
Add `trustpub_only` flag
2 parents d0ca089 + 0f75f00 commit da6c66d

File tree

61 files changed

+1149
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1149
-6
lines changed

crates/crates_io_api_types/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ pub struct EncodableCrate {
361361
/// Whether the crate name was an exact match.
362362
#[schema(deprecated)]
363363
pub exact_match: bool,
364+
365+
/// Whether this crate can only be published via Trusted Publishing.
366+
pub trustpub_only: bool,
364367
}
365368

366369
impl EncodableCrate {
@@ -386,6 +389,7 @@ impl EncodableCrate {
386389
homepage,
387390
documentation,
388391
repository,
392+
trustpub_only,
389393
..
390394
} = krate;
391395
let versions_link = match versions {
@@ -451,6 +455,7 @@ impl EncodableCrate {
451455
exact_match,
452456
description,
453457
repository,
458+
trustpub_only,
454459
links: EncodableCrateLinks {
455460
version_downloads: format!("/api/v1/crates/{name}/downloads"),
456461
versions: versions_link,
@@ -1201,6 +1206,7 @@ mod tests {
12011206
reverse_dependencies: "".to_string(),
12021207
},
12031208
exact_match: false,
1209+
trustpub_only: false,
12041210
};
12051211
let json = serde_json::to_string(&crt).unwrap();
12061212
assert_some!(json.as_str().find(r#""updated_at":"2017-01-06T14:23:11Z""#));

crates/crates_io_database/src/models/krate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub struct Crate {
3737
pub repository: Option<String>,
3838
pub max_upload_size: Option<i32>,
3939
pub max_features: Option<i16>,
40+
pub trustpub_only: bool,
4041
}
4142

4243
/// We literally never want to select `textsearchable_index_col`
@@ -52,6 +53,7 @@ type AllColumns = (
5253
crates::repository,
5354
crates::max_upload_size,
5455
crates::max_features,
56+
crates::trustpub_only,
5557
);
5658

5759
pub const ALL_COLUMNS: AllColumns = (
@@ -65,6 +67,7 @@ pub const ALL_COLUMNS: AllColumns = (
6567
crates::repository,
6668
crates::max_upload_size,
6769
crates::max_features,
70+
crates::trustpub_only,
6871
);
6972

7073
pub const MAX_NAME_LENGTH: usize = 64;

crates/crates_io_database/src/schema.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ diesel::table! {
374374
///
375375
/// (Automatically generated by Diesel.)
376376
textsearchable_index_col -> Tsvector,
377+
/// When true, this crate can only be published via Trusted Publishing, not with API tokens
378+
trustpub_only -> Bool,
377379
/// The `updated_at` column of the `crates` table.
378380
///
379381
/// Its SQL type is `Timestamptz`.

crates/crates_io_database_dump/src/dump-db.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ textsearchable_index_col = "private" # This Postgres specific and can be derived
9292
repository = "public"
9393
max_upload_size = "public"
9494
max_features = "public"
95+
trustpub_only = "public"
9596

9697
[crates_categories]
9798
dependencies = ["categories", "crates"]

crates/crates_io_database_dump/src/snapshots/crates_io_database_dump__tests__sql_scripts@export.sql.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ BEGIN ISOLATION LEVEL REPEATABLE READ, READ ONLY;
66

77
\copy "categories" ("category", "crates_cnt", "created_at", "description", "id", "path", "slug") TO 'data/categories.csv' WITH CSV HEADER
88
\copy "crate_downloads" ("crate_id", "downloads") TO 'data/crate_downloads.csv' WITH CSV HEADER
9-
\copy "crates" ("created_at", "description", "documentation", "homepage", "id", "max_features", "max_upload_size", "name", "readme", "repository", "updated_at") TO 'data/crates.csv' WITH CSV HEADER
9+
\copy "crates" ("created_at", "description", "documentation", "homepage", "id", "max_features", "max_upload_size", "name", "readme", "repository", "trustpub_only", "updated_at") TO 'data/crates.csv' WITH CSV HEADER
1010
\copy "keywords" ("crates_cnt", "created_at", "id", "keyword") TO 'data/keywords.csv' WITH CSV HEADER
1111
\copy "metadata" ("total_downloads") TO 'data/metadata.csv' WITH CSV HEADER
1212
\copy "reserved_crate_names" ("name") TO 'data/reserved_crate_names.csv' WITH CSV HEADER

crates/crates_io_database_dump/src/snapshots/crates_io_database_dump__tests__sql_scripts@import.sql.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ BEGIN;
5050

5151
\copy "categories" ("category", "crates_cnt", "created_at", "description", "id", "path", "slug") FROM 'data/categories.csv' WITH CSV HEADER
5252
\copy "crate_downloads" ("crate_id", "downloads") FROM 'data/crate_downloads.csv' WITH CSV HEADER
53-
\copy "crates" ("created_at", "description", "documentation", "homepage", "id", "max_features", "max_upload_size", "name", "readme", "repository", "updated_at") FROM 'data/crates.csv' WITH CSV HEADER
53+
\copy "crates" ("created_at", "description", "documentation", "homepage", "id", "max_features", "max_upload_size", "name", "readme", "repository", "trustpub_only", "updated_at") FROM 'data/crates.csv' WITH CSV HEADER
5454
\copy "keywords" ("crates_cnt", "created_at", "id", "keyword") FROM 'data/keywords.csv' WITH CSV HEADER
5555
\copy "metadata" ("total_downloads") FROM 'data/metadata.csv' WITH CSV HEADER
5656
\copy "reserved_crate_names" ("name") FROM 'data/reserved_crate_names.csv' WITH CSV HEADER

crates/crates_io_test_utils/src/builders/krate.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct CrateBuilder<'a> {
1616
krate: NewCrate<'a>,
1717
owner_id: i32,
1818
recent_downloads: Option<i32>,
19+
trustpub_only: bool,
1920
updated_at: Option<DateTime<Utc>>,
2021
versions: Vec<VersionBuilder>,
2122
}
@@ -34,6 +35,7 @@ impl<'a> CrateBuilder<'a> {
3435
},
3536
owner_id,
3637
recent_downloads: None,
38+
trustpub_only: false,
3739
updated_at: None,
3840
versions: Vec::new(),
3941
}
@@ -113,6 +115,12 @@ impl<'a> CrateBuilder<'a> {
113115
self
114116
}
115117

118+
/// Sets the crate's `trustpub_only` flag.
119+
pub fn trustpub_only(mut self, trustpub_only: bool) -> Self {
120+
self.trustpub_only = trustpub_only;
121+
self
122+
}
123+
116124
pub async fn build(mut self, connection: &mut AsyncPgConnection) -> anyhow::Result<Crate> {
117125
use diesel::{insert_into, select, update};
118126

@@ -171,6 +179,14 @@ impl<'a> CrateBuilder<'a> {
171179
.await?;
172180
}
173181

182+
if self.trustpub_only {
183+
krate = update(&krate)
184+
.set(crates::trustpub_only.eq(true))
185+
.returning(Crate::as_returning())
186+
.get_result(connection)
187+
.await?;
188+
}
189+
174190
update_default_version(krate.id, connection).await?;
175191

176192
Ok(krate)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE crates DROP COLUMN trustpub_only;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE crates ADD COLUMN trustpub_only BOOLEAN NOT NULL DEFAULT FALSE;
2+
COMMENT ON COLUMN crates.trustpub_only IS 'When true, this crate can only be published via Trusted Publishing, not with API tokens';

src/controllers/krate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub mod owners;
1515
pub mod publish;
1616
pub mod rev_deps;
1717
pub mod search;
18+
pub mod update;
1819
pub mod versions;
1920

2021
#[derive(Deserialize, FromRequestParts, IntoParams)]

0 commit comments

Comments
 (0)