Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/ci-files/run-sanity-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set +e

./gradlew rewriteDryRun -Dorg.gradle.jvmargs=-Xmx8G \
--configure-on-demand \
--console plain \
--max-workers=1 \
-Pqa --profile

gradle_return_code=$?

set -e

exit $gradle_return_code
15 changes: 9 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import org.sonar.build.BlackBoxTest

import static org.gradle.api.JavaVersion.VERSION_17

plugins {
// Ordered alphabetically
plugins { // natural order
id 'com.github.hierynomus.license' version '0.16.1'
id "com.github.hierynomus.license-report" version "0.16.1" apply false
id 'com.gradleup.shadow' version '8.3.6' apply false
id 'com.github.hierynomus.license-report' version '0.16.1' apply false
id 'com.google.protobuf' version '0.8.19' apply false
id 'com.gradleup.shadow' version '8.3.6' apply false
id 'com.jfrog.artifactory' version '6.0.2'
id "de.undercouch.download" version "5.6.0" apply false
id 'de.undercouch.download' version '5.6.0' apply false
id 'io.spring.dependency-management' version '1.1.7'
id "org.cyclonedx.bom" version "2.3.1" apply false
id 'org.cyclonedx.bom' version '2.3.1' apply false
id 'org.openrewrite.rewrite' version '7.19.0' apply false
id 'org.sonarqube' version '7.0.0.6105'
}

Expand Down Expand Up @@ -200,7 +200,10 @@ allprojects {
}
}

apply from: 'gradle/rewrite.gradle'

apply plugin: 'org.sonarqube'

sonar {
properties {
property 'sonar.projectName', projectTitle
Expand Down
25 changes: 25 additions & 0 deletions gradle/rewrite.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

apply plugin: 'org.openrewrite.rewrite'

rewrite {
activeRecipe('org.opensearch.openrewrite.SanityCheck')
exclusion('**package-info.java')
setExportDatatables(true)
setFailOnDryRunResults(true)
}

dependencies {
rewrite(platform('org.openrewrite.recipe:rewrite-recipe-bom:3.17.0'))
rewrite('org.openrewrite.recipe:rewrite-migrate-java:3.20.0')
rewrite('org.openrewrite.recipe:rewrite-java-security:3.19.2')
rewrite('org.openrewrite.recipe:rewrite-rewrite:0.14.1')
rewrite('org.openrewrite.recipe:rewrite-static-analysis:2.20.0')
rewrite('org.openrewrite.recipe:rewrite-third-party:0.30.0')
}
16 changes: 16 additions & 0 deletions rewrite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
type: specs.openrewrite.org/v1beta/recipe
name: org.opensearch.openrewrite.SanityCheck
displayName: Apply all Java & Gradle best practices
description: Comprehensive code quality recipe combining modernization, security, and best practices.
tags:
- java
- gradle
- static-analysis
- cleanup
recipeList:
- org.openrewrite.gradle.EnableGradleBuildCache
- org.openrewrite.gradle.EnableGradleParallelExecution
- org.openrewrite.java.RemoveUnusedImports
- org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods
---
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,12 @@
package org.sonar.auth.github;

import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.oauth.OAuth20Service;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.lang.String.format;
import static org.sonar.auth.OAuthRestClient.executePaginatedRequest;
import static org.sonar.auth.OAuthRestClient.executeRequest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,6 @@ private CeTaskSubmit createTaskSubmit(String type, @Nullable Component component
.build();
}

private ComponentDto insertComponent(ComponentDto componentDto) {
return db.components().insertComponent(componentDto);
}

private UserDto insertUser(UserDto userDto) {
db.getDbClient().userDao().insert(session, userDto);
session.commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Stream;
Expand Down Expand Up @@ -489,16 +487,6 @@ public static Object[][] anyValidLeakPeriodSettingValue() {
};
}

private List<SnapshotDto> createSnapshots(ComponentDto project) {
ArrayList<SnapshotDto> list = new ArrayList<>();
list.add(dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setLast(false))); // 2008-11-11
list.add(dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226494680000L).setLast(false))); // 2008-11-12
list.add(dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227157200000L).setLast(false))); // 2008-11-20
list.add(dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227358680000L).setLast(false))); // 2008-11-22
list.add(dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227934800000L).setLast(true))); // 2008-11-29
return list;
}

