From ca625d5e33235a2a55002edb6984208c9fc02784 Mon Sep 17 00:00:00 2001 From: Erin Schnabel Date: Mon, 15 Jul 2024 21:21:09 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Avoid=20NPE=20for=20unknown=20gr?= =?UTF-8?q?oup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../automation/github/context/QueryContext.java | 3 +++ .../automation/github/context/TeamList.java | 14 ++++++++++---- .../automation/github/voting/VoteInformation.java | 14 ++++++++------ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/bot-github-core/src/main/java/org/commonhaus/automation/github/context/QueryContext.java b/bot-github-core/src/main/java/org/commonhaus/automation/github/context/QueryContext.java index 7dab3b5..f93d544 100644 --- a/bot-github-core/src/main/java/org/commonhaus/automation/github/context/QueryContext.java +++ b/bot-github-core/src/main/java/org/commonhaus/automation/github/context/QueryContext.java @@ -365,6 +365,9 @@ public boolean isLoginIncluded(String login, List groups) { for (var g : groups) { if (g.startsWith("@")) { TeamList team = getTeamList(g.substring(1)); + if (team.isEmpty()) { + return false; + } return team.members.stream().anyMatch(m -> m.login.equals(login)); } return g.equals(login); diff --git a/bot-github-core/src/main/java/org/commonhaus/automation/github/context/TeamList.java b/bot-github-core/src/main/java/org/commonhaus/automation/github/context/TeamList.java index a7e1555..8d86757 100644 --- a/bot-github-core/src/main/java/org/commonhaus/automation/github/context/TeamList.java +++ b/bot-github-core/src/main/java/org/commonhaus/automation/github/context/TeamList.java @@ -13,15 +13,17 @@ public class TeamList { public TeamList(String name, Collection members) { this.name = name; - this.members = members.stream() - .map(DataActor::new) - .collect(Collectors.toSet()); + this.members = members == null + ? Set.of() + : members.stream() + .map(DataActor::new) + .collect(Collectors.toSet()); } /** testing */ public TeamList(String name, Set members) { this.name = name; - this.members = members; + this.members = members == null ? Set.of() : members; } public TeamList removeExcludedMembers(Predicate predicate) { @@ -43,4 +45,8 @@ public String toString() { .map(actor -> actor.login) .collect(Collectors.joining(","))); } + + public boolean isEmpty() { + return members == null || members.isEmpty(); + } } diff --git a/commonhaus-bot/src/main/java/org/commonhaus/automation/github/voting/VoteInformation.java b/commonhaus-bot/src/main/java/org/commonhaus/automation/github/voting/VoteInformation.java index 0b197ce..8fd3d87 100644 --- a/commonhaus-bot/src/main/java/org/commonhaus/automation/github/voting/VoteInformation.java +++ b/commonhaus-bot/src/main/java/org/commonhaus/automation/github/voting/VoteInformation.java @@ -47,20 +47,22 @@ public VoteInformation(VoteEvent event) { QueryContext qc = event.getQueryContext(); VoteConfig voteConfig = event.getVotingConfig(); String bodyString = event.getBody(); + String groupValue = null; + TeamList teamList = null; // Test body for "Voting group" followed by a team name Matcher groupM = groupPattern.matcher(bodyString); if (groupM.find()) { - this.group = groupM.group(1); - this.teamList = qc.getTeamList(this.group); + groupValue = groupM.group(1); + teamList = qc.getTeamList(groupValue); if (teamList != null) { teamList.removeExcludedMembers( a -> qc.isBot(a.login) || voteConfig.isMemberExcluded(a.login)); } - } else { - this.group = null; - this.teamList = null; } + this.group = groupValue; + this.teamList = teamList; + this.votingThreshold = voteConfig.votingThreshold(this.group); // Test body for "vote::marthas" or "vote::manual" @@ -108,7 +110,7 @@ public boolean isValid() { } public boolean invalidGroup() { - return teamList == null; + return teamList == null || teamList.isEmpty(); } public boolean invalidReactions() {