Skip to content

Commit

Permalink
feat: 新增适用于虚拟仓库的ArtifactContext复制方法 TencentBlueKing#1024
Browse files Browse the repository at this point in the history
* feat: 新增适用于虚拟仓库的ArtifactContext复制方法 TencentBlueKing#1024

* refactor: ArtifactInfo.copy方法优化 TencentBlueKing#1024
  • Loading branch information
scplsy authored Mar 6, 2024
1 parent e918dc8 commit 09bb2dd
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ dependencies {
api(project(":common:common-api"))
api("com.tencent.devops:devops-boot-starter-plugin")
compileOnly("com.google.guava:guava")
implementation("org.apache.commons:commons-lang3")
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ package com.tencent.bkrepo.common.artifact.api
import com.tencent.bkrepo.common.api.constant.CharPool.AT
import com.tencent.bkrepo.common.api.constant.CharPool.SLASH
import com.tencent.bkrepo.common.artifact.path.PathUtils
import org.apache.commons.lang3.reflect.FieldUtils
import java.lang.reflect.Modifier
import kotlin.reflect.full.primaryConstructor

/**
* 构件信息
Expand Down Expand Up @@ -131,4 +134,27 @@ open class ArtifactInfo(
getArtifactVersion()?.let { builder.append(AT).append(it) }
return builder.toString()
}

/**
* 根据当前对象的属性值以及传入的[projectId]和[repoName]构造新的实例
*/
fun copy(projectId: String? = null, repoName: String? = null): ArtifactInfo {
val constructor = this::class.primaryConstructor!!
val paramMap = constructor.parameters.associateWith { param ->
when (param.name) {
ArtifactInfo::projectId.name -> projectId ?: this.projectId
ArtifactInfo::repoName.name -> repoName ?: this.repoName
else -> FieldUtils.readField(this, param.name, true)
}
}
val newInstance = constructor.callBy(paramMap)
val fields = FieldUtils.getAllFieldsList(this::class.java)
fields.forEach {
if (it.name != this::projectId.name && it.name != this::repoName.name && !Modifier.isStatic(it.modifiers)) {
val value = FieldUtils.readField(it, this, true)
FieldUtils.writeField(it, newInstance, value, true)
}
}
return newInstance
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import com.tencent.bkrepo.common.storage.credentials.StorageCredentials
import com.tencent.bkrepo.repository.pojo.repo.RepositoryDetail
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import kotlin.reflect.full.primaryConstructor

/**
* 构件上下文
Expand All @@ -70,6 +71,7 @@ open class ArtifactContext(
/**
* 使用当前实例的属性,拷贝出一个新的[ArtifactContext]实例
* 传入的[repositoryDetail]会替换当前实例的仓库信息
* 适用于CompositeRepository
*/
fun copy(repositoryDetail: RepositoryDetail): ArtifactContext {
val context = this.javaClass.newInstance()
Expand All @@ -78,6 +80,22 @@ open class ArtifactContext(
return context
}

/**
* 使用传入的[repositoryDetail]构造新的[ArtifactContext]实例
* [instantiation]: 实例化方法, 为null时使用主构造函数
* 适用于VirtualRepository: projectId, repoName, storageCredentials均会复制到新的实例, 并构造新的artifactInfo
*/
open fun copy(
repositoryDetail: RepositoryDetail,
instantiation: ((ArtifactInfo) -> ArtifactContext)?
): ArtifactContext {
val artifactInfo = this.artifactInfo.copy(repositoryDetail.projectId, repositoryDetail.name)
val context = instantiation?.invoke(artifactInfo)
?: this::class.primaryConstructor!!.call(repositoryDetail, artifactInfo)
context.contextAttributes = this.contextAttributes
return context
}

/**
* 获取context Attributes map
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import com.tencent.bkrepo.common.security.util.SecurityUtils
import com.tencent.bkrepo.repository.pojo.node.NodeDetail
import com.tencent.bkrepo.repository.pojo.packages.PackageVersion
import com.tencent.bkrepo.repository.pojo.repo.RepositoryDetail
import kotlin.reflect.full.primaryConstructor

/**
* 构件下载context
Expand All @@ -57,6 +58,17 @@ open class ArtifactDownloadContext(
val artifacts = artifacts
var shareUserId: String = StringPool.EMPTY

override fun copy(
repositoryDetail: RepositoryDetail,
instantiation: ((ArtifactInfo) -> ArtifactContext)?
): ArtifactContext {
return super.copy(repositoryDetail) { artifactInfo ->
this::class.primaryConstructor!!.call(
repositoryDetail, artifactInfo, artifacts, this.userId, useDisposition
)
}
}

@Suppress("UNCHECKED_CAST")
fun getInterceptors(): List<DownloadInterceptor<*, NodeDetail>> {
return DownloadInterceptorFactory.buildInterceptors(repo.configuration.settings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ import com.tencent.bkrepo.repository.pojo.repo.RepositoryDetail
*/
open class ArtifactQueryContext(
repo: RepositoryDetail? = null,
artifact: ArtifactInfo? = null,
artifact: ArtifactInfo? = null
) : ArtifactContext(repo, artifact)
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@

package com.tencent.bkrepo.common.artifact.repository.context

import com.tencent.bkrepo.common.artifact.api.ArtifactInfo
import com.tencent.bkrepo.repository.pojo.repo.RepositoryDetail

/**
* 构件删除context
*/
open class ArtifactRemoveContext : ArtifactContext()
open class ArtifactRemoveContext(
repo: RepositoryDetail? = null,
artifact: ArtifactInfo? = null
) : ArtifactContext(repo, artifact)
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ import com.tencent.bkrepo.repository.pojo.repo.RepositoryDetail
*/
open class ArtifactSearchContext(
repo: RepositoryDetail? = null,
artifact: ArtifactInfo? = null,
artifact: ArtifactInfo? = null
) : ArtifactContext(repo, artifact)
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,35 @@ open class ArtifactUploadContext : ArtifactContext {
this.artifactFileMap = artifactFileMap
}

constructor(
repo: RepositoryDetail,
artifactFileMap: ArtifactFileMap,
artifactInfo: ArtifactInfo? = null
) : super(repo, artifactInfo) {
this.artifactFileMap = artifactFileMap
}

override fun copy(
repositoryDetail: RepositoryDetail,
instantiation: ((ArtifactInfo) -> ArtifactContext)?
): ArtifactContext {
return super.copy(repositoryDetail) { artifactInfo ->
if (artifactFile != null) {
javaClass.getConstructor(
RepositoryDetail::class.java,
ArtifactFile::class.java,
ArtifactInfo::class.java
).newInstance(repositoryDetail, artifactFile, artifactInfo)
} else {
javaClass.getConstructor(
RepositoryDetail::class.java,
ArtifactFileMap::class.java,
ArtifactInfo::class.java
).newInstance(repositoryDetail, artifactFileMap, artifactInfo)
}
}
}

/**
* 根据[name]获取构件文件[ArtifactFile]
*
Expand Down

0 comments on commit 09bb2dd

Please sign in to comment.