Skip to content

Commit 58f87c0

Browse files
committed
Github user id is not optional
This patch removes the Optional id in the GitHub User struct. Unsure why it was this way, but I think it is reasonable to expect that any GitHub User has an id.
1 parent 5b0aa6e commit 58f87c0

File tree

6 files changed

+37
-44
lines changed

6 files changed

+37
-44
lines changed

src/github.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use tracing as log;
1818
#[derive(Debug, PartialEq, Eq, serde::Deserialize)]
1919
pub struct User {
2020
pub login: String,
21-
pub id: Option<u64>,
21+
pub id: u64,
2222
}
2323

2424
impl GithubClient {
@@ -200,17 +200,20 @@ impl User {
200200
);
201201
Ok(in_all || is_triager || is_pri_member || is_async_member)
202202
}
203+
}
203204

204-
// Returns the ID of the given user, if the user is in the `all` team.
205-
pub async fn get_id<'a>(&'a self, client: &'a GithubClient) -> anyhow::Result<Option<u64>> {
206-
let permission = crate::team_data::teams(client).await?;
207-
let map = permission.teams;
208-
Ok(map["all"]
209-
.members
210-
.iter()
211-
.find(|g| g.github == self.login)
212-
.map(|u| u.github_id))
213-
}
205+
// Returns the ID of the given user, if the user is in the `all` team.
206+
pub async fn get_id_for_username<'a>(
207+
client: &'a GithubClient,
208+
login: &str,
209+
) -> anyhow::Result<Option<u64>> {
210+
let permission = crate::team_data::teams(client).await?;
211+
let map = permission.teams;
212+
Ok(map["all"]
213+
.members
214+
.iter()
215+
.find(|g| g.github == login)
216+
.map(|u| u.github_id))
214217
}
215218

