Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【PAC】feat:支持从代码库维度查看对应的代码库事件 #8122 #9725

Merged
merged 12 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ enum class CodeEventType {
*/
val CODE_GITHUB_EVENTS = listOf(
PUSH,
MERGE_REQUEST,
MERGE_REQUEST_ACCEPT,
PULL_REQUEST,
CREATE,
REVIEW,
ISSUES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import com.tencent.devops.common.api.util.MessageUtil
import com.tencent.devops.common.client.Client
import com.tencent.devops.common.redis.RedisOperation
import com.tencent.devops.common.service.config.CommonConfig
import com.tencent.devops.common.service.utils.CommonUtils
import com.tencent.devops.common.service.utils.CookieUtil
import com.tencent.devops.common.service.utils.SpringContextUtil
import com.tencent.devops.common.web.service.ServiceLocaleResource
Expand Down Expand Up @@ -223,9 +222,8 @@ object I18nUtil {
}
}

fun I18Variable.getCodeLanMessage(language: String? = CommonUtils.ZH_CN): String {
fun I18Variable.getCodeLanMessage(): String {
return getCodeLanMessage(
language = language,
messageCode = code,
params = params.toTypedArray()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@ interface OpScmWebhookResource {
@ApiOperation("更新webhook 事件信息")
@PUT
@Path("/updateWebhookEventInfo")
fun updateWebhookEventInfo(): Result<Boolean>
fun updateWebhookEventInfo(
@ApiParam("待更新的项目ID", required = false)
@QueryParam("projectId")
projectId: String?,
@ApiParam("待更新代码库平台项目名", required = false)
projectNames: List<String>?
): Result<Boolean>
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ class PipelineTriggerDetailBuilder {
buildId = buildId,
buildNum = buildNum,
reason = reason,
reasonDetailList = reasonDetailList
reasonDetailList = if (status == PipelineTriggerStatus.SUCCEED.name) {
null
} else {
reasonDetailList
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,5 @@ data class PipelineTriggerEventVo(
@ApiModelProperty("原因")
var reason: String? = null,
@ApiModelProperty("失败原因详情", required = false)
var reasonDetailList: List<String>? = null,
@ApiModelProperty("失败原因", required = false)
var failReason: String = ""
var reasonDetailList: List<String>? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import com.tencent.devops.common.web.utils.I18nUtil
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty

@Suppress("IMPLICIT_CAST_TO_ANY")
@ApiModel("流水线触发类型")
enum class PipelineTriggerType {
// WEB_HOOK 触发
Expand Down Expand Up @@ -109,5 +108,18 @@ enum class PipelineTriggerType {
null
}
}

fun webhookTrigger(triggerType: String): Boolean {
return listOf(
CODE_SVN.name,
CODE_GIT.name,
CODE_GITLAB.name,
GITHUB.name,
CODE_TGIT.name,
CODE_P4.name
).contains(
triggerType
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ class PipelineWebhookDao {

fun getByProjectNameAndType(
dslContext: DSLContext,
projectName: String,
projectNames: List<String>,
repositoryType: String
): List<WebhookTriggerPipeline>? {
with(T_PIPELINE_WEBHOOK) {
return dslContext.select(PROJECT_ID, PIPELINE_ID).from(this)
.where(PROJECT_NAME.eq(projectName))
.where(PROJECT_NAME.`in`(projectNames))
.and(REPOSITORY_TYPE.eq(repositoryType))
.and(DELETE.eq(false))
.groupBy(PROJECT_ID, PIPELINE_ID)
Expand All @@ -176,6 +176,24 @@ class PipelineWebhookDao {
}
}

fun get(
dslContext: DSLContext,
projectId: String,
pipelineId: String,
repositoryHashId: String,
eventType: String
): PipelineWebhook? {
with(T_PIPELINE_WEBHOOK) {
val record = dslContext.selectFrom(this)
.where(PROJECT_ID.eq(projectId))
.and(PIPELINE_ID.eq(pipelineId))
.and(REPOSITORY_HASH_ID.eq(repositoryHashId))
.and(EVENT_TYPE.eq(eventType))
.fetchAny()
return record?.map { convert(record) }
}
}

fun listTriggerPipeline(
dslContext: DSLContext,
projectId: String,
Expand Down Expand Up @@ -272,14 +290,31 @@ class PipelineWebhookDao {
}?.map { convert(it) }
}

fun listPipelines(
fun groupPipelineList(
dslContext: DSLContext,
projectId: String?,
projectNames: List<String>?,
offset: Int,
limit: Int
): List<WebhookTriggerPipeline> {
return with(T_PIPELINE_WEBHOOK) {
dslContext.select(PROJECT_ID, PIPELINE_ID).from(this)
.where(DELETE.eq(false))
.let {
if (projectId.isNullOrBlank()) {
it
} else {
it.and(PROJECT_ID.eq(projectId))
}
}
.let {
if (projectNames.isNullOrEmpty()) {
it
} else {
it.and(PROJECT_NAME.`in`(projectNames))
}
}
.groupBy(PROJECT_ID, PIPELINE_ID)
.limit(offset, limit)
.fetch().map {
WebhookTriggerPipeline(
Expand Down Expand Up @@ -311,6 +346,21 @@ class PipelineWebhookDao {
}
}

fun updateProjectName(
dslContext: DSLContext,
projectId: String,
projectName: String,
id: Long
) {
with(T_PIPELINE_WEBHOOK) {
dslContext.update(this)
.set(PROJECT_NAME, projectName)
.where(PROJECT_ID.eq(projectId))
.and(ID.eq(id))
.execute()
}
}

companion object {
private val logger = LoggerFactory.getLogger(PipelineWebhookDao::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ class OpScmWebhookResourceImpl(
return Result(true)
}

override fun updateWebhookEventInfo(): Result<Boolean> {
pipelineWebhookUpgradeService.updateWebhookEventInfo()
override fun updateWebhookEventInfo(
projectId: String?,
projectNames: List<String>?
): Result<Boolean> {
pipelineWebhookUpgradeService.updateWebhookEventInfo(
projectId = projectId,
projectNames = projectNames
)
return Result(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ class PipelineTriggerEventDao {
PROJECT_ID,
EVENT_ID,
count().`as`("total"),
count(`when`(STATUS.eq(PipelineTriggerStatus.SUCCEED.name), 1))
count(`when`(STATUS.eq(PipelineTriggerStatus.SUCCEED.name), 1)).`as`("success")
)
.from(this)
.where(conditions)
Expand Down Expand Up @@ -468,7 +468,7 @@ class PipelineTriggerEventDao {
.where(PROJECT_ID.eq(projectId))
.and(REQUEST_ID.eq(requestId))
.and(EVENT_SOURCE.eq(eventSource))
.fetchOne()
.fetchAny()
}
return record?.let { convertEvent(it) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,25 @@ class PipelineWebhookService @Autowired constructor(
element: Element,
variables: Map<String, String>
) {
val (repositoryType, eventType, repositoryConfig) =
val (scmType, eventType, repositoryConfig) =
RepositoryConfigUtils.buildWebhookConfig(element, variables)
logger.info("$pipelineId| Trying to add the $repositoryType web hook for repo($repositoryConfig)")
logger.info("$pipelineId| Trying to add the $scmType web hook for repo($repositoryConfig)")
val repository = registerWebhook(
projectId = projectId,
repositoryType = repositoryType,
scmType = scmType,
repositoryConfig = repositoryConfig,
codeEventType = eventType,
elementVersion = element.version
) ?: return
val pipelineWebhook = PipelineWebhook(
projectId = projectId,
pipelineId = pipelineId,
repositoryType = repositoryType,
repositoryType = scmType,
repoType = repositoryConfig.repositoryType,
repoHashId = repositoryConfig.repositoryHashId,
repoName = repositoryConfig.repositoryName,
taskId = element.id,
projectName = getProjectName(repository.projectName),
projectName = getProjectName(repositoryType = scmType.name, projectName = repository.projectName),
repositoryHashId = repository.repoHashId,
eventType = eventType?.name ?: "",
externalId = repository.getExternalId()
Expand All @@ -159,7 +159,7 @@ class PipelineWebhookService @Autowired constructor(

private fun registerWebhook(
projectId: String,
repositoryType: ScmType,
scmType: ScmType,
repositoryConfig: RepositoryConfig,
codeEventType: CodeEventType?,
elementVersion: String
Expand All @@ -172,7 +172,7 @@ class PipelineWebhookService @Autowired constructor(
)
try {
redisLock.lock()
return when (repositoryType) {
return when (scmType) {
ScmType.CODE_GIT ->
scmProxyService.addGitWebhook(
projectId = projectId,
Expand Down Expand Up @@ -292,7 +292,7 @@ class PipelineWebhookService @Autowired constructor(
fun getTriggerPipelines(name: String, repositoryType: String): List<WebhookTriggerPipeline> {
return pipelineWebhookDao.getByProjectNameAndType(
dslContext = dslContext,
projectName = getProjectName(name),
projectNames = getTriggerProjectName(repositoryType = repositoryType, projectName = name),
repositoryType = repositoryType
) ?: emptyList()
}
Expand All @@ -310,13 +310,26 @@ class PipelineWebhookService @Autowired constructor(
) ?: emptyList()
}

fun getProjectName(projectName: String): String {
// 如果项目名是三层的,比如a/b/c,那对应的rep_name是b
fun getProjectName(repositoryType: String, projectName: String): String {
val repoSplit = projectName.split("/")
// 如果代码库是svn类型,并且项目名是三层的,比如a/b/c,那对应的rep_name是b,工蜂svn webhook返回的rep_name结构
if (repositoryType == ScmType.CODE_SVN.name && repoSplit.size == 3) {
return repoSplit[1].trim()
}
return projectName
}

fun getTriggerProjectName(repositoryType: String, projectName: String): List<String> {
val repoSplit = projectName.split("/")
if (repoSplit.size != 3) {
return projectName
return listOf(projectName)
}
// 兼容历史数据,如果代码库类型是git,并且路径是三层,数据库只保留了第二层,导致查询的webhook数据量很大
return if (repositoryType == ScmType.CODE_SVN.name) {
listOf(repoSplit[1].trim())
} else {
listOf(repoSplit[1].trim(), projectName)
}
return repoSplit[1].trim()
}

fun listWebhook(
Expand Down Expand Up @@ -349,4 +362,19 @@ class PipelineWebhookService @Autowired constructor(
limit = limit.limit
) ?: emptyList()
}

fun get(
projectId: String,
pipelineId: String,
repositoryHashId: String,
eventType: String
): PipelineWebhook? {
return pipelineWebhookDao.get(
dslContext = dslContext,
projectId = projectId,
pipelineId = pipelineId,
repositoryHashId = repositoryHashId,
eventType = eventType
)
}
}
Loading