-
Notifications
You must be signed in to change notification settings - Fork 714
/
JenkinsConfiguredWithReadmeRule.java
133 lines (122 loc) · 5.91 KB
/
JenkinsConfiguredWithReadmeRule.java
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package io.jenkins.plugins.casc.misc;
import static java.lang.reflect.Modifier.isPublic;
import static java.lang.reflect.Modifier.isStatic;
import com.vladsch.flexmark.ast.FencedCodeBlock;
import com.vladsch.flexmark.ast.util.TextCollectingVisitor;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.profile.pegdown.Extensions;
import com.vladsch.flexmark.profile.pegdown.PegdownOptionsAdapter;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.DataHolder;
import com.vladsch.flexmark.util.data.MutableDataSet;
import io.jenkins.plugins.casc.ConfigurationAsCode;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
import org.hamcrest.core.StringContains;
/**
* @author v1v (Victor Martinez)
*/
public class JenkinsConfiguredWithReadmeRule extends JenkinsConfiguredRule {
static final DataHolder OPTIONS = PegdownOptionsAdapter.flexmarkOptions(Extensions.ALL);
static final Parser PARSER = Parser.builder(OPTIONS).build();
@Override
public void before() throws Throwable {
super.before();
ConfiguredWithReadme configuredWithReadme = getConfiguredWithReadme();
if (Objects.nonNull(configuredWithReadme)) {
final Class<?> clazz = env.description().getTestClass();
final String[] resource = configuredWithReadme.value();
final List<String> configs = Arrays.stream(resource)
.map(s -> {
try {
String currentResource = s.replaceAll("#.*", "");
int position = getFencedCodeBlockIndex(s);
File codeBlockFile = File.createTempFile("integrations", "markdown");
InputStream inputStream = clazz.getClassLoader().getResourceAsStream(currentResource);
List<String> lines =
Collections.singletonList(transformFencedCodeBlockFromMarkdownToString(inputStream)
.get(position));
Path file = Paths.get(codeBlockFile.getCanonicalPath());
Files.write(file, lines, StandardCharsets.UTF_8);
return codeBlockFile.toURI().toString();
} catch (IOException e) {
throw new AssertionError("Exception when accessing the resources: " + s, e);
}
})
.collect(Collectors.toList());
try {
ConfigurationAsCode.get().configure(configs);
} catch (Throwable t) {
if (!configuredWithReadme.expected().isInstance(t)) {
throw new AssertionError("Unexpected exception ", t);
} else {
if (!StringUtils.isBlank(configuredWithReadme.message())) {
boolean match =
new StringContains(false, configuredWithReadme.message()).matches(t.getMessage());
if (!match) {
throw new AssertionError("Exception did not contain the expected string: "
+ configuredWithReadme.message() + "\nMessage was:\n" + t.getMessage());
}
}
}
}
}
}
private ConfiguredWithReadme getConfiguredWithReadme() {
ConfiguredWithReadme configuredWithReadme = env.description().getAnnotation(ConfiguredWithReadme.class);
if (Objects.nonNull(configuredWithReadme)) {
return configuredWithReadme;
}
for (Field field : env.description().getTestClass().getFields()) {
if (field.isAnnotationPresent(ConfiguredWithReadme.class)) {
int m = field.getModifiers();
Class<?> clazz = field.getType();
if (isPublic(m) && isStatic(m) && clazz.isAssignableFrom(JenkinsConfiguredWithReadmeRule.class)) {
configuredWithReadme = field.getAnnotation(ConfiguredWithReadme.class);
if (Objects.nonNull(configuredWithReadme)) {
return configuredWithReadme;
}
} else {
throw new IllegalStateException("Field must be public static JenkinsConfiguredWithReadmeRule");
}
}
}
return null;
}
private List<String> transformFencedCodeBlockFromMarkdownToString(InputStream markdownContent) throws IOException {
ArrayList<String> results = new ArrayList<>();
final MutableDataSet FORMAT_OPTIONS = new MutableDataSet();
FORMAT_OPTIONS.set(Parser.EXTENSIONS, OPTIONS.get(Parser.EXTENSIONS));
Reader targetReader = new InputStreamReader(markdownContent);
Node document = PARSER.parseReader(targetReader);
TextCollectingVisitor textCollectingVisitor = new TextCollectingVisitor();
Node fencedCodeBlock = document.getChildOfType(FencedCodeBlock.class);
while (fencedCodeBlock != null) {
results.add(textCollectingVisitor.collectAndGetText(fencedCodeBlock));
fencedCodeBlock = fencedCodeBlock.getNextAny(FencedCodeBlock.class);
}
return results;
}
private int getFencedCodeBlockIndex(String resource) {
int position = 0;
if (resource.matches(".*#\\d+$")) {
position = Integer.parseInt(resource.replaceAll(".*#", ""));
}
return position;
}
}