Skip to content

Commit 8028f47

Browse files
maxdeviantnoaccOS
authored andcommitted
collab: Fix GitHub user retrieval in seed script (zed-industries#18296)
This PR fixes the GitHub user retrieval in the database seed script. The users returned from the [list users](https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#list-users) endpoint don't have a `created_at` timestamp, so we need to fetch them individually. I want to rework this further at a later date, this is just a bandaid to get things working again. Release Notes: - N/A
1 parent a796da1 commit 8028f47

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

crates/collab/src/db/queries/users.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,12 @@ impl Database {
298298
result
299299
}
300300

301+
/// Returns all feature flags.
302+
pub async fn list_feature_flags(&self) -> Result<Vec<feature_flag::Model>> {
303+
self.transaction(|tx| async move { Ok(feature_flag::Entity::find().all(&*tx).await?) })
304+
.await
305+
}
306+
301307
/// Creates a new feature flag.
302308
pub async fn create_user_flag(&self, flag: &str, enabled_for_all: bool) -> Result<FlagId> {
303309
self.transaction(|tx| async move {

crates/collab/src/seed.rs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,23 @@ struct GithubUser {
1616
created_at: DateTime<Utc>,
1717
}
1818

19+
/// A GitHub user returned from the [List users](https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#list-users) endpoint.
20+
///
21+
/// Notably, this data type does not have the `created_at` field.
22+
#[derive(Debug, Deserialize)]
23+
struct ListGithubUser {
24+
id: i32,
25+
login: String,
26+
email: Option<String>,
27+
}
28+
1929
#[derive(Deserialize)]
2030
struct SeedConfig {
21-
// Which users to create as admins.
31+
/// Which users to create as admins.
2232
admins: Vec<String>,
23-
// Which channels to create (all admins are invited to all channels)
33+
/// Which channels to create (all admins are invited to all channels).
2434
channels: Vec<String>,
25-
// Number of random users to create from the Github API
35+
/// Number of random users to create from the Github API.
2636
number_of_users: Option<usize>,
2737
}
2838

@@ -47,11 +57,21 @@ pub async fn seed(config: &Config, db: &Database, force: bool) -> anyhow::Result
4757
let flag_names = ["remoting", "language-models"];
4858
let mut flags = Vec::new();
4959

60+
let existing_feature_flags = db.list_feature_flags().await?;
61+
5062
for flag_name in flag_names {
63+
if existing_feature_flags
64+
.iter()
65+
.any(|flag| flag.flag == flag_name)
66+
{
67+
log::info!("Flag {flag_name:?} already exists");
68+
continue;
69+
}
70+
5171
let flag = db
5272
.create_user_flag(flag_name, false)
5373
.await
54-
.unwrap_or_else(|_| panic!("failed to create flag: '{flag_name}'"));
74+
.unwrap_or_else(|err| panic!("failed to create flag: '{flag_name}': {err}"));
5575
flags.push(flag);
5676
}
5777

@@ -121,9 +141,19 @@ pub async fn seed(config: &Config, db: &Database, force: bool) -> anyhow::Result
121141
if let Some(last_user_id) = last_user_id {
122142
write!(&mut uri, "&since={}", last_user_id).unwrap();
123143
}
124-
let users = fetch_github::<Vec<GithubUser>>(&client, &uri).await;
144+
let users = fetch_github::<Vec<ListGithubUser>>(&client, &uri).await;
125145

126146
for github_user in users {
147+
log::info!("Seeding {:?} from GitHub", github_user.login);
148+
149+
// Fetch the user to get their `created_at` timestamp, since it
150+
// isn't on the list response.
151+
let github_user: GithubUser = fetch_github(
152+
&client,
153+
&format!("https://api.github.com/user/{}", github_user.id),
154+
)
155+
.await;
156+
127157
last_user_id = Some(github_user.id);
128158
user_count += 1;
129159
let user = db
@@ -143,6 +173,9 @@ pub async fn seed(config: &Config, db: &Database, force: bool) -> anyhow::Result
143173
flag, user.id
144174
))?;
145175
}
176+
177+
// Sleep to avoid getting rate-limited by GitHub.
178+
tokio::time::sleep(std::time::Duration::from_millis(250)).await;
146179
}
147180
}
148181
}

0 commit comments

Comments
 (0)