Skip to content

Commit

Permalink
Merge pull request #235 from Shikkanime/dev
Browse files Browse the repository at this point in the history
Fix issues Sonarqube
  • Loading branch information
Ziedelth authored Mar 3, 2024
2 parents 3faebba + cf53c8d commit b71b60e
Show file tree
Hide file tree
Showing 26 changed files with 497 additions and 143 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/global_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: Global workflow

on:
pull_request:
types: [ opened, synchronize, reopened ]
push:
branches:
- dev
Expand Down Expand Up @@ -33,7 +34,7 @@ jobs:
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
run: ./gradlew sonar --info -Dsonar.pullrequest.key=${{ github.event.pull_request.number }} -Dsonar.pullrequest.branch=${{ github.event.pull_request.head.ref }} -Dsonar.pullrequest.base=${{ github.event.pull_request.base.ref }}
run: ./gradlew sonar --info -Dsonar.pullrequest.key=${{ github.event.pull_request.number }} -Dsonar.pullrequest.branch=${{ github.event.pull_request.head.ref }} -Dsonar.pullrequest.base=${{ github.event.pull_request.base.ref }} -Dsonar.qualitygate.wait=true

- name: Cache gradle dependencies
uses: actions/cache@v4
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ sonar {
properties {
property("sonar.projectKey", "core")
property("sonar.projectName", "core")
property("sonar.exclusions", "**/fr/shikkanime/socialnetworks/**")
}
}

Expand Down
30 changes: 20 additions & 10 deletions src/main/kotlin/fr/shikkanime/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,50 @@ import fr.shikkanime.utils.LoggerFactory
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import java.util.concurrent.atomic.AtomicReference

private val logger = LoggerFactory.getLogger(Constant.NAME)

fun main() {
logger.info("Starting ${Constant.NAME}...")
initAll(AtomicReference())
}

