Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

team_repo: Query permission instead of team #8047

Merged
merged 1 commit into from
Feb 1, 2024
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
15 changes: 6 additions & 9 deletions src/team_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@ use reqwest::{Certificate, Client};
#[automock]
#[async_trait]
pub trait TeamRepo {
async fn get_team(&self, name: &str) -> anyhow::Result<Team>;
async fn get_permission(&self, name: &str) -> anyhow::Result<Permission>;
}

#[derive(Debug, Clone, Deserialize)]
pub struct Team {
pub name: String,
pub kind: String,
pub members: Vec<Member>,
pub struct Permission {
pub people: Vec<Person>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct Member {
pub struct Person {
pub name: String,
pub github: String,
pub github_id: i32,
pub is_lead: bool,
}

pub struct TeamRepoImpl {
Expand Down Expand Up @@ -62,8 +59,8 @@ fn build_client() -> Client {

#[async_trait]
impl TeamRepo for TeamRepoImpl {
async fn get_team(&self, name: &str) -> anyhow::Result<Team> {
let url = format!("https://team-api.infra.rust-lang.org/v1/teams/{name}.json");
async fn get_permission(&self, name: &str) -> anyhow::Result<Permission> {
let url = format!("https://team-api.infra.rust-lang.org/v1/permissions/{name}.json");
let response = self.client.get(url).send().await?.error_for_status()?;
Ok(response.json().await?)
}
Expand Down
32 changes: 12 additions & 20 deletions src/tests/worker/sync_admins.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::util::TestApp;
use crates_io::schema::{emails, users};
use crates_io::team_repo::{Member, MockTeamRepo, Team};
use crates_io::team_repo::{MockTeamRepo, Permission, Person};
use crates_io::worker::jobs::SyncAdmins;
use crates_io_worker::BackgroundJob;
use diesel::prelude::*;
Expand All @@ -10,19 +10,16 @@ use regex::Regex;

#[test]
fn test_sync_admins_job() {
let mock_response = mock_team(
"crates-io",
vec![
mock_member("existing-admin", 1),
mock_member("new-admin", 3),
mock_member("new-admin-without-account", 4),
],
);
let mock_response = mock_permission(vec![
mock_person("existing-admin", 1),
mock_person("new-admin", 3),
mock_person("new-admin-without-account", 4),
]);

let mut team_repo = MockTeamRepo::new();
team_repo
.expect_get_team()
.with(mockall::predicate::eq("crates-io-admins"))
.expect_get_permission()
.with(mockall::predicate::eq("crates_io_admin"))
.returning(move |_| Ok(mock_response.clone()));

let (app, _) = TestApp::full().with_team_repo(team_repo).empty();
Expand Down Expand Up @@ -61,22 +58,17 @@ fn test_sync_admins_job() {
assert_eq!(emails.len(), 2);
}

fn mock_team(name: impl Into<String>, members: Vec<Member>) -> Team {
Team {
name: name.into(),
kind: "marker-team".to_string(),
members,
}
fn mock_permission(people: Vec<Person>) -> Permission {
Permission { people }
}

fn mock_member(name: impl Into<String>, github_id: i32) -> Member {
fn mock_person(name: impl Into<String>, github_id: i32) -> Person {
let name = name.into();
let github = name.clone();
Member {
Person {
name,
github,
github_id,
is_lead: false,
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/worker/jobs/sync_admins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use std::collections::HashSet;
use std::fmt::{Display, Formatter};
use std::sync::Arc;

/// See <https://github.com/rust-lang/team/blob/master/teams/crates-io-admins.toml>.
const TEAM_NAME: &str = "crates-io-admins";
/// See <https://github.com/rust-lang/team/pull/1197>.
const PERMISSION_NAME: &str = "crates_io_admin";

#[derive(Serialize, Deserialize)]
pub struct SyncAdmins;
Expand All @@ -23,7 +23,7 @@ impl BackgroundJob for SyncAdmins {
async fn run(&self, ctx: Self::Context) -> anyhow::Result<()> {
info!("Syncing admins from rust-lang/team repo…");

let repo_admins = ctx.team_repo.get_team(TEAM_NAME).await?.members;
let repo_admins = ctx.team_repo.get_permission(PERMISSION_NAME).await?.people;
let repo_admin_ids = repo_admins
.iter()
.map(|m| m.github_id)
Expand Down