Search in sources :

Example 1 with IssueExportMoreThanOneGroupException

use of net.nemerosa.ontrack.extension.issues.model.IssueExportMoreThanOneGroupException in project ontrack by nemerosa.

the class IssueServiceUtils method groupIssues.

public static Map<String, List<Issue>> groupIssues(IssueServiceConfiguration issueServiceConfiguration, List<? extends Issue> issues, IssueChangeLogExportRequest request, BiFunction<IssueServiceConfiguration, Issue, Set<String>> issueTypesFn) {
    // Excluded issues
    Set<String> excludedTypes = request.getExcludedTypes();
    // Gets the grouping specification
    Map<String, Set<String>> groupingSpecification = request.getGroupingSpecification();
    // Map of issues, ordered by group
    Map<String, List<Issue>> groupedIssues = new LinkedHashMap<>();
    // Pre-enter the empty group list, in order to guarantee the ordering
    for (String groupName : groupingSpecification.keySet()) {
        groupedIssues.put(groupName, new ArrayList<>());
    }
    // For all issues
    for (Issue issue : issues) {
        // Issue type(s)
        Set<String> issueTypes = issueTypesFn.apply(issueServiceConfiguration, issue);
        // Excluded issue?
        if (Collections.disjoint(excludedTypes, issueTypes)) {
            // Issue is not excluded
            // Gets the groups this issue belongs to
            Set<String> issueGroups = getIssueGroups(issueTypes, groupingSpecification);
            // Target group
            String targetGroup;
            if (issueGroups.size() > 1) {
                throw new IssueExportMoreThanOneGroupException(issue.getKey(), issueGroups);
            } else if (issueGroups.isEmpty()) {
                if (groupingSpecification.isEmpty()) {
                    targetGroup = IssueExportService.NO_GROUP;
                } else {
                    targetGroup = request.getAltGroup();
                }
            } else {
                targetGroup = Iterables.get(issueGroups, 0);
            }
            // Grouping
            List<Issue> issueList = groupedIssues.get(targetGroup);
            if (issueList == null) {
                issueList = new ArrayList<>();
                groupedIssues.put(targetGroup, issueList);
            }
            issueList.add(issue);
        }
    }
    // Prunes empty groups
    Iterator<Map.Entry<String, List<Issue>>> iterator = groupedIssues.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, List<Issue>> entry = iterator.next();
        if (entry.getValue().isEmpty()) {
            iterator.remove();
        }
    }
    // OK
    return groupedIssues;
}
Also used : Issue(net.nemerosa.ontrack.extension.issues.model.Issue) IssueExportMoreThanOneGroupException(net.nemerosa.ontrack.extension.issues.model.IssueExportMoreThanOneGroupException)

Aggregations

Issue (net.nemerosa.ontrack.extension.issues.model.Issue)1 IssueExportMoreThanOneGroupException (net.nemerosa.ontrack.extension.issues.model.IssueExportMoreThanOneGroupException)1