Browse Source

SONAR-10851 Make branch validation less strict on scanner side

No need to be more strict than server side.
tags/7.5
Julien HENRY 6 years ago
parent
commit
dce951b720
16 changed files with 35 additions and 41 deletions
  1. 1
    1
      server/sonar-server/src/main/java/org/sonar/ce/settings/ProjectConfigurationFactory.java
  2. 1
    1
      server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolder.java
  3. 7
    7
      server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImpl.java
  4. 1
    1
      server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/Branch.java
  5. 2
    2
      server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolder.java
  6. 1
    1
      server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java
  7. 1
    1
      server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterImpl.java
  8. 1
    1
      server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/DefaultBranchImpl.java
  9. 1
    1
      server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/SendIssueNotificationsStep.java
  10. 7
    7
      server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java
  11. 3
    3
      server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java
  12. 4
    4
      server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolderRule.java
  13. 1
    1
      server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java
  14. 1
    1
      server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterImplTest.java
  15. 1
    1
      server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistComponentsStepTest.java
  16. 2
    8
      server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/SendIssueNotificationsStepTest.java

+ 1
- 1
server/sonar-server/src/main/java/org/sonar/ce/settings/ProjectConfigurationFactory.java View File

@@ -46,7 +46,7 @@ public class ProjectConfigurationFactory {
Settings projectSettings = new ChildSettings(globalSettings);
addSettings(projectSettings, projectKey);
if (branch.getType() == BranchType.PULL_REQUEST) {
addSettings(projectSettings, generatePullRequestKey(projectKey, branch.getPullRequestId()));
addSettings(projectSettings, generatePullRequestKey(projectKey, branch.getPullRequestKey()));
} else {
addSettings(projectSettings, generateBranchKey(projectKey, branch.getName()));
}

+ 1
- 1
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolder.java View File

@@ -105,7 +105,7 @@ public interface AnalysisMetadataHolder {
*
* @throws IllegalStateException if current analysis is not a pull request
*/
String getPullRequestId();
String getPullRequestKey();

/**
* The project as represented by the main branch. It is used to load settings

+ 7
- 7
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImpl.java View File

@@ -40,7 +40,7 @@ public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder
private final InitializedProperty<Analysis> baseProjectSnapshot = new InitializedProperty<>();
private final InitializedProperty<Boolean> crossProjectDuplicationEnabled = new InitializedProperty<>();
private final InitializedProperty<Branch> branch = new InitializedProperty<>();
private final InitializedProperty<String> pullRequestId = new InitializedProperty<>();
private final InitializedProperty<String> pullRequestKey = new InitializedProperty<>();
private final InitializedProperty<Project> project = new InitializedProperty<>();
private final InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
private final InitializedProperty<Map<String, QualityProfile>> qProfilesPerLanguage = new InitializedProperty<>();
@@ -151,16 +151,16 @@ public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder
}

@Override
public MutableAnalysisMetadataHolder setPullRequestId(String pullRequestId) {
checkState(!this.pullRequestId.isInitialized(), "Pull request id has already been set");
this.pullRequestId.setProperty(pullRequestId);
public MutableAnalysisMetadataHolder setPullRequestKey(String pullRequestKey) {
checkState(!this.pullRequestKey.isInitialized(), "Pull request key has already been set");
this.pullRequestKey.setProperty(pullRequestKey);
return this;
}

@Override
public String getPullRequestId() {
checkState(pullRequestId.isInitialized(), "Pull request id has not been set");
return pullRequestId.getProperty();
public String getPullRequestKey() {
checkState(pullRequestKey.isInitialized(), "Pull request key has not been set");
return pullRequestKey.getProperty();
}

@Override

+ 1
- 1
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/Branch.java View File

@@ -57,5 +57,5 @@ public interface Branch extends ComponentKeyGenerator {
/**
* @throws IllegalStateException if this branch configuration is not a pull request.
*/
String getPullRequestId();
String getPullRequestKey();
}

+ 2
- 2
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolder.java View File

@@ -62,9 +62,9 @@ public interface MutableAnalysisMetadataHolder extends AnalysisMetadataHolder {
MutableAnalysisMetadataHolder setBranch(Branch branch);

/**
* @throws IllegalStateException if pull request id has already been set
* @throws IllegalStateException if pull request key has already been set
*/
MutableAnalysisMetadataHolder setPullRequestId(String pullRequestId);
MutableAnalysisMetadataHolder setPullRequestKey(String pullRequestKey);

/**
* @throws IllegalStateException if project has already been set

+ 1
- 1
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java View File

@@ -181,7 +181,7 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor
private BranchImpl createBranch() {
org.sonar.server.computation.task.projectanalysis.analysis.Branch analysisBranch = analysisMetadataHolder.getBranch();
if (!analysisBranch.isLegacyFeature()) {
String branchKey = analysisBranch.getType() == PULL_REQUEST ? analysisBranch.getPullRequestId() : analysisBranch.getName();
String branchKey = analysisBranch.getType() == PULL_REQUEST ? analysisBranch.getPullRequestKey() : analysisBranch.getName();
return new BranchImpl(analysisBranch.isMain(), branchKey, Branch.Type.valueOf(analysisBranch.getType().name()));
}
return null;

+ 1
- 1
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterImpl.java View File

@@ -88,7 +88,7 @@ public class BranchPersisterImpl implements BranchPersister {
dto.setMergeBranchUuid(branch.getMergeBranchUuid().orElse(null));

if (branch.getType() == BranchType.PULL_REQUEST) {
dto.setKey(analysisMetadataHolder.getPullRequestId());
dto.setKey(analysisMetadataHolder.getPullRequestKey());

DbProjectBranches.PullRequestData pullRequestData = DbProjectBranches.PullRequestData.newBuilder()
.setBranch(branch.getName())

+ 1
- 1
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/DefaultBranchImpl.java View File

@@ -86,7 +86,7 @@ public class DefaultBranchImpl implements Branch {
}

@Override
public String getPullRequestId() {
public String getPullRequestKey() {
throw new IllegalStateException("Only a branch of type PULL_REQUEST can have a pull request id.");
}


+ 1
- 1
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/SendIssueNotificationsStep.java View File

@@ -224,7 +224,7 @@ public class SendIssueNotificationsStep implements ComputationStep {
@CheckForNull
private String getPullRequest() {
Branch branch = analysisMetadataHolder.getBranch();
return branch.getType() == PULL_REQUEST ? analysisMetadataHolder.getPullRequestId() : null;
return branch.getType() == PULL_REQUEST ? analysisMetadataHolder.getPullRequestKey() : null;
}

}

+ 7
- 7
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java View File

@@ -247,27 +247,27 @@ public class AnalysisMetadataHolderImplTest {
AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();

String pullRequestId = "pr-123";
underTest.setPullRequestId(pullRequestId);
underTest.setPullRequestKey(pullRequestId);

assertThat(underTest.getPullRequestId()).isEqualTo(pullRequestId);
assertThat(underTest.getPullRequestKey()).isEqualTo(pullRequestId);
}

@Test
public void getPullRequestId_throws_ISE_when_holder_is_not_initialized() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Pull request id has not been set");
expectedException.expectMessage("Pull request key has not been set");

new AnalysisMetadataHolderImpl().getPullRequestId();
new AnalysisMetadataHolderImpl().getPullRequestKey();
}

@Test
public void setPullRequestId_throws_ISE_when_called_twice() {
AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
underTest.setPullRequestId("pr-123");
underTest.setPullRequestKey("pr-123");

expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Pull request id has already been set");
underTest.setPullRequestId("pr-234");
expectedException.expectMessage("Pull request key has already been set");
underTest.setPullRequestKey("pr-234");
}

@Test

+ 3
- 3
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java View File

@@ -171,13 +171,13 @@ public class AnalysisMetadataHolderRule extends ExternalResource implements Muta
}

@Override
public MutableAnalysisMetadataHolder setPullRequestId(String pullRequestId) {
this.pullRequestId.setProperty(pullRequestId);
public MutableAnalysisMetadataHolder setPullRequestKey(String pullRequestKey) {
this.pullRequestId.setProperty(pullRequestKey);
return this;
}

@Override
public String getPullRequestId() {
public String getPullRequestKey() {
checkState(pullRequestId.isInitialized(), "Pull request id has not been set");
return pullRequestId.getProperty();
}

+ 4
- 4
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolderRule.java View File

@@ -122,13 +122,13 @@ public class MutableAnalysisMetadataHolderRule extends ExternalResource implemen
}

@Override
public String getPullRequestId() {
return delegate.getPullRequestId();
public String getPullRequestKey() {
return delegate.getPullRequestKey();
}

@Override
public MutableAnalysisMetadataHolder setPullRequestId(String pullRequestId) {
delegate.setPullRequestId(pullRequestId);
public MutableAnalysisMetadataHolder setPullRequestKey(String pullRequestKey) {
delegate.setPullRequestKey(pullRequestKey);
return this;
}


+ 1
- 1
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java View File

@@ -302,7 +302,7 @@ public class PostProjectAnalysisTasksExecutorTest {
}

@Override
public String getPullRequestId() {
public String getPullRequestKey() {
throw new UnsupportedOperationException();
}


+ 1
- 1
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterImplTest.java View File

@@ -87,7 +87,7 @@ public class BranchPersisterImplTest {
public void persist_pull_request_data() {
String pullRequestId = "pr-123";
analysisMetadataHolder.setBranch(createBranch(BranchType.PULL_REQUEST, false, pullRequestId));
analysisMetadataHolder.setPullRequestId(pullRequestId);
analysisMetadataHolder.setPullRequestKey(pullRequestId);
treeRootHolder.setRoot(BRANCH);

// add main branch in project table and in metadata

+ 1
- 1
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistComponentsStepTest.java View File

@@ -944,7 +944,7 @@ public class ReportPersistComponentsStepTest extends BaseStepTest {
}

@Override
public String getPullRequestId() {
public String getPullRequestKey() {
throw new UnsupportedOperationException();
}


+ 2
- 8
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/SendIssueNotificationsStepTest.java View File

@@ -20,23 +20,18 @@
package org.sonar.server.computation.task.projectanalysis.step;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.apache.commons.lang.RandomStringUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.ArgumentCaptor;
import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;
import org.sonar.api.notifications.Notification;
import org.sonar.api.rules.RuleType;
import org.sonar.api.utils.Duration;
@@ -68,7 +63,6 @@ import static java.util.Arrays.stream;
import static java.util.Collections.shuffle;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Stream.concat;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentCaptor.forClass;
import static org.mockito.ArgumentMatchers.eq;
@@ -233,7 +227,7 @@ public class SendIssueNotificationsStepTest extends BaseStepTest {
new DefaultIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setCreationDate(new Date(ANALYSE_DATE))).close();
when(notificationService.hasProjectSubscribersForTypes(branch.uuid(), NOTIF_TYPES)).thenReturn(true);
analysisMetadataHolder.setBranch(newPullRequest());
analysisMetadataHolder.setPullRequestId(PULL_REQUEST_ID);
analysisMetadataHolder.setPullRequestKey(PULL_REQUEST_ID);

underTest.execute();

@@ -500,7 +494,7 @@ public class SendIssueNotificationsStepTest extends BaseStepTest {
when(branch.isMain()).thenReturn(false);
when(branch.getType()).thenReturn(PULL_REQUEST);
when(branch.getName()).thenReturn(BRANCH_NAME);
when(branch.getPullRequestId()).thenReturn(PULL_REQUEST_ID);
when(branch.getPullRequestKey()).thenReturn(PULL_REQUEST_ID);
return branch;
}


Loading…
Cancel
Save