Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into rbac
Browse files Browse the repository at this point in the history
  • Loading branch information
bkci-bot committed Dec 1, 2023
2 parents f7550bf + ab65eb4 commit 9068f65
Show file tree
Hide file tree
Showing 17 changed files with 133 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ data class CodeGithubWebHookTriggerElement(
)
}

CodeEventType.REVIEW -> {
listOf(
TriggerElementPropUtils.selector(name = "includeCrState", value = includeCrState)
)
}

CodeEventType.ISSUES -> {
listOf(
TriggerElementPropUtils.selector(name = "includeIssueAction", value = includeIssueAction)
)
}

CodeEventType.NOTE -> {
listOf(
TriggerElementPropUtils.selector(name = "includeNoteTypes", value = includeNoteTypes),
TriggerElementPropUtils.vuexInput(name = "includeNoteComment", value = includeNoteComment)
)
}

else -> emptyList()
}
return props.filterNotNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ interface OpScmWebhookResource {
fun updateWebhookEventInfo(
@ApiParam("待更新的项目ID", required = false)
@QueryParam("projectId")
projectId: String?,
@ApiParam("待更新代码库平台项目名", required = false)
projectNames: List<String>?
projectId: String?
): Result<Boolean>
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ data class PipelineWebhook(
var eventType: String? = null,
@ApiModelProperty("代码库hashId,插件配置解析后的代码库ID", required = false)
var repositoryHashId: String? = null,
@ApiModelProperty("事件源外联Id", required = false)
var externalId: String? = null
@ApiModelProperty("代码库平台ID", required = false)
var externalId: String? = null,
@ApiModelProperty("代码库平台仓库名", required = false)
var externalName: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class PipelineWebhookDao {
TASK_ID,
EVENT_TYPE,
REPOSITORY_HASH_ID,
EXTERNAL_ID
EXTERNAL_ID,
EXTERNAL_NAME
)
.values(
projectId,
Expand All @@ -71,7 +72,8 @@ class PipelineWebhookDao {
taskId,
eventType,
repositoryHashId,
externalId
externalId,
externalName
)
.onDuplicateKeyUpdate()
.set(REPO_TYPE, repoType?.name)
Expand All @@ -81,6 +83,7 @@ class PipelineWebhookDao {
.set(EVENT_TYPE, eventType)
.set(REPOSITORY_HASH_ID, repositoryHashId)
.set(EXTERNAL_ID, externalId)
.set(EXTERNAL_NAME, externalName)
.execute()
}
}
Expand Down Expand Up @@ -158,12 +161,12 @@ class PipelineWebhookDao {

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

fun listWebhookPipeline(
dslContext: DSLContext,
projectName: String,
repositoryType: String,
eventType: String
): List<PipelineWebhook>? {
with(T_PIPELINE_WEBHOOK) {
return dslContext.selectFrom(this)
.where(PROJECT_NAME.eq(projectName))
.and(REPOSITORY_TYPE.eq(repositoryType))
.and(EVENT_TYPE.eq(eventType))
.and(DELETE.eq(false))
.fetch().map { convert(it) }
}
}

fun updateProjectNameAndTaskId(
dslContext: DSLContext,
projectId: String,
Expand Down Expand Up @@ -293,7 +280,6 @@ class PipelineWebhookDao {
fun groupPipelineList(
dslContext: DSLContext,
projectId: String?,
projectNames: List<String>?,
offset: Int,
limit: Int
): List<WebhookTriggerPipeline> {
Expand All @@ -307,13 +293,6 @@ class PipelineWebhookDao {
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 {
Expand All @@ -330,6 +309,7 @@ class PipelineWebhookDao {
repositoryHashId: String?,
eventType: String,
externalId: String?,
externalName: String?,
pipelineId: String,
projectId: String,
taskId: String
Expand All @@ -339,28 +319,14 @@ class PipelineWebhookDao {
.set(REPOSITORY_HASH_ID, repositoryHashId)
.set(EVENT_TYPE, eventType)
.set(EXTERNAL_ID, externalId)
.set(EXTERNAL_NAME, externalName)
.where(PROJECT_ID.eq(projectId))
.and(PIPELINE_ID.eq(pipelineId))
.and(TASK_ID.eq(taskId))
.execute()
}
}

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 @@ -778,10 +778,10 @@ class PipelineRepositoryService constructor(
)
if (info != null && srcTemplate != null) {
info.templateInfo = TemplateInfo(
templateId = srcTemplate.id,
templateId = template.templateId,
templateName = srcTemplate.templateName,
version = srcTemplate.version,
versionName = srcTemplate.versionName,
version = template.version,
versionName = template.versionName,
instanceType = PipelineInstanceTypeEnum.valueOf(template.instanceType)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ class OpScmWebhookResourceImpl(
}

override fun updateWebhookEventInfo(
projectId: String?,
projectNames: List<String>?
projectId: String?
): Result<Boolean> {
pipelineWebhookUpgradeService.updateWebhookEventInfo(
projectId = projectId,
projectNames = projectNames
projectId = projectId
)
return Result(true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ class PipelineWebhookService @Autowired constructor(
repoHashId = repositoryConfig.repositoryHashId,
repoName = repositoryConfig.repositoryName,
taskId = element.id,
projectName = getProjectName(repositoryType = scmType.name, projectName = repository.projectName),
projectName = getProjectName(repository.projectName),
repositoryHashId = repository.repoHashId,
eventType = eventType?.name ?: "",
externalId = repository.getExternalId()
externalId = repository.getExternalId(),
externalName = getExternalName(scmType = scmType, repository.projectName)
)
pipelineWebhookDao.save(
dslContext = dslContext,
Expand Down Expand Up @@ -292,7 +293,7 @@ class PipelineWebhookService @Autowired constructor(
fun getTriggerPipelines(name: String, repositoryType: String): List<WebhookTriggerPipeline> {
return pipelineWebhookDao.getByProjectNameAndType(
dslContext = dslContext,
projectNames = getTriggerProjectName(repositoryType = repositoryType, projectName = name),
projectName = getProjectName(name),
repositoryType = repositoryType
) ?: emptyList()
}
Expand All @@ -310,26 +311,25 @@ class PipelineWebhookService @Autowired constructor(
) ?: emptyList()
}

fun getProjectName(repositoryType: String, projectName: String): String {
fun getProjectName(projectName: String): String {
// 如果项目名是三层的,比如a/b/c,那对应的rep_name是b
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()
if (repoSplit.size != 3) {
return projectName
}
return projectName
return repoSplit[1].trim()
}

fun getTriggerProjectName(repositoryType: String, projectName: String): List<String> {
/**
* 获取代码库平台仓库名
*/
fun getExternalName(scmType: ScmType, projectName: String): String {
val repoSplit = projectName.split("/")
if (repoSplit.size != 3) {
return listOf(projectName)
}
// 兼容历史数据,如果代码库类型是git,并且路径是三层,数据库只保留了第二层,导致查询的webhook数据量很大
return if (repositoryType == ScmType.CODE_SVN.name) {
listOf(repoSplit[1].trim())
} else {
listOf(repoSplit[1].trim(), projectName)
// 如果代码库是svn类型,并且项目名是三层的,比如a/b/c,那对应的rep_name是b,工蜂svn webhook返回的rep_name结构
if (scmType == ScmType.CODE_SVN && repoSplit.size == 3) {
return repoSplit[1].trim()
}
return projectName
}

fun listWebhook(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,7 @@ class PipelineWebhookUpgradeService(
pipelineWebhookDao.updateProjectNameAndTaskId(
dslContext = dslContext,
projectId = projectId,
projectName = pipelineWebhookService.getProjectName(
repositoryType = repo.getScmType().name,
projectName = repo.projectName
),
projectName = pipelineWebhookService.getProjectName(repo.projectName),
taskId = element.id!!,
id = id!!
)
Expand Down Expand Up @@ -286,15 +283,14 @@ class PipelineWebhookUpgradeService(
}

fun updateWebhookEventInfo(
projectId: String?,
projectNames: List<String>?
projectId: String?
) {
val startTime = System.currentTimeMillis()
val threadPoolExecutor = Executors.newSingleThreadExecutor()
threadPoolExecutor.submit {
logger.info("PipelineWebhookService:begin updateWebhookEventInfo threadPoolExecutor")
try {
updateWebhookEventInfoTask(projectId = projectId, projectNames = projectNames)
updateWebhookEventInfoTask(projectId = projectId)
} catch (ignored: Exception) {
logger.warn("PipelineWebhookService:updateWebhookEventInfo failed", ignored)
} finally {
Expand All @@ -304,10 +300,7 @@ class PipelineWebhookUpgradeService(
}
}

private fun updateWebhookEventInfoTask(
projectId: String?,
projectNames: List<String>?
) {
private fun updateWebhookEventInfoTask(projectId: String?) {
var offset = 0
val limit = 1000
val repoCache = mutableMapOf<String, Optional<Repository>>()
Expand All @@ -317,7 +310,6 @@ class PipelineWebhookUpgradeService(
val pipelines = pipelineWebhookDao.groupPipelineList(
dslContext = dslContext,
projectId = projectId,
projectNames = projectNames,
limit = limit,
offset = offset
)
Expand All @@ -333,6 +325,7 @@ class PipelineWebhookUpgradeService(
} while (pipelines.size == 1000)
}

@Suppress("CyclomaticComplexMethod", "ComplexMethod")
private fun updatePipelineEventInfo(
projectId: String,
pipelineId: String,
Expand Down Expand Up @@ -377,16 +370,11 @@ class PipelineWebhookUpgradeService(
}
val repository = getAndCacheRepo(projectId, webhookRepositoryConfig, repoCache)

// 历史原因,假如git的projectName有三个,如aaa/bbb/ccc,只读取了bbb,导致触发时获取的流水线数量很多,修复数据
// 历史原因,假如git的projectName有三个,如aaa/bbb/ccc,只读取了bbb,导致触发时获取的流水线数量很多,记录日志
if (repository != null && webhook.projectName != repository.projectName) {
logger.info(
"webhook projectName different from repo projectName|webhook:$webhook|repo:$repository"
)
pipelineWebhookDao.updateProjectName(
dslContext = dslContext,
projectId = projectId,
projectName = repository.projectName,
id = webhook.id!!
"webhook projectName different from repo projectName|$projectId|$pipelineId|" +
"webhook:${webhook.projectName}|repo:${repository.projectName}"
)
}
val repositoryHashId = when {
Expand All @@ -396,10 +384,19 @@ class PipelineWebhookUpgradeService(

else -> null
}
val externalName = when {
repository != null -> pipelineWebhookService.getExternalName(
scmType = repository.getScmType(),
projectName = repository.projectName
)

else -> webhook.projectName
}
pipelineWebhookDao.updateWebhookEventInfo(
dslContext = dslContext,
eventType = elementEventType?.name ?: "",
externalId = repository?.getExternalId(),
externalName = externalName,
projectId = projectId,
pipelineId = pipelineId,
taskId = webhook.taskId!!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ interface UserRepositoryResource {
@ApiParam("代码库哈希ID", required = true)
@PathParam("repositoryHashId")
repositoryHashId: String,
@ApiParam("事件类型", required = false)
@QueryParam("eventType")
eventType: String?,
@ApiParam("触发条件MD5", required = false)
@QueryParam("triggerConditionMd5")
triggerConditionMd5: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ data class RepoTriggerRefVo(
val atomCode: String,
@ApiModelProperty("触发类型")
val triggerType: String,
@ApiModelProperty("事件类型")
val eventType: String,
@ApiModelProperty("事件类型描述,根据[eventType]进行国际化")
val eventTypeDesc: String,
@ApiModelProperty("插件参数")
val taskParams: Map<String, Any>,
@ApiModelProperty("触发条件")
Expand All @@ -48,5 +48,7 @@ data class RepoTriggerRefVo(
val triggerConditionMd5: String?,
@ApiModelProperty("流水线引用数量")
val pipelineRefCount: Int,
val atomLogo: String? = null
val atomLogo: String? = null,
@ApiModelProperty("事件类型key")
val eventType: String
)
Loading

0 comments on commit 9068f65

Please sign in to comment.