Skip to content

Commit d23607e

Browse files
authored
Merge pull request #1756 from Kobzol/improve-diff
Use destructuring in diffs to make it harder to forget to include diff fields
2 parents 7a5744e + b50324a commit d23607e

File tree

2 files changed

+110
-40
lines changed

2 files changed

+110
-40
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ jobs:
3838
3939
- uses: Swatinem/rust-cache@9d47c6ad4b02e050fd481d890b2ea34778fd09d6
4040

41-
- name: Build the validation tool
42-
run: cargo build
41+
- name: Build the team binary
42+
run: RUSTFLAGS="--deny warnings" cargo build
4343

4444
- name: Validate the repository contents
4545
run: cargo run -- check --strict

sync-team/src/github/mod.rs

Lines changed: 108 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -676,25 +676,33 @@ impl CreateRepoDiff {
676676

677677
impl std::fmt::Display for CreateRepoDiff {
678678
fn fmt(&self, mut f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
679+
let CreateRepoDiff {
680+
org,
681+
name,
682+
settings,
683+
permissions,
684+
branch_protections,
685+
} = self;
686+
679687
let RepoSettings {
680688
description,
681689
homepage,
682690
archived: _,
683691
auto_merge_enabled,
684-
} = &self.settings;
692+
} = &settings;
685693

686694
writeln!(f, "➕ Creating repo:")?;
687-
writeln!(f, " Org: {}", self.org)?;
688-
writeln!(f, " Name: {}", self.name)?;
689-
writeln!(f, " Description: {:?}", description)?;
690-
writeln!(f, " Homepage: {:?}", homepage)?;
691-
writeln!(f, " Auto-merge: {}", auto_merge_enabled)?;
695+
writeln!(f, " Org: {org}")?;
696+
writeln!(f, " Name: {name}")?;
697+
writeln!(f, " Description: {description}")?;
698+
writeln!(f, " Homepage: {homepage:?}")?;
699+
writeln!(f, " Auto-merge: {auto_merge_enabled}")?;
692700
writeln!(f, " Permissions:")?;
693-
for diff in &self.permissions {
701+
for diff in permissions {
694702
write!(f, "{diff}")?;
695703
}
696704
writeln!(f, " Branch Protections:")?;
697-
for (branch_name, branch_protection) in &self.branch_protections {
705+
for (branch_name, branch_protection) in branch_protections {
698706
writeln!(&mut f, " {branch_name}")?;
699707
log_branch_protection(branch_protection, None, &mut f)?;
700708
}
@@ -719,9 +727,18 @@ impl UpdateRepoDiff {
719727
return true;
720728
}
721729

722-
self.settings_diff.0 == self.settings_diff.1
723-
&& self.permission_diffs.is_empty()
724-
&& self.branch_protection_diffs.is_empty()
730+
let UpdateRepoDiff {
731+
org: _,
732+
name: _,
733+
repo_node_id: _,
734+
settings_diff,
735+
permission_diffs,
736+
branch_protection_diffs,
737+
} = self;
738+
739+
settings_diff.0 == settings_diff.1
740+
&& permission_diffs.is_empty()
741+
&& branch_protection_diffs.is_empty()
725742
}
726743

727744
fn can_be_modified(&self) -> bool {
@@ -770,8 +787,18 @@ impl std::fmt::Display for UpdateRepoDiff {
770787
if self.noop() {
771788
return Ok(());
772789
}
773-
writeln!(f, "📝 Editing repo '{}/{}':", self.org, self.name)?;
774-
let (settings_old, settings_new) = &self.settings_diff;
790+
791+
let UpdateRepoDiff {
792+
org,
793+
name,
794+
repo_node_id: _,
795+
settings_diff,
796+
permission_diffs,
797+
branch_protection_diffs,
798+
} = self;
799+
800+
writeln!(f, "📝 Editing repo '{org}/{name}':")?;
801+
let (settings_old, settings_new) = &settings_diff;
775802
let RepoSettings {
776803
description,
777804
homepage,
@@ -803,17 +830,17 @@ impl std::fmt::Display for UpdateRepoDiff {
803830
(true, false) => writeln!(f, " Disable auto-merge")?,
804831
_ => {}
805832
}
806-
if !self.permission_diffs.is_empty() {
833+
if !permission_diffs.is_empty() {
807834
writeln!(f, " Permission Changes:")?;
835+
for permission_diff in permission_diffs {
836+
write!(f, "{permission_diff}")?;
837+
}
808838
}
809-
for permission_diff in &self.permission_diffs {
810-
write!(f, "{permission_diff}")?;
811-
}
812-
if !self.branch_protection_diffs.is_empty() {
839+
if !branch_protection_diffs.is_empty() {
813840
writeln!(f, " Branch Protections:")?;
814-
}
815-
for branch_protection_diff in &self.branch_protection_diffs {
816-
write!(f, "{branch_protection_diff}")?;
841+
for branch_protection_diff in branch_protection_diffs {
842+
write!(f, "{branch_protection_diff}")?;
843+
}
817844
}
818845

819846
Ok(())
@@ -854,11 +881,13 @@ impl RepoPermissionAssignmentDiff {
854881

855882
impl std::fmt::Display for RepoPermissionAssignmentDiff {
856883
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
857-
let name = match &self.collaborator {
884+
let RepoPermissionAssignmentDiff { collaborator, diff } = self;
885+
886+
let name = match &collaborator {
858887
RepoCollaborator::Team(name) => format!("team '{name}'"),
859888
RepoCollaborator::User(name) => format!("user '{name}'"),
860889
};
861-
match &self.diff {
890+
match &diff {
862891
RepoPermissionDiff::Create(p) => {
863892
writeln!(f, " Giving {name} {p} permission")
864893
}
@@ -950,9 +979,21 @@ fn log_branch_protection(
950979
new: Option<&api::BranchProtection>,
951980
mut result: impl Write,
952981
) -> std::fmt::Result {
982+
let api::BranchProtection {
983+
// Pattern identifies the branch protection, so it has to be same between `current`
984+
// and `new`.
985+
pattern: _,
986+
is_admin_enforced,
987+
dismisses_stale_reviews,
988+
required_approving_review_count,
989+
required_status_check_contexts,
990+
push_allowances,
991+
requires_approving_reviews,
992+
} = current;
993+
953994
macro_rules! log {
954995
($str:literal, $field1:ident) => {
955-
let old = &current.$field1;
996+
let old = $field1;
956997
let new = new.map(|n| &n.$field1);
957998
log!($str, old, new);
958999
};
@@ -968,10 +1009,12 @@ fn log_branch_protection(
9681009
}
9691010

9701011
log!("Dismiss Stale Reviews", dismisses_stale_reviews);
1012+
log!("Is admin enforced", is_admin_enforced);
9711013
log!(
9721014
"Required Approving Review Count",
9731015
required_approving_review_count
9741016
);
1017+
log!("Requires PR", requires_approving_reviews);
9751018
log!("Required Checks", required_status_check_contexts);
9761019
log!("Allowances", push_allowances);
9771020
Ok(())
@@ -1042,20 +1085,28 @@ impl CreateTeamDiff {
10421085

10431086
impl std::fmt::Display for CreateTeamDiff {
10441087
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1088+
let CreateTeamDiff {
1089+
org,
1090+
name,
1091+
description,
1092+
privacy,
1093+
members,
1094+
} = self;
1095+
10451096
writeln!(f, "➕ Creating team:")?;
1046-
writeln!(f, " Org: {}", self.org)?;
1047-
writeln!(f, " Name: {}", self.name)?;
1048-
writeln!(f, " Description: {}", self.description)?;
1097+
writeln!(f, " Org: {org}")?;
1098+
writeln!(f, " Name: {name}")?;
1099+
writeln!(f, " Description: {description}")?;
10491100
writeln!(
10501101
f,
10511102
" Privacy: {}",
1052-
match self.privacy {
1103+
match privacy {
10531104
TeamPrivacy::Secret => "secret",
10541105
TeamPrivacy::Closed => "closed",
10551106
}
10561107
)?;
10571108
writeln!(f, " Members:")?;
1058-
for (name, role) in &self.members {
1109+
for (name, role) in members {
10591110
writeln!(f, " {name}: {role}")?;
10601111
}
10611112
Ok(())
@@ -1095,10 +1146,19 @@ impl EditTeamDiff {
10951146
}
10961147

10971148
fn noop(&self) -> bool {
1098-
self.name_diff.is_none()
1099-
&& self.description_diff.is_none()
1100-
&& self.privacy_diff.is_none()
1101-
&& self.member_diffs.iter().all(|(_, d)| d.is_noop())
1149+
let EditTeamDiff {
1150+
org: _,
1151+
name: _,
1152+
name_diff,
1153+
description_diff,
1154+
privacy_diff,
1155+
member_diffs,
1156+
} = self;
1157+
1158+
name_diff.is_none()
1159+
&& description_diff.is_none()
1160+
&& privacy_diff.is_none()
1161+
&& member_diffs.iter().all(|(_, d)| d.is_noop())
11021162
}
11031163
}
11041164

@@ -1107,21 +1167,31 @@ impl std::fmt::Display for EditTeamDiff {
11071167
if self.noop() {
11081168
return Ok(());
11091169
}
1110-
writeln!(f, "📝 Editing team '{}/{}':", self.org, self.name)?;
1111-
if let Some(n) = &self.name_diff {
1170+
1171+
let EditTeamDiff {
1172+
org,
1173+
name,
1174+
name_diff,
1175+
description_diff,
1176+
privacy_diff,
1177+
member_diffs,
1178+
} = self;
1179+
1180+
writeln!(f, "📝 Editing team '{org}/{name}':")?;
1181+
if let Some(n) = name_diff {
11121182
writeln!(f, " New name: {n}")?;
11131183
}
1114-
if let Some((old, new)) = &self.description_diff {
1184+
if let Some((old, new)) = &description_diff {
11151185
writeln!(f, " New description: '{old}' => '{new}'")?;
11161186
}
1117-
if let Some((old, new)) = &self.privacy_diff {
1187+
if let Some((old, new)) = &privacy_diff {
11181188
let display = |privacy: &TeamPrivacy| match privacy {
11191189
TeamPrivacy::Secret => "secret",
11201190
TeamPrivacy::Closed => "closed",
11211191
};
11221192
writeln!(f, " New privacy: '{}' => '{}'", display(old), display(new))?;
11231193
}
1124-
for (member, diff) in &self.member_diffs {
1194+
for (member, diff) in member_diffs {
11251195
match diff {
11261196
MemberDiff::Create(r) => {
11271197
writeln!(f, " Adding member '{member}' with {r} role")?;

0 commit comments

Comments
 (0)