Skip to content

Commit

Permalink
fix: sonarqube quality issues (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
grigoriev authored Aug 26, 2024
1 parent d783261 commit f7873d3
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 34 deletions.
48 changes: 38 additions & 10 deletions .github/workflows/maven-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,41 @@ jobs:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
MARKDOWN2HTML_MAVEN_PLUGIN_FAIL_ON_ERROR: true
steps:
# Checkout the repository

This comment has been minimized.

Copy link
@ariwk

ariwk Aug 26, 2024

Collaborator

why do you repeat yourself here and everywhere down here?

- name: 📄 Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
with:
fetch-depth: 0

# Set up JDK and Maven
- name: 🧱 Set up JDK and Maven
uses: actions/setup-java@6a0805fcefea3d4657a47ac4c165951e33482018 # v4
with:
distribution: adopt
java-version: 17
gpg-private-key: ${{ secrets.COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_GPG_PRIVATE_KEY }}

# Get the project version
- name: 📝 Store project version
id: project_version
run: echo "project_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_OUTPUT

# Generate cache key
- name: 📝 Store cache key
id: cache_key
run: echo "cache_key=${{ runner.os }}-mvn-${{ hashFiles('**/pom.xml') }}-${{ github.sha }}" >> $GITHUB_OUTPUT

# Prepare cache using cache key
- name: 💾 Prepare Cache
id: prepare-cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4
with:
path: |
/home/runner/.m2
/home/runner/work
key: ${{ runner.os }}-mvn-${{ hashFiles('**/pom.xml') }}
key: ${{ steps.cache_key.outputs.cache_key }}

# Generate settings.xml for Maven
- name: 🔘 Generate settings.xml
uses: whelk-io/maven-settings-xml-action@9dc09b23833fa9aa7f27b63db287951856f3433d # v22
with:
Expand Down Expand Up @@ -81,21 +98,19 @@ jobs:
}
}
]
# Print settings.xml
- name: 🔘 Print settings.xml
run: cat /home/runner/.m2/settings.xml

# Build with Maven
- name: 📦 Build with Maven
run: mvn --batch-mode clean package sonar:sonar
- name: 📝 Store project version
id: project_version
run: echo "project_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_OUTPUT
- name: 📝 Store cache key
id: cache_key
run: echo "cache_key=${{ runner.os }}-mvn-${{ hashFiles('**/pom.xml') }}" >> $GITHUB_OUTPUT
outputs:
project_version: ${{ steps.project_version.outputs.project_version }}
cache_key: ${{ steps.cache_key.outputs.cache_key }}

# deploy to Maven Central
# Deploy release to Maven Central
deploy-maven-central:
needs: build
runs-on: ubuntu-latest
Expand All @@ -107,12 +122,15 @@ jobs:
COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_TOKEN: ${{ secrets.COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_TOKEN }}
COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_GPG_PASSPHRASE: ${{ secrets.COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_GPG_PASSPHRASE }}
steps:
# Set up JDK and Maven
- name: 🧱 Set up JDK and Maven
uses: actions/setup-java@6a0805fcefea3d4657a47ac4c165951e33482018 # v4
with:
distribution: adopt
java-version: 17
gpg-private-key: ${{ secrets.COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_GPG_PRIVATE_KEY }}

# Restore cache using cache key
- name: 💾 Restore Cache
id: restore-cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4
Expand All @@ -121,6 +139,8 @@ jobs:
/home/runner/.m2
/home/runner/work
key: ${{ needs.build.outputs.cache_key }}

# Deploy artifacts to Maven Central
- name: 📦 Publish to Maven Central
run: mvn --batch-mode deploy -P gpg-sign -P nexus-staging

Expand All @@ -137,11 +157,14 @@ jobs:
S3_SBB_POLARION_MAVEN_REPO_RW_SECRET_ACCESS_KEY: ${{ secrets.S3_SBB_POLARION_MAVEN_REPO_RW_SECRET_ACCESS_KEY }}
GITHUB_TOKEN: ${{ github.token }}
steps:
# Set up JDK and Maven
- name: 🧱 Set up JDK and Maven
uses: actions/setup-java@6a0805fcefea3d4657a47ac4c165951e33482018 # v4
with:
distribution: adopt
java-version: 17

# Restore cache using cache key
- name: 💾 Restore Cache
id: restore-cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4
Expand All @@ -150,8 +173,13 @@ jobs:
/home/runner/.m2
/home/runner/work
key: ${{ needs.build.outputs.cache_key }}

# Deploy artifacts to GitHub Packages
- name: 📦 Publish to GitHub Packages
run: mvn --batch-mode -Dmaven.javadoc.skip=true -Dmaven.source.skip=true deploy -P deploy-github-packages

