From 49479e36dabd792f57a548c50068f4bc3df20da4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Tue, 9 Jan 2024 16:50:33 +0100 Subject: [PATCH] Simplify external API of diffing Also pass teams and repos to `create_diff` explicitly, which will allow easier mocking. --- src/github/mod.rs | 22 ++++++++++++++++------ src/main.rs | 7 ++++--- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/github/mod.rs b/src/github/mod.rs index 6aacca9..0451662 100644 --- a/src/github/mod.rs +++ b/src/github/mod.rs @@ -1,7 +1,7 @@ mod api; use self::api::{BranchProtectionOp, TeamPrivacy, TeamRole}; -use crate::{github::api::RepoPermission, TeamApi}; +use crate::github::api::RepoPermission; use log::debug; use rust_team_data::v1::Bot; use std::collections::{HashMap, HashSet}; @@ -12,7 +12,16 @@ pub(crate) use self::api::{GitHubRead, GitHubWrite}; static DEFAULT_DESCRIPTION: &str = "Managed by the rust-lang/team repository."; static DEFAULT_PRIVACY: TeamPrivacy = TeamPrivacy::Closed; -pub(crate) struct SyncGitHub { +pub(crate) fn create_diff( + github: GitHubRead, + teams: Vec, + repos: Vec, +) -> anyhow::Result { + let github = SyncGitHub::new(github, teams, repos)?; + github.diff_all() +} + +struct SyncGitHub { github: GitHubRead, teams: Vec, repos: Vec, @@ -21,10 +30,11 @@ pub(crate) struct SyncGitHub { } impl SyncGitHub { - pub(crate) fn new(github: GitHubRead, team_api: &TeamApi) -> anyhow::Result { - let teams = team_api.get_teams()?; - let repos = team_api.get_repos()?; - + pub(crate) fn new( + github: GitHubRead, + teams: Vec, + repos: Vec, + ) -> anyhow::Result { debug!("caching mapping between user ids and usernames"); let users = teams .iter() diff --git a/src/main.rs b/src/main.rs index b214600..a2e04e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ mod team_api; mod utils; mod zulip; -use crate::github::{GitHubRead, GitHubWrite, SyncGitHub}; +use crate::github::{create_diff, GitHubRead, GitHubWrite}; use crate::team_api::TeamApi; use anyhow::Context; use log::{error, info, warn}; @@ -82,8 +82,9 @@ fn app() -> anyhow::Result<()> { "github" => { let token = get_env("GITHUB_TOKEN")?; let gh_read = GitHubRead::new(token.clone())?; - let sync = SyncGitHub::new(gh_read, &team_api)?; - let diff = sync.diff_all()?; + let teams = team_api.get_teams()?; + let repos = team_api.get_repos()?; + let diff = create_diff(gh_read, teams, repos)?; info!("{}", diff); if !only_print_plan { let gh_write = GitHubWrite::new(token, dry_run)?;