Browse Source

SONAR-20156 New field previousUncompliantValue in api/new_code_periods/show

tags/10.2.0.77647
Zipeng WU 9 months ago
parent
commit
30027b2d9a

+ 23
- 19
server/sonar-db-dao/src/it/java/org/sonar/db/newcodeperiod/NewCodePeriodDaoIT.java View File

@@ -51,7 +51,7 @@ public class NewCodePeriodDaoIT {

@Test
public void insert_new_code_period() {
insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5");
insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5", null);

Optional<NewCodePeriodDto> resultOpt = underTest.selectByUuid(dbSession, "1");

@@ -77,7 +77,7 @@ public class NewCodePeriodDaoIT {
"defghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijab" +
"cdefghijabcdefghijabcdefghijabcdefghijxxxxx";

insert("1", "proj-uuid", "branch-uuid", REFERENCE_BRANCH, branchWithLongName);
insert("1", "proj-uuid", "branch-uuid", REFERENCE_BRANCH, branchWithLongName, null);

assertThat(db.select("select uuid as \"UUID\", value as \"VALUE\" from new_code_periods"))
.extracting(r -> r.get("UUID"), r -> r.get("VALUE"))
@@ -91,7 +91,7 @@ public class NewCodePeriodDaoIT {

@Test
public void update_new_code_period() {
insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5");
insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5", null);

underTest.update(dbSession, new NewCodePeriodDto()
.setUuid("1")
@@ -120,7 +120,7 @@ public class NewCodePeriodDaoIT {

@Test
public void insert_with_upsert() {
insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5");
insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5", null);

Optional<NewCodePeriodDto> resultOpt = underTest.selectByUuid(dbSession, "1");

@@ -143,7 +143,7 @@ public class NewCodePeriodDaoIT {

@Test
public void update_with_upsert() {
insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5");
insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5", null);

underTest.upsert(dbSession, new NewCodePeriodDto()
.setUuid("1")
@@ -173,7 +173,7 @@ public class NewCodePeriodDaoIT {

@Test
public void select_by_project_and_branch_uuids() {
insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5");
insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5", null);

Optional<NewCodePeriodDto> resultOpt = underTest.selectByBranch(dbSession, "proj-uuid", "branch-uuid");
assertThat(resultOpt)
@@ -199,17 +199,17 @@ public class NewCodePeriodDaoIT {
BranchDto branch2 = db.components().insertProjectBranch(project);
BranchDto branch3 = db.components().insertProjectBranch(project);

insert("1", project.getUuid(), null, REFERENCE_BRANCH, mainBranch.getKey());
insert("2", project.getUuid(), branch1.getUuid(), REFERENCE_BRANCH, mainBranch.getKey());
insert("3", project.getUuid(), branch2.getUuid(), NUMBER_OF_DAYS, "5");
insert("4", project.getUuid(), project.getUuid(), PREVIOUS_VERSION, null);
insert("1", project.getUuid(), null, REFERENCE_BRANCH, mainBranch.getKey(), null);
insert("2", project.getUuid(), branch1.getUuid(), REFERENCE_BRANCH, mainBranch.getKey(), null);
insert("3", project.getUuid(), branch2.getUuid(), NUMBER_OF_DAYS, "5", null);
insert("4", project.getUuid(), project.getUuid(), PREVIOUS_VERSION, null, null);
db.commit();
assertThat(underTest.selectBranchesReferencing(dbSession, project.getUuid(), mainBranch.getKey())).containsOnly(branch1.getUuid(), branch3.getUuid());
}

@Test
public void select_by_project_uuid() {
insert("1", "proj-uuid", null, NUMBER_OF_DAYS, "5");
insert("1", "proj-uuid", null, NUMBER_OF_DAYS, "90", "130");

Optional<NewCodePeriodDto> resultOpt = underTest.selectByProject(dbSession, "proj-uuid");
assertThat(resultOpt)
@@ -221,14 +221,15 @@ public class NewCodePeriodDaoIT {
assertThat(result.getProjectUuid()).isEqualTo("proj-uuid");
assertThat(result.getBranchUuid()).isNull();
assertThat(result.getType()).isEqualTo(NUMBER_OF_DAYS);
assertThat(result.getValue()).isEqualTo("5");
assertThat(result.getValue()).isEqualTo("90");
assertThat(result.getPreviousNonCompliantValue()).isEqualTo("130");
assertThat(result.getCreatedAt()).isNotZero();
assertThat(result.getUpdatedAt()).isNotZero();
}

@Test
public void select_global() {
insert("1", null, null, NUMBER_OF_DAYS, "30");
insert("1", null, null, NUMBER_OF_DAYS, "30", null);

Optional<NewCodePeriodDto> newCodePeriodDto = underTest.selectGlobal(dbSession);
assertThat(newCodePeriodDto).isNotEmpty();
@@ -239,13 +240,14 @@ public class NewCodePeriodDaoIT {
assertThat(result.getBranchUuid()).isNull();
assertThat(result.getType()).isEqualTo(NUMBER_OF_DAYS);
assertThat(result.getValue()).isEqualTo("30");
assertThat(result.getPreviousNonCompliantValue()).isNull();
assertThat(result.getCreatedAt()).isNotZero();
assertThat(result.getUpdatedAt()).isNotZero();
}

@Test
public void exists_by_project_analysis_is_true() {
insert("1", "proj-uuid", "branch-uuid", SPECIFIC_ANALYSIS, "analysis-uuid");
insert("1", "proj-uuid", "branch-uuid", SPECIFIC_ANALYSIS, "analysis-uuid", null);

boolean exists = underTest.existsByProjectAnalysisUuid(dbSession, "analysis-uuid");
assertThat(exists).isTrue();
@@ -253,7 +255,7 @@ public class NewCodePeriodDaoIT {

@Test
public void delete_by_project_uuid_and_branch_uuid() {
insert("1", "proj-uuid", "branch-uuid", SPECIFIC_ANALYSIS, "analysis-uuid");
insert("1", "proj-uuid", "branch-uuid", SPECIFIC_ANALYSIS, "analysis-uuid", null);

underTest.delete(dbSession, "proj-uuid", "branch-uuid");
db.commit();
@@ -262,7 +264,7 @@ public class NewCodePeriodDaoIT {

@Test
public void delete_by_project_uuid() {
insert("1", "proj-uuid", null, SPECIFIC_ANALYSIS, "analysis-uuid");
insert("1", "proj-uuid", null, SPECIFIC_ANALYSIS, "analysis-uuid", null);

underTest.delete(dbSession, "proj-uuid", null);
db.commit();
@@ -271,7 +273,7 @@ public class NewCodePeriodDaoIT {

@Test
public void delete_global() {
insert("1", null, null, SPECIFIC_ANALYSIS, "analysis-uuid");
insert("1", null, null, SPECIFIC_ANALYSIS, "analysis-uuid", null);

underTest.delete(dbSession, null, null);
db.commit();
@@ -310,13 +312,15 @@ public class NewCodePeriodDaoIT {
.isEqualTo(expected);
}

private void insert(String uuid, @Nullable String projectUuid, @Nullable String branchUuid, NewCodePeriodType type, @Nullable String value) {
private void insert(String uuid, @Nullable String projectUuid, @Nullable String branchUuid, NewCodePeriodType type,
@Nullable String value, @Nullable String previousNonCompliantValue) {
underTest.insert(dbSession, new NewCodePeriodDto()
.setUuid(uuid)
.setProjectUuid(projectUuid)
.setBranchUuid(branchUuid)
.setType(type)
.setValue(value));
.setValue(value)
.setPreviousNonCompliantValue(previousNonCompliantValue));
db.commit();
}
}

+ 10
- 0
server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodDto.java View File

@@ -27,6 +27,7 @@ public class NewCodePeriodDto {
private String projectUuid = null;
private String branchUuid = null;
private NewCodePeriodType type = null;
private String previousNonCompliantValue = null;
private String value = null;
private long updatedAt = 0L;
private long createdAt = 0L;
@@ -100,4 +101,13 @@ public class NewCodePeriodDto {
this.value = value;
return this;
}

public String getPreviousNonCompliantValue() {
return previousNonCompliantValue;
}

public NewCodePeriodDto setPreviousNonCompliantValue(String previousNonCompliantValue) {
this.previousNonCompliantValue = previousNonCompliantValue;
return this;
}
}

+ 4
- 1
server/sonar-db-dao/src/main/resources/org/sonar/db/newcodeperiod/NewCodePeriodMapper.xml View File

@@ -9,6 +9,7 @@
ncp.branch_uuid as branchUuid,
ncp.type,
ncp.value,
ncp.previous_non_compliant_value as previousNonCompliantValue,
ncp.updated_at as updatedAt,
ncp.created_at as createdAt
</sql>
@@ -32,13 +33,14 @@

<insert id="insert" parameterType="org.sonar.db.newcodeperiod.NewCodePeriodDto">
INSERT INTO new_code_periods (
uuid, project_uuid, branch_uuid, type, value, updated_at, created_at)
uuid, project_uuid, branch_uuid, type, value, previous_non_compliant_value, updated_at, created_at)
VALUES (
#{uuid, jdbcType=VARCHAR},
#{projectUuid, jdbcType=VARCHAR},
#{branchUuid, jdbcType=VARCHAR},
#{type, jdbcType=VARCHAR},
#{value, jdbcType=VARCHAR},
#{previousNonCompliantValue, jdbcType=VARCHAR},
#{updatedAt, jdbcType=TIMESTAMP},
#{createdAt, jdbcType=TIMESTAMP}
)
@@ -49,6 +51,7 @@
SET
type=#{type, jdbcType=VARCHAR},
value=#{value, jdbcType=VARCHAR},
previous_non_compliant_value=#{previousNonCompliantValue, jdbcType=VARCHAR},
updated_at=#{updatedAt, jdbcType=TIMESTAMP}
WHERE
<choose>

+ 28
- 0
server/sonar-webserver-webapi/src/it/java/org/sonar/server/newcodeperiod/ws/SetActionIT.java View File

@@ -43,6 +43,8 @@ import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ProjectData;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.newcodeperiod.NewCodePeriodDao;
import org.sonar.db.newcodeperiod.NewCodePeriodDbTester;
import org.sonar.db.newcodeperiod.NewCodePeriodDto;
import org.sonar.db.newcodeperiod.NewCodePeriodType;
import org.sonar.db.project.ProjectDto;
import org.sonar.server.component.ComponentFinder;
@@ -324,6 +326,32 @@ public class SetActionIT {
assertTableContainsOnly(project.getUuid(), null, NewCodePeriodType.NUMBER_OF_DAYS, "5");
}

@Test
public void update_project_new_code_period() {
ProjectDto project = db.components().insertPublicProject().getProjectDto();
logInAsProjectAdministrator(project);
var currentTime = System.currentTimeMillis();

db.newCodePeriods().insert(new NewCodePeriodDto()
.setProjectUuid(project.getUuid())
.setType(NewCodePeriodType.NUMBER_OF_DAYS)
.setPreviousNonCompliantValue("100")
.setUpdatedAt(currentTime)
.setValue("90"));

ws.newRequest()
.setParam("project", project.getKey())
.setParam("type", "number_of_days")
.setParam("value", "30")
.execute();

var ncd = db.getDbClient().newCodePeriodDao().selectByProject(dbSession, project.getUuid());
assertThat(ncd).isPresent();
assertThat(ncd.get()).extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getPreviousNonCompliantValue)
.containsExactly(NewCodePeriodType.NUMBER_OF_DAYS, "30", null);

}

@Test
@UseDataProvider("provideNewCodePeriodTypeAndValue")
public void never_set_project_value_in_community_edition(NewCodePeriodType type, @Nullable String value) {

+ 23
- 11
server/sonar-webserver-webapi/src/it/java/org/sonar/server/newcodeperiod/ws/ShowActionIT.java View File

@@ -48,6 +48,8 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonarqube.ws.NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS;
import static org.sonarqube.ws.NewCodePeriods.NewCodePeriodType.PREVIOUS_VERSION;

public class ShowActionIT {
@Rule
@@ -62,7 +64,7 @@ public class ShowActionIT {
private WsActionTester ws;

@Before
public void setup(){
public void setup() {
when(documentationLinkGenerator.getDocumentationLink(any())).thenReturn("https://docs.sonarqube.org/latest/project-administration/defining-new-code/");
ws = new WsActionTester(new ShowAction(dbClient, userSession, componentFinder, dao, documentationLinkGenerator));
}
@@ -122,24 +124,28 @@ public class ShowActionIT {
ShowWSResponse response = ws.newRequest()
.executeProtobuf(ShowWSResponse.class);

assertResponse(response, "", "", NewCodePeriods.NewCodePeriodType.PREVIOUS_VERSION, "", false);
assertResponse(response, "", "", PREVIOUS_VERSION, "", false, null);
}

@Test
public void show_project_setting() {
ProjectDto project = db.components().insertPublicProject().getProjectDto();
logInAsProjectAdministrator(project);
var currentTime = System.currentTimeMillis();

tester.insert(new NewCodePeriodDto()
.setProjectUuid(project.getUuid())
.setType(NewCodePeriodType.NUMBER_OF_DAYS)
.setValue("4"));
.setPreviousNonCompliantValue("100")
.setUpdatedAt(currentTime)
.setValue("90"));

ShowWSResponse response = ws.newRequest()
.setParam("project", project.getKey())
.executeProtobuf(ShowWSResponse.class);

assertResponse(response, project.getKey(), "", NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "4", false);
assertResponse(response, project.getKey(), "", NUMBER_OF_DAYS, "90", false, "100");
assertUpdatedAt(response, currentTime);
}

@Test
@@ -160,7 +166,7 @@ public class ShowActionIT {
.setParam("branch", "branch")
.executeProtobuf(ShowWSResponse.class);

assertResponse(response, project.getKey(), "branch", NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "1", false);
assertResponse(response, project.getKey(), "branch", NUMBER_OF_DAYS, "1", false, null);
}

@Test
@@ -173,7 +179,7 @@ public class ShowActionIT {
.setParam("project", project.getKey())
.executeProtobuf(ShowWSResponse.class);

assertResponse(response, project.getKey(), "", NewCodePeriods.NewCodePeriodType.PREVIOUS_VERSION, "", true);
assertResponse(response, project.getKey(), "", PREVIOUS_VERSION, "", true, null);
}

@Test
@@ -193,7 +199,7 @@ public class ShowActionIT {
.setParam("branch", "branch")
.executeProtobuf(ShowWSResponse.class);

assertResponse(response, project.getKey(), "branch", NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "1", true);
assertResponse(response, project.getKey(), "branch", NUMBER_OF_DAYS, "1", true, null);
}

@Test
@@ -208,7 +214,7 @@ public class ShowActionIT {
.setParam("branch", "branch")
.executeProtobuf(ShowWSResponse.class);

assertResponse(response, project.getKey(), "branch", NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "3", true);
assertResponse(response, project.getKey(), "branch", NUMBER_OF_DAYS, "3", true, null);
}

@Test
@@ -219,7 +225,7 @@ public class ShowActionIT {
.setParam("project", "unknown")
.executeProtobuf(ShowWSResponse.class);

assertResponse(response, "", "", NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "3", true);
assertResponse(response, "", "", NUMBER_OF_DAYS, "3", true, null);
}

@Test
@@ -234,15 +240,21 @@ public class ShowActionIT {
.setParam("branch", "unknown")
.executeProtobuf(ShowWSResponse.class);

assertResponse(response, project.getKey(), "", NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "3", true);
assertResponse(response, project.getKey(), "", NUMBER_OF_DAYS, "3", true, null);
}

private void assertResponse(ShowWSResponse response, String projectKey, String branchKey, NewCodePeriods.NewCodePeriodType type, String value, boolean inherited) {
private void assertResponse(ShowWSResponse response, String projectKey, String branchKey, NewCodePeriods.NewCodePeriodType type, String value, boolean inherited,
String previousNonCompliantValue) {
assertThat(response.getBranchKey()).isEqualTo(branchKey);
assertThat(response.getProjectKey()).isEqualTo(projectKey);
assertThat(response.getInherited()).isEqualTo(inherited);
assertThat(response.getValue()).isEqualTo(value);
assertThat(response.getType()).isEqualTo(type);
assertThat(response.getPreviousNonCompliantValue()).isEqualTo(previousNonCompliantValue == null ? "" : previousNonCompliantValue);
}

private static void assertUpdatedAt(ShowWSResponse response, long currentTime) {
assertThat(response.getUpdatedAt()).isEqualTo(currentTime);
}

private void logInAsProjectAdministrator(ProjectDto project) {

+ 6
- 2
server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ShowAction.java View File

@@ -128,8 +128,8 @@ public class ShowAction implements NewCodePeriodsWsAction {

private void checkPermission(ProjectDto project) {
if (userSession.hasEntityPermission(UserRole.SCAN, project) ||
userSession.hasEntityPermission(UserRole.ADMIN, project) ||
userSession.hasPermission(SCAN)) {
userSession.hasEntityPermission(UserRole.ADMIN, project) ||
userSession.hasPermission(SCAN)) {
return;
}
throw insufficientPrivilegesException();
@@ -155,11 +155,15 @@ public class ShowAction implements NewCodePeriodsWsAction {
private static ShowWSResponse.Builder build(NewCodePeriodDto dto, boolean inherited) {
ShowWSResponse.Builder builder = ShowWSResponse.newBuilder()
.setType(convertType(dto.getType()))
.setUpdatedAt(dto.getUpdatedAt())
.setInherited(inherited);

if (dto.getValue() != null) {
builder.setValue(dto.getValue());
}
if (dto.getPreviousNonCompliantValue() != null) {
builder.setPreviousNonCompliantValue(dto.getPreviousNonCompliantValue());
}
return builder;
}


+ 2
- 0
sonar-ws/src/main/protobuf/ws-newcodeperiods.proto View File

@@ -32,6 +32,8 @@ message ShowWSResponse {
string value = 4;
bool inherited = 5;
string effectiveValue = 6;
string previousNonCompliantValue = 7;
int64 updatedAt = 8;
}

// WS api/new_code_periods/list

Loading…
Cancel
Save