Skip to content

Commit 7626525

Browse files
committed
Warn on failed parent team updates and don't count them as changes
update_team now logs a rejected (422) update as a warning with the error from GitHub instead of a debug message. sync_team uses the value update_team returns, so a failed parent update is no longer reported as a change. Other successful changes still make sync_team return true. Part of #6
1 parent b8fc255 commit 7626525

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎lib/entitlements/backend/github_team/service.rb‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,8 @@ def sync_team(desired_state, current_state)
197197
desired_parent_team_id = team_by_name(org_name: org, team_name: desired_metadata["parent_team_name"])[:id]
198198
unless desired_parent_team_id.nil?
199199
# TODO: I'm hard-coding a block for deletes, for now. I'm doing that by making sure we dont set the desired parent_team_id to nil for teams where it is already set
200-
update_team(team: current_state, metadata: { parent_team_id: desired_parent_team_id })
200+
changed_parent_team = update_team(team: current_state, metadata: { parent_team_id: desired_parent_team_id })
201201
end
202-
changed_parent_team = true
203202
end
204203
end
205204

@@ -329,7 +328,7 @@ def update_team(team:, metadata: {})
329328

330329
true
331330
rescue Octokit::UnprocessableEntity => e
332-
Entitlements.logger.debug "update_team(team=#{team.team_name}) ERROR - #{e.message}"
331+
Entitlements.logger.warn "update_team(team=#{team.team_name}) ERROR - #{e.message}"
333332
false
334333
end
335334

‎spec/unit/entitlements/backend/github_team/service_spec.rb‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,36 @@
459459
expect(result).to eq(true)
460460
end
461461

462+
it "returns false when the parent team update fails" do
463+
expect(subject).to receive(:team_by_name).with(org_name: "kittensinc",
464+
team_name: "cuddly-kittens").and_return({ id: 10 })
465+
expect(subject).to receive(:update_team).with(team: team_data_old,
466+
metadata: { parent_team_id: 10 }).and_return(false)
467+
expect(logger).to receive(:debug).with(/sync_team\(russian-blues=1001\): Parent team change found - From No Parent Team to cuddly-kittens/)
468+
expect(logger).to receive(:debug).with(/sync_team\(russian-blues=1001\): Added 0, removed 0/)
469+
result = subject.sync_team(team_metadata_add, team_data_old)
470+
expect(result).to eq(false)
471+
end
472+
473+
it "returns true when the parent team update fails but members change" do
474+
team_metadata_add_member = Entitlements::Backend::GitHubTeam::Models::Team.new(
475+
team_id: 1001,
476+
team_name: "russian-blues",
477+
members: Set.new(%w[blackmanx highlander ragamuffin MAINECOON]),
478+
ou: "ou=kittensinc,dc=github,dc=com",
479+
metadata: { "parent_team_name" => "cuddly-kittens" }
480+
)
481+
expect(subject).to receive(:team_by_name).with(org_name: "kittensinc",
482+
team_name: "cuddly-kittens").and_return({ id: 10 })
483+
expect(subject).to receive(:update_team).with(team: team_data_old,
484+
metadata: { parent_team_id: 10 }).and_return(false)
485+
expect(subject).to receive(:add_user_to_team).with(user: "highlander", team: team_data_old).and_return(true)
486+
expect(logger).to receive(:debug).with(/sync_team\(russian-blues=1001\): Parent team change found - From No Parent Team to cuddly-kittens/)
487+
expect(logger).to receive(:debug).with(/sync_team\(russian-blues=1001\): Added 1, removed 0/)
488+
result = subject.sync_team(team_metadata_add_member, team_data_old)
489+
expect(result).to eq(true)
490+
end
491+
462492
it "returns true when there were metadata changes to add maintainer" do
463493
allow(subject).to receive(:read_team).with(team_metadata_add_maintainer).and_return(team_metadata_maintainer_old)
464494
expect(logger).to receive(:debug).with(/sync_team\(russian-blues=1001\): Maintainer members change found - From \["ragamuffin"\] to \["blackmanx", "ragamuffin"\]/)
@@ -971,7 +1001,7 @@
9711001
allow(subject).to receive(:octokit).and_return(octokit)
9721002
expect(octokit).to receive(:update_team).and_raise(Octokit::UnprocessableEntity)
9731003
expect(logger).to receive(:debug).with("update_team(team=mainecoon)")
974-
expect(logger).to receive(:debug).with("update_team(team=mainecoon) ERROR - Octokit::UnprocessableEntity")
1004+
expect(logger).to receive(:warn).with("update_team(team=mainecoon) ERROR - Octokit::UnprocessableEntity")
9751005

9761006
created = subject.update_team(team: team_data_new)
9771007
expect(created).to eq(false)

0 commit comments

Comments
 (0)