Skip to content

Commit

Permalink
Publish events on artifact deletion.
Browse files Browse the repository at this point in the history
  • Loading branch information
CamaradeRoman committed Jul 22, 2024
1 parent fd713d6 commit 6d66568
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import org.octopusden.octopus.dms.entity.ComponentVersionArtifact
import org.springframework.data.jpa.repository.JpaRepository

interface ComponentVersionArtifactRepository : JpaRepository<ComponentVersionArtifact, Long> {
fun findByArtifact(artifact: Artifact): List<ComponentVersionArtifact>

fun findByComponentVersion(componentVersion: ComponentVersion): List<ComponentVersionArtifact>

fun findByComponentVersionComponentNameAndComponentVersionVersion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@ import org.octopusden.octopus.dms.dto.DownloadArtifactDTO
import org.octopusden.octopus.dms.entity.DebianArtifact
import org.octopusden.octopus.dms.entity.MavenArtifact
import org.octopusden.octopus.dms.entity.RpmArtifact
import org.octopusden.octopus.dms.event.DeleteComponentVersionArtifactEvent
import org.octopusden.octopus.dms.exception.ArtifactAlreadyExistsException
import org.octopusden.octopus.dms.exception.NotFoundException
import org.octopusden.octopus.dms.repository.ArtifactRepository
import org.octopusden.octopus.dms.repository.ComponentVersionArtifactRepository
import org.octopusden.octopus.dms.service.ArtifactService
import org.octopusden.octopus.dms.service.StorageService
import org.slf4j.LoggerFactory
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.multipart.MultipartFile

@Service
class ArtifactServiceImpl(
private val storageService: StorageService,
private val artifactRepository: ArtifactRepository
private val artifactRepository: ArtifactRepository,
private val componentVersionArtifactRepository: ComponentVersionArtifactRepository,
private val applicationEventPublisher: ApplicationEventPublisher
) : ArtifactService {
override fun repositories(repositoryType: RepositoryType): List<String> {
log.info("Get $repositoryType repositories")
Expand Down Expand Up @@ -101,9 +106,18 @@ class ArtifactServiceImpl(
@Transactional(readOnly = false)
override fun delete(id: Long, dryRun: Boolean) {
log.info("Delete artifact with ID '$id'")
artifactRepository.findById(id).ifPresent {
if (!dryRun) artifactRepository.delete(it)
log.info("$it deleted")
artifactRepository.findById(id).ifPresent { artifact ->
if (!dryRun) {
componentVersionArtifactRepository.findByArtifact(artifact).forEach {
applicationEventPublisher.publishEvent(
DeleteComponentVersionArtifactEvent(
it.componentVersion.component.name, it.componentVersion.version, it.toFullDTO()
)
)
}
artifactRepository.delete(artifact)
}
log.info("$artifact deleted")
}
}

Expand Down

0 comments on commit 6d66568

Please sign in to comment.