diff --git a/src/backend/ci/core/artifactory/api-artifactory/src/main/kotlin/com/tencent/devops/artifactory/api/service/OpenArtifactoryResource.kt b/src/backend/ci/core/artifactory/api-artifactory/src/main/kotlin/com/tencent/devops/artifactory/api/service/OpenArtifactoryResource.kt new file mode 100644 index 00000000000..9b90979fb6e --- /dev/null +++ b/src/backend/ci/core/artifactory/api-artifactory/src/main/kotlin/com/tencent/devops/artifactory/api/service/OpenArtifactoryResource.kt @@ -0,0 +1,42 @@ +package com.tencent.devops.artifactory.api.service + +import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_BK_TOKEN +import com.tencent.devops.common.api.auth.AUTH_HEADER_USER_ID +import io.swagger.annotations.Api +import io.swagger.annotations.ApiOperation +import io.swagger.annotations.ApiParam +import javax.ws.rs.Consumes +import javax.ws.rs.HeaderParam +import javax.ws.rs.PUT +import javax.ws.rs.Path +import javax.ws.rs.PathParam +import javax.ws.rs.Produces +import javax.ws.rs.core.MediaType + +@Api(tags = ["OPEN_ARTIFACTORY"], description = "open_artifactory") +@Path("/open/artifactories/") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +interface OpenArtifactoryResource { + + @PUT + @Path("/projects/{projectId}/pipeline/{pipelineId}/buildId/{buildId}/artifactList") + @ApiOperation("更新流水线构件列表") + fun updateArtifactList( + @ApiParam("认证token", required = true) + @HeaderParam(AUTH_HEADER_DEVOPS_BK_TOKEN) + token: String, + @ApiParam("用户id", required = true) + @HeaderParam(AUTH_HEADER_USER_ID) + userId: String, + @ApiParam("项目id", required = true) + @PathParam("projectId") + projectId: String, + @ApiParam("流水线id", required = true) + @PathParam("pipelineId") + pipelineId: String, + @ApiParam("构建id", required = true) + @PathParam("buildId") + buildId: String + ) +} \ No newline at end of file diff --git a/src/backend/ci/core/artifactory/biz-artifactory/src/main/kotlin/com/tencent/devops/artifactory/mq/PipelineBuildArtifactoryListener.kt b/src/backend/ci/core/artifactory/biz-artifactory/src/main/kotlin/com/tencent/devops/artifactory/mq/PipelineBuildArtifactoryListener.kt index 59c605b3f8d..0b95407e42a 100644 --- a/src/backend/ci/core/artifactory/biz-artifactory/src/main/kotlin/com/tencent/devops/artifactory/mq/PipelineBuildArtifactoryListener.kt +++ b/src/backend/ci/core/artifactory/biz-artifactory/src/main/kotlin/com/tencent/devops/artifactory/mq/PipelineBuildArtifactoryListener.kt @@ -34,7 +34,11 @@ import com.tencent.devops.common.client.Client import com.tencent.devops.common.event.dispatcher.pipeline.PipelineEventDispatcher import com.tencent.devops.common.event.listener.pipeline.BaseListener import com.tencent.devops.common.event.pojo.pipeline.PipelineBuildFinishBroadCastEvent +import com.tencent.devops.process.api.service.ServiceBuildResource import com.tencent.devops.process.api.service.ServicePipelineRuntimeResource +import com.tencent.devops.process.utils.PIPELINE_START_PARENT_BUILD_ID +import com.tencent.devops.process.utils.PIPELINE_START_PARENT_PIPELINE_ID +import com.tencent.devops.process.utils.PIPELINE_START_PARENT_PROJECT_ID import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Component @@ -58,6 +62,44 @@ class PipelineBuildArtifactoryListener @Autowired constructor( val buildId = event.buildId val pipelineId = event.pipelineId + val (parentProjectId, parentPipelineId, parentBuildId) = getParentPipelineVars( + userId, projectId, pipelineId, buildId + ) + + updateArtifactList(userId, projectId, pipelineId, buildId) + updateArtifactList(userId, parentProjectId, parentPipelineId, parentBuildId) + } + + private fun getParentPipelineVars( + userId: String, + projectId: String, + pipelineId: String, + buildId: String + ): Triple { + val parentPipelineVars = client.get(ServiceBuildResource::class).getBuildVariableValue( + userId = userId, + projectId = projectId, + pipelineId = pipelineId, + buildId = buildId, + variableNames = listOf( + PIPELINE_START_PARENT_PROJECT_ID, + PIPELINE_START_PARENT_PIPELINE_ID, + PIPELINE_START_PARENT_BUILD_ID + ) + ).data!! + logger.info("[$pipelineId|$buildId] get parent pipeline vars: $parentPipelineVars") + val parentProjectId = parentPipelineVars[PIPELINE_START_PARENT_PROJECT_ID].toString() + val parentPipelineId = parentPipelineVars[PIPELINE_START_PARENT_PIPELINE_ID].toString() + val parentBuildId = parentPipelineVars[PIPELINE_START_PARENT_BUILD_ID].toString() + return Triple(parentProjectId, parentPipelineId, parentBuildId) + } + + private fun updateArtifactList( + userId: String, + projectId: String, + pipelineId: String, + buildId: String, + ) { val artifactList: List = try { pipelineBuildArtifactoryService.getArtifactList(userId, projectId, pipelineId, buildId) } catch (ignored: Throwable) { diff --git a/src/backend/ci/core/artifactory/biz-artifactory/src/main/kotlin/com/tencent/devops/artifactory/resources/OpenArtifactoryResourceImpl.kt b/src/backend/ci/core/artifactory/biz-artifactory/src/main/kotlin/com/tencent/devops/artifactory/resources/OpenArtifactoryResourceImpl.kt new file mode 100644 index 00000000000..1b45ec2c555 --- /dev/null +++ b/src/backend/ci/core/artifactory/biz-artifactory/src/main/kotlin/com/tencent/devops/artifactory/resources/OpenArtifactoryResourceImpl.kt @@ -0,0 +1,49 @@ +package com.tencent.devops.artifactory.resources + +import com.tencent.devops.artifactory.api.service.OpenArtifactoryResource +import com.tencent.devops.artifactory.service.PipelineBuildArtifactoryService +import com.tencent.devops.common.api.constant.CommonMessageCode +import com.tencent.devops.common.api.exception.ErrorCodeException +import com.tencent.devops.common.api.util.JsonUtil +import com.tencent.devops.common.client.Client +import com.tencent.devops.common.client.ClientTokenService +import com.tencent.devops.process.api.service.ServicePipelineRuntimeResource +import org.slf4j.LoggerFactory + +class OpenArtifactoryResourceImpl( + private val clientTokenService: ClientTokenService, + private val pipelineBuildArtifactoryService: PipelineBuildArtifactoryService, + private val client: Client +) : OpenArtifactoryResource { + + override fun updateArtifactList( + token: String, + userId: String, + projectId: String, + pipelineId: String, + buildId: String + ) { + val validateTokenFlag = clientTokenService.checkToken(null, token) + if (!validateTokenFlag) { + throw ErrorCodeException( + errorCode = CommonMessageCode.PARAMETER_IS_INVALID, + params = arrayOf(token) + ) + } + + val artifactList = pipelineBuildArtifactoryService.getArtifactList(userId, projectId, pipelineId, buildId) + logger.info("[$pipelineId]|getArtifactList-$buildId artifact: ${JsonUtil.toJson(artifactList)}") + val result = client.get(ServicePipelineRuntimeResource::class).updateArtifactList( + userId = userId, + projectId = projectId, + pipelineId = pipelineId, + buildId = buildId, + artifactoryFileList = artifactList + ) + logger.info("[$buildId]|update artifact result: ${result.status} ${result.message}") + } + + companion object { + private val logger = LoggerFactory.getLogger(OpenArtifactoryResourceImpl::class.java) + } +} \ No newline at end of file