return getMapper(dbSession).selectByTask(taskUuid);
}
+ /**
+ * @return the non dismissed messages for the specific task and specific user, if any, in ascending order of column {@code CREATED_AT}.
+ */
+ public List<CeTaskMessageDto> selectNonDismissedByUserAndTask(DbSession dbSession, String taskUuid, String userUuid) {
+ return getMapper(dbSession).selectNonDismissedByUserAndTask(taskUuid, userUuid);
+ }
+
public void deleteByType(DbSession session, CeTaskMessageType type) {
getMapper(session).deleteByType(type.name());
}
List<CeTaskMessageDto> selectByTask(@Param("taskUuid") String taskUuid);
+ List<CeTaskMessageDto> selectNonDismissedByUserAndTask(@Param("taskUuid") String taskUuid, @Param("userUuid") String userUuid);
+
void deleteByType(@Param("ceMessageType") String ceMessageType);
}
ctm.created_at asc
</select>
+ <select id="selectNonDismissedByUserAndTask" resultType="org.sonar.db.ce.CeTaskMessageDto">
+ select
+ <include refid="columns"/>
+ from
+ ce_task_message ctm
+ where
+ ctm.task_uuid=#{taskUuid,jdbcType=VARCHAR}
+ and not exists (
+ select 1
+ from user_dismissed_messages udm
+ where ctm.message_type = udm.message_type and udm.user_uuid=#{userUuid,jdbcType=VARCHAR}
+ )
+ order by
+ ctm.created_at asc
+ </select>
+
<insert id="insert" parameterType="org.sonar.db.ce.CeTaskMessageDto" useGeneratedKeys="false">
insert into ce_task_message
(
import org.sonar.api.utils.System2;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.project.ProjectDto;
+import org.sonar.db.user.UserDto;
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
import static org.assertj.core.api.Assertions.assertThat;
.containsExactlyInAnyOrder(messages[0].getUuid(), messages[2].getUuid());
}
+ @Test
+ public void selectNonDismissedByUserAndTask_returns_empty_on_empty_table() {
+ UserDto user = dbTester.users().insertUser();
+ String taskUuid = "17ae66e6-fe83-4c80-b704-4b04e9c5abe8";
+
+ List<CeTaskMessageDto> dto = underTest.selectNonDismissedByUserAndTask(dbTester.getSession(), taskUuid, user.getUuid());
+
+ assertThat(dto).isEmpty();
+ }
+
+ @Test
+ public void selectNonDismissedByUserAndTask_returns_non_dismissed_messages() {
+ UserDto user = dbTester.users().insertUser();
+ ProjectDto project = dbTester.components().insertPrivateProjectDto();
+ dbTester.users().insertUserDismissedMessage(user, project, CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE);
+ String taskUuid = "17ae66e6-fe83-4c80-b704-4b04e9c5abe8";
+ CeTaskMessageDto msg1 = insertMessage(taskUuid, 1, 1_222_333L);
+ insertMessage(taskUuid, 2, 1_222_334L, CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE);
+ CeTaskMessageDto msg3 = insertMessage(taskUuid, 3, 1_222_335L);
+ List<CeTaskMessageDto> messages = underTest.selectNonDismissedByUserAndTask(dbTester.getSession(), taskUuid, user.getUuid());
+
+ assertThat(messages).hasSize(2);
+ assertThat(messages).extracting(CeTaskMessageDto::getUuid).containsExactlyInAnyOrder(msg1.getUuid(), msg3.getUuid());
+ }
+
private CeTaskMessageDto insertMessage(String taskUuid, int i, long createdAt) {
return insertMessage(taskUuid, i, createdAt, CeTaskMessageType.GENERIC);
}
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.ce.CeActivityDto;
+import org.sonar.db.ce.CeTaskMessageDto;
import org.sonar.db.ce.CeTaskTypes;
import org.sonar.db.component.BranchDto;
import org.sonar.db.project.ProjectDto;
private AnalysisStatusWsResponse.Component formatComponent(DbSession dbSession, ProjectDto project, @Nullable CeActivityDto lastActivity,
@Nullable String branchKey, @Nullable String pullRequestKey) {
-
AnalysisStatusWsResponse.Component.Builder builder = AnalysisStatusWsResponse.Component.newBuilder()
.setOrganization(getOrganizationKey(dbSession, project))
.setKey(project.getKey())
if (lastActivity == null) {
return builder.build();
}
- List<AnalysisStatusWsResponse.Warning> warnings = dbClient.ceTaskMessageDao().selectByTask(dbSession, lastActivity.getUuid()).stream()
- .map(dto -> AnalysisStatusWsResponse.Warning.newBuilder()
- .setKey(dto.getUuid())
- .setMessage(dto.getMessage())
- .setDismissable(dto.getType().isDismissible())
- .build())
- .collect(Collectors.toList());
- builder.addAllWarnings(warnings);
+ List<CeTaskMessageDto> warnings;
+ String userUuid = userSession.getUuid();
+ if (userUuid != null) {
+ warnings = dbClient.ceTaskMessageDao().selectNonDismissedByUserAndTask(dbSession, lastActivity.getUuid(), userUuid);
+ } else {
+ warnings = dbClient.ceTaskMessageDao().selectByTask(dbSession, lastActivity.getUuid());
+ }
+ List<AnalysisStatusWsResponse.Warning> result = warnings.stream().map(dto -> AnalysisStatusWsResponse.Warning.newBuilder()
+ .setKey(dto.getUuid())
+ .setMessage(dto.getMessage())
+ .setDismissable(dto.getType().isDismissible())
+ .build())
+ .collect(Collectors.toList());
+ builder.addAllWarnings(result);
return builder.build();
}
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Rule
- public UserSessionRule userSession = UserSessionRule.standalone().logIn().setSystemAdministrator();
+ public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
@Test
public void no_errors_no_warnings() {
ComponentDto project = db.components().insertPrivateProject();
- userSession.addProjectPermission(UserRole.USER, project);
+ userSession.logIn().setSystemAdministrator().addProjectPermission(UserRole.USER, project);
Ce.AnalysisStatusWsResponse response = ws.newRequest()
.setParam(PARAM_COMPONENT, project.getKey())
assertThat(response.getComponent().getWarningsList()).isEmpty();
}
+ @Test
+ public void allows_unauthenticated_access() {
+ ComponentDto project = db.components().insertPublicProject(db.getDefaultOrganization());
+ userSession.registerComponents(project);
+ SnapshotDto analysis = db.components().insertSnapshot(project);
+ CeActivityDto activity = insertActivity("task-uuid" + counter++, project, SUCCESS, analysis, REPORT);
+ createTaskMessage(activity, WARNING_IN_MAIN);
+ createTaskMessage(activity, "Dismissible warning", CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE);
+
+ Ce.AnalysisStatusWsResponse response = ws.newRequest()
+ .setParam(PARAM_COMPONENT, project.getKey())
+ .executeProtobuf(Ce.AnalysisStatusWsResponse.class);
+
+ assertThat(response.getComponent().getWarningsList()).hasSize(2);
+ }
+
@Test
public void return_warnings_for_last_analysis_of_main() {
ComponentDto project = db.components().insertPrivateProject();
- userSession.addProjectPermission(UserRole.USER, project);
+ userSession.logIn().setSystemAdministrator().addProjectPermission(UserRole.USER, project);
SnapshotDto analysis = db.components().insertSnapshot(project);
CeActivityDto activity = insertActivity("task-uuid" + counter++, project, SUCCESS, analysis, REPORT);
@Test
public void return_warnings_for_last_analysis_of_branch() {
ComponentDto project = db.components().insertPrivateProject();
- userSession.addProjectPermission(UserRole.USER, project);
+ userSession.logIn().setSystemAdministrator().addProjectPermission(UserRole.USER, project);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(BRANCH_WITH_WARNING));
SnapshotDto analysis = db.components().insertSnapshot(branch);
@Test
public void return_warnings_for_last_analysis_of_pull_request() {
ComponentDto project = db.components().insertPrivateProject();
- userSession.addProjectPermission(UserRole.USER, project);
+ userSession.logIn().setSystemAdministrator().addProjectPermission(UserRole.USER, project);
ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> {
b.setBranchType(BranchType.PULL_REQUEST);
@Test
public void return_warnings_per_branch() {
ComponentDto project = db.components().insertPrivateProject();
- userSession.addProjectPermission(UserRole.USER, project);
+ userSession.logIn().setSystemAdministrator().addProjectPermission(UserRole.USER, project);
SnapshotDto analysis = db.components().insertSnapshot(project);
CeActivityDto activity = insertActivity("task-uuid" + counter++, project, SUCCESS, analysis, REPORT);
@Test
public void response_contains_branch_or_pullRequest_for_branch_or_pullRequest_only() {
ComponentDto project = db.components().insertPrivateProject();
- userSession.addProjectPermission(UserRole.USER, project);
+ userSession.logIn().setSystemAdministrator().addProjectPermission(UserRole.USER, project);
db.components().insertProjectBranch(project, b -> b.setKey(BRANCH_WITHOUT_WARNING));
db.getDbClient().ceTaskMessageDao().insert(db.getSession(), ceTaskMessage);
db.commit();
- userSession.addProjectPermission(UserRole.USER, project);
+ userSession.logIn().setSystemAdministrator().addProjectPermission(UserRole.USER, project);
String result = ws.newRequest()
.setParam(PARAM_COMPONENT, project.getKey())