Issue toIssue(Violation violation) {
DefaultIssue issue = new DefaultIssue();
issue.setComponentKey(violation.getResource().getEffectiveKey());
- if (violation.getPermanentId() != null) {
- issue.setKey(violation.getPermanentId().toString());
- } else {
- issue.setKey(UUID.randomUUID().toString());
- }
+ issue.setKey(UUID.randomUUID().toString());
issue.setRuleRepositoryKey(violation.getRule().getRepositoryKey());
issue.setRuleKey(violation.getRule().getKey());
issue.setCost(violation.getCost());
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.batch.issue;
+
+import org.sonar.api.issue.Issue;
+import org.sonar.api.issue.IssueActions;
+import org.sonar.core.issue.DefaultIssue;
+
+import javax.annotation.Nullable;
+
+public class ScanIssueActions implements IssueActions {
+
+ @Override
+ public Issue comment(Issue issue, String userLogin, String comment) {
+ throw new UnsupportedOperationException("TODO");
+ }
+
+ @Override
+ public Issue setSeverity(Issue issue, String severity) {
+ DefaultIssue impl = (DefaultIssue) issue;
+ impl.setSeverity(severity);
+ return impl;
+ }
+
+ @Override
+ public Issue setManualSeverity(Issue issue, String severity) {
+ DefaultIssue impl = (DefaultIssue) issue;
+ impl.setSeverity(severity);
+ impl.setManualSeverity(true);
+ return impl;
+ }
+
+ @Override
+ public Issue setMessage(Issue issue, String message) {
+ DefaultIssue impl = (DefaultIssue) issue;
+ impl.setMessage(message);
+ return impl;
+ }
+
+ @Override
+ public Issue setCost(Issue issue, @Nullable Double cost) {
+ DefaultIssue impl = (DefaultIssue) issue;
+ impl.setCost(cost);
+ return impl;
+ }
+
+ @Override
+ public Issue setResolution(Issue issue, String resolution) {
+ DefaultIssue impl = (DefaultIssue) issue;
+ impl.setResolution(resolution);
+ return impl;
+ }
+
+ @Override
+ public Issue assign(Issue issue, String userLogin) {
+ DefaultIssue impl = (DefaultIssue) issue;
+ impl.setUserLogin(userLogin);
+ return impl;
+ }
+
+ @Override
+ public Issue setAttribute(Issue issue, String key, @Nullable String value) {
+ DefaultIssue impl = (DefaultIssue) issue;
+ impl.setAttribute(key, value);
+ return impl;
+ }
+}
import org.sonar.batch.issue.DeprecatedViolations;
import org.sonar.batch.issue.IssueCache;
import org.sonar.batch.issue.IssuePersister;
+import org.sonar.batch.issue.ScanIssueActions;
import org.sonar.batch.phases.GraphPersister;
import org.sonar.batch.scan.maven.FakeMavenPluginExecutor;
import org.sonar.batch.scan.maven.MavenPluginExecutor;
LastSnapshots.class,
SnapshotCache.class,
+ ScanIssueActions.class,
DeprecatedViolations.class,
IssueCache.class,
IssuePersister.class,
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.batch.issue;
+
+import org.junit.Test;
+import org.sonar.api.issue.Issue;
+import org.sonar.core.issue.DefaultIssue;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class ScanIssueActionsTest {
+
+ ScanIssueActions actions = new ScanIssueActions();
+
+ @Test
+ public void should_set_severity() throws Exception {
+ Issue issue = new DefaultIssue();
+ issue = actions.setSeverity(issue, Issue.SEVERITY_INFO);
+ assertThat(issue.severity()).isEqualTo(Issue.SEVERITY_INFO);
+ }
+
+ @Test
+ public void should_set_cost() throws Exception {
+ Issue issue = new DefaultIssue();
+ issue = actions.setCost(issue, 123.0);
+ assertThat(issue.cost()).isEqualTo(123.0);
+ }
+}
public class DefaultIssue implements Issue {
private static final Set<String> SEVERITIES = ImmutableSet.of(SEVERITY_BLOCKER, SEVERITY_CRITICAL, SEVERITY_MAJOR, SEVERITY_MINOR, SEVERITY_INFO);
+ private static final Set<String> RESOLUTIONS = ImmutableSet.of(RESOLUTION_FALSE_POSITIVE, RESOLUTION_FIXED);
+ private static final Set<String> STATUSES = ImmutableSet.of(STATUS_OPEN, STATUS_CLOSED, STATUS_REOPENED, STATUS_RESOLVED);
private String key;
private String componentKey;
private String ruleKey;
private String ruleRepositoryKey;
private String severity;
+ private boolean manualSeverity = false;
private String title;
private String message;
private Integer line;
return componentKey;
}
- public DefaultIssue setComponentKey(String componentKey) {
- this.componentKey = componentKey;
+ public DefaultIssue setComponentKey(String s) {
+ this.componentKey = s;
return this;
}
return this;
}
+ public boolean isManualSeverity() {
+ return manualSeverity;
+ }
+
+ public DefaultIssue setManualSeverity(boolean b) {
+ this.manualSeverity = b;
+ return this;
+ }
+
public String title() {
return title;
}
return status;
}
- public DefaultIssue setStatus(@Nullable String status) {
- this.status = status;
+ public DefaultIssue setStatus(@Nullable String s) {
+ Preconditions.checkArgument(s == null || STATUSES.contains(s), "Not a valid status: " + s);
+ this.status = s;
return this;
}
return resolution;
}
- public DefaultIssue setResolution(@Nullable String resolution) {
- this.resolution = resolution;
+ public DefaultIssue setResolution(@Nullable String s) {
+ Preconditions.checkArgument(s == null || RESOLUTIONS.contains(s), "Not a valid resolution: " + s);
+ this.resolution = s;
return this;
}
return attributes == null ? null : attributes.get(key);
}
- public DefaultIssue setAttribute(String key, String value) {
+ public DefaultIssue setAttribute(String key, @Nullable String value) {
if (attributes == null) {
attributes = Maps.newHashMap();
}
- attributes.put(key, value);
+ if (value == null) {
+ attributes.remove(key);
+ } else {
+ attributes.put(key, value);
+ }
return this;
}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.core.issue;
+
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
+
+public class DefaultIssueTest {
+
+ DefaultIssue issue = new DefaultIssue();
+
+ @Test
+ public void test_attributes() throws Exception {
+ assertThat(issue.attribute("foo")).isNull();
+ issue.setAttribute("foo", "bar");
+ assertThat(issue.attribute("foo")).isEqualTo("bar");
+ issue.setAttribute("foo", "newbar");
+ assertThat(issue.attribute("foo")).isEqualTo("newbar");
+ issue.setAttribute("foo", null);
+ assertThat(issue.attribute("foo")).isNull();
+ }
+
+ @Test
+ public void should_fail_on_bad_status() {
+ try {
+ issue.setStatus("FOO");
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertThat(e).hasMessage("Not a valid status: FOO");
+ }
+ }
+
+ @Test
+ public void should_fail_on_bad_resolution() {
+ try {
+ issue.setResolution("FOO");
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertThat(e).hasMessage("Not a valid resolution: FOO");
+ }
+ }
+
+ @Test
+ public void should_fail_on_bad_severity() {
+ try {
+ issue.setSeverity("FOO");
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertThat(e).hasMessage("Not a valid severity: FOO");
+ }
+ }
+}
String STATUS_REOPENED = "REOPENED";
String STATUS_RESOLVED = "RESOLVED";
String STATUS_CLOSED = "CLOSED";
+
String RESOLUTION_FALSE_POSITIVE = "FALSE-POSITIVE";
String RESOLUTION_FIXED = "FIXED";
+
String SEVERITY_INFO = "INFO";
String SEVERITY_MINOR = "MINOR";
String SEVERITY_MAJOR = "MAJOR";
String SEVERITY_BLOCKER = "BLOCKER";
/**
- * Unique key
+ * Unique generated key
*/
String key();
public interface IssueActions extends BatchComponent, ServerComponent {
- IssueActions comment(Issue issue, String userLogin, String comment);
- IssueActions setSeverity(Issue issue, String severity);
- IssueActions setMessage(Issue issue, String message);
- IssueActions setCost(Issue issue, @Nullable Double cost);
- IssueActions setResolution(Issue issue, String resolution);
- IssueActions assign(Issue issue, String userLogin);
- IssueActions setAttribute(Issue issue, String key, @Nullable String value);
+ Issue comment(Issue issue, String userLogin, String comment);
+
+ Issue setSeverity(Issue issue, String severity);
+
+ Issue setManualSeverity(Issue issue, String severity);
+
+ Issue setMessage(Issue issue, String message);
+
+ Issue setCost(Issue issue, @Nullable Double cost);
+
+ Issue setResolution(Issue issue, String resolution);
+
+ Issue assign(Issue issue, String userLogin);
+
+ Issue setAttribute(Issue issue, String key, @Nullable String value);
}