Skip to content

Commit e8479b5

Browse files
authored
Merge pull request #11113 from Turbo87/trustpub-create-config
Implement `PUT /api/v1/trusted_publishing/github_configs` API endpoint
2 parents a492e2d + ea00c1c commit e8479b5

25 files changed

+1059
-0
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ crates_io_pagerduty = { path = "crates/crates_io_pagerduty" }
7474
crates_io_session = { path = "crates/crates_io_session" }
7575
crates_io_tarball = { path = "crates/crates_io_tarball" }
7676
crates_io_team_repo = { path = "crates/crates_io_team_repo" }
77+
crates_io_trustpub = { path = "crates/crates_io_trustpub" }
7778
crates_io_worker = { path = "crates/crates_io_worker" }
7879
csv = "=1.3.1"
7980
chrono = { version = "=0.4.41", default-features = false, features = ["serde"] }

crates/crates_io_database/src/models/mod.rs

Lines changed: 1 addition & 0 deletions
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;
Lines changed: 37 additions & 0 deletions
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 created_at: DateTime<Utc>,
11+
pub crate_id: i32,
12+
pub repository_owner: String,
13+
pub repository_owner_id: i32,
14+
pub repository_name: String,
15+
pub workflow_filename: String,
16+
pub environment: Option<String>,
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+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod github_config;
2+
3+
pub use self::github_config::{GitHubConfig, NewGitHubConfig};

crates/crates_io_github/examples/test_github_client.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ enum Request {
1212
#[clap(long, env = "GITHUB_ACCESS_TOKEN", hide_env_values = true)]
1313
access_token: SecretString,
1414
},
15+
GetUser {
16+
name: String,
17+
#[clap(long, env = "GITHUB_ACCESS_TOKEN", hide_env_values = true)]
18+
access_token: SecretString,
19+
},
1520
OrgByName {
1621
org_name: String,
1722
#[clap(long, env = "GITHUB_ACCESS_TOKEN", hide_env_values = true)]
@@ -58,6 +63,11 @@ async fn main() -> Result<()> {
5863
let response = github_client.current_user(&access_token).await?;
5964
println!("{response:#?}");
6065
}
66+
Request::GetUser { name, access_token } => {
67+
let access_token = AccessToken::new(access_token.expose_secret().into());
68+
let response = github_client.get_user(&name, &access_token).await?;
69+
println!("{response:#?}");
70+
}
6171
Request::OrgByName {
6272
org_name,
6373
access_token,

crates/crates_io_github/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Result<T> = std::result::Result<T, GitHubError>;
2020
#[async_trait]
2121
pub trait GitHubClient: Send + Sync {
2222
async fn current_user(&self, auth: &AccessToken) -> Result<GitHubUser>;
23+
async fn get_user(&self, name: &str, auth: &AccessToken) -> Result<GitHubUser>;
2324
async fn org_by_name(&self, org_name: &str, auth: &AccessToken) -> Result<GitHubOrganization>;
2425
async fn team_by_name(
2526
&self,
@@ -102,6 +103,11 @@ impl GitHubClient for RealGitHubClient {
102103
self.request("/user", auth).await
103104
}
104105

106+
async fn get_user(&self, name: &str, auth: &AccessToken) -> Result<GitHubUser> {
107+
let url = format!("/users/{name}");
108+
self.request(&url, auth).await
109+
}
110+
105111
async fn org_by_name(&self, org_name: &str, auth: &AccessToken) -> Result<GitHubOrganization> {
106112
let url = format!("/orgs/{org_name}");
107113
self.request(&url, auth).await

crates/crates_io_trustpub/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "crates_io_trustpub"
3+
version = "0.0.0"
4+
license = "MIT OR Apache-2.0"
5+
edition = "2024"
6+
7+
[lints]
8+
workspace = true
9+
10+
[dependencies]
11+
regex = "=1.11.1"
12+
thiserror = "=2.0.12"
13+
14+
[dev-dependencies]
15+
claims = "=0.8.0"
16+
insta = "=1.43.1"

crates/crates_io_trustpub/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# crates_io_trustpub
2+
3+
This crate contains code related to the "[Trusted Publishing](https://github.com/rust-lang/rfcs/pull/3691)" feature of crates.io.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod validation;

0 commit comments

Comments
 (0)