import org.sonar.core.issue.DefaultIssue;
import org.sonar.core.issue.DefaultIssueBuilder;
import org.sonar.core.issue.IssueChangeContext;
-import org.sonar.core.issue.IssueType;
-import org.sonar.server.issue.workflow.IssueWorkflow;
-import org.sonar.server.issue.workflow.Transition;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.server.issue.index.IssueDoc;
import org.sonar.server.issue.index.IssueIndex;
import org.sonar.server.issue.notification.IssueChangeNotification;
+import org.sonar.server.issue.workflow.IssueWorkflow;
+import org.sonar.server.issue.workflow.Transition;
import org.sonar.server.notification.NotificationManager;
import org.sonar.server.source.SourceService;
import org.sonar.server.user.UserSession;
package org.sonar.server.issue.ws;
import com.google.common.base.Function;
-import com.google.common.collect.FluentIterable;
import com.google.common.collect.Lists;
import com.google.common.io.Resources;
import java.util.Collection;
import org.sonar.api.server.ws.WebService.Param;
import org.sonar.api.utils.Paging;
import org.sonar.core.issue.IssueType;
-import org.sonar.server.rule.RuleKeyFunctions;
import org.sonar.server.es.Facets;
import org.sonar.server.es.SearchOptions;
import org.sonar.server.es.SearchResult;
import org.sonar.server.issue.IssueService;
import org.sonar.server.issue.index.IssueDoc;
import org.sonar.server.issue.index.IssueIndex;
+import org.sonar.server.rule.RuleKeyFunctions;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Issues.SearchWsResponse;
import org.sonarqube.ws.client.issue.IssueFilterParameters;
addMandatoryValuesToFacet(facets, IssueFilterParameters.RULES, request.getRules());
addMandatoryValuesToFacet(facets, IssueFilterParameters.LANGUAGES, request.getLanguages());
addMandatoryValuesToFacet(facets, IssueFilterParameters.TAGS, request.getTags());
- addMandatoryValuesToFacet(facets, IssueFilterParameters.TYPES, request.getTypes());
+ addMandatoryValuesToFacet(facets, IssueFilterParameters.TYPES, IssueType.ALL_NAMES);
List<String> actionPlans = Lists.newArrayList("");
List<String> actionPlansFromRequest = request.getActionPlans();
if (actionPlansFromRequest != null) {
import java.util.Iterator;
import org.elasticsearch.action.index.IndexRequest;
-import org.elasticsearch.action.update.UpdateRequest;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.server.es.BaseIndexer;
userSessionRule.login("john");
WsTester.Result result = wsTester.newGetRequest(IssuesWs.API_ENDPOINT, SearchAction.SEARCH_ACTION)
.setParam("resolved", "false")
- .setParam(WebService.Param.FACETS, "statuses,severities,resolutions,projectUuids,rules,fileUuids,assignees,languages,actionPlans")
+ .setParam(WebService.Param.FACETS, "statuses,severities,resolutions,projectUuids,rules,fileUuids,assignees,languages,actionPlans,types")
.execute();
result.assertJson(this.getClass(), "display_facets.json");
}
"count": 1
}
]
+ },
+ {
+ "property": "types",
+ "values": [
+ {
+ "val": "CODE_SMELL",
+ "count": 1
+ },
+ {
+ "val": "BUG",
+ "count": 0
+ },
+ {
+ "val": "VULNERABILITY",
+ "count": 0
+ }
+ ]
}
]}
--- /dev/null
+#
+# SonarQube, open source software quality management tool.
+# Copyright (C) 2008-2014 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# SonarQube 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.
+#
+# SonarQube 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.
+#
+
+#
+# SonarQube 5.5
+# MMF-141
+#
+class FeedIssuesTypes < ActiveRecord::Migration
+
+ def self.up
+ execute_java_migration('org.sonar.db.version.v55.FeedIssueTypes')
+ end
+
+end
*/
package org.sonar.core.issue;
+import com.google.common.base.Enums;
+import com.google.common.collect.Lists;
+import java.util.List;
+
import static java.lang.String.format;
public enum IssueType {
}
throw new IllegalArgumentException(format("Unsupported value for db column ISSUES.ISSUE_TYPE: %d", dbConstant));
}
+
+ public static List<String> ALL_NAMES = Lists.transform(Lists.newArrayList(values()), Enums.stringConverter(IssueType.class).reverse());
}
--- /dev/null
+/*
+ * 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.core.issue;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class IssueTypeTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Test
+ public void test_valueOf_db_constant() {
+ assertThat(IssueType.valueOf(1)).isEqualTo(IssueType.CODE_SMELL);
+ assertThat(IssueType.valueOf(2)).isEqualTo(IssueType.BUG);
+ }
+
+ @Test
+ public void valueOf_throws_ISE_if_unsupported_db_constant() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Unsupported value for db column ISSUES.ISSUE_TYPE: 4");
+ IssueType.valueOf(4);
+ }
+
+ @Test
+ public void test_ALL_NAMES() {
+ assertThat(IssueType.ALL_NAMES).containsOnly("BUG", "VULNERABILITY", "CODE_SMELL");
+ }
+}
public class DatabaseVersion {
- public static final int LAST_VERSION = 1106;
+ public static final int LAST_VERSION = 1107;
/**
* The minimum supported version which can be upgraded. Lower
import org.sonar.db.version.v55.AddIssuesType;
import org.sonar.db.version.v55.AddRulesLongDateColumns;
import org.sonar.db.version.v55.DeleteMeasuresWithCharacteristicId;
+import org.sonar.db.version.v55.FeedIssueTypes;
import org.sonar.db.version.v55.FeedRulesLongDateColumns;
public class MigrationStepModule extends Module {
AddRulesLongDateColumns.class,
FeedRulesLongDateColumns.class,
DeleteMeasuresWithCharacteristicId.class,
- AddIssuesType.class
+ AddIssuesType.class,
+ FeedIssueTypes.class
);
}
}
--- /dev/null
+/*
+ * 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.db.version.v55;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.Splitter;
+import java.sql.SQLException;
+import java.util.List;
+import org.sonar.api.utils.System2;
+import org.sonar.core.issue.IssueType;
+import org.sonar.db.Database;
+import org.sonar.db.version.BaseDataChange;
+import org.sonar.db.version.MassUpdate;
+import org.sonar.db.version.MassUpdate.Handler;
+import org.sonar.db.version.Select.Row;
+import org.sonar.db.version.SqlStatement;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.apache.commons.lang.StringUtils.defaultString;
+
+/**
+ * Duplicates RuleTagsToTypeConverter from API on purpose. Db migration must be isolated and must not
+ * depend on external code that may evolve over time.
+ *
+ * https://jira.sonarsource.com/browse/MMF-141
+ */
+public class FeedIssueTypes extends BaseDataChange {
+
+ private final long now;
+
+ public FeedIssueTypes(Database db, System2 system) {
+ super(db);
+ this.now = system.now();
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ final Splitter tagSplitter = Splitter.on(',');
+ final Joiner tagJoiner = Joiner.on(',').skipNulls();
+
+ MassUpdate update = context.prepareMassUpdate().rowPluralName("issues");
+ update.select("SELECT id, tags FROM issues WHERE issue_type IS NULL OR issue_type=0");
+ update.update("UPDATE issues SET issue_type=?, tags=?, updated_at=? WHERE id=?");
+ update.execute(new Handler() {
+ @Override
+ public boolean handle(Row row, SqlStatement update) throws SQLException {
+ long id = row.getLong(1);
+
+ // See algorithm to deduce type from tags in RuleTagsToTypeConverter
+ List<String> tags = newArrayList(tagSplitter.split(defaultString(row.getNullableString(2))));
+ IssueType type = tagsToType(tags);
+ tags.remove("bug");
+ tags.remove("security");
+
+ update.setInt(1, type.getDbConstant());
+ update.setString(2, tagJoiner.join(tags));
+ update.setLong(3, now);
+ update.setLong(4, id);
+ return true;
+ }
+ });
+ }
+
+ static IssueType tagsToType(List<String> tags) {
+ IssueType type = IssueType.CODE_SMELL;
+ if (tags.contains("bug")) {
+ type = IssueType.BUG;
+ } else if (tags.contains("security")) {
+ type = IssueType.VULNERABILITY;
+ }
+ return type;
+ }
+
+}
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1101');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1102');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1103');
-INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1106');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1107');
INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482', null, null);
ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;
assertThat(issue.getRuleId()).isEqualTo(500);
assertThat(issue.getLanguage()).isEqualTo("java");
assertThat(issue.getSeverity()).isEqualTo("BLOCKER");
+ assertThat(issue.getType()).isEqualTo(2);
assertThat(issue.isManualSeverity()).isFalse();
assertThat(issue.getMessage()).isNull();
assertThat(issue.getLine()).isEqualTo(200);
public void verify_count_of_added_MigrationStep_types() {
ComponentContainer container = new ComponentContainer();
new MigrationStepModule().configure(container);
- assertThat(container.size()).isEqualTo(55);
+ assertThat(container.size()).isEqualTo(56);
}
}
--- /dev/null
+/*
+ * 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.db.version.v55;
+
+import java.util.Arrays;
+import java.util.Collections;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.core.issue.IssueType;
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+import org.sonar.db.issue.IssueDto;
+import org.sonar.db.version.MigrationStep;
+
+import static java.util.Arrays.asList;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.sonar.db.version.v55.FeedIssueTypes.tagsToType;
+
+public class FeedIssueTypesTest {
+
+ @Rule
+ public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedIssueTypesTest.class, "schema.sql");
+
+ @Test
+ public void test_tagsToType() {
+ assertThat(tagsToType(asList("misra", "bug"))).isEqualTo(IssueType.BUG);
+ assertThat(tagsToType(asList("misra", "security"))).isEqualTo(IssueType.VULNERABILITY);
+
+ // "bug" has priority on "security"
+ assertThat(tagsToType(asList("security", "bug"))).isEqualTo(IssueType.BUG);
+
+ // default is "code smell"
+ assertThat(tagsToType(asList("clumsy", "spring"))).isEqualTo(IssueType.CODE_SMELL);
+ assertThat(tagsToType(Collections.<String>emptyList())).isEqualTo(IssueType.CODE_SMELL);
+ }
+
+ @Test
+ public void test_migration() throws Exception {
+ try (DbSession dbSession = db.getSession()) {
+ IssueDto codeSmell = new IssueDto().setKee("code_smell").setTags(Arrays.asList("clumsy", "spring"));
+ IssueDto withoutTags = new IssueDto().setKee("without_tags");
+ IssueDto bug = new IssueDto().setKee("bug").setTags(Arrays.asList("clumsy", "bug"));
+ db.getDbClient().issueDao().insert(dbSession, codeSmell, withoutTags, bug);
+ dbSession.commit();
+
+ MigrationStep underTest = new FeedIssueTypes(db.database(), mock(System2.class));
+ underTest.execute();
+
+ assertType("code_smell", IssueType.CODE_SMELL);
+ assertType("without_tags", IssueType.CODE_SMELL);
+ assertType("bug", IssueType.BUG);
+ }
+ }
+
+ private void assertType(String issueKey, IssueType expectedType) {
+ Number type = (Number)db.selectFirst("select * from issues where kee='" + issueKey + "'").get("ISSUE_TYPE");
+ assertThat(type.intValue()).isEqualTo(expectedType.getDbConstant());
+ }
+
+}
System2 system = mock(System2.class);
- MigrationStep migration = new FeedRulesLongDateColumns(db.database(), system);
+ MigrationStep underTest = new FeedRulesLongDateColumns(db.database(), system);
@Before
public void setUp() throws Exception {
public void execute() throws Exception {
db.prepareDbUnit(getClass(), "execute.xml");
- migration.execute();
+ underTest.execute();
assertThat(db.countSql("select count(*) from rules where created_at_ms is not null and updated_at_ms is not null")).isEqualTo(3);
// Only 1 rules not updated
created_at="1400000000000"
updated_at="1450000000000"
locations="[null]"
+ issue_type="2"
/>
<issues
created_at="1400000000000"
updated_at="1450000000000"
locations="[null]"
+ issue_type="2"
/>
</dataset>
--- /dev/null
+CREATE TABLE "ISSUES" (
+ "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "KEE" VARCHAR(50) UNIQUE NOT NULL,
+ "COMPONENT_UUID" VARCHAR(50),
+ "PROJECT_UUID" VARCHAR(50),
+ "RULE_ID" INTEGER,
+ "SEVERITY" VARCHAR(10),
+ "MANUAL_SEVERITY" BOOLEAN NOT NULL,
+ "MESSAGE" VARCHAR(4000),
+ "LINE" INTEGER,
+ "EFFORT_TO_FIX" DOUBLE,
+ "TECHNICAL_DEBT" INTEGER,
+ "STATUS" VARCHAR(20),
+ "RESOLUTION" VARCHAR(20),
+ "CHECKSUM" VARCHAR(1000),
+ "REPORTER" VARCHAR(255),
+ "ASSIGNEE" VARCHAR(255),
+ "AUTHOR_LOGIN" VARCHAR(255),
+ "ACTION_PLAN_KEY" VARCHAR(50) NULL,
+ "ISSUE_ATTRIBUTES" VARCHAR(4000),
+ "TAGS" VARCHAR(4000),
+ "ISSUE_CREATION_DATE" BIGINT,
+ "ISSUE_CLOSE_DATE" BIGINT,
+ "ISSUE_UPDATE_DATE" BIGINT,
+ "CREATED_AT" BIGINT,
+ "UPDATED_AT" BIGINT,
+ "LOCATIONS" BLOB(167772150),
+ "ISSUE_TYPE" TINYINT
+);
this.status = newRule.status;
this.debtRemediationFunction = newRule.debtRemediationFunction;
this.effortToFixDescription = newRule.effortToFixDescription;
- this.type = (newRule.type == null ? RuleTagsToTypeConverter.convert(newRule.tags) : newRule.type);
+ this.type = newRule.type == null ? RuleTagsToTypeConverter.convert(newRule.tags) : newRule.type;
this.tags = ImmutableSortedSet.copyOf(Sets.difference(newRule.tags, RuleTagsToTypeConverter.RESERVED_TAGS));
ImmutableMap.Builder<String, Param> paramsBuilder = ImmutableMap.builder();
for (NewParam newParam : newRule.paramsByKey.values()) {
public static final String FACET_ASSIGNED_TO_ME = "assigned_to_me";
- public static final List<String> ALL = ImmutableList.of(ISSUES, SEVERITIES, STATUSES, RESOLUTIONS, RESOLVED, COMPONENTS, COMPONENT_ROOTS, RULES, ACTION_PLANS, REPORTERS, TAGS, TYPES,
- ASSIGNEES, LANGUAGES, ASSIGNED, PLANNED, HIDE_RULES, CREATED_AT, CREATED_AFTER, CREATED_BEFORE, CREATED_IN_LAST, COMPONENT_UUIDS, COMPONENT_ROOT_UUIDS, FACET_MODE,
- PROJECTS, PROJECT_UUIDS, PROJECT_KEYS, COMPONENT_KEYS, MODULE_UUIDS, DIRECTORIES, FILE_UUIDS, AUTHORS, HIDE_COMMENTS, PAGE_SIZE, PAGE_INDEX, SORT, ASC);
+ public static final List<String> ALL = ImmutableList.of(ISSUES, SEVERITIES, STATUSES, RESOLUTIONS, RESOLVED,
+ COMPONENTS, COMPONENT_ROOTS, RULES, ACTION_PLANS, REPORTERS, TAGS, TYPES,
+ ASSIGNEES, LANGUAGES, ASSIGNED, PLANNED, HIDE_RULES, CREATED_AT, CREATED_AFTER, CREATED_BEFORE, CREATED_IN_LAST,
+ COMPONENT_UUIDS, COMPONENT_ROOT_UUIDS, FACET_MODE,
+ PROJECTS, PROJECT_UUIDS, PROJECT_KEYS, COMPONENT_KEYS, MODULE_UUIDS, DIRECTORIES, FILE_UUIDS, AUTHORS,
+ HIDE_COMMENTS, PAGE_SIZE, PAGE_INDEX, SORT, ASC);
public static final List<String> ALL_WITHOUT_PAGINATION = ImmutableList.copyOf(Iterables.filter(ALL, new Predicate<String>() {
@Override