-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
CreateAction.java
164 lines (144 loc) · 7 KB
/
CreateAction.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
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact 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 org.sonar.server.measure.custom.ws;
import org.sonar.api.resources.Scopes;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.text.JsonWriter;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.measure.custom.CustomMeasureDto;
import org.sonar.db.metric.MetricDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.user.UserSession;
import static com.google.common.base.Preconditions.checkArgument;
import static org.sonar.server.component.ComponentFinder.ParamNames.PROJECT_ID_AND_KEY;
import static org.sonar.server.measure.custom.ws.CustomMeasureValidator.checkPermissions;
import static org.sonar.server.measure.custom.ws.CustomMeasureValueDescription.measureValueDescription;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
public class CreateAction implements CustomMeasuresWsAction {
public static final String ACTION = "create";
public static final String PARAM_PROJECT_ID = "projectId";
public static final String PARAM_PROJECT_KEY = "projectKey";
public static final String PARAM_METRIC_ID = "metricId";
public static final String PARAM_METRIC_KEY = "metricKey";
public static final String PARAM_VALUE = "value";
public static final String PARAM_DESCRIPTION = "description";
private final DbClient dbClient;
private final UserSession userSession;
private final System2 system;
private final CustomMeasureValidator validator;
private final CustomMeasureJsonWriter customMeasureJsonWriter;
private final ComponentFinder componentFinder;
public CreateAction(DbClient dbClient, UserSession userSession, System2 system, CustomMeasureValidator validator, CustomMeasureJsonWriter customMeasureJsonWriter,
ComponentFinder componentFinder) {
this.dbClient = dbClient;
this.userSession = userSession;
this.system = system;
this.validator = validator;
this.customMeasureJsonWriter = customMeasureJsonWriter;
this.componentFinder = componentFinder;
}
@Override
public void define(WebService.NewController context) {
WebService.NewAction action = context.createAction(ACTION)
.setDescription("Create a custom measure.<br /> " +
"The project id or the project key must be provided (only project and module custom measures can be created). The metric id or the metric key must be provided.<br/>" +
"Requires 'Administer System' permission or 'Administer' permission on the project.")
.setSince("5.2")
.setPost(true)
.setHandler(this);
action.createParam(PARAM_PROJECT_ID)
.setDescription("Project id")
.setExampleValue("ce4c03d6-430f-40a9-b777-ad877c00aa4d");
action.createParam(PARAM_PROJECT_KEY)
.setDescription("Project key")
.setExampleValue(KEY_PROJECT_EXAMPLE_001);
action.createParam(PARAM_METRIC_ID)
.setDescription("Metric id")
.setExampleValue("16");
action.createParam(PARAM_METRIC_KEY)
.setDescription("Metric key")
.setExampleValue("ncloc");
action.createParam(PARAM_VALUE)
.setRequired(true)
.setDescription(measureValueDescription())
.setExampleValue("47");
action.createParam(PARAM_DESCRIPTION)
.setDescription("Description")
.setExampleValue("Team size growing.");
}
@Override
public void handle(Request request, Response response) throws Exception {
DbSession dbSession = dbClient.openSession(false);
String valueAsString = request.mandatoryParam(PARAM_VALUE);
String description = request.param(PARAM_DESCRIPTION);
long now = system.now();
try {
ComponentDto component = componentFinder.getByUuidOrKey(dbSession, request.param(PARAM_PROJECT_ID), request.param(PARAM_PROJECT_KEY), PROJECT_ID_AND_KEY);
MetricDto metric = searchMetric(dbSession, request);
checkPermissions(userSession, component);
checkIsProjectOrModule(component);
checkMeasureDoesNotExistAlready(dbSession, component, metric);
UserDto user = dbClient.userDao().selectOrFailByLogin(dbSession, userSession.getLogin());
CustomMeasureDto measure = new CustomMeasureDto()
.setComponentUuid(component.uuid())
.setMetricId(metric.getId())
.setDescription(description)
.setUserLogin(user.getLogin())
.setCreatedAt(now)
.setUpdatedAt(now);
validator.setMeasureValue(measure, valueAsString, metric);
dbClient.customMeasureDao().insert(dbSession, measure);
dbSession.commit();
JsonWriter json = response.newJsonWriter();
customMeasureJsonWriter.write(json, measure, metric, component, user, true, CustomMeasureJsonWriter.OPTIONAL_FIELDS);
json.close();
} finally {
dbClient.closeSession(dbSession);
}
}
private static void checkIsProjectOrModule(ComponentDto component) {
if (!Scopes.PROJECT.equals(component.scope())) {
throw new BadRequestException(String.format("Component '%s' (id: %s) must be a project or a module.", component.key(), component.uuid()));
}
}
private void checkMeasureDoesNotExistAlready(DbSession dbSession, ComponentDto component, MetricDto metric) {
int nbMeasuresOnSameMetricAndMeasure = dbClient.customMeasureDao().countByComponentIdAndMetricId(dbSession, component.uuid(), metric.getId());
if (nbMeasuresOnSameMetricAndMeasure > 0) {
throw new BadRequestException(String.format("A measure already exists for project '%s' (id: %s) and metric '%s' (id: '%d')",
component.key(), component.uuid(), metric.getKey(), metric.getId()));
}
}
private MetricDto searchMetric(DbSession dbSession, Request request) {
Integer metricId = request.paramAsInt(PARAM_METRIC_ID);
String metricKey = request.param(PARAM_METRIC_KEY);
checkArgument(metricId != null ^ metricKey != null, "The metric id or the metric key must be provided, not both.");
if (metricId != null) {
return dbClient.metricDao().selectOrFailById(dbSession, metricId);
}
return dbClient.metricDao().selectOrFailByKey(dbSession, metricKey);
}
}