|
| 1 | +package io.nthienan.phdiff.comment; |
| 2 | + |
| 3 | +import org.apache.commons.lang.StringUtils; |
| 4 | +import org.sonar.api.CoreProperties; |
| 5 | +import org.sonar.api.batch.BatchSide; |
| 6 | +import org.sonar.api.batch.InstantiationStrategy; |
| 7 | +import org.sonar.api.batch.rule.Severity; |
| 8 | +import org.sonar.api.config.Settings; |
| 9 | + |
| 10 | +import java.io.UnsupportedEncodingException; |
| 11 | +import java.net.URLEncoder; |
| 12 | + |
| 13 | +/** |
| 14 | + * Support Remarkup format <br\> |
| 15 | + * References: {@code https://secure.phabricator.com/book/phabricator/article/remarkup} |
| 16 | + * |
| 17 | + * @author nthienan |
| 18 | + */ |
| 19 | +@BatchSide |
| 20 | +@InstantiationStrategy(InstantiationStrategy.PER_BATCH) |
| 21 | +public class RemarkupUtils { |
| 22 | + |
| 23 | + private final String ruleUrlPrefix; |
| 24 | + private final String projectKey; |
| 25 | + |
| 26 | + public static String bold(String str) { |
| 27 | + return String.format("**%s**", str); |
| 28 | + } |
| 29 | + |
| 30 | + public static String italics(String str) { |
| 31 | + return String.format("//%s//", str); |
| 32 | + } |
| 33 | + |
| 34 | + public static String code(String str) { |
| 35 | + return String.format("`%s`", str); |
| 36 | + } |
| 37 | + |
| 38 | + public static String icon(String icon, String color) { |
| 39 | + String colorStr = ""; |
| 40 | + if (StringUtils.isNotBlank(color)) { |
| 41 | + colorStr = "color=" + color; |
| 42 | + } |
| 43 | + return String.format("{icon %s %s}", icon, colorStr); |
| 44 | + } |
| 45 | + |
| 46 | + public static String link(String url, String title) { |
| 47 | + if (StringUtils.isNotBlank(title)) { |
| 48 | + return String.format("[[%s|%s]]", encodeForUrl(url), title); |
| 49 | + } |
| 50 | + return encodeForUrl(url); |
| 51 | + } |
| 52 | + |
| 53 | + public static String encodeForUrl(String url) { |
| 54 | + try { |
| 55 | + return URLEncoder.encode(url, "UTF-8"); |
| 56 | + } catch (UnsupportedEncodingException e) { |
| 57 | + throw new IllegalStateException("Encoding not supported", e); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public RemarkupUtils(Settings settings) { |
| 62 | + // If server base URL was not configured in SQ server then is is better to take URL configured on batch side |
| 63 | + String baseUrl = settings.hasKey(CoreProperties.SERVER_BASE_URL) ? settings.getString(CoreProperties.SERVER_BASE_URL) : settings.getString("sonar.host.url"); |
| 64 | + this.projectKey = settings.getString(CoreProperties.PROJECT_KEY_PROPERTY); |
| 65 | + if (!baseUrl.endsWith("/")) { |
| 66 | + baseUrl += "/"; |
| 67 | + } |
| 68 | + this.ruleUrlPrefix = baseUrl; |
| 69 | + } |
| 70 | + |
| 71 | + public String icon(Severity severity) { |
| 72 | + String result; |
| 73 | + switch (severity) { |
| 74 | + case BLOCKER: |
| 75 | + result = icon("bug", "red"); |
| 76 | + break; |
| 77 | + case CRITICAL: |
| 78 | + result = icon("arrow-circle-up", "red"); |
| 79 | + break; |
| 80 | + case MINOR: |
| 81 | + result = icon("info-circle", "green"); |
| 82 | + break; |
| 83 | + case INFO: |
| 84 | + result = icon("chevron-circle-down", "green"); |
| 85 | + break; |
| 86 | + default: |
| 87 | + // Major |
| 88 | + result = icon("chevron-circle-up", "red"); |
| 89 | + } |
| 90 | + return result; |
| 91 | + } |
| 92 | + |
| 93 | + public String source(String componentKey, int line) { |
| 94 | + return String.format("%s - %s", italics(String.valueOf(line)), code(componentKey.replace(projectKey, ""))); |
| 95 | + } |
| 96 | + |
| 97 | + public String message(String message) { |
| 98 | + return message; |
| 99 | + } |
| 100 | + |
| 101 | + public String rule(String ruleKey) { |
| 102 | + return link(String.format("%s/coding_rules#rule_key=%s", this.ruleUrlPrefix, ruleKey), "View rule"); |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments