import java.util.Date;
import java.util.Optional;
-import org.sonar.ce.task.projectanalysis.component.Component;
-import org.sonar.core.issue.DefaultIssue;
-import org.sonar.core.issue.IssueChangeContext;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
+import org.sonar.ce.task.projectanalysis.component.Component;
import org.sonar.ce.task.projectanalysis.filemove.MovedFilesRepository;
import org.sonar.ce.task.projectanalysis.filemove.MovedFilesRepository.OriginalFile;
+import org.sonar.core.issue.DefaultIssue;
import org.sonar.server.issue.IssueFieldsSetter;
import static com.google.common.base.Preconditions.checkState;
"Issue %s doesn't belong to file %s registered as original file of current file %s",
issue, originalFile.getUuid(), component);
- // changes the issue's component uuid, add a change and set issue as changed to enforce it is persisted to DB
- issueUpdater.setIssueMoved(issue, component.getUuid(), IssueChangeContext.createUser(new Date(analysisMetadataHolder.getAnalysisDate()), null));
- // other fields (such as module, modulePath, componentKey) are read-only and set/reset for consistency only
- issue.setComponentKey(component.getKey());
- issue.setModuleUuidPath(null);
+ // changes the issue's component uuid, and set issue as changed to enforce it is persisted to DB
+ issueUpdater.setIssueComponent(issue, component.getUuid(), component.getKey(), new Date(analysisMetadataHolder.getAnalysisDate()));
}
}
class ProjectTrackerBaseLazyInput extends BaseInputFactory.BaseLazyInput {
- private AnalysisMetadataHolder analysisMetadataHolder;
- private ComponentsWithUnprocessedIssues componentsWithUnprocessedIssues;
- private DbClient dbClient;
- private IssueFieldsSetter issueUpdater;
- private ComponentIssuesLoader issuesLoader;
- private ReportModulesPath reportModulesPath;
+ private final AnalysisMetadataHolder analysisMetadataHolder;
+ private final ComponentsWithUnprocessedIssues componentsWithUnprocessedIssues;
+ private final DbClient dbClient;
+ private final IssueFieldsSetter issueUpdater;
+ private final ComponentIssuesLoader issuesLoader;
+ private final ReportModulesPath reportModulesPath;
ProjectTrackerBaseLazyInput(AnalysisMetadataHolder analysisMetadataHolder, ComponentsWithUnprocessedIssues componentsWithUnprocessedIssues, DbClient dbClient,
IssueFieldsSetter issueUpdater, ComponentIssuesLoader issuesLoader, ReportModulesPath reportModulesPath, Component component) {
if (StringUtils.isNotBlank(modulePath)) {
issueUpdater.setMessage(i, "[" + modulePath + "] " + i.getMessage(), context);
}
- issueUpdater.setIssueMoved(i, component.getUuid(), context);
- // other fields (such as module, modulePath, componentKey) are read-only and set/reset for consistency only
- i.setComponentKey(component.getKey());
- i.setModuleUuidPath(null);
+ issueUpdater.setIssueComponent(i, component.getUuid(), component.getKey(), context.date());
}
return issuesOnModule;
}
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
-import org.mockito.ArgumentCaptor;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
import org.sonar.ce.task.projectanalysis.component.Component;
import org.sonar.ce.task.projectanalysis.component.ReportComponent;
import org.sonar.ce.task.projectanalysis.filemove.MovedFilesRepository;
import org.sonar.core.issue.DefaultIssue;
-import org.sonar.core.issue.IssueChangeContext;
import org.sonar.server.issue.IssueFieldsSetter;
-import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
@org.junit.Rule
public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
- private MovedFilesRepository movedFilesRepository = mock(MovedFilesRepository.class);
- private MovedIssueVisitor underTest = new MovedIssueVisitor(analysisMetadataHolder, movedFilesRepository, new IssueFieldsSetter());
+ private final MovedFilesRepository movedFilesRepository = mock(MovedFilesRepository.class);
+ private final MovedIssueVisitor underTest = new MovedIssueVisitor(analysisMetadataHolder, movedFilesRepository, new IssueFieldsSetter());
@Before
public void setUp() {
verify(issue).setComponentUuid(FILE.getUuid());
verify(issue).setComponentKey(FILE.getKey());
verify(issue).setModuleUuidPath(null);
+ verify(issue).setUpdateDate(new Date(ANALYSIS_DATE));
verify(issue).setChanged(true);
- ArgumentCaptor<IssueChangeContext> issueChangeContextCaptor = ArgumentCaptor.forClass(IssueChangeContext.class);
- verify(issue).setFieldChange(issueChangeContextCaptor.capture(), eq("file"), eq(originalFile.getUuid()), eq(FILE.getUuid()));
- assertThat(issueChangeContextCaptor.getValue().date()).isEqualTo(new Date(ANALYSIS_DATE));
- assertThat(issueChangeContextCaptor.getValue().userUuid()).isNull();
- assertThat(issueChangeContextCaptor.getValue().scan()).isFalse();
}
private DefaultIssue mockIssue(String fileUuid) {
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private IssueChangeDao underTest = db.getDbClient().issueChangeDao();
+ private final IssueChangeDao underTest = db.getDbClient().issueChangeDao();
@Test
public void select_issue_changelog_from_issue_key() {
return false;
}
- public boolean setIssueMoved(DefaultIssue issue, String newComponentUuid, IssueChangeContext context) {
+ public void setIssueComponent(DefaultIssue issue, String newComponentUuid, String newComponentKey, Date updateDate) {
if (!Objects.equals(newComponentUuid, issue.componentUuid())) {
- issue.setFieldChange(context, FILE, issue.componentUuid(), newComponentUuid);
issue.setComponentUuid(newComponentUuid);
- issue.setUpdateDate(context.date());
+ issue.setUpdateDate(updateDate);
issue.setChanged(true);
- return true;
}
- return false;
+
+ // other fields (such as module, modulePath, componentKey) are read-only and set/reset for consistency only
+ issue.setComponentKey(newComponentKey);
+ issue.setModuleUuidPath(null);
}
private static boolean relevantDateDifference(@Nullable Date left, @Nullable Date right) {
import java.util.Calendar;
import java.util.Date;
-import java.util.Map;
import java.util.Random;
import org.apache.commons.lang.time.DateUtils;
import org.junit.Test;
private final String DEFAULT_RULE_DESCRIPTION_CONTEXT_KEY = "spring";
- private DefaultIssue issue = new DefaultIssue();
- private IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid");
- private IssueFieldsSetter underTest = new IssueFieldsSetter();
+ private final DefaultIssue issue = new DefaultIssue();
+ private final IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid");
+ private final IssueFieldsSetter underTest = new IssueFieldsSetter();
@Test
public void assign() {
}
@Test
- public void setIssueMoved_has_no_effect_if_component_uuid_is_not_changed() {
- String componentUuid = "a";
+ public void setIssueComponent_has_no_effect_if_component_uuid_is_not_changed() {
+ String componentKey = "key";
+ String componentUuid = "uuid";
+
issue.setComponentUuid(componentUuid);
+ issue.setComponentKey(componentKey);
- underTest.setIssueMoved(issue, componentUuid, context);
+ underTest.setIssueComponent(issue, componentUuid, componentKey, context.date());
- assertThat(issue.changes()).isEmpty();
assertThat(issue.componentUuid()).isEqualTo(componentUuid);
+ assertThat(issue.componentKey()).isEqualTo(componentKey);
assertThat(issue.isChanged()).isFalse();
assertThat(issue.updateDate()).isNull();
assertThat(issue.mustSendNotifications()).isFalse();
}
@Test
- public void setIssueMoved_changes_componentUuid_adds_a_change() {
+ public void setIssueComponent_changes_component_uuid() {
String oldComponentUuid = "a";
String newComponentUuid = "b";
+ String componentKey = "key";
+
issue.setComponentUuid(oldComponentUuid);
- underTest.setIssueMoved(issue, newComponentUuid, context);
+ underTest.setIssueComponent(issue, newComponentUuid, componentKey, context.date());
- assertThat(issue.changes()).hasSize(1);
- FieldDiffs fieldDiffs = issue.changes().get(0);
- assertThat(fieldDiffs.creationDate()).isEqualTo(context.date());
- assertThat(fieldDiffs.diffs()).hasSize(1);
- Map.Entry<String, FieldDiffs.Diff> entry = fieldDiffs.diffs().entrySet().iterator().next();
- assertThat(entry.getKey()).isEqualTo("file");
- assertThat(entry.getValue().oldValue()).isEqualTo(oldComponentUuid);
- assertThat(entry.getValue().newValue()).isEqualTo(newComponentUuid);
assertThat(issue.componentUuid()).isEqualTo(newComponentUuid);
+ assertThat(issue.componentKey()).isEqualTo(componentKey);
assertThat(issue.isChanged()).isTrue();
assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(context.date(), Calendar.SECOND));
}
}
public FormattingContext newFormattingContext(DbSession dbSession, Set<IssueDto> dtos, Load load) {
- return newFormattingContext(dbSession, dtos, load, ImmutableSet.of(), ImmutableSet.of());
+ return newFormattingContext(dbSession, dtos, load, Set.of(), Set.of());
}
public FormattingContext newFormattingContext(DbSession dbSession, Set<IssueDto> dtos, Load load, Set<UserDto> preloadedUsers, Set<ComponentDto> preloadedComponents) {
import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ISSUE;
public class ChangelogAction implements IssuesWsAction {
-
private final DbClient dbClient;
private final IssueFinder issueFinder;
private final IssueChangeWSSupport issueChangeSupport;
import static org.sonar.api.web.UserRole.CODEVIEWER;
import static org.sonar.api.web.UserRole.USER;
import static org.sonar.db.component.ComponentTesting.newFileDto;
-import static org.sonar.db.issue.IssueTesting.newDto;
-import static org.sonar.db.rule.RuleTesting.newRuleDto;
import static org.sonar.db.user.UserTesting.newUserDto;
import static org.sonar.test.JsonAssert.assertJson;
private ComponentDto project;
private ComponentDto file;
- private IssueFinder issueFinder = new IssueFinder(db.getDbClient(), userSession);
- private IssueChangeWSSupport issueChangeSupport = new IssueChangeWSSupport(db.getDbClient(), new AvatarResolverImpl(), userSession);
- private ChangelogAction underTest = new ChangelogAction(db.getDbClient(), issueFinder, issueChangeSupport);
- private WsActionTester tester = new WsActionTester(underTest);
+ private final IssueFinder issueFinder = new IssueFinder(db.getDbClient(), userSession);
+ private final IssueChangeWSSupport issueChangeSupport = new IssueChangeWSSupport(db.getDbClient(), new AvatarResolverImpl(), userSession);
+ private final ChangelogAction underTest = new ChangelogAction(db.getDbClient(), issueFinder, issueChangeSupport);
+ private final WsActionTester tester = new WsActionTester(underTest);
@Before
public void setUp() {
assertThat(result.getChangelogList().get(0).getDiffsList()).extracting(Diff::getKey, Diff::getOldValue, Diff::getNewValue).containsOnly(tuple("severity", "MAJOR", "BLOCKER"));
}
- @Test
- public void changelog_of_file_move_contains_file_names() {
- RuleDto rule = db.rules().insert(newRuleDto());
- ComponentDto project = db.components().insertPrivateProject();
- ComponentDto file1 = db.components().insertComponent(newFileDto(project));
- ComponentDto file2 = db.components().insertComponent(newFileDto(project));
- IssueDto issueDto = db.issues().insertIssue(newDto(rule, file2, project));
- userSession.logIn("john")
- .addProjectPermission(USER, project, file);
- db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setDiff("file", file1.uuid(), file2.uuid()).setCreationDate(new Date()));
-
- ChangelogWsResponse result = call(issueDto.getKey());
-
- assertThat(result.getChangelogList()).hasSize(1);
- assertThat(result.getChangelogList().get(0).hasUser()).isFalse();
- assertThat(result.getChangelogList().get(0).getCreationDate()).isNotEmpty();
- assertThat(result.getChangelogList().get(0).getDiffsList()).extracting(Diff::getKey, Diff::getOldValue, Diff::getNewValue)
- .containsOnly(tuple("file", file1.longName(), file2.longName()));
- }
-
- @Test
- public void changelog_of_file_move_is_empty_when_files_does_not_exists() {
- IssueDto issueDto = insertNewIssue();
- userSession.logIn("john")
- .addProjectPermission(USER, project, file);
- db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setDiff("file", "UNKNOWN_1", "UNKNOWN_2").setCreationDate(new Date()));
-
- ChangelogWsResponse result = call(issueDto.getKey());
-
- assertThat(result.getChangelogList()).hasSize(1);
- assertThat(result.getChangelogList().get(0).getDiffsList()).extracting(Diff::getKey, Diff::hasOldValue, Diff::hasNewValue)
- .containsOnly(tuple("file", false, false));
- }
-
@Test
public void return_changelog_on_user_without_email() {
UserDto user = db.users().insertUser(UserTesting.newUserDto("john", "John", null));