Skip to content

Commit 0fe372b

Browse files
authored
Merge pull request #1908 from Kobzol/user-db
Extend user loading from the DB
2 parents 4188c2a + c7f47c3 commit 0fe372b

File tree

7 files changed

+66
-16
lines changed

7 files changed

+66
-16
lines changed

src/db.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod issue_data;
1111
pub mod jobs;
1212
pub mod notifications;
1313
pub mod rustc_commits;
14+
pub mod users;
1415

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

src/db/notifications.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,6 @@ pub struct Notification {
1515
pub team_name: Option<String>,
1616
}
1717

18-
/// Add a new user (if not existing)
19-
pub async fn record_username(db: &DbClient, user_id: u64, username: &str) -> anyhow::Result<()> {
20-
db.execute(
21-
"INSERT INTO users (user_id, username) VALUES ($1, $2) ON CONFLICT DO NOTHING",
22-
&[&(user_id as i64), &username],
23-
)
24-
.await
25-
.context("inserting user id / username")?;
26-
Ok(())
27-
}
28-
2918
pub async fn record_ping(db: &DbClient, notification: &Notification) -> anyhow::Result<()> {
3019
db.execute("INSERT INTO notifications (user_id, origin_url, origin_html, time, short_description, team_name, idx)
3120
VALUES (

src/db/users.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use crate::github::User;
2+
use anyhow::Context;
3+
use tokio_postgres::Client as DbClient;
4+
5+
/// Add a new user.
6+
/// If an user already exists, updates their username.
7+
pub async fn record_username(db: &DbClient, user_id: u64, username: &str) -> anyhow::Result<()> {
8+
db.execute(
9+
r"
10+
INSERT INTO users (user_id, username) VALUES ($1, $2)
11+
ON CONFLICT (user_id)
12+
DO UPDATE SET username = $2",
13+
&[&(user_id as i64), &username],
14+
)
15+
.await
16+
.context("inserting user id / username")?;
17+
Ok(())
18+
}
19+
20+
/// Return a user from the DB.
21+
pub async fn get_user(db: &DbClient, user_id: u64) -> anyhow::Result<Option<User>> {
22+
let row = db
23+
.query_opt(
24+
r"
25+
SELECT username
26+
FROM users
27+
WHERE user_id = $1;",
28+
&[&(user_id as i64)],
29+
)
30+
.await
31+
.context("cannot load user from DB")?;
32+
Ok(row.map(|row| {
33+
let username: &str = row.get(0);
34+
User {
35+
id: user_id,
36+
login: username.to_string(),
37+
}
38+
}))
39+
}
40+
41+
#[cfg(test)]
42+
mod tests {
43+
use crate::db::users::{get_user, record_username};
44+
use crate::tests::run_test;
45+
46+
#[tokio::test]
47+
async fn update_username_on_conflict() {
48+
run_test(|ctx| async {
49+
let db = ctx.db_client().await;
50+
51+
record_username(&db, 1, "Foo").await?;
52+
record_username(&db, 1, "Bar").await?;
53+
54+
assert_eq!(get_user(&db, 1).await?.unwrap().login, "Bar");
55+
56+
Ok(ctx)
57+
})
58+
.await;
59+
}
60+
}

src/handlers/notification.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//!
55
//! Parsing is done in the `parser::command::ping` module.
66
7-
use crate::db::notifications;
7+
use crate::db::{notifications, users};
88
use crate::github::get_id_for_username;
99
use crate::{
1010
github::{self, Event},
@@ -92,7 +92,7 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
9292
continue;
9393
}
9494

95-
if let Err(err) = notifications::record_username(&client, user.id, &user.login)
95+
if let Err(err) = users::record_username(&client, user.id, &user.login)
9696
.await
9797
.context("failed to record username")
9898
{

src/handlers/pr_tracking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
//! - Removes the PR from the workqueue of one team member (after the PR has been unassigned or closed)
88
99
use super::assign::{FindReviewerError, REVIEWER_HAS_NO_CAPACITY, SELF_ASSIGN_HAS_NO_CAPACITY};
10+
use crate::db::users::record_username;
1011
use crate::github::User;
1112
use crate::{
1213
config::ReviewPrefsConfig,
13-
db::notifications::record_username,
1414
github::{IssuesAction, IssuesEvent},
1515
handlers::Context,
1616
ReviewPrefs,

src/handlers/pull_requests_assignment_update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashMap;
22

3-
use crate::db::notifications::record_username;
3+
use crate::db::users::record_username;
44
use crate::github::retrieve_pull_requests;
55
use crate::jobs::Job;
66
use crate::ReviewPrefs;

src/tests/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::db;
2-
use crate::db::notifications::record_username;
2+
use crate::db::users::record_username;
33
use crate::db::{make_client, ClientPool, PooledClient};
44
use crate::github::GithubClient;
55
use crate::handlers::Context;

0 commit comments

Comments
 (0)