Skip to content

Commit

Permalink
Merge pull request #6 from gdgib/G2-1617-Flags
Browse files Browse the repository at this point in the history
G2-1617 Use flags for condition creation
  • Loading branch information
gdgib authored Dec 25, 2024
2 parents ef06f0b + e9aba60 commit 5108c0e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,16 @@ protected static class LinkType {

protected static Changes computeChanges(CreateConfig config) {
final Changes.ChangesBuilder retVal = Changes.builder();
for (CreateIssue raw : config.getIssues()) {
final Set<String> disabledSummaries = config.getDisabledIssues().stream().map(CreateIssue::getSummary).collect(Collectors.toSet());
for (CreateIssue raw : config.getEnabledIssues()) {
// Integrate the configuration into the issue & record it
final CreateIssue issue = raw.fallback(config);
retVal.issue(issue);

// Record all the links
for (String relationship : issue.getRelationships().keySet()) {
for (String target : issue.getRelationships().get(relationship)) {
retVal.link(new LinkIssuesInput(issue.getSummary(), target, relationship, null));
if (!disabledSummaries.contains(target)) retVal.link(new LinkIssuesInput(issue.getSummary(), target, relationship, null));
}
}
}
Expand Down Expand Up @@ -201,8 +202,10 @@ public List<String> createIssues(InputStream stream) throws JsonParseException,
final CreateConfig config = load(stream);
if ((config.getIssues() == null) || config.getIssues().isEmpty()) return Collections.emptyList();

config.validateFlags();
final Changes changes = computeChanges(config);
verifyChanges(changes);
//return changes.getIssues().stream().map(CreateIssue::getSummary).collect(Collectors.toList());
return implementChanges(changes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.g2forge.alexandria.java.core.helpers.HCollection;
import com.g2forge.alexandria.java.core.helpers.HCollector;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.Singular;

@Data
Expand Down Expand Up @@ -34,4 +41,31 @@ public class CreateConfig implements ICreateConfig {

@Singular
protected final Map<String, Set<String>> relationships;

@Singular
protected final Map<String, Boolean> flags;

@Getter(lazy = true)
@JsonIgnore
private final Map<String, Boolean> specifiedFlags = getFlags().entrySet().stream().filter(entry -> entry.getValue() != null).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

@Getter(lazy = true)
@JsonIgnore
private final Set<String> disabledFlags = getSpecifiedFlags().entrySet().stream().filter(entry -> !entry.getValue()).map(Map.Entry::getKey).collect(Collectors.toSet());

@JsonIgnore
public List<CreateIssue> getEnabledIssues() {
return getIssues().stream().filter(issue -> issue.isEnabled(this)).collect(Collectors.toList());
}

@JsonIgnore
public List<CreateIssue> getDisabledIssues() {
return getIssues().stream().filter(issue -> !issue.isEnabled(this)).collect(Collectors.toList());
}

public void validateFlags() {
final Set<String> referencedFlags = getIssues().stream().flatMap(issue -> issue.getFlags() == null ? Stream.empty() : issue.getFlags().stream()).collect(Collectors.toSet());
final Set<String> unknownFlags = HCollection.difference(referencedFlags, getSpecifiedFlags().keySet());
if (!unknownFlags.isEmpty()) throw new IllegalArgumentException("The following flags are refenced by issues, but are neither enabled nor disabled: " + unknownFlags.stream().collect(HCollector.joining(", ", ", & ")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.g2forge.alexandria.java.function.IFunction1;

import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -41,14 +43,20 @@ public class CreateIssue implements ICreateConfig {
@Singular
protected final Map<String, Set<String>> relationships;

@Singular
protected final Set<String> flags;

public CreateIssue fallback(CreateConfig config) {
final CreateIssueBuilder retVal = builder();

// Configurable fields
retVal.project(IFunction1.create(ICreateConfig::getProject).applyWithFallback(this, config));
retVal.type(IFunction1.create(ICreateConfig::getType).applyWithFallback(this, config));
retVal.epic(IFunction1.create(ICreateConfig::getEpic).applyWithFallback(this, config));
retVal.components(Stream.of(this, config).map(ICreateConfig::getComponents).flatMap(l -> l == null ? Stream.empty() : l.stream()).collect(Collectors.toSet()));

// Only add the components from the config if it's the same project (or we have no project)
retVal.components(Stream.of(this, (getProject() == null) || getProject().equals(config.getProject()) ? config : null).filter(Objects::nonNull).map(ICreateConfig::getComponents).flatMap(l -> l == null ? Stream.empty() : l.stream()).collect(Collectors.toSet()));

retVal.labels(Stream.of(this, config).map(ICreateConfig::getLabels).flatMap(l -> l == null ? Stream.empty() : l.stream()).collect(Collectors.toSet()));
retVal.securityLevel(IFunction1.create(ICreateConfig::getSecurityLevel).applyWithFallback(this, config));
retVal.assignee(IFunction1.create(ICreateConfig::getAssignee).applyWithFallback(this, config));
Expand All @@ -69,4 +77,9 @@ public CreateIssue fallback(CreateConfig config) {

return retVal.build();
}

@JsonIgnore
public boolean isEnabled(CreateConfig config) {
return (getFlags() == null) || !getFlags().stream().anyMatch(config.getDisabledFlags()::contains);
}
}

0 comments on commit 5108c0e

Please sign in to comment.