-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildPulseCommand.java
175 lines (138 loc) · 6.15 KB
/
BuildPulseCommand.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package io.jenkins.plugins;
import hudson.FilePath;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import org.kohsuke.stapler.verb.POST;
@SuppressWarnings("lgtm[jenkins/plaintext-storage]")
public class BuildPulseCommand {
private String accountId;
private String repositoryId;
private String junitXmlReportPaths;
private String keyId;
private String secret;
private String workspacePath;
// optional arguments
private String coveragePaths;
private String tags;
private String quota;
private String repositoryPath;
public static String[] submit(String accountId, String repositoryId, String junitXmlReportPaths, String repositoryPath, String keyId, String secret, String workspacePath, String coveragePaths, String tags, String quota) throws IOException, InterruptedException {
BuildPulseCommand command = new BuildPulseCommand(accountId, repositoryId, junitXmlReportPaths, repositoryPath, keyId, secret, workspacePath, coveragePaths, tags, quota);
return command.downloadAndGetCommand();
}
public BuildPulseCommand(String accountId, String repositoryId, String junitXmlReportPaths, String repositoryPath,
String keyId, String secret, String workspacePath, String coveragePaths, String tags, String quota) {
this.accountId = accountId;
this.repositoryId = repositoryId;
this.junitXmlReportPaths = junitXmlReportPaths;
this.keyId = keyId;
this.secret = secret;
this.workspacePath = workspacePath;
// optional arguments
this.repositoryPath = repositoryPath.length() > 0 ? repositoryPath : workspacePath;
if (coveragePaths != null) {
this.coveragePaths = coveragePaths;
}
if (tags != null) {
this.tags = tags;
}
if (quota != null) {
this.quota = quota;
}
}
public String[] downloadAndGetCommand() throws IOException, InterruptedException {
this.validateArguments();
ArrayList<String> validatedJunitXmlReportPaths = this.resolveFilePaths(this.junitXmlReportPaths);
ArrayList<String> validatedCoverageReportPaths = this.resolveFilePaths(this.coveragePaths);
FilePath binaryFilePath = this.downloadBuildPulseBinary();
ArrayList<String> command = new ArrayList<String>(Arrays.asList(binaryFilePath.getRemote(), "submit"));
command.addAll(validatedJunitXmlReportPaths);
command.addAll(Arrays.asList("--account-id", this.accountId, "--repository-id", this.repositoryId, "--repository-dir", this.repositoryPath));
if (validatedCoverageReportPaths.size() > 0) {
command.add("--coverage-files");
command.addAll(validatedCoverageReportPaths);
}
if (this.tags != null && this.tags.length() > 0) {
command.add("--tags");
command.add(this.tags);
}
if (this.quota != null && this.quota.length() > 0) {
command.add("--quota-id");
command.add(this.quota);
}
command.add("--disable-coverage-auto");
return command.toArray(new String[0]);
}
@POST
@SuppressWarnings("lgtm[jenkins/no-permission-check]")
private FilePath downloadBuildPulseBinary() throws IOException, InterruptedException {
String binaryFileName = determineBinaryFileName();
URL binaryUrl = new URL(
"https://github.com/buildpulse/test-reporter/releases/latest/download/" + binaryFileName);
String tempDirectoryPath = System.getProperty("java.io.tmpdir");
FilePath binaryFilePath = new FilePath(Paths.get(tempDirectoryPath, binaryFileName).toFile());
if (!binaryFilePath.exists()) {
try (InputStream in = binaryUrl.openStream();
FileOutputStream out = new FileOutputStream(binaryFilePath.getRemote())) {
byte[] buffer = new byte[4096];
int n;
while ((n = in.read(buffer)) != -1) {
out.write(buffer, 0, n);
}
}
}
// Making the binary executable
binaryFilePath.chmod(0755);
return binaryFilePath;
}
private void validateArguments() throws RuntimeException, IOException {
if (!this.accountId.matches("^[0-9]+$")) {
throw new RuntimeException("Account ID doesn't match expected format: '^[0-9]+$'");
}
if (!this.repositoryId.matches("^[0-9]+$")) {
throw new RuntimeException("Repository ID doesn't match expected format: '^[0-9]+$'");
}
Path path = Paths.get(this.repositoryPath);
if (!Files.isDirectory(path)) {
throw new RuntimeException("Repository ID doesn't match expected format: '^[0-9]+$'");
}
if (this.keyId.length() == 0) {
throw new RuntimeException("BuildPulse key is required");
}
if (this.secret.length() == 0) {
throw new RuntimeException("BuildPulse secret is required");
}
}
private String determineBinaryFileName() {
String osName = System.getProperty("os.name").toLowerCase();
String binaryFileName;
if (osName.contains("win")) {
binaryFileName = "test-reporter-windows-amd64.exe";
} else if (osName.contains("mac")) {
binaryFileName = "test-reporter-darwin-amd64";
} else {
binaryFileName = "test-reporter-linux-amd64";
}
return binaryFileName;
}
private ArrayList<String> resolveFilePaths(String pathsString) throws IOException {
if (pathsString == null) {
return new ArrayList<String>();
}
String[] paths = pathsString.split(" ");
ArrayList<String> resolvedPaths = new ArrayList<String>();
for (int i = 0; i < paths.length; i++) {
String path = paths[i];
resolvedPaths.add(new File(this.workspacePath, path.toString()).getPath());
}
return resolvedPaths;
}
}