Allow checking CLA signatures by GitHub login

This will be used by CLA Bot.

Co-authored-by: Marshall <marshall@zed.dev>
This commit is contained in:
Max Brunsfeld 2024-01-22 13:55:12 -08:00
parent 56556d71a1
commit cd7f377c31
3 changed files with 39 additions and 9 deletions

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
auth, auth,
db::{User, UserId}, db::{ContributorSelector, User, UserId},
rpc, AppState, Error, Result, rpc, AppState, Error, Result,
}; };
use anyhow::anyhow; use anyhow::anyhow;
@ -141,7 +141,24 @@ async fn get_contributors(Extension(app): Extension<Arc<AppState>>) -> Result<Js
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct CheckIsContributorParams { struct CheckIsContributorParams {
github_user_id: i32, github_user_id: Option<i32>,
github_login: Option<String>,
}
impl CheckIsContributorParams {
fn as_contributor_selector(self) -> Result<ContributorSelector> {
if let Some(github_user_id) = self.github_user_id {
return Ok(ContributorSelector::GitHubUserId { github_user_id });
}
if let Some(github_login) = self.github_login {
return Ok(ContributorSelector::GitHubLogin { github_login });
}
Err(anyhow!(
"must be one of `github_user_id` or `github_login`."
))?
}
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
@ -153,10 +170,11 @@ async fn check_is_contributor(
Extension(app): Extension<Arc<AppState>>, Extension(app): Extension<Arc<AppState>>,
Query(params): Query<CheckIsContributorParams>, Query(params): Query<CheckIsContributorParams>,
) -> Result<Json<CheckIsContributorResponse>> { ) -> Result<Json<CheckIsContributorResponse>> {
let params = params.as_contributor_selector()?;
Ok(Json(CheckIsContributorResponse { Ok(Json(CheckIsContributorResponse {
signed_at: app signed_at: app
.db .db
.get_contributor_sign_timestamp(params.github_user_id) .get_contributor_sign_timestamp(&params)
.await? .await?
.map(|ts| ts.and_utc().to_rfc3339_opts(SecondsFormat::Millis, true)), .map(|ts| ts.and_utc().to_rfc3339_opts(SecondsFormat::Millis, true)),
})) }))

View file

@ -44,6 +44,7 @@ use tables::*;
use tokio::sync::{Mutex, OwnedMutexGuard}; use tokio::sync::{Mutex, OwnedMutexGuard};
pub use ids::*; pub use ids::*;
pub use queries::contributors::ContributorSelector;
pub use sea_orm::ConnectOptions; pub use sea_orm::ConnectOptions;
pub use tables::user::Model as User; pub use tables::user::Model as User;

View file

@ -1,5 +1,11 @@
use super::*; use super::*;
#[derive(Debug)]
pub enum ContributorSelector {
GitHubUserId { github_user_id: i32 },
GitHubLogin { github_login: String },
}
impl Database { impl Database {
/// Retrieves the GitHub logins of all users who have signed the CLA. /// Retrieves the GitHub logins of all users who have signed the CLA.
pub async fn get_contributors(&self) -> Result<Vec<String>> { pub async fn get_contributors(&self) -> Result<Vec<String>> {
@ -24,14 +30,19 @@ impl Database {
/// Records that a given user has signed the CLA. /// Records that a given user has signed the CLA.
pub async fn get_contributor_sign_timestamp( pub async fn get_contributor_sign_timestamp(
&self, &self,
github_user_id: i32, selector: &ContributorSelector,
) -> Result<Option<DateTime>> { ) -> Result<Option<DateTime>> {
self.transaction(|tx| async move { self.transaction(|tx| async move {
let Some(user) = user::Entity::find() let condition = match selector {
.filter(user::Column::GithubUserId.eq(github_user_id)) ContributorSelector::GitHubUserId { github_user_id } => {
.one(&*tx) user::Column::GithubUserId.eq(*github_user_id)
.await? }
else { ContributorSelector::GitHubLogin { github_login } => {
user::Column::GithubLogin.eq(github_login)
}
};
let Some(user) = user::Entity::find().filter(condition).one(&*tx).await? else {
return Ok(None); return Ok(None);
}; };
let Some(contributor) = contributor::Entity::find_by_id(user.id).one(&*tx).await? let Some(contributor) = contributor::Entity::find_by_id(user.id).one(&*tx).await?