private long milisSinceEpoch(int year, int month, int day, int hour) {
return ZonedDateTime.of(year, month, day, hour, 0, 0, 0, ZoneId.systemDefault())
.toInstant().toEpochMilli();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.sonar.ce.task.projectanalysis.step;

import java.util.Date;
import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.utils.DateUtils;
Expand All @@ -35,13 +34,10 @@
import org.sonar.ce.task.step.TestComputationStepContext;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.component.BranchType;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotTesting;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;

public class ValidateProjectStepIT {
Expand Down Expand Up @@ -110,11 +106,4 @@ public void fail_when_project_key_is_invalid() {
"Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.",
"You should update the project key with the expected format.");
}

private void setBranch(BranchType type, @Nullable String mergeBranchUuid) {
Branch branch = mock(Branch.class);
when(branch.getType()).thenReturn(type);
when(branch.getReferenceBranchUuid()).thenReturn(mergeBranchUuid);
analysisMetadataHolder.setBranch(branch);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -516,22 +516,6 @@ private void assertDtoIsView(ComponentDto dto) {
assertThat(dto.getCreatedAt()).isEqualTo(now);
}

/**
* Assertions to verify the DTO created from {@link #createViewBuilder(ViewAttributes.Type)} ()}
*/
private void assertDtoIsApplication(ComponentDto dto) {
assertThat(dto.name()).isEqualTo(VIEW_NAME);
assertThat(dto.longName()).isEqualTo(VIEW_NAME);
assertThat(dto.description()).isEqualTo(VIEW_DESCRIPTION);
assertThat(dto.path()).isNull();
assertThat(dto.uuid()).isEqualTo(VIEW_UUID);
assertThat(dto.branchUuid()).isEqualTo(VIEW_UUID);
assertThat(dto.qualifier()).isEqualTo(ComponentQualifiers.APP);
assertThat(dto.scope()).isEqualTo(ComponentScopes.PROJECT);
assertThat(dto.getCopyComponentUuid()).isNull();
assertThat(dto.getCreatedAt()).isEqualTo(now);
}

/**
* Assertions to verify the DTO created from {@link #createProjectView1Builder(ComponentDto, Long)}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.sonar.api.ExtensionPoint;
import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.ce.common.scanner.ScannerReportReader;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.ce.task.projectanalysis.analysis.Branch;
import org.sonar.ce.task.projectanalysis.component.Component;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
package org.sonar.ce.task.projectanalysis.event;

import org.junit.Test;
import org.sonar.ce.task.projectanalysis.component.Component;
import org.sonar.ce.task.projectanalysis.component.ReportComponent;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand Down Expand Up @@ -50,8 +48,4 @@ public void can_add_and_retrieve_many_events() {

assertThat(underTest.getEvents()).extracting("name").containsOnly(EVENT_1.getName(), EVENT_2.getName());
}

private static Component newComponent(int i) {
return ReportComponent.builder(Component.Type.PROJECT, i).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
package org.sonar.ce.task.projectanalysis.measure;

import java.util.UUID;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
Expand All @@ -41,7 +40,6 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sonar.api.measures.CoreMetrics.NCLOC;
import static org.sonar.ce.task.projectanalysis.component.ReportComponent.DUMB_PROJECT;
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.Durations;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
import org.sonar.ce.task.projectanalysis.analysis.Branch;
import org.sonar.ce.task.projectanalysis.component.ReportComponent;
Expand Down Expand Up @@ -740,21 +739,6 @@ private static Branch newNonMainBranch(BranchType branchType, String branchName)
return nonMainBranch;
}

private static Durations readDurationsField(NewIssuesNotification notification) {
return readField(notification, "durations");
}

private static Durations readField(NewIssuesNotification notification, String fieldName) {
try {
Field durationsField = NewIssuesNotification.class.getDeclaredField(fieldName);
durationsField.setAccessible(true);
Object o = durationsField.get(notification);
return (Durations) o;
} catch (IllegalAccessException | NoSuchFieldException e) {
throw new RuntimeException(e);
}
}

private static DetailsSupplier readDetailsSupplier(NewIssuesNotification notification) {
try {
Field durationsField = NewIssuesNotification.class.getDeclaredField("detailsSupplier");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
package org.sonar.ce.task.projectanalysis.purge;

import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import java.util.Arrays;
import java.util.Collections;
import java.util.function.Predicate;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -38,7 +36,6 @@
import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
import org.sonar.ce.task.projectanalysis.component.ViewsComponent;
import org.sonar.ce.task.projectanalysis.step.BaseStepTest;
import org.sonar.ce.task.projectanalysis.util.WrapInSingleElementArray;
import org.sonar.ce.task.step.ComputationStep;
import org.sonar.ce.task.step.TestComputationStepContext;
import org.sonar.db.DbClient;
Expand Down Expand Up @@ -100,13 +97,6 @@ private void verify_call_purge_method_of_the_purge_task(Component project) {
assertThat(argumentCaptor.getValue()).isEqualTo(PROJECT_UUID);
}

private static Object[][] dataproviderFromComponentTypeValues(Predicate<Component.Type> predicate) {
return Arrays.stream(Component.Type.values())
.filter(predicate)
.map(WrapInSingleElementArray.INSTANCE)
.toArray(Object[][]::new);
}

@Override
protected ComputationStep step() {
return underTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,6 @@ private static QualityProfile qp(String qpName, String languageKey, Date date) {
return new QualityProfile(qpName + "-" + languageKey, qpName, languageKey, date);
}

/**
* Just a trick to use variable args which is shorter than writing new QualityProfile[] { }
*/
private static QualityProfile[] arrayOf(QualityProfile... qps) {
return qps;
}

private static Measure newMeasure(@Nullable QualityProfile... qps) {
return Measure.newMeasureBuilder().create(toJson(qps));
}
Expand Down
26 changes: 0 additions & 26 deletions server/sonar-db-dao/src/it/java/org/sonar/db/ce/CeQueueDaoIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@
*/
package org.sonar.db.ce;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.function.Consumer;
Expand Down Expand Up @@ -786,28 +782,6 @@ private void insertCharacteristic(String key, String value, String uuid, String
db.getDbClient().ceTaskCharacteristicsDao().insert(db.getSession(), dto1);
}

private static Iterable<Map<String, Object>> upperizeKeys(List<Map<String, Object>> select) {
return select.stream().map((Function<Map<String, Object>, Map<String, Object>>) input -> {
Map<String, Object> res = new HashMap<>(input.size());
for (Map.Entry<String, Object> entry : input.entrySet()) {
res.put(entry.getKey().toUpperCase(), entry.getValue());
}
return res;
}).toList();
}

private void verifyCeQueueStatuses(String[] taskUuids, CeQueueDto.Status[] statuses) {
Map<String, Object>[] rows = new Map[taskUuids.length];
for (int i = 0; i < taskUuids.length; i++) {
rows[i] = rowMap(taskUuids[i], statuses[i]);
}
assertThat(upperizeKeys(db.select(SELECT_QUEUE_UUID_AND_STATUS_QUERY))).containsOnly(rows);
}

private static Map<String, Object> rowMap(String uuid, CeQueueDto.Status status) {
return ImmutableMap.of("UUID", uuid, "STATUS", status.name());
}

private void mockSystem2ForSingleCall(long now) {
Mockito.reset(mockedSystem2);
when(mockedSystem2.now())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;

import static java.lang.System.lineSeparator;
import static java.util.Collections.singleton;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
Expand Down
Loading