Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* 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 org.sonar.server.component.ws;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Ordering;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.server.ws.Change;
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.server.ws.WebService.Param;
import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.property.PropertyDto;
import org.sonar.db.property.PropertyQuery;
import org.sonar.server.component.ws.FilterParser.Criterion;
import org.sonar.server.es.Facets;
import org.sonar.server.es.SearchIdResult;
import org.sonar.server.es.SearchOptions;
import org.sonar.server.measure.index.ProjectMeasuresIndex;
import org.sonar.server.measure.index.ProjectMeasuresQuery;
import org.sonar.server.project.Visibility;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Common;
import org.sonarqube.ws.WsComponents.Component;
import org.sonarqube.ws.WsComponents.SearchProjectsWsResponse;
import org.sonarqube.ws.client.component.SearchProjectsRequest;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.collect.Sets.newHashSet;
import static java.lang.String.format;
import static java.util.Collections.emptyMap;
import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY;
import static org.sonar.api.server.ws.WebService.Param.FIELDS;
import static org.sonar.api.utils.DateUtils.formatDateTime;
import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.core.util.stream.MoreCollectors.toSet;
import static org.sonar.db.measure.ProjectMeasuresIndexerIterator.METRIC_KEYS;
import static org.sonar.server.component.ws.ProjectMeasuresQueryFactory.IS_FAVORITE_CRITERION;
import static org.sonar.server.component.ws.ProjectMeasuresQueryFactory.newProjectMeasuresQuery;
import static org.sonar.server.component.ws.ProjectMeasuresQueryValidator.NON_METRIC_SORT_KEYS;
import static org.sonar.server.measure.index.ProjectMeasuresIndex.SUPPORTED_FACETS;
import static org.sonar.server.measure.index.ProjectMeasuresQuery.SORT_BY_LAST_ANALYSIS_DATE;
import static org.sonar.server.measure.index.ProjectMeasuresQuery.SORT_BY_NAME;
import static org.sonar.server.ws.WsUtils.checkFound;
import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_FILTER;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_ORGANIZATION;
import static org.sonarqube.ws.client.component.SearchProjectsRequest.DEFAULT_PAGE_SIZE;
import static org.sonarqube.ws.client.component.SearchProjectsRequest.MAX_PAGE_SIZE;
import static org.sonarqube.ws.client.project.ProjectsWsParameters.FILTER_LANGUAGES;
import static org.sonarqube.ws.client.project.ProjectsWsParameters.FILTER_TAGS;
public class SearchProjectsAction implements ComponentsWsAction {
private static final String ANALYSIS_DATE = "analysisDate";
private static final String LEAK_PERIOD_DATE = "leakPeriodDate";
private static final Set POSSIBLE_FIELDS = newHashSet(ANALYSIS_DATE, LEAK_PERIOD_DATE);
private final DbClient dbClient;
private final ProjectMeasuresIndex index;
private final UserSession userSession;
public SearchProjectsAction(DbClient dbClient, ProjectMeasuresIndex index, UserSession userSession) {
this.dbClient = dbClient;
this.index = index;
this.userSession = userSession;
}
@Override
public void define(WebService.NewController context) {
WebService.NewAction action = context.createAction("search_projects")
.setSince("6.2")
.setDescription("Search for projects")
.addPagingParams(DEFAULT_PAGE_SIZE, MAX_PAGE_SIZE)
.setInternal(true)
.setResponseExample(getClass().getResource("search_projects-example.json"))
.setChangelog(
new Change("6.4", format("The '%s' parameter accepts '%s' to filter by language", FILTER_LANGUAGES, PARAM_FILTER)),
new Change("6.4", "The 'visibility' field is added"),
new Change("6.5", "The 'filter' parameter now allows 'NO_DATA' as value for numeric metrics"),
new Change("6.5", "Added the option 'analysisDate' for the 'sort' parameter"),
new Change("6.5", format("Value '%s' is added to parameter '%s'", LEAK_PERIOD_DATE, FIELDS)))
.setHandler(this);
action.createFieldsParam(POSSIBLE_FIELDS)
.setDescription("Comma-separated list of the fields to be returned in response")
.setSince("6.4");
action.createParam(PARAM_ORGANIZATION)
.setDescription("the organization to search projects in")
.setRequired(false)
.setInternal(true)
.setSince("6.3");
action.createParam(Param.FACETS)
.setDescription("Comma-separated list of the facets to be computed. No facet is computed by default.")
.setPossibleValues(SUPPORTED_FACETS.stream().sorted().collect(MoreCollectors.toList(SUPPORTED_FACETS.size())));
action
.createParam(PARAM_FILTER)
.setDescription("Filter of projects on name, key, measure value, quality gate, language, tag or whether a project is a favorite or not. " +
"The filter must be encoded to form a valid URL (for example '=' must be replaced by '%3D'). " +
"Examples of use:" +
"
" +
"
to filter my favorite projects with a failed quality gate and a coverage greater than or equals to 60% and a coverage strictly lower than 80%: " +
" filter=\"alert_status = ERROR and isFavorite and coverage >= 60 and coverage < 80\"
" +
"
to filter projects with a reliability, security and maintainability rating equals or worse than B: " +
" filter=\"reliability_rating>=2 and security_rating>=2 and sqale_rating>=2\"
" +
"
to filter projects without duplication data: " +
" filter=\"duplicated_lines_density = NO_DATA\"
" +
"
" +
"To filter on project name or key, use the 'query' keyword, for instance : filter='query = \"Sonar\"'. " +
" " +
"To filter on a numeric metric, provide the metric key. " +
"These are the supported metric keys: " +
"
" +
METRIC_KEYS.stream().sorted().map(key -> "
" + key + "
").collect(Collectors.joining()) +
"
" +
" " +
"To filter on a rating, provide the corresponding metric key (ex: reliability_rating for reliability rating). " +
"The possible values are:" +
"
" +
"
'1' for rating A
" +
"
'2' for rating B
" +
"
'3' for rating C
" +
"
'4' for rating D
" +
"
'5' for rating E
" +
"
" +
"To filter on a Quality Gate status use the metric key 'alert_status'. Only the '=' operator can be used. " +
"The possible values are:" +
"
" +
"
'OK' for Passed
" +
"
'WARN' for Warning
" +
"
'ERROR' for Failed
" +
"
" +
"To filter on language keys use the language key: " +
"
" +
"
to filter on a single language you can use 'language = java'
" +
"
to filter on several languages you must use 'language IN (java, js)'
" +
"
" +
"Use the WS api/languages/list to find the key of a language. " +
"To filter on tags use the 'tag' keyword:" +
"
" +
"
to filter on one tag you can use tag = finance
" +
"
to filter on several tags you must use tag in (offshore, java)