Skip to content

Commit a9f0337

Browse files
committed
refactor: code clean up
1 parent fb88ac8 commit a9f0337

File tree

4 files changed

+181
-9
lines changed

4 files changed

+181
-9
lines changed

sync-team/src/github/api/read.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::github::api::Ruleset;
12
use crate::github::api::{
23
BranchProtection, GraphNode, GraphNodes, GraphPageInfo, HttpClient, Login, Repo, RepoTeam,
34
RepoUser, Team, TeamMember, TeamRole, team_node_id, url::GitHubUrl, user_node_id,
@@ -550,8 +551,6 @@ impl GithubRead for GitHubApiRead {
550551
org: &str,
551552
repo: &str,
552553
) -> anyhow::Result<Vec<crate::github::api::Ruleset>> {
553-
use crate::github::api::Ruleset;
554-
555554
#[derive(serde::Deserialize)]
556555
struct RulesetsResponse {
557556
#[serde(default)]

sync-team/src/github/api/write.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,23 @@ impl GitHubWrite {
517517
);
518518
self.upsert_environment(org, repo, name, branches, tags)
519519
}
520+
521+
/// Update an environment in a repository
522+
pub(crate) fn update_environment(
523+
&self,
524+
org: &str,
525+
repo: &str,
526+
name: &str,
527+
branches: &[String],
528+
tags: &[String],
529+
) -> anyhow::Result<()> {
530+
debug!(
531+
"Updating environment '{name}' in '{org}/{repo}' with branches: {:?}, tags: {:?}",
532+
branches, tags
533+
);
534+
self.upsert_environment(org, repo, name, branches, tags)
535+
}
536+
520537
/// Internal helper to create or update an environment
521538
fn upsert_environment(
522539
&self,

sync-team/src/github/mod.rs

Lines changed: 117 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,8 @@ impl SyncGitHub {
390390

391391
let permission_diffs = self.diff_permissions(expected_repo)?;
392392

393-
// Always diff branch protections
394393
let branch_protection_diffs = self.diff_branch_protections(&actual_repo, expected_repo)?;
395394

396-
// Additionally diff rulesets if configured
397395
let ruleset_diffs = if self.should_use_rulesets(expected_repo) {
398396
self.diff_rulesets(expected_repo)?
399397
} else {
@@ -520,12 +518,12 @@ impl SyncGitHub {
520518
) -> anyhow::Result<Vec<EnvironmentDiff>> {
521519
let mut environment_diffs = Vec::new();
522520

523-
let actual_environments: HashSet<String> = self
521+
let actual_environments_map = self
524522
.github
525-
.repo_environments(&expected_repo.org, &expected_repo.name)?
526-
.into_keys()
527-
.collect();
523+
.repo_environments(&expected_repo.org, &expected_repo.name)?;
528524

525+
let actual_environments: HashSet<String> =
526+
actual_environments_map.keys().cloned().collect();
529527
let expected_environments: HashSet<String> =
530528
expected_repo.environments.keys().cloned().collect();
531529

@@ -540,6 +538,68 @@ impl SyncGitHub {
540538
environment_diffs.push(EnvironmentDiff::Create(env_name, env.clone()));
541539
}
542540

541+
// Environments to update (sorted for deterministic output)
542+
let mut to_update: Vec<_> = expected_environments
543+
.intersection(&actual_environments)
544+
.cloned()
545+
.collect();
546+
to_update.sort();
547+
for env_name in to_update {
548+
let expected_env = expected_repo.environments.get(&env_name).unwrap();
549+
let actual_env = actual_environments_map.get(&env_name).unwrap();
550+
551+
let expected_branches: HashSet<_> = expected_env.branches.iter().collect();
552+
let actual_branches: HashSet<_> = actual_env.branches.iter().collect();
553+
554+
let expected_tags: HashSet<_> = expected_env.tags.iter().collect();
555+
let actual_tags: HashSet<_> = actual_env.tags.iter().collect();
556+
557+
let mut add_branches: Vec<_> = expected_branches
558+
.difference(&actual_branches)
559+
.map(|s| s.to_string())
560+
.collect();
561+
add_branches.sort();
562+
563+
let mut remove_branches: Vec<_> = actual_branches
564+
.difference(&expected_branches)
565+
.map(|s| s.to_string())
566+
.collect();
567+
remove_branches.sort();
568+
569+
let mut add_tags: Vec<_> = expected_tags
570+
.difference(&actual_tags)
571+
.map(|s| s.to_string())
572+
.collect();
573+
add_tags.sort();
574+
575+
let mut remove_tags: Vec<_> = actual_tags
576+
.difference(&expected_tags)
577+
.map(|s| s.to_string())
578+
.collect();
579+
remove_tags.sort();
580+
581+
if !add_branches.is_empty()
582+
|| !remove_branches.is_empty()
583+
|| !add_tags.is_empty()
584+
|| !remove_tags.is_empty()
585+
{
586+
let mut new_branches = expected_env.branches.clone();
587+
new_branches.sort();
588+
let mut new_tags = expected_env.tags.clone();
589+
new_tags.sort();
590+
591+
environment_diffs.push(EnvironmentDiff::Update {
592+
name: env_name,
593+
add_branches,
594+
remove_branches,
595+
add_tags,
596+
remove_tags,
597+
new_branches,
598+
new_tags,
599+
});
600+
}
601+
}
602+
543603
// Environments to delete (sorted for deterministic output)
544604
let mut to_delete: Vec<_> = actual_environments
545605
.difference(&expected_environments)
@@ -1161,6 +1221,15 @@ struct UpdateRepoDiff {
11611221
#[derive(Debug)]
11621222
enum EnvironmentDiff {
11631223
Create(String, rust_team_data::v1::Environment),
1224+
Update {
1225+
name: String,
1226+
add_branches: Vec<String>,
1227+
remove_branches: Vec<String>,
1228+
add_tags: Vec<String>,
1229+
remove_tags: Vec<String>,
1230+
new_branches: Vec<String>,
1231+
new_tags: Vec<String>,
1232+
},
11641233
Delete(String),
11651234
}
11661235

@@ -1230,6 +1299,14 @@ impl UpdateRepoDiff {
12301299
EnvironmentDiff::Create(name, env) => {
12311300
sync.create_environment(&self.org, &self.name, name, &env.branches, &env.tags)?;
12321301
}
1302+
EnvironmentDiff::Update {
1303+
name,
1304+
new_branches,
1305+
new_tags,
1306+
..
1307+
} => {
1308+
sync.update_environment(&self.org, &self.name, name, new_branches, new_tags)?;
1309+
}
12331310
EnvironmentDiff::Delete(name) => {
12341311
sync.delete_environment(&self.org, &self.name, name)?;
12351312
}
@@ -1325,6 +1402,40 @@ impl std::fmt::Display for UpdateRepoDiff {
13251402
writeln!(f, " Tags: {}", env.tags.join(", "))?;
13261403
}
13271404
}
1405+
EnvironmentDiff::Update {
1406+
name,
1407+
add_branches,
1408+
remove_branches,
1409+
add_tags,
1410+
remove_tags,
1411+
new_branches: _,
1412+
new_tags: _,
1413+
} => {
1414+
writeln!(f, " 🔄 Update: {name}")?;
1415+
if !add_branches.is_empty() {
1416+
writeln!(f, " Adding branches: {}", add_branches.join(", "))?;
1417+
}
1418+
if !remove_branches.is_empty() {
1419+
writeln!(
1420+
f,
1421+
" Removing branches: {}",
1422+
remove_branches.join(", ")
1423+
)?;
1424+
}
1425+
if !add_tags.is_empty() {
1426+
writeln!(f, " Adding tags: {}", add_tags.join(", "))?;
1427+
}
1428+
if !remove_tags.is_empty() {
1429+
writeln!(f, " Removing tags: {}", remove_tags.join(", "))?;
1430+
}
1431+
if add_branches.is_empty()
1432+
&& remove_branches.is_empty()
1433+
&& add_tags.is_empty()
1434+
&& remove_tags.is_empty()
1435+
{
1436+
writeln!(f, " No pattern changes")?;
1437+
}
1438+
}
13281439
EnvironmentDiff::Delete(name) => writeln!(f, " ❌ Delete: {name}")?,
13291440
}
13301441
}

sync-team/src/github/tests/mod.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,5 +1164,50 @@ fn repo_environment_update_branches() {
11641164
);
11651165

11661166
let diff = model.diff_repos(gh);
1167-
insta::assert_debug_snapshot!(diff, @"[]");
1167+
insta::assert_debug_snapshot!(diff, @r#"
1168+
[
1169+
Update(
1170+
UpdateRepoDiff {
1171+
org: "rust-lang",
1172+
name: "repo1",
1173+
repo_node_id: "0",
1174+
settings_diff: (
1175+
RepoSettings {
1176+
description: "",
1177+
homepage: None,
1178+
archived: false,
1179+
auto_merge_enabled: false,
1180+
},
1181+
RepoSettings {
1182+
description: "",
1183+
homepage: None,
1184+
archived: false,
1185+
auto_merge_enabled: false,
1186+
},
1187+
),
1188+
permission_diffs: [],
1189+
branch_protection_diffs: [],
1190+
ruleset_diffs: [],
1191+
environment_diffs: [
1192+
Update {
1193+
name: "production",
1194+
add_branches: [
1195+
"stable",
1196+
],
1197+
remove_branches: [
1198+
"release/*",
1199+
],
1200+
add_tags: [],
1201+
remove_tags: [],
1202+
new_branches: [
1203+
"main",
1204+
"stable",
1205+
],
1206+
new_tags: [],
1207+
},
1208+
],
1209+
},
1210+
),
1211+
]
1212+
"#);
11681213
}

0 commit comments

Comments
 (0)