fun initAll(adminPassword: AtomicReference<String>?, port: Int = 37100, wait: Boolean = true): NettyApplicationEngine {
ImageService.loadCache()

val memberService = Constant.injector.getInstance(MemberService::class.java)
if (adminPassword != null) {
val memberService = Constant.injector.getInstance(MemberService::class.java)

try {
memberService.initDefaultAdminUser()
} catch (e: IllegalStateException) {
logger.info("Admin user already exists")
try {
adminPassword.set(memberService.initDefaultAdminUser())
} catch (e: IllegalStateException) {
logger.info("Admin user already exists")
}
}

Constant.injector.getInstance(AnimeService::class.java).preIndex()
ImageService.addAll()

logger.info("Starting jobs...")
// Every 10 seconds
JobManager.scheduleJob("*/10 * * * * ?", MetricJob::class.java)
// Every minute
JobManager.scheduleJob("0 * * * * ?", FetchEpisodesJob::class.java)
// Every hour
JobManager.scheduleJob("0 0 * * * ?", SavingImageCacheJob::class.java)
JobManager.scheduleJob("0 */10 * * * ?", GarbageCollectorJob::class.java)
JobManager.scheduleJob("0 0 0 * * ?", DeleteOldMetricsJob::class.java)
JobManager.scheduleJob("0 0 * * * ?", FetchDeprecatedEpisodeJob::class.java)
// Every day at midnight
JobManager.scheduleJob("0 0 0 * * ?", DeleteOldMetricsJob::class.java)
JobManager.start()

logger.info("Starting server...")
embeddedServer(
return embeddedServer(
Netty,
port = 37100,
port = port,
host = "0.0.0.0",
module = Application::module
).start(wait = true)
).start(wait = wait)
}

fun Application.module() {
Expand Down
7 changes: 0 additions & 7 deletions src/main/kotlin/fr/shikkanime/jobs/GarbageCollectorJob.kt

This file was deleted.

6 changes: 3 additions & 3 deletions src/main/kotlin/fr/shikkanime/modules/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private suspend fun handleRequest(
}

private suspend fun handleMultipartResponse(call: ApplicationCall, response: Response) {
val map = response.data as Map<String, Any>
val map = response.data as Map<String, Any> // NOSONAR
call.respondBytes(map["image"] as ByteArray, map["contentType"] as ContentType)
}

Expand All @@ -266,8 +266,8 @@ private suspend fun handleTemplateResponse(
val configCacheService = Constant.injector.getInstance(ConfigCacheService::class.java)
val simulcastCacheService = Constant.injector.getInstance(SimulcastCacheService::class.java)

val map = response.data as Map<String, Any>
val modelMap = (map["model"] as Map<String, Any?>).toMutableMap()
val map = response.data as Map<String, Any> // NOSONAR
val modelMap = (map["model"] as Map<String, Any?>).toMutableMap() // NOSONAR

val linkObjects = LinkObject.list()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ import jakarta.persistence.TypedQuery
import org.hibernate.ScrollMode
import org.hibernate.jpa.AvailableHints
import org.hibernate.query.Query
import java.lang.reflect.ParameterizedType
import java.util.*

abstract class AbstractRepository<E : ShikkEntity> {
@Inject
protected lateinit var database: Database

protected fun getEntityClass(): Class<E> {
return (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<E>
}
protected abstract fun getEntityClass(): Class<E>

protected fun <T> inTransaction(block: (EntityManager) -> T): T {
val entityManager = database.entityManager
Expand Down Expand Up @@ -55,7 +52,7 @@ abstract class AbstractRepository<E : ShikkEntity> {

if (scrollableResults.first() && scrollableResults.scroll((limit * page) - limit)) {
for (i in 0 until limit) {
list.add(scrollableResults.get() as E)
list.add(scrollableResults.get() as E) // NOSONAR
if (!scrollableResults.next()) break
}
total = if (scrollableResults.last()) scrollableResults.rowNumber + 1L else 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import org.hibernate.search.mapper.orm.Search
import java.util.*

class AnimeRepository : AbstractRepository<Anime>() {
override fun getEntityClass() = Anime::class.java

private fun Anime.initialize(): Anime {
Hibernate.initialize(this.simulcasts)
return this
Expand Down Expand Up @@ -112,7 +114,8 @@ class AnimeRepository : AbstractRepository<Anime>() {
fun findAllByName(name: String, countryCode: CountryCode?, page: Int, limit: Int): Pageable<Anime> {
val searchSession = Search.session(database.entityManager)

val searchResult = searchSession.search(Anime::class.java)
@Suppress("UNCHECKED_CAST")
val searchResult = searchSession.search(getEntityClass())
.where { w -> findWhere(w, name, countryCode) }
.fetch((limit * page) - limit, limit) as SearchResult<Anime>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package fr.shikkanime.repositories
import fr.shikkanime.entities.Config

class ConfigRepository : AbstractRepository<Config>() {
override fun getEntityClass() = Config::class.java

fun findAllByName(name: String): List<Config> {
return inTransaction {
createReadOnlyQuery(it, "FROM Config c WHERE LOWER(c.propertyKey) LIKE :name", getEntityClass())
Expand Down
12 changes: 11 additions & 1 deletion src/main/kotlin/fr/shikkanime/repositories/EpisodeRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import java.time.ZonedDateTime
import java.util.*

class EpisodeRepository : AbstractRepository<Episode>() {
override fun getEntityClass() = Episode::class.java

private fun Episode.initialize(): Episode {
Hibernate.initialize(this.anime?.simulcasts)
return this
Expand Down Expand Up @@ -137,7 +139,15 @@ class EpisodeRepository : AbstractRepository<Episode>() {
return inTransaction {
createReadOnlyQuery(
it,
"FROM Episode WHERE platform = :platform AND ((lastUpdateDateTime < :lastUpdateDateTime OR lastUpdateDateTime IS NULL) OR description IS NULL OR image = :defaultImage)",
"""
FROM Episode
WHERE platform = :platform
AND (
(lastUpdateDateTime < :lastUpdateDateTime OR lastUpdateDateTime IS NULL) OR
(description IS NULL OR description = '') OR
image = :defaultImage
)
""".trimIndent(),
getEntityClass()
)
.setParameter("platform", platform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import org.hibernate.Hibernate
import java.util.*

class MemberRepository : AbstractRepository<Member>() {
override fun getEntityClass() = Member::class.java

private fun Member.initialize(): Member {
Hibernate.initialize(this.roles)
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import fr.shikkanime.entities.Metric
import java.time.ZonedDateTime

class MetricRepository : AbstractRepository<Metric>() {
override fun getEntityClass() = Metric::class.java

fun findAllAfter(date: ZonedDateTime): List<Metric> {
return inTransaction {
createReadOnlyQuery(it, "FROM Metric WHERE date > :date ORDER BY date", Metric::class.java)
createReadOnlyQuery(it, "FROM Metric WHERE date > :date ORDER BY date", getEntityClass())
.setParameter("date", date)
.resultList
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package fr.shikkanime.repositories
import fr.shikkanime.entities.Simulcast

class SimulcastRepository : AbstractRepository<Simulcast>() {
override fun getEntityClass() = Simulcast::class.java

fun findBySeasonAndYear(season: String, year: Int): Simulcast? {
return inTransaction {
createReadOnlyQuery(it, "FROM Simulcast WHERE season = :season AND year = :year", Simulcast::class.java)
createReadOnlyQuery(it, "FROM Simulcast WHERE season = :season AND year = :year", getEntityClass())
.setParameter("season", season)
.setParameter("year", year)
.resultList
Expand Down
Loading

0 comments on commit b71b60e

Please sign in to comment.