-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathMsBuildSQRunnerEnd.java
More file actions
147 lines (123 loc) · 5.38 KB
/
MsBuildSQRunnerEnd.java
File metadata and controls
147 lines (123 loc) · 5.38 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
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
/*
* SonarQube Scanner for Jenkins
* Copyright (C) 2007-2025 SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package hudson.plugins.sonar;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.AbortException;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.sonar.action.SonarMarkerAction;
import hudson.plugins.sonar.client.HttpClient;
import hudson.plugins.sonar.client.OkHttpClientSingleton;
import hudson.plugins.sonar.utils.BuilderUtils;
import hudson.plugins.sonar.utils.SonarUtils;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import hudson.util.ArgumentListBuilder;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import jenkins.model.Jenkins;
import org.apache.commons.lang3.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import static hudson.plugins.sonar.utils.SonarUtils.PROPERTY_SONAR_LOGIN;
import static hudson.plugins.sonar.utils.SonarUtils.PROPERTY_SONAR_TOKEN;
public class MsBuildSQRunnerEnd extends AbstractMsBuildSQRunner {
@DataBoundConstructor
public MsBuildSQRunnerEnd() {
// will use MSBuild SQ Scanner installation defined in Begin
}
@Override
public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException {
ArgumentListBuilder args = new ArgumentListBuilder();
EnvVars env = BuilderUtils.getEnvAndBuildVars(run, listener);
SonarQubeScannerMsBuildParams beginParams = run.getAction(SonarQubeScannerMsBuildParams.class);
if (beginParams == null) {
throw new AbortException(Messages.MsBuildScannerEnd_NoBeginStep());
}
String scannerName = beginParams.getScannerName();
String sonarInstName = beginParams.getSqServerName();
SonarInstallation sonarInstallation = getSonarInstallation(sonarInstName, listener);
MsBuildSQRunnerInstallation msBuildScanner = Jenkins.get().getDescriptorByType(MsBuildSQRunnerBegin.DescriptorImpl.class)
.getMsBuildScannerInstallation(scannerName);
String scannerPath = getScannerPath(msBuildScanner, env, launcher, listener, workspace);
if (isDotNetCoreTool(scannerPath)) {
addDotNetCommand(args);
}
args.add(scannerPath);
addArgs(args, env, sonarInstallation, run);
int result = launcher.launch().cmds(args).envs(env).stdout(listener).pwd(BuilderUtils.getModuleRoot(run, workspace)).join();
if (result != 0) {
addBadge(run, listener, workspace, sonarInstallation);
throw new AbortException(Messages.MSBuildScanner_ExecFailed(result));
}
addBadge(run, listener, workspace, sonarInstallation);
}
private void addArgs(ArgumentListBuilder args, EnvVars env, SonarInstallation sonarInstallation, Run<?, ?> run) {
Map<String, String> props = getSonarProps(sonarInstallation, run);
args.add("end");
// expand macros using itself
EnvVars.resolve(props);
for (Map.Entry<String, String> e : props.entrySet()) {
if (!StringUtils.isEmpty(e.getValue())) {
// expand macros using environment variables and hide token
boolean hide = e.getKey().contains(PROPERTY_SONAR_LOGIN) || e.getKey().contains(PROPERTY_SONAR_TOKEN);
args.addKeyValuePair("/d:", e.getKey(), env.expand(e.getValue()), hide);
}
}
args.addTokenized(sonarInstallation.getAdditionalProperties());
}
private Map<String, String> getSonarProps(SonarInstallation inst, Run<?, ?> run) {
Map<String, String> map = new LinkedHashMap<>();
String token = inst.getServerAuthenticationToken(run);
if (!StringUtils.isBlank(token)) {
map.put(SonarUtils.getTokenProperty(inst, new HttpClient(OkHttpClientSingleton.getInstance())), token);
}
return map;
}
private static void addBadge(Run<?, ?> run, TaskListener listener, FilePath workspace, SonarInstallation sonarInstallation) throws IOException, InterruptedException {
SonarUtils.addBuildInfoTo(run, listener, workspace, sonarInstallation);
}
@Override
public Action getProjectAction(AbstractProject<?, ?> project) {
return new SonarMarkerAction();
}
@Extension
public static class DescriptorImpl extends BuildStepDescriptor<Builder> {
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}
@Override
public String getHelpFile() {
return "/plugin/sonar/help-ms-build-sq-scanner-end.html";
}
@NonNull
@Override
public String getDisplayName() {
return Messages.MsBuildScannerEnd_DisplayName();
}
}
}