130 lines
4.1 KiB
Java
130 lines
4.1 KiB
Java
// Copyright (c) 2021, Fr.Terrot. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package io.gitea.model;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.eclipse.mylyn.tasks.core.ITask.PriorityLevel;
|
|
|
|
import io.gitea.StringUtil;
|
|
|
|
/**
|
|
* Extract Mylyn PriorityLevel from Issue label
|
|
*
|
|
*/
|
|
public enum GiteaPriorityLevel {
|
|
HIGHEST(PriorityLevel.P1), HIGH(PriorityLevel.P2), MIDDLE(PriorityLevel.P3), LOW(PriorityLevel.P4),
|
|
LOWEST(PriorityLevel.P5);
|
|
|
|
private PriorityLevel level;
|
|
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @private
|
|
*/
|
|
private GiteaPriorityLevel(PriorityLevel p) {
|
|
this.level = p;
|
|
}
|
|
|
|
/**
|
|
* Get GiteaPriority level.
|
|
*
|
|
* @return PriorityLevel
|
|
*/
|
|
public PriorityLevel getLevel() {
|
|
return level;
|
|
}
|
|
|
|
/**
|
|
* GiteaPriority level as String
|
|
*
|
|
* {@code giteaPriority.ToString() <=> giteaPriority.GetLevel().toString()}
|
|
*
|
|
* To Get GiteaPriority as String use standard Enum name() API.
|
|
*
|
|
* @{code HIGHEST.toString() -> "P1" HIGHEST.name() -> "HIGHEST"}
|
|
*/
|
|
public String toString() {
|
|
return level.toString();
|
|
}
|
|
|
|
/**
|
|
* Get GiteaPriority from PriorityLevel
|
|
*
|
|
* @param level
|
|
* @return
|
|
* @throws IllegalArgumentException
|
|
*/
|
|
public static GiteaPriorityLevel getPriority(PriorityLevel level) throws IllegalArgumentException {
|
|
return Arrays.stream(values()).filter(v -> v.level.equals(level)).findFirst()
|
|
.orElseThrow(() -> new IllegalArgumentException("unknown priority level:" + level.toString()));
|
|
}
|
|
|
|
// P1 .. P5
|
|
private static String valueRegex = "(?::?priority:)?(p[1-5])";
|
|
private static Pattern valuePattern = Pattern.compile(valueRegex, Pattern.CASE_INSENSITIVE);
|
|
// Xtra very_normal is supported, surprising but not a big issue, kept it regexp
|
|
// 'simple'
|
|
private static String alternativeRegex = "(?::?priority:)?(?:very_?)?(high|low|normal)";
|
|
private static Pattern alternativePattern = Pattern.compile(alternativeRegex, Pattern.CASE_INSENSITIVE);
|
|
|
|
private static String priorityRegex = "(?::?priority:)?(highest|middle|low|lowest)";
|
|
private static Pattern priorityPattern = Pattern.compile(priorityRegex, Pattern.CASE_INSENSITIVE);
|
|
|
|
private static String allPrioritiesRegex = "((?::?priority:)?(?:p[1-5]|(?:high|low)(?:est)?|(?:very_?)(?:high|low)|normal|middle))";
|
|
private static Pattern allPrioritiesPattern = Pattern.compile(allPrioritiesRegex, Pattern.CASE_INSENSITIVE);
|
|
|
|
|
|
/**
|
|
* Return the Priority from labels value.
|
|
*
|
|
* @param labels as string
|
|
*
|
|
* @return found priority or MIDDLE if no priority found.
|
|
*/
|
|
public static GiteaPriorityLevel getPriority(String labels) {
|
|
|
|
Matcher m = priorityPattern.matcher(labels);
|
|
if (m.find()) {
|
|
return GiteaPriorityLevel.valueOf(m.group(1).toUpperCase());
|
|
}
|
|
m = valuePattern.matcher(labels);
|
|
if (m.find()) {
|
|
return GiteaPriorityLevel.getPriority(PriorityLevel.valueOf(m.group(1).toUpperCase()));
|
|
}
|
|
|
|
m = alternativePattern.matcher(labels);
|
|
if (m.find()) {
|
|
switch (m.group(1).toLowerCase()) {
|
|
case "high":
|
|
return GiteaPriorityLevel.HIGHEST;
|
|
case "normal":
|
|
return GiteaPriorityLevel.MIDDLE;
|
|
case "low":
|
|
return GiteaPriorityLevel.LOWEST;
|
|
default:
|
|
}
|
|
}
|
|
return GiteaPriorityLevel.MIDDLE;
|
|
}
|
|
|
|
/** Extract for the labels list the ones which are priority level.
|
|
*
|
|
*/
|
|
public static List<String> getAllPriorityLabels(List<String> labels) {
|
|
List<String> priorities = new ArrayList<String>();
|
|
labels.forEach((label) -> {
|
|
if (allPrioritiesPattern.matcher(label).find()) priorities.add(label);
|
|
});
|
|
return priorities;
|
|
}
|
|
}
|