Skip to content

Commit

Permalink
bug: 构建历史中没有流水线完成后上传的构件 TencentBlueKing#6538
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoxuwan committed Apr 15, 2022
1 parent e26500d commit f189b67
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, String, String> {
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<FileInfo> = try {
pipelineBuildArtifactoryService.getArtifactList(userId, projectId, pipelineId, buildId)
} catch (ignored: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit f189b67

Please sign in to comment.