Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod issue_data;
pub mod jobs;
pub mod notifications;
pub mod rustc_commits;
pub mod users;

const CERT_URL: &str = "https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem";

Expand Down
11 changes: 0 additions & 11 deletions src/db/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ pub struct Notification {
pub team_name: Option<String>,
}

/// Add a new user (if not existing)
pub async fn record_username(db: &DbClient, user_id: u64, username: &str) -> anyhow::Result<()> {
db.execute(
"INSERT INTO users (user_id, username) VALUES ($1, $2) ON CONFLICT DO NOTHING",
&[&(user_id as i64), &username],
)
.await
.context("inserting user id / username")?;
Ok(())
}

pub async fn record_ping(db: &DbClient, notification: &Notification) -> anyhow::Result<()> {
db.execute("INSERT INTO notifications (user_id, origin_url, origin_html, time, short_description, team_name, idx)
VALUES (
Expand Down
60 changes: 60 additions & 0 deletions src/db/users.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::github::User;
use anyhow::Context;
use tokio_postgres::Client as DbClient;

/// Add a new user.
/// If an user already exists, updates their username.
pub async fn record_username(db: &DbClient, user_id: u64, username: &str) -> anyhow::Result<()> {
db.execute(
r"
INSERT INTO users (user_id, username) VALUES ($1, $2)
ON CONFLICT (user_id)
DO UPDATE SET username = $2",
&[&(user_id as i64), &username],
)
.await
.context("inserting user id / username")?;
Ok(())
}

/// Return a user from the DB.
pub async fn get_user(db: &DbClient, user_id: u64) -> anyhow::Result<Option<User>> {
let row = db
.query_opt(
r"
SELECT username
FROM users
WHERE user_id = $1;",
&[&(user_id as i64)],
)
.await
.context("cannot load user from DB")?;
Ok(row.map(|row| {
let username: &str = row.get(0);
User {
id: user_id,
login: username.to_string(),
}
}))
Comment on lines +32 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you SELECT * then you can just Ok(row.into()) but it's a matter of preference ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't currently implement From<tokio_postgres::Row> for User anywhere :( Which goes back to my comment that we kind of misuse the GitHub user to also mean the database user.

Introducing a separate type would mean doing some conversions, but I guess that if we want to keep the users table, it's the Right Thing™ to do, in the long term. What do you think? :)

}

#[cfg(test)]
mod tests {
use crate::db::users::{get_user, record_username};
use crate::tests::run_test;

#[tokio::test]
async fn update_username_on_conflict() {
run_test(|ctx| async {
let db = ctx.db_client().await;

record_username(&db, 1, "Foo").await?;
record_username(&db, 1, "Bar").await?;

assert_eq!(get_user(&db, 1).await?.unwrap().login, "Bar");

Ok(ctx)
})
.await;
}
}
4 changes: 2 additions & 2 deletions src/handlers/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!
//! Parsing is done in the `parser::command::ping` module.

use crate::db::notifications;
use crate::db::{notifications, users};
use crate::github::get_id_for_username;
use crate::{
github::{self, Event},
Expand Down Expand Up @@ -92,7 +92,7 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
continue;
}

if let Err(err) = notifications::record_username(&client, user.id, &user.login)
if let Err(err) = users::record_username(&client, user.id, &user.login)
.await
.context("failed to record username")
{
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/pr_tracking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
//! - Removes the PR from the workqueue of one team member (after the PR has been unassigned or closed)

use super::assign::{FindReviewerError, REVIEWER_HAS_NO_CAPACITY, SELF_ASSIGN_HAS_NO_CAPACITY};
use crate::db::users::record_username;
use crate::github::User;
use crate::{
config::ReviewPrefsConfig,
db::notifications::record_username,
github::{IssuesAction, IssuesEvent},
handlers::Context,
ReviewPrefs,
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/pull_requests_assignment_update.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use crate::db::notifications::record_username;
use crate::db::users::record_username;
use crate::github::retrieve_pull_requests;
use crate::jobs::Job;
use crate::ReviewPrefs;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::db;
use crate::db::notifications::record_username;
use crate::db::users::record_username;
use crate::db::{make_client, ClientPool, PooledClient};
use crate::github::GithubClient;
use crate::handlers::Context;
Expand Down