Skip to content

Commit db62001

Browse files
authored
models/team: Replace NewTeam::new() fn with Builder pattern (#9990)
Having two `i32` arguments in the `new()` fn was a bit dangerous and could easily lead to someone unintentionally swapping the values. This commit changes the `NewTeam` construction to use the builder pattern with named fns instead to avoid the potential confusion.
1 parent d8888c6 commit db62001

File tree

4 files changed

+42
-55
lines changed

4 files changed

+42
-55
lines changed

src/models/team.rs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use bon::Builder;
12
use diesel_async::AsyncPgConnection;
23
use http::StatusCode;
34

@@ -35,7 +36,7 @@ pub struct Team {
3536
pub org_id: Option<i32>,
3637
}
3738

38-
#[derive(Insertable, AsChangeset, Debug)]
39+
#[derive(Insertable, AsChangeset, Debug, Builder)]
3940
#[diesel(table_name = teams, check_for_backend(diesel::pg::Pg))]
4041
pub struct NewTeam<'a> {
4142
pub login: &'a str,
@@ -46,22 +47,6 @@ pub struct NewTeam<'a> {
4647
}
4748

4849
impl<'a> NewTeam<'a> {
49-
pub fn new(
50-
login: &'a str,
51-
org_id: i32,
52-
github_id: i32,
53-
name: Option<&'a str>,
54-
avatar: Option<&'a str>,
55-
) -> Self {
56-
NewTeam {
57-
login,
58-
github_id,
59-
name,
60-
avatar,
61-
org_id,
62-
}
63-
}
64-
6550
pub fn create_or_update(&self, conn: &mut impl Conn) -> QueryResult<Team> {
6651
use diesel::insert_into;
6752
use diesel::RunQueryDsl;
@@ -174,15 +159,15 @@ impl Team {
174159

175160
let org = Handle::current().block_on(app.github.org_by_name(org_name, &token))?;
176161

177-
NewTeam::new(
178-
&login.to_lowercase(),
179-
org_id,
180-
team.id,
181-
team.name.as_deref(),
182-
org.avatar_url.as_deref(),
183-
)
184-
.create_or_update(conn)
185-
.map_err(Into::into)
162+
NewTeam::builder()
163+
.login(&login.to_lowercase())
164+
.org_id(org_id)
165+
.github_id(team.id)
166+
.maybe_name(team.name.as_deref())
167+
.maybe_avatar(org.avatar_url.as_deref())
168+
.build()
169+
.create_or_update(conn)
170+
.map_err(Into::into)
186171
}
187172

188173
/// Phones home to Github to ask if this User is a member of the given team.

src/tests/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,11 @@ fn new_user(login: &str) -> NewUser<'_> {
104104
}
105105

106106
fn new_team(login: &str) -> NewTeam<'_> {
107-
NewTeam {
108-
org_id: next_gh_id(),
109-
github_id: next_gh_id(),
110-
login,
111-
name: None,
112-
avatar: None,
113-
}
107+
NewTeam::builder()
108+
.login(login)
109+
.org_id(next_gh_id())
110+
.github_id(next_gh_id())
111+
.build()
114112
}
115113

116114
fn add_team_to_crate(

src/tests/team.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,16 @@ async fn add_renamed_team() {
9595

9696
// create team with same ID and different name compared to http mock
9797
// used for `async_add_named_owner`.await
98-
NewTeam::new(
99-
"github:test-org:old-core", // different team name
100-
1000, // same org ID
101-
2001, // same team id as `core` team
102-
None,
103-
None,
104-
)
105-
.create_or_update(&mut conn)
106-
.unwrap();
98+
let new_team = NewTeam::builder()
99+
// different team name
100+
.login("github:test-org:old-core")
101+
// same org ID
102+
.org_id(1000)
103+
// same team id as `core` team
104+
.github_id(2001)
105+
.build();
106+
107+
new_team.create_or_update(&mut conn).unwrap();
107108

108109
assert_eq!(
109110
teams::table.count().get_result::<i64>(&mut conn).unwrap(),
@@ -437,9 +438,13 @@ async fn crates_by_team_id_not_including_deleted_owners() {
437438
let user = app.db_new_user("user-all-teams").await;
438439
let user = user.as_model();
439440

440-
let t = NewTeam::new("github:test-org:core", 1000, 2001, None, None)
441-
.create_or_update(&mut conn)
442-
.unwrap();
441+
let new_team = NewTeam::builder()
442+
.login("github:test-org:core")
443+
.org_id(1000)
444+
.github_id(2001)
445+
.build();
446+
447+
let t = new_team.create_or_update(&mut conn).unwrap();
443448

444449
let krate = CrateBuilder::new("foo", user.id).expect_build(&mut conn);
445450
add_team_to_crate(&t, &krate, user, &mut conn).unwrap();

src/typosquat/test_util.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,15 @@ pub mod faker {
4848
}
4949

5050
pub fn team(conn: &mut PgConnection, org: &str, team: &str) -> anyhow::Result<Owner> {
51-
Ok(Owner::Team(
52-
NewTeam::new(
53-
&format!("github:{org}:{team}"),
54-
next_gh_id(),
55-
next_gh_id(),
56-
Some(team),
57-
None,
58-
)
59-
.create_or_update(conn)?,
60-
))
51+
let login = format!("github:{org}:{team}");
52+
let team = NewTeam::builder()
53+
.login(&login)
54+
.org_id(next_gh_id())
55+
.github_id(next_gh_id())
56+
.name(team)
57+
.build();
58+
59+
Ok(Owner::Team(team.create_or_update(conn)?))
6160
}
6261

6362
pub fn user(conn: &mut PgConnection, login: &str) -> QueryResult<User> {

0 commit comments

Comments
 (0)