216219
pub async fn get_team(
@@ -2670,7 +2673,7 @@ pub async fn retrieve_pull_requests(
26702673
prs_processed.push((
26712674
User {
26722675
login: user.login.clone(),
2673-
id: Some(user_id),
2676+
id: user_id,
26742677
},
26752678
pr.number,
26762679
));

src/handlers/notification.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! Parsing is done in the `parser::command::ping` module.
66
77
use crate::db::notifications;
8+
use crate::github::get_id_for_username;
89
use crate::{
910
github::{self, Event},
1011
handlers::Context,
@@ -58,7 +59,7 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
5859
if let Some(from) = event.comment_from() {
5960
for login in parser::get_mentions(from).into_iter() {
6061
if let Some((users, _)) = id_from_user(ctx, login).await? {
61-
users_notified.extend(users.into_iter().map(|user| user.id.unwrap()));
62+
users_notified.extend(users.into_iter().map(|user| user.id));
6263
}
6364
}
6465
};
@@ -68,7 +69,7 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
6869
//
6970
// If the user intended to ping themselves, they can add the GitHub comment
7071
// via the Zulip interface.
71-
match event.user().get_id(&ctx.github).await {
72+
match get_id_for_username(&ctx.github, &event.user().login).await {
7273
Ok(Some(id)) => {
7374
users_notified.insert(id.try_into().unwrap());
7475
}
@@ -86,12 +87,12 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
8687

8788
let client = ctx.db.get().await;
8889
for user in users {
89-
if !users_notified.insert(user.id.unwrap()) {
90+
if !users_notified.insert(user.id) {
9091
// Skip users already associated with this event.
9192
continue;
9293
}
9394

94-
if let Err(err) = notifications::record_username(&client, user.id.unwrap(), &user.login)
95+
if let Err(err) = notifications::record_username(&client, user.id, &user.login)
9596
.await
9697
.context("failed to record username")
9798
{
@@ -101,7 +102,7 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
101102
if let Err(err) = notifications::record_ping(
102103
&client,
103104
&notifications::Notification {
104-
user_id: user.id.unwrap(),
105+
user_id: user.id,
105106
origin_url: event.html_url().unwrap().to_owned(),
106107
origin_html: body.to_owned(),
107108
time: event.time().unwrap(),
@@ -166,30 +167,25 @@ async fn id_from_user(
166167
team.members
167168
.into_iter()
168169
.map(|member| github::User {
169-
id: Some(member.github_id),
170+
id: member.github_id,
170171
login: member.github,
171172
})
172173
.collect::<Vec<github::User>>(),
173174
Some(team.name),
174175
)))
175176
} else {
176-
let user = github::User {
177-
login: login.to_owned(),
178-
id: None,
179-
};
180-
let id = user
181-
.get_id(&ctx.github)
177+
let id = get_id_for_username(&ctx.github, login)
182178
.await
183-
.with_context(|| format!("failed to get user {} ID", user.login))?;
179+
.with_context(|| format!("failed to get user {} ID", login))?;
184180
let Some(id) = id else {
185181
// If the user was not in the team(s) then just don't record it.
186-
log::trace!("Skipping {} because no id found", user.login);
182+
log::trace!("Skipping {} because no id found", login);
187183
return Ok(None);
188184
};
189185
Ok(Some((
190186
vec![github::User {
191-
login: user.login.clone(),
192-
id: Some(id),
187+
login: login.to_string(),
188+
id,
193189
}],
194190
None,
195191
)))

src/handlers/pr_tracking.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,18 @@ pub(super) async fn handle_input<'a>(
5959
};
6060

6161
// ensure the team member object of this action exists in the `users` table
62-
record_username(&db_client, assignee.id.unwrap(), &assignee.login)
62+
record_username(&db_client, assignee.id, &assignee.login)
6363
.await
6464
.context("failed to record username")?;
6565

6666
if matches!(event.action, IssuesAction::Unassigned { .. }) {
67-
delete_pr_from_workqueue(&db_client, assignee.id.unwrap(), event.issue.number)
67+
delete_pr_from_workqueue(&db_client, assignee.id, event.issue.number)
6868
.await
6969
.context("Failed to remove PR from workqueue")?;
7070
}
7171

7272
if matches!(event.action, IssuesAction::Assigned { .. }) {
73-
upsert_pr_into_workqueue(&db_client, assignee.id.unwrap(), event.issue.number)
73+
upsert_pr_into_workqueue(&db_client, assignee.id, event.issue.number)
7474
.await
7575
.context("Failed to add PR to workqueue")?;
7676
}

src/handlers/pull_requests_assignment_update.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@ impl Job for PullRequestAssignmentUpdate {
3030

3131
// aggregate by user first
3232
let aggregated = prs.into_iter().fold(HashMap::new(), |mut acc, (user, pr)| {
33-
let (_, prs) = acc
34-
.entry(user.id.unwrap())
35-
.or_insert_with(|| (user, Vec::new()));
33+
let (_, prs) = acc.entry(user.id).or_insert_with(|| (user, Vec::new()));
3634
prs.push(pr);
3735
acc
3836
});
3937

4038
// populate the table
4139
for (_user_id, (assignee, prs)) in &aggregated {
42-
let assignee_id = assignee.id.expect("checked");
40+
let assignee_id = assignee.id;
4341
let _ = record_username(&db, assignee_id, &assignee.login).await;
4442
create_team_member_workqueue(&db, assignee_id, &prs).await?;
4543
}

src/handlers/rustc_commits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
3232
return Ok(());
3333
}
3434

35-
if event.comment.user.id != Some(BORS_GH_ID) {
35+
if event.comment.user.id != BORS_GH_ID {
3636
log::trace!("Ignoring non-bors comment, user: {:?}", event.comment.user);
3737
return Ok(());
3838
}

src/zulip.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::db::notifications::add_metadata;
22
use crate::db::notifications::{self, delete_ping, move_indices, record_ping, Identifier};
3-
use crate::github::{self, GithubClient};
3+
use crate::github::{get_id_for_username, GithubClient};
44
use crate::handlers::docs_update::docs_update;
55
use crate::handlers::pull_requests_assignment_update::get_review_prefs;
66
use crate::handlers::Context;
@@ -241,13 +241,9 @@ async fn execute_for_other_user(
241241
Some(username) => username,
242242
None => anyhow::bail!("no username provided"),
243243
};
244-
let user_id = match (github::User {
245-
login: username.to_owned(),
246-
id: None,
247-
})
248-
.get_id(&ctx.github)
249-
.await
250-
.context("getting ID of github user")?
244+
let user_id = match get_id_for_username(&ctx.github, username)
245+
.await
246+
.context("getting ID of github user")?
251247
{
252248
Some(id) => id.try_into().unwrap(),
253249
None => anyhow::bail!("Can only authorize for other GitHub users."),

0 commit comments

Comments
 (0)