import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.ResourceType;
import org.sonar.api.resources.ResourceTypes;
+import org.sonar.api.resources.Scopes;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
private final Configuration config;
public ComponentAction(DbClient dbClient, PageRepository pageRepository, ResourceTypes resourceTypes, UserSession userSession,
- ComponentFinder componentFinder, QualityGateFinder qualityGateFinder, Configuration config) {
+ ComponentFinder componentFinder, QualityGateFinder qualityGateFinder, Configuration config) {
this.dbClient = dbClient;
this.pageRepository = pageRepository;
this.resourceTypes = resourceTypes;
try (JsonWriter json = response.newJsonWriter()) {
json.beginObject();
- boolean isFavourite = isFavourite(session, rootProject);
+ boolean isFavourite = isFavourite(session, rootProject, component);
writeComponent(json, component, analysis.orElse(null), isFavourite);
writeProfiles(json, session, component);
writeQualityGate(json, session, rootProject);
}
}
- private boolean isFavourite(DbSession session, ComponentDto component) {
+ private boolean isFavourite(DbSession session, ComponentDto rootComponent, ComponentDto component) {
PropertyQuery propertyQuery = PropertyQuery.builder()
.setUserUuid(userSession.getUuid())
.setKey("favourite")
- .setComponentUuid(component.uuid())
+ .setComponentUuid(isSubview(component) ? component.uuid() : rootComponent.uuid())
.build();
List<PropertyDto> componentFavourites = dbClient.propertiesDao().selectByQuery(propertyQuery, session);
return componentFavourites.size() == 1;
}
+ private static boolean isSubview(ComponentDto component) {
+ return Qualifiers.SUBVIEW.equals(component.qualifier()) && Scopes.PROJECT.equals(component.scope());
+ }
+
private void writeProfiles(JsonWriter json, DbSession dbSession, ComponentDto component) {
Set<QualityProfile> qualityProfiles = dbClient.liveMeasureDao().selectMeasure(dbSession, component.projectUuid(), QUALITY_PROFILES_KEY)
.map(LiveMeasureDto::getDataAsString)
import static org.sonar.db.component.ComponentTesting.newFileDto;
import static org.sonar.db.component.ComponentTesting.newModuleDto;
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
+import static org.sonar.db.component.ComponentTesting.newSubPortfolio;
import static org.sonar.db.component.SnapshotTesting.newAnalysis;
import static org.sonar.db.measure.MeasureTesting.newLiveMeasure;
import static org.sonar.db.metric.MetricTesting.newMetricDto;
"}\n");
}
+ @Test
+ public void return_favourite_for_subportfolio() {
+ db.qualityGates().createDefaultQualityGate();
+ ComponentDto portfolio = componentDbTester.insertPrivatePortfolio();
+ ComponentDto subportfolio = componentDbTester.insertComponent(newSubPortfolio(portfolio));
+ UserDto user = db.users().insertUser("obiwan");
+
+ // set favourite for sub portfolio
+ propertyDbTester.insertProperty(new PropertyDto().setKey("favourite").setComponentUuid(subportfolio.uuid()).setUserUuid(user.getUuid()),
+ subportfolio.getKey(), subportfolio.name(), subportfolio.qualifier(), user.getLogin());
+
+ userSession.logIn(user).addProjectPermission(UserRole.USER, subportfolio);
+ init();
+
+ String json = ws.newRequest()
+ .setParam("component", subportfolio.getKey())
+ .execute()
+ .getInput();
+
+ assertJson(json).isSimilarTo("{" +
+ " \"key\": \"" + subportfolio.getDbKey() + "\"," +
+ " \"isFavorite\": true," +
+ " \"id\": \"" + subportfolio.uuid() + "\"," +
+ " \"name\": \"" + subportfolio.name() + "\"" +
+ "}");
+ }
+
+ @Test
+ public void return_favourite_for_portfolio() {
+ db.qualityGates().createDefaultQualityGate();
+ ComponentDto portfolio = componentDbTester.insertPrivatePortfolio();
+ ComponentDto subportfolio = componentDbTester.insertComponent(newSubPortfolio(portfolio));
+ UserDto user = db.users().insertUser("obiwan");
+
+ // set favourite for sub portfolio
+ propertyDbTester.insertProperty(new PropertyDto().setKey("favourite").setComponentUuid(portfolio.uuid()).setUserUuid(user.getUuid()),
+ subportfolio.getKey(), portfolio.name(), portfolio.qualifier(), user.getLogin());
+
+ userSession.logIn(user).addProjectPermission(UserRole.USER, portfolio);
+ init();
+
+ String json = ws.newRequest()
+ .setParam("component", portfolio.getKey())
+ .execute()
+ .getInput();
+
+ assertJson(json).isSimilarTo("{" +
+ " \"key\": \"" + portfolio.getDbKey() + "\"," +
+ " \"isFavorite\": true," +
+ " \"id\": \"" + portfolio.uuid() + "\"," +
+ " \"name\": \"" + portfolio.name() + "\"" +
+ "}");
+ }
+
@Test
public void return_component_info_when_snapshot() {
ComponentDto project = insertProject();
private ComponentDto insertProject() {
db.qualityGates().createDefaultQualityGate();
- return db.components().insertPrivateProject("abcd", p ->
- p.setDbKey("polop")
- .setName("Polop")
- .setDescription("test project")
- .setQualifier(Qualifiers.PROJECT)
- .setScope(Scopes.PROJECT));
+ return db.components().insertPrivateProject("abcd", p -> p.setDbKey("polop")
+ .setName("Polop")
+ .setDescription("test project")
+ .setQualifier(Qualifiers.PROJECT)
+ .setScope(Scopes.PROJECT));
}
private void init(Page... pages) {
when(pluginRepository.getPluginInfo(any())).thenReturn(new PluginInfo("unused").setVersion(Version.create("1.0")));
CoreExtensionRepository coreExtensionRepository = mock(CoreExtensionRepository.class);
when(coreExtensionRepository.isInstalled(any())).thenReturn(false);
- PageRepository pageRepository = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[]{context -> {
+ PageRepository pageRepository = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[] {context -> {
for (Page page : pages) {
context.addPage(page);
}
.setAdmin(true)
.build();
- return new Page[]{page1, page2, adminPage};
+ return new Page[] {page1, page2, adminPage};
}
private void verifySuccess(String componentKey) {
favorite.action.remove=Remove project from favorites
favorite.check.TRK=Click to mark this project as favorite.
favorite.check.VW=Click to mark this portfolio as favorite.
-favorite.check.SVW=Click to mark this sub-ortfolio as favorite.
+favorite.check.SVW=Click to mark this sub-portfolio as favorite.
favorite.check.APP=Click to mark this application as favorite.
favorite.check.FIL=Click to mark this file as favorite.
favorite.check.UTS=Click to mark this test file as favorite.
favorite.current.TRK=This project is marked as favorite.
favorite.current.VW=This portfolio is marked as favorite.
-favorite.current.SVW=This sub-ortfolio is marked as favorite.
+favorite.current.SVW=This sub-portfolio is marked as favorite.
favorite.current.APP=This application is marked as favorite.
favorite.current.FIL=This file is marked as favorite.
favorite.current.UTS=This test file is marked as favorite.