From 1802b244e252df063c10a27a33f4819324e982a3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?L=C3=A9o=20Geoffroy?= Date: Tue, 11 Jul 2023 14:29:14 +0200 Subject: [PATCH] SONAR-19850 Fix project export status --- .../server/projectdump/ws/StatusActionIT.java | 38 +++++++++---------- .../server/projectdump/ws/StatusAction.java | 8 ++-- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/projectdump/ws/StatusActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/projectdump/ws/StatusActionIT.java index 27e43f5c6c8..ee7479374f8 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/projectdump/ws/StatusActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/projectdump/ws/StatusActionIT.java @@ -58,10 +58,8 @@ import static org.sonar.test.JsonAssert.assertJson; public class StatusActionIT { - private static final String SOME_UUID = "some uuid"; private static final String ID_PARAM = "id"; private static final String KEY_PARAM = "key"; - private static final String SOME_KEY = "some key"; @Rule public DbTester db = DbTester.create(System2.INSTANCE); @@ -84,7 +82,8 @@ public class StatusActionIT { @Before public void setUp() throws Exception { - project = insertProject(SOME_UUID, SOME_KEY); + project = db.components().insertPrivateProject().getProjectDto(); + logInAsProjectAdministrator("user"); when(config.get("sonar.path.data")).thenReturn(Optional.of("data")); @@ -126,7 +125,7 @@ public class StatusActionIT { public void fails_with_BRE_if_both_params_are_provided() { assertThatThrownBy(() -> { underTest.newRequest() - .setParam(ID_PARAM, SOME_UUID).setParam(KEY_PARAM, SOME_KEY) + .setParam(ID_PARAM, project.getUuid()).setParam(KEY_PARAM, project.getKey()) .execute(); }) .isInstanceOf(BadRequestException.class) @@ -162,7 +161,7 @@ public class StatusActionIT { @Test public void project_without_snapshot_can_be_imported_but_not_exported() { String response = underTest.newRequest() - .setParam(KEY_PARAM, SOME_KEY) + .setParam(KEY_PARAM, project.getKey()) .execute() .getInput(); @@ -176,7 +175,7 @@ public class StatusActionIT { insertSnapshot(project, false); String response = underTest.newRequest() - .setParam(KEY_PARAM, SOME_KEY) + .setParam(KEY_PARAM, project.getKey()) .execute() .getInput(); @@ -191,7 +190,7 @@ public class StatusActionIT { insertSnapshot(project, false); String response = underTest.newRequest() - .setParam(KEY_PARAM, SOME_KEY) + .setParam(KEY_PARAM, project.getKey()) .execute() .getInput(); @@ -201,10 +200,10 @@ public class StatusActionIT { @Test public void exportedDump_field_contains_absolute_path_if_file_exists_and_is_regular_file() throws IOException { - final String exportDumpFilePath = ensureDumpFileExists(SOME_KEY, false); + final String exportDumpFilePath = ensureDumpFileExists(project.getKey(), false); String response = underTest.newRequest() - .setParam(KEY_PARAM, SOME_KEY) + .setParam(KEY_PARAM, project.getKey()) .execute() .getInput(); @@ -214,10 +213,10 @@ public class StatusActionIT { @Test public void exportedDump_field_contains_absolute_path_if_file_exists_and_is_link() throws IOException { - final String exportDumpFilePath = ensureDumpFileExists(SOME_KEY, false); + final String exportDumpFilePath = ensureDumpFileExists(project.getKey(), false); String response = underTest.newRequest() - .setParam(KEY_PARAM, SOME_KEY) + .setParam(KEY_PARAM, project.getKey()) .execute() .getInput(); @@ -230,7 +229,7 @@ public class StatusActionIT { Files.createDirectories(Paths.get(exportDirectoryPathname)); String response = underTest.newRequest() - .setParam(KEY_PARAM, SOME_KEY) + .setParam(KEY_PARAM, project.getKey()) .execute() .getInput(); @@ -240,10 +239,10 @@ public class StatusActionIT { @Test public void dumpToImport_field_contains_absolute_path_if_file_exists_and_is_regular_file() throws IOException { - final String importDumpFilePath = ensureDumpFileExists(SOME_KEY, true); + final String importDumpFilePath = ensureDumpFileExists(project.getKey(), true); String response = underTest.newRequest() - .setParam(KEY_PARAM, SOME_KEY) + .setParam(KEY_PARAM, project.getKey()) .execute() .getInput(); @@ -253,10 +252,10 @@ public class StatusActionIT { @Test public void dumpToImport_field_contains_absolute_path_if_file_exists_and_is_link() throws IOException { - final String importDumpFilePath = ensureDumpFileExists(SOME_KEY, true); + final String importDumpFilePath = ensureDumpFileExists(project.getKey(), true); String response = underTest.newRequest() - .setParam(KEY_PARAM, SOME_KEY) + .setParam(KEY_PARAM, project.getKey()) .execute() .getInput(); @@ -269,7 +268,7 @@ public class StatusActionIT { Files.createDirectories(Paths.get(importDirectoryPathname)); String response = underTest.newRequest() - .setParam(KEY_PARAM, SOME_KEY) + .setParam(KEY_PARAM, project.getKey()) .execute() .getInput(); @@ -290,12 +289,9 @@ public class StatusActionIT { .isInstanceOf(NotFoundException.class); } - private ProjectDto insertProject(String uuid, String key) { - return db.components().insertPrivateProject(c -> c.setBranchUuid(uuid).setUuid(uuid).setKey(key)).getProjectDto(); - } private void insertSnapshot(ProjectDto projectDto, boolean last) { - dbClient.snapshotDao().insert(dbSession, SnapshotTesting.newAnalysis(projectDto.getUuid()).setLast(last)); + db.components().insertSnapshot(projectDto, snapshotDto -> snapshotDto.setLast(last)); dbSession.commit(); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectdump/ws/StatusAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectdump/ws/StatusAction.java index 33bd6fd9dce..a71ceb419e8 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectdump/ws/StatusAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectdump/ws/StatusAction.java @@ -32,6 +32,7 @@ import org.sonar.api.utils.text.JsonWriter; import org.sonar.api.web.UserRole; import org.sonar.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.component.BranchDto; import org.sonar.db.project.ProjectDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.user.UserSession; @@ -96,12 +97,13 @@ public class StatusAction implements ProjectDumpAction { try (DbSession dbSession = dbClient.openSession(false)) { ProjectDto project = getProject(dbSession, uuid, key); + BranchDto mainBranch = componentFinder.getMainBranch(dbSession, project); userSession.checkEntityPermission(UserRole.ADMIN, project); WsResponse wsResponse = new WsResponse(); checkDumps(project, wsResponse); - SnapshotsStatus snapshots = checkSnapshots(dbSession, project); + SnapshotsStatus snapshots = checkSnapshots(dbSession, mainBranch); if (snapshots.hasLast) { wsResponse.setCanBeExported(); } else if (!snapshots.hasAny) { @@ -111,7 +113,7 @@ public class StatusAction implements ProjectDumpAction { } } - private SnapshotsStatus checkSnapshots(DbSession dbSession, ProjectDto project) throws SQLException { + private SnapshotsStatus checkSnapshots(DbSession dbSession, BranchDto mainBranch) throws SQLException { PreparedStatement stmt = null; ResultSet rs = null; try { @@ -123,7 +125,7 @@ public class StatusAction implements ProjectDumpAction { " group by" + " islast"; stmt = dbClient.getMyBatis().newScrollingSelectStatement(dbSession, sql); - stmt.setString(1, project.getUuid()); + stmt.setString(1, mainBranch.getUuid()); rs = stmt.executeQuery(); SnapshotsStatus res = new SnapshotsStatus(); while (rs.next()) { -- 2.39.5