Skip to content

Commit 0695ca1

Browse files
claire-villard-sonarsourcesonartech
authored andcommitted
SONAR-26047 Add the analysis count in the System Info file
1 parent 0ce6f9e commit 0695ca1

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* SonarQube
3+
* Copyright (C) 2009-2025 SonarSource Sàrl
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.server.platform.db.migration.version.v202601;
21+
22+
import java.sql.SQLException;
23+
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.api.extension.RegisterExtension;
25+
import org.sonar.api.impl.utils.TestSystem2;
26+
import org.sonar.db.MigrationDbTester;
27+
import org.sonar.server.platform.db.migration.step.DataChange;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
class PopulateAnalysesCounterStartDatePropertyTest {
32+
33+
private static final String TARGET_PROPERTY = "analyses.counter.date";
34+
private static final long NOW = 1_000_000_000L;
35+
36+
private final TestSystem2 system2 = new TestSystem2().setNow(NOW);
37+
38+
@RegisterExtension
39+
public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(PopulateAnalysesCounterStartDateProperty.class);
40+
41+
private final DataChange underTest = new PopulateAnalysesCounterStartDateProperty(db.database(), system2);
42+
43+
@Test
44+
void execute_shouldInitializeProperty() throws SQLException {
45+
underTest.execute();
46+
47+
assertThat(db.select("select text_value from internal_properties where kee = '" + TARGET_PROPERTY + "'"))
48+
.hasSize(1)
49+
.extracting(m -> m.get("text_value"))
50+
.containsExactly(String.valueOf(NOW));
51+
}
52+
53+
@Test
54+
void execute_shouldKeepExistingValue() throws SQLException {
55+
db.executeInsert("internal_properties",
56+
"kee", TARGET_PROPERTY,
57+
"is_empty", false,
58+
"text_value", "12",
59+
"created_at", NOW);
60+
61+
underTest.execute();
62+
63+
assertThat(db.select("select text_value from internal_properties where kee = '" + TARGET_PROPERTY + "'"))
64+
.hasSize(1)
65+
.extracting(m -> m.get("text_value"))
66+
.containsExactly("12");
67+
}
68+
69+
@Test
70+
void execute_shouldBeReentrant() throws SQLException {
71+
underTest.execute();
72+
underTest.execute();
73+
74+
assertThat(db.select("select text_value from internal_properties where kee = '" + TARGET_PROPERTY + "'"))
75+
.hasSize(1);
76+
}
77+
}

server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v202601/DbVersion202601.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class DbVersion202601 implements DbVersion {
2828
public void addSteps(MigrationStepRegistry registry) {
2929
registry
3030
.add(2026_01_000, "Create 'components_copy_component_uuid' index", CreateIndexOnCopyComponentUuid.class)
31+
.add(2026_01_001, "Populate the 'analyses.counter.date' internal property", PopulateAnalysesCounterStartDateProperty.class)
3132
;
3233
}
3334

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* SonarQube
3+
* Copyright (C) 2009-2025 SonarSource Sàrl
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.server.platform.db.migration.version.v202601;
21+
22+
import java.sql.SQLException;
23+
import org.apache.commons.lang3.StringUtils;
24+
import org.sonar.api.utils.System2;
25+
import org.sonar.db.Database;
26+
import org.sonar.server.platform.db.migration.step.DataChange;
27+
28+
public class PopulateAnalysesCounterStartDateProperty extends DataChange {
29+
30+
private static final String ANALYSES_COUNTER_DATE = "analyses.counter.date";
31+
private final System2 system2;
32+
33+
PopulateAnalysesCounterStartDateProperty(Database db, System2 system2) {
34+
super(db);
35+
this.system2 = system2;
36+
}
37+
38+
@Override
39+
protected void execute(Context context) throws SQLException {
40+
final String existingValue = context.prepareSelect("select text_value from internal_properties where kee = ?")
41+
.setString(1, ANALYSES_COUNTER_DATE)
42+
.get(r -> r.getString(1));
43+
44+
if (StringUtils.isEmpty(existingValue)) {
45+
long now = system2.now();
46+
context.prepareUpsert("""
47+
insert into internal_properties (kee, is_empty, text_value, created_at)
48+
values (?, ?, ?, ?)
49+
""")
50+
.setString(1, ANALYSES_COUNTER_DATE)
51+
.setBoolean(2, false)
52+
.setString(3, String.valueOf(now))
53+
.setLong(4, now)
54+
.execute()
55+
.commit();
56+
}
57+
}
58+
}

server/sonar-server-common/src/main/java/org/sonar/server/property/InternalProperties.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ public interface InternalProperties {
5555

5656
String DEFAULT_ADMIN_CREDENTIAL_USAGE_EMAIL = "default.admin.cred";
5757

58+
/**
59+
* Analysis counters
60+
*/
61+
String BRANCH_ANALYSES_COUNT = "analyses.counter.branch";
62+
String PR_ANALYSES_COUNT = "analyses.counter.pr";
63+
String ANALYSIS_COUNTER_DATE = "analyses.counter.date";
64+
5865
/**
5966
* Read the value of the specified property.
6067
*

0 commit comments

Comments
 (0)