Skip to content

Commit

Permalink
Simplify external API of diffing
Browse files Browse the repository at this point in the history
Also pass teams and repos to `create_diff` explicitly, which will allow easier mocking.
  • Loading branch information
Kobzol committed Jan 9, 2024
1 parent 0857aed commit 49479e3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/github/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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<rust_team_data::v1::Team>,
repos: Vec<rust_team_data::v1::Repo>,
) -> anyhow::Result<Diff> {
let github = SyncGitHub::new(github, teams, repos)?;
github.diff_all()
}

struct SyncGitHub {
github: GitHubRead,
teams: Vec<rust_team_data::v1::Team>,
repos: Vec<rust_team_data::v1::Repo>,
Expand All @@ -21,10 +30,11 @@ pub(crate) struct SyncGitHub {
}

impl SyncGitHub {
pub(crate) fn new(github: GitHubRead, team_api: &TeamApi) -> anyhow::Result<Self> {
let teams = team_api.get_teams()?;
let repos = team_api.get_repos()?;

pub(crate) fn new(
github: GitHubRead,
teams: Vec<rust_team_data::v1::Team>,
repos: Vec<rust_team_data::v1::Repo>,
) -> anyhow::Result<Self> {
debug!("caching mapping between user ids and usernames");
let users = teams
.iter()
Expand Down
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)?;
Expand Down

0 comments on commit 49479e3

Please sign in to comment.