Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update groups.groovy #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 19 additions & 38 deletions vars/groups.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,24 @@ import groovy.json.JsonOutput
def createGroup(String groupName, List groupMembers, String groupType) {
return JsonOutput.toJson([
name: groupName,
members: groupMembers,
discordID: groupDiscordID,
swarmID: groupSwarmID,
type: groupType
]);
}

def getGroupMembers(groups, String groupName) {
for(group in groups) {
if(group.name == groupName) {
return group.members;
}
}

log.error("Tried to get group members for group '${groupName}', but this group couldn't be found!");
return [];
}

def getGroupType(groups, String groupName) {
for(group in groups) {
if(group.name == groupName) {
return group.type;
}
}

log.error("Tried to get group type for group '${groupName}', but this group couldn't be found!");
return [];
}

def mentionGroup(groups, String groupName) {
def groupMembers = getGroupMembers(groups, groupName);
def groupType = getGroupType(groups, groupName);

if(!groupMembers && !groupType) {
return '';
}
def groupType = ""
def discordID = ""
def groupsParsed = new JsonSlurper().parseText(groups)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't loadGroups already parse the groups?

If you're passing the full group json into this function, maybe split loadGroups up into loadGroupsFromFile and loadGroupsFromString, or have loadGroups return the json string rather than a parsed version. (This would also mean that saveGroups shouldn't convert the groups to json, as they would already be in that format).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah missed that function. I suppose we can remove loadGroups (and pass file as string as argument) or convert loadGroups to just reading in the file and parsing it using JsonSlurper and return the JsonObject.


groupsParsed.groups.each { group ->
if (group.name == groupName)
{
groupType = group.type
discordID = group.discordID
}
}
Comment on lines +18 to +24
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to break out of this loop prematurely?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Breaking out of for-each loops in Groovy is illegal ([https://stackoverflow.com/questions/3049790/can-you-break-from-a-groovy-each-closure]). However using find ([https://www.tutorialspoint.com/groovy/groovy_find.htm]) we can break out of the loop prematurely using return true.


def tagTemplate = '';

Expand All @@ -59,15 +43,12 @@ def mentionGroup(groups, String groupName) {

def mentionString = '';

for(member in groupMembers) {
discordID = member.value;

if(discordID?.trim()) {
mentionString += "${tagTemplate.replace('[id]', discordID)}, ";
}
}

// Remove trailing ', '
if(discordID?.trim()) {
mentionString += "${tagTemplate.replace('[id]', discordID)}, ";
}

// Not needed anymore?
Remove trailing ', '
message = message.substring(0, message.length() - 2);
Comment on lines +46 to 52
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the , at the end of the mention string might not be needed at all if this now tags a single Discord ID rather than a list of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea


message = "${groupName}: ${message}";
Expand Down