import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.rule.RuleKey;
+import org.sonar.api.web.UserRole;
import org.sonar.batch.protocol.input.FileData;
import org.sonar.batch.protocol.input.ProjectRepositories;
import org.sonar.core.UtcDateUtils;
import javax.annotation.Nullable;
import java.util.Collections;
+import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
ComponentDto module = dbClient.componentDao().getNullableByKey(session, query.getModuleKey());
// Current project/module can be null when analysing a new project
if (module != null) {
+ UserSession.get().checkComponentPermission(UserRole.USER, query.getModuleKey(),
+ "You're not authorized to access to project '" + module.name() + "', please contact your SonarQube administrator.");
+
ComponentDto project = getProject(module, session);
if (!project.key().equals(module.key())) {
addSettings(ref, module.getKey(), getSettingsFromParents(module, hasScanPerm, session));
addSettingsToChildrenModules(ref, query.getModuleKey(), Maps.<String, String>newHashMap(), treeModuleSettings, hasScanPerm, session);
addFileData(session, ref, modulesTree, module.uuid());
+
+ // FIXME need real value but actually only used to know if there is a previous analysis in local issue tracking mode so any value is
+ // ok
+ ref.setLastAnalysisDate(new Date());
+ } else {
+ ref.setLastAnalysisDate(null);
}
addProfiles(ref, projectKey, query.getProfileName(), session);
throw new ForbiddenException("You're not authorized to execute any SonarQube analysis. Please contact your SonarQube administrator.");
}
if (!preview && !hasScanPerm) {
- throw new ForbiddenException("You're only authorized to execute a local (dry run) SonarQube analysis without pushing the results to the SonarQube server. " +
+ throw new ForbiddenException("You're only authorized to execute a local (preview) SonarQube analysis without pushing the results to the SonarQube server. " +
"Please contact your SonarQube administrator.");
}
+ if (preview && !hasPreviewPerm) {
+ throw new ForbiddenException("You're not authorized to execute a preview analysis. Please contact your SonarQube administrator.");
+ }
}
private Map<String, String> moduleUuidsByKey(ComponentDto module, List<ComponentDto> moduleChildren) {
import org.sonar.api.rule.Severity;
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.api.utils.DateUtils;
+import org.sonar.api.web.UserRole;
import org.sonar.batch.protocol.input.ActiveRule;
import org.sonar.batch.protocol.input.FileData;
import org.sonar.batch.protocol.input.ProjectRepositories;
@Test
public void return_project_settings() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-
ComponentDto project = ComponentTesting.newProjectDto();
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), project.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, project);
addDefaultProfile();
@Test
public void not_returned_secured_settings_with_only_preview_permission() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.PREVIEW_EXECUTION);
-
ComponentDto project = ComponentTesting.newProjectDto();
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.PREVIEW_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), project.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, project);
addDefaultProfile();
@Test
public void return_project_with_module_settings() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-
ComponentDto project = ComponentTesting.newProjectDto();
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), project.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, project);
addDefaultProfile();
@Test
public void return_project_with_module_settings_inherited_from_project() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-
ComponentDto project = ComponentTesting.newProjectDto();
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), project.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, project);
addDefaultProfile();
@Test
public void return_project_with_module_with_sub_module() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-
ComponentDto project = ComponentTesting.newProjectDto();
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), project.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, project);
addDefaultProfile();
@Test
public void return_project_with_two_modules() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-
ComponentDto project = ComponentTesting.newProjectDto();
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), project.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, project);
addDefaultProfile();
@Test
public void return_provisioned_project_settings() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-
// No snapshot attached on the project -> provisioned project
ComponentDto project = ComponentTesting.newProjectDto();
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), project.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, project);
addDefaultProfile();
@Test
public void return_sub_module_settings() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
ComponentDto project = ComponentTesting.newProjectDto();
tester.get(DbClient.class).componentDao().insert(dbSession, project);
// No module properties
ComponentDto subModule = ComponentTesting.newModuleDto(module);
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), subModule.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, subModule);
// Sub module properties
@Test
public void return_sub_module_settings_including_settings_from_parent_modules() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-
ComponentDto project = ComponentTesting.newProjectDto();
tester.get(DbClient.class).componentDao().insert(dbSession, project);
addDefaultProfile();
tester.get(DbClient.class).propertiesDao().setProperty(new PropertyDto().setKey("sonar.jira.login.secured").setValue("john").setResourceId(module.getId()), dbSession);
ComponentDto subModule = ComponentTesting.newModuleDto(module);
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), subModule.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, subModule);
// Sub module properties
@Test
public void return_sub_module_settings_only_inherited_from_project() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-
ComponentDto project = ComponentTesting.newProjectDto();
tester.get(DbClient.class).componentDao().insert(dbSession, project);
addDefaultProfile();
// No module property
ComponentDto subModule = ComponentTesting.newModuleDto(module);
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), subModule.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, subModule);
// No sub module property
@Test
public void return_sub_module_settings_inherited_from_project_and_module() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-
ComponentDto project = ComponentTesting.newProjectDto();
tester.get(DbClient.class).componentDao().insert(dbSession, project);
addDefaultProfile();
tester.get(DbClient.class).propertiesDao().setProperty(new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR-SERVER").setResourceId(module.getId()), dbSession);
ComponentDto subModule = ComponentTesting.newModuleDto(module);
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), subModule.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, subModule);
// No sub module property
@Test
public void return_quality_profile_from_project_profile() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
Date ruleUpdatedAt = DateUtils.parseDateTime("2014-01-14T13:00:00+0100");
ComponentDto project = ComponentTesting.newProjectDto();
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), project.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, project);
QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(
@Test
public void return_quality_profile_from_default_profile() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
Date ruleUpdatedAt = DateUtils.parseDateTime("2014-01-14T13:00:00+0100");
ComponentDto project = ComponentTesting.newProjectDto();
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), project.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, project);
QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(
@Test
public void return_quality_profile_from_given_profile_name() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
Date ruleUpdatedAt = DateUtils.parseDateTime("2014-01-14T13:00:00+0100");
ComponentDto project = ComponentTesting.newProjectDto();
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), project.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, project);
QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(
@Test
public void return_provisioned_project_profile() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
Date ruleUpdatedAt = DateUtils.parseDateTime("2014-01-14T13:00:00+0100");
// No snapshot attached on the project -> provisioned project
ComponentDto project = ComponentTesting.newProjectDto();
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), project.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, project);
QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(
@Test
public void fail_when_no_quality_profile_for_a_language() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-
ComponentDto project = ComponentTesting.newProjectDto().setKey("org.codehaus.sonar:sonar");
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), project.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, project);
dbSession.commit();
@Test
public void return_active_rules() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
Date ruleUpdatedAt = DateUtils.parseDateTime("2014-01-14T13:00:00+0100");
ComponentDto project = ComponentTesting.newProjectDto();
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), project.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, project);
QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(
@Test
public void return_manual_rules() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-
ComponentDto project = ComponentTesting.newProjectDto();
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), project.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, project);
addDefaultProfile();
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(ForbiddenException.class).hasMessage(
- "You're only authorized to execute a local (dry run) SonarQube analysis without pushing the results to the SonarQube server. " +
+ "You're only authorized to execute a local (preview) SonarQube analysis without pushing the results to the SonarQube server. " +
"Please contact your SonarQube administrator.");
}
}
@Test
public void return_file_data_from_single_project() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-
ComponentDto project = ComponentTesting.newProjectDto();
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), project.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, project);
addDefaultProfile();
@Test
public void return_file_data_from_multi_modules() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-
ComponentDto project = ComponentTesting.newProjectDto();
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), project.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, project);
addDefaultProfile();
@Test
public void return_file_data_from_module() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-
ComponentDto project = ComponentTesting.newProjectDto();
tester.get(DbClient.class).componentDao().insert(dbSession, project);
addDefaultProfile();
tester.get(FileSourceDao.class).insert(newFileSourceDto(projectFile).setSrcHash("123456"));
ComponentDto module = ComponentTesting.newModuleDto(project);
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentPermission(UserRole.USER, project.getKey(), module.getKey());
tester.get(DbClient.class).componentDao().insert(dbSession, module);
// File on module
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.sonar.api.BatchComponent;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.batch.index.BatchResource;
import org.sonar.batch.index.ResourceCache;
import org.sonar.batch.issue.IssueCache;
+import org.sonar.batch.protocol.input.ProjectRepositories;
import org.sonar.batch.scan.LastLineHashes;
import org.sonar.batch.scan.filesystem.InputPathCache;
import org.sonar.core.issue.IssueUpdater;
public class LocalIssueTracking implements BatchComponent {
+ private static final Logger LOG = LoggerFactory.getLogger(LocalIssueTracking.class);
+
private final IssueCache issueCache;
private final IssueTracking tracking;
private final LastLineHashes lastLineHashes;
private final InputPathCache inputPathCache;
private final ResourceCache resourceCache;
private final PreviousIssueRepository previousIssueCache;
+ private final ProjectRepositories projectRepositories;
public LocalIssueTracking(ResourceCache resourceCache, IssueCache issueCache, IssueTracking tracking,
LastLineHashes lastLineHashes, IssueWorkflow workflow, IssueUpdater updater,
- ActiveRules activeRules, InputPathCache inputPathCache, PreviousIssueRepository previousIssueCache) {
+ ActiveRules activeRules, InputPathCache inputPathCache, PreviousIssueRepository previousIssueCache, ProjectRepositories projectRepositories) {
this.resourceCache = resourceCache;
this.issueCache = issueCache;
this.tracking = tracking;
this.updater = updater;
this.inputPathCache = inputPathCache;
this.previousIssueCache = previousIssueCache;
+ this.projectRepositories = projectRepositories;
this.changeContext = IssueChangeContext.createScan(((Project) resourceCache.getRoot().resource()).getAnalysisDate());
this.activeRules = activeRules;
}
public void execute() {
+ if (projectRepositories.lastAnalysisDate() == null) {
+ LOG.debug("No previous analysis, skipping issue tracking");
+ return;
+ }
+
previousIssueCache.load();
for (BatchResource component : resourceCache.all()) {