# Upload assets to GitHub Release
- name: 📦 Upload assets
run: gh release upload v${{ needs.build.outputs.project_version }} **/target/*-${{ needs.build.outputs.project_version }}.jar && gh release upload v${{ needs.build.outputs.project_version }}
**/target/*-${{ needs.build.outputs.project_version }}-tests.jar
run: |-
gh release upload v${{ needs.build.outputs.project_version }} **/target/*-${{ needs.build.outputs.project_version }}.jar
gh release upload v${{ needs.build.outputs.project_version }} **/target/*-${{ needs.build.outputs.project_version }}-tests.jar
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ch.sbb.polarion.extension.generic.rest.controller;

import ch.sbb.polarion.extension.generic.exception.ObjectNotFoundException;
import ch.sbb.polarion.extension.generic.service.PolarionService;
import ch.sbb.polarion.extension.generic.settings.GenericNamedSettings;
import ch.sbb.polarion.extension.generic.settings.NamedSettingsRegistry;
Expand Down Expand Up @@ -109,12 +108,7 @@ public void deleteSetting(@PathParam("feature") String feature, @PathParam("name
public SettingsModel readSetting(@PathParam("feature") String feature, @PathParam("name") String name,
@QueryParam("scope") @DefaultValue("") String scope, @QueryParam("revision") String revision) {
GenericNamedSettings<?> settings = NamedSettingsRegistry.INSTANCE.getByFeatureName(feature);
SettingsModel model = settings.read(scope, SettingId.fromName(name), revision);
if (model != null) {
return model;
} else {
throw new ObjectNotFoundException("Cannot find data using specified parameters");
}
return settings.read(scope, SettingId.fromName(name), revision);
}

@PUT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected GenericNamedSettings(String featureName, SettingsService settingsServi
}

@SuppressWarnings("unused")
public T load(@Nullable String project, @NotNull SettingId id) {
public @NotNull T load(@Nullable String project, @NotNull SettingId id) {
try {
return read(ScopeUtils.getScopeFromProject(project), id, null);
} catch (ObjectNotFoundException e) {
Expand Down Expand Up @@ -233,7 +233,7 @@ private SettingName buildSettingName(String scope, String id) {
}

private @NotNull T saveDefaultSettingsInGlobalScope() {
T defaultModel = defaultValues();
@NotNull T defaultModel = defaultValues();
defaultModel.setName(DEFAULT_NAME);
return save(DEFAULT_SCOPE, SettingId.fromId(DEFAULT_NAME), defaultModel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface NamedSettings<T extends SettingsModel> {

@SneakyThrows
@SuppressWarnings("unchecked")
@NotNull default T fromString(String content) {
default @NotNull T fromString(String content) {
T model = ((Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]).getConstructor().newInstance();
model.deserialize(content);
return model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import com.polarion.subterra.base.data.model.ICustomField;
import org.junit.jupiter.api.Test;

import java.util.Objects;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@

class JAXBUtilsTest {

private final String TEST_OBJECT = "" +
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + System.lineSeparator() +
private static final String TEST_OBJECT = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + System.lineSeparator() +
"<TestObject>" + System.lineSeparator() +
" <testKey>key</testKey>" + System.lineSeparator() +
" <testValue>100</testValue>" + System.lineSeparator() +
"</TestObject>" + System.lineSeparator() +
"";
"</TestObject>" + System.lineSeparator();

@Test
void deserialize() throws JAXBException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ void testReadSetting() {
NamedSettingsRegistry.INSTANCE.register(List.of(settings));
SettingsModel settingsModel = controller.readSetting("feature1", "name1", "testScope", "1");
assertEquals("feature1Model", settingsModel.getName());

when(settings.read(any(), any(), any())).thenReturn(null);
ObjectNotFoundException exception = assertThrows(ObjectNotFoundException.class,
() -> controller.readSetting("feature1", "name1", "testScope", "1"));
assertEquals("Cannot find data using specified parameters", exception.getMessage());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void filterRequestWithValidBearerToken() throws IOException, AuthenticationFaile


@Test
void filterRequestWithFailedAuthentication() throws IOException, AuthenticationFailedException {
void filterRequestWithFailedAuthentication() throws AuthenticationFailedException {
when(requestContext.getHeaderString(HttpHeaders.AUTHORIZATION)).thenReturn("Bearer failed_token");
when(requestContext.getHeaderString(AuthenticationFilter.X_POLARION_REST_TOKEN_HEADER)).thenReturn(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void testRevisionsList() {
when(mockedConnection.exists(any(ILocation.class))).thenReturn(true);
revisions = settingsService.listRevisions(location, null);
assertEquals(4, revisions.size());
assertEquals(Arrays.asList("789", "555", "456", "73"), revisions.stream().map(Revision::getName).collect(Collectors.toList()));
assertEquals(Arrays.asList("789", "555", "456", "73"), revisions.stream().map(Revision::getName).toList());
assertEquals("author3", revisions.get(0).getAuthor());
assertEquals("USER NAME 2", revisions.get(2).getAuthor());
assertEquals(Set.of("baselineName"), revisions.stream().map(Revision::getBaseline).collect(Collectors.toSet()));
Expand Down

0 comments on commit f7873d3

Please sign in to comment.