-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathMessage.java
More file actions
98 lines (80 loc) · 3.62 KB
/
Copy pathMessage.java
File metadata and controls
98 lines (80 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Copyright 2015-2016 Artem Stasiuk
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.github.terma.jenkins.githubprcoveragestatus;
import org.springframework.web.util.UriUtils;
import java.nio.charset.StandardCharsets;
@SuppressWarnings("WeakerAccess")
class Message {
//see http://shields.io/ for reference
private static final String BADGE_TEMPLATE = "https://img.shields.io/badge/coverage-%s-%s.svg";
private static final String COLOR_RED = "red";
private static final String COLOR_YELLOW = "yellow";
private static final String COLOR_GREEN = "brightgreen";
private final float coverage;
private final float masterCoverage;
public Message(float coverage, float masterCoverage) {
this.coverage = Percent.roundFourAfterDigit(coverage);
this.masterCoverage = Percent.roundFourAfterDigit(masterCoverage);
}
public String forConsole() {
return String.format("Coverage %s changed %s vs master %s",
Percent.toWholeNoSignString(coverage),
Percent.toString(Percent.change(coverage, masterCoverage)),
Percent.toWholeNoSignString(masterCoverage));
}
public String forComment(
final String buildUrl, final String jenkinsUrl,
final int yellowThreshold, final int greenThreshold,
final boolean useShieldsIo) {
final String icon = forIcon();
if (useShieldsIo) {
return "[ + ")](" + buildUrl + ")";
} else {
return "[](" + buildUrl + ")";
}
}
public String forStatusCheck() {
return String.format("Coverage %s changed %s vs master %s",
Percent.toWholeNoSignString(coverage),
Percent.toString(Percent.change(coverage, masterCoverage)),
Percent.toWholeNoSignString(masterCoverage));
}
private String shieldIoUrl(String icon, final int yellowThreshold, final int greenThreshold) {
final String color = getColor(yellowThreshold, greenThreshold);
// dash should be encoded as two dash
icon = icon.replace("-", "--");
return String.format(BADGE_TEMPLATE, UriUtils.encodePathSegment(icon, StandardCharsets.UTF_8), color);
}
private String getColor(int yellowThreshold, int greenThreshold) {
String color = COLOR_GREEN;
final int coveragePercent = Percent.of(coverage);
if (coveragePercent < yellowThreshold) {
color = COLOR_RED;
} else if (coveragePercent < greenThreshold) {
color = COLOR_YELLOW;
}
return color;
}
/**
* Example: 92% (+23%) vs master 70%
*/
public String forIcon() {
return String.format("%s (%s) vs master %s",
Percent.toWholeNoSignString(coverage),
Percent.toString(Percent.change(coverage, masterCoverage)),
Percent.toWholeNoSignString(masterCoverage));
}
}