Skip to content

Commit 94638c8

Browse files
committed
WIP
1 parent 577a4e8 commit 94638c8

38 files changed

+6705
-68
lines changed

Cargo.lock

+92
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ crates_io_env_vars = { path = "crates/crates_io_env_vars" }
7070
crates_io_github = { path = "crates/crates_io_github" }
7171
crates_io_index = { path = "crates/crates_io_index" }
7272
crates_io_markdown = { path = "crates/crates_io_markdown" }
73+
crates_io_trusted_publishing = { path = "crates/crates_io_trusted_publishing" }
7374
crates_io_pagerduty = { path = "crates/crates_io_pagerduty" }
7475
crates_io_session = { path = "crates/crates_io_session" }
7576
crates_io_tarball = { path = "crates/crates_io_tarball" }
@@ -139,13 +140,15 @@ utoipa-axum = "=0.2.0"
139140
bytes = "=1.10.1"
140141
crates_io_github = { path = "crates/crates_io_github", features = ["mock"] }
141142
crates_io_index = { path = "crates/crates_io_index", features = ["testing"] }
143+
crates_io_trusted_publishing = { path = "crates/crates_io_trusted_publishing", features = ["mock"] }
142144
crates_io_tarball = { path = "crates/crates_io_tarball", features = ["builder"] }
143145
crates_io_team_repo = { path = "crates/crates_io_team_repo", features = ["mock"] }
144146
crates_io_test_db = { path = "crates/crates_io_test_db" }
145147
claims = "=0.8.0"
146148
diesel = { version = "=2.2.10", features = ["r2d2"] }
147149
googletest = "=0.14.0"
148150
insta = { version = "=1.43.0", features = ["glob", "json", "redactions"] }
151+
jsonwebtoken = "=9.3.1"
149152
regex = "=1.11.1"
150153
sentry = { version = "=0.37.0", features = ["test"] }
151154
tokio = "=1.44.2"

crates/crates_io_database/src/models/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ pub mod krate;
3333
mod owner;
3434
pub mod team;
3535
pub mod token;
36+
pub mod trustpub;
3637
pub mod user;
3738
pub mod version;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use crate::schema::trustpub_configs_github;
2+
use chrono::{DateTime, Utc};
3+
use diesel::prelude::*;
4+
use diesel_async::{AsyncPgConnection, RunQueryDsl};
5+
6+
#[derive(Debug, Identifiable, Queryable, Selectable)]
7+
#[diesel(table_name = trustpub_configs_github, check_for_backend(diesel::pg::Pg))]
8+
pub struct GitHubConfig {
9+
pub id: i32,
10+
pub crate_id: i32,
11+
pub repository_owner: String,
12+
pub repository_owner_id: i32,
13+
pub repository_name: String,
14+
pub workflow_filename: String,
15+
pub environment: Option<String>,
16+
pub created_at: DateTime<Utc>,
17+
}
18+
19+
#[derive(Debug, Insertable)]
20+
#[diesel(table_name = trustpub_configs_github, check_for_backend(diesel::pg::Pg))]
21+
pub struct NewGitHubConfig<'a> {
22+
pub crate_id: i32,
23+
pub repository_owner: &'a str,
24+
pub repository_owner_id: i32,
25+
pub repository_name: &'a str,
26+
pub workflow_filename: &'a str,
27+
pub environment: Option<&'a str>,
28+
}
29+
30+
impl NewGitHubConfig<'_> {
31+
pub async fn insert(&self, conn: &mut AsyncPgConnection) -> QueryResult<GitHubConfig> {
32+
self.insert_into(trustpub_configs_github::table)
33+
.returning(GitHubConfig::as_returning())
34+
.get_result(conn)
35+
.await
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod github_config;
2+
pub mod token;
3+
pub mod used_jti;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use crate::schema::trustpub_tokens;
2+
use chrono::{DateTime, Utc};
3+
use diesel::prelude::*;
4+
use diesel_async::{AsyncPgConnection, RunQueryDsl};
5+
6+
#[derive(Debug, Insertable)]
7+
#[diesel(table_name = trustpub_tokens, check_for_backend(diesel::pg::Pg))]
8+
pub struct NewToken<'a> {
9+
pub expires_at: DateTime<Utc>,
10+
pub hashed_token: &'a [u8],
11+
pub crate_ids: &'a [i32],
12+
}
13+
14+
impl NewToken<'_> {
15+
pub async fn insert(&self, conn: &mut AsyncPgConnection) -> QueryResult<()> {
16+
self.insert_into(trustpub_tokens::table)
17+
.execute(conn)
18+
.await?;
19+
20+
Ok(())
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::schema::trustpub_used_jtis;
2+
use chrono::{DateTime, Utc};
3+
use diesel::prelude::*;
4+
use diesel_async::{AsyncPgConnection, RunQueryDsl};
5+
6+
#[derive(Debug, Insertable)]
7+
#[diesel(table_name = trustpub_used_jtis, check_for_backend(diesel::pg::Pg))]
8+
pub struct NewUsedJti<'a> {
9+
pub jti: &'a str,
10+
pub expires_at: DateTime<Utc>,
11+
}
12+
13+
impl<'a> NewUsedJti<'a> {
14+
pub fn new(jti: &'a str, expires_at: DateTime<Utc>) -> Self {
15+
Self { jti, expires_at }
16+
}
17+
18+
pub async fn insert(&self, conn: &mut AsyncPgConnection) -> QueryResult<usize> {
19+
diesel::insert_into(trustpub_used_jtis::table)
20+
.values(self)
21+
.execute(conn)
22+
.await
23+
}
24+
}

crates/crates_io_database/src/models/version.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub struct NewVersion<'a> {
9191
license: Option<&'a str>,
9292
#[builder(default, name = "size")]
9393
crate_size: i32,
94-
published_by: i32,
94+
published_by: Option<i32>,
9595
checksum: &'a str,
9696
links: Option<&'a str>,
9797
rust_version: Option<&'a str>,
@@ -110,7 +110,7 @@ impl NewVersion<'_> {
110110
pub async fn save(
111111
&self,
112112
conn: &mut AsyncPgConnection,
113-
published_by_email: &str,
113+
published_by_email: Option<&str>,
114114
) -> QueryResult<Version> {
115115
use diesel::insert_into;
116116

@@ -122,13 +122,15 @@ impl NewVersion<'_> {
122122
.get_result(conn)
123123
.await?;
124124

125-
insert_into(versions_published_by::table)
126-
.values((
127-
versions_published_by::version_id.eq(version.id),
128-
versions_published_by::email.eq(published_by_email),
129-
))
130-
.execute(conn)
131-
.await?;
125+
if let Some(published_by_email) = published_by_email {
126+
insert_into(versions_published_by::table)
127+
.values((
128+
versions_published_by::version_id.eq(version.id),
129+
versions_published_by::email.eq(published_by_email),
130+
))
131+
.execute(conn)
132+
.await?;
133+
}
132134

133135
Ok(version)
134136
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "crates_io_trusted_publishing"
3+
version = "0.0.0"
4+
license = "MIT OR Apache-2.0"
5+
edition = "2024"
6+
7+
[lints]
8+
workspace = true
9+
10+
[features]
11+
mock = ["dep:mockall", "dep:serde_json"]
12+
13+
[dependencies]
14+
anyhow = "=1.0.98"
15+
async-trait = "=0.1.88"
16+
chrono = { version = "=0.4.40", features = ["serde"] }
17+
jsonwebtoken = "=9.3.1"
18+
mockall = { version = "=0.13.1", optional = true }
19+
reqwest = { version = "=0.12.15", features = ["gzip", "json"] }
20+
regex = "=1.11.1"
21+
serde = { version = "=1.0.219", features = ["derive"] }
22+
serde_json = { version = "=1.0.140", optional = true }
23+
tokio = { version = "=1.44.2", features = ["sync"] }
24+
25+
[dev-dependencies]
26+
insta = { version = "=1.43.0", features = ["json", "redactions"] }
27+
mockito = "=1.7.0"
28+
tokio = { version = "=1.44.2", features = ["macros", "rt-multi-thread"] }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# crates_io_trusted_publishing

0 commit comments

Comments
 (0)