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

feat:MR 事件触发器支持 WIP #10683 #10760

Merged
merged 8 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -160,7 +160,8 @@ class TriggerTransfer @Autowired(required = false) constructor(
repositoryName = triggerOn.repoName,
enableThirdFilter = !mr.custom?.url.isNullOrBlank(),
thirdUrl = mr.custom?.url,
thirdSecretToken = mr.custom?.credentials
thirdSecretToken = mr.custom?.credentials,
skipWip = mr.skipWip
).checkTriggerElementEnable(mr.enable).apply {
version = "2.*"
}
Expand Down Expand Up @@ -296,7 +297,8 @@ class TriggerTransfer @Autowired(required = false) constructor(
custom = if (git.enableThirdFilter == true) CustomFilter(
url = git.thirdUrl,
credentials = git.thirdSecretToken
) else null
) else null,
skipWip = git.skipWip
)
CodeEventType.MERGE_REQUEST_ACCEPT ->
throw PipelineTransferException(
Expand Down Expand Up @@ -463,7 +465,8 @@ class TriggerTransfer @Autowired(required = false) constructor(
),
eventType = CodeEventType.MERGE_REQUEST,
repositoryType = repositoryType,
repositoryName = triggerOn.repoName
repositoryName = triggerOn.repoName,
skipWip = mr.skipWip
)
)
).checkTriggerElementEnable(mr.enable).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ data class WebHookTriggerElementChanger(
@get:Schema(title = "第三方应用鉴权token")
val thirdSecretToken: String? = null,
@get:Schema(title = "是否启用插件")
val enable: Boolean
val enable: Boolean,
@get:Schema(title = "跳过WIP")
val skipWip: Boolean? = false
) {
constructor(data: CodeGitWebHookTriggerElement) : this(
name = data.name,
Expand Down Expand Up @@ -133,7 +135,8 @@ data class WebHookTriggerElementChanger(
enableThirdFilter = data.enableThirdFilter,
thirdUrl = data.thirdUrl,
thirdSecretToken = data.thirdSecretToken,
enable = data.isElementEnable()
enable = data.isElementEnable(),
skipWip = data.skipWip
)

constructor(data: CodeTGitWebHookTriggerElement) : this(
Expand Down Expand Up @@ -165,7 +168,8 @@ data class WebHookTriggerElementChanger(
includeMrAction = data.data.input.includeMrAction,
includePushAction = data.data.input.includePushAction,
enableThirdFilter = data.data.input.enableThirdFilter,
enable = data.isElementEnable()
enable = data.isElementEnable(),
skipWip = data.data.input.skipWip
)

constructor(data: CodeGithubWebHookTriggerElement) : this(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,9 @@ data class MrRule(

@get:Schema(title = "custom-filter")
@JsonProperty("custom-filter")
val custom: CustomFilter? = null
val custom: CustomFilter? = null,

@JsonProperty("skip-wip")
@get:Schema(title = "skip-wip")
var skipWip: Boolean? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@
"type" : "string"
}
}
},
"skip-wip" : {
"type" : "boolean"
}
}
} ]
Expand Down Expand Up @@ -943,6 +946,9 @@
"type" : "string"
}
}
},
"skip-wip" : {
"type" : "boolean"
}
}
} ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ data class CodeGitWebHookTriggerElement(
@get:Schema(title = "第三方应用地址")
val thirdUrl: String? = null,
@get:Schema(title = "第三方应用鉴权token")
val thirdSecretToken: String? = null
val thirdSecretToken: String? = null,
@get:Schema(title = "跳过WIP")
val skipWip: Boolean? = false
) : WebHookTriggerElement(name, id, status) {
companion object {
const val classType = "codeGitWebHookTrigger"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,7 @@ data class CodeTGitWebHookTriggerInput(
@get:Schema(title = "push事件action")
val includePushAction: List<String>? = null,
@get:Schema(title = "是否启用第三方过滤")
val enableThirdFilter: Boolean? = false
val enableThirdFilter: Boolean? = false,
@get:Schema(title = "跳过WIP")
val skipWip: Boolean? = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ object WebhookI18nConstants {
// Github Push操作类型不匹配
const val PUSH_ACTION_NOT_MATCH = "bkRepoTriggerPushActionNotMatch"

// WIP阶段不触发
const val MR_SKIP_WIP = "bkRepoTriggerSkipWipNotMatch"

// 事件回放
const val EVENT_REPLAY_DESC = "bkEventReplayDesc"
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,7 @@ data class WebHookParams(
var thirdUrl: String? = null,
var thirdSecretToken: String? = null,
// 插件版本
var version: String? = null
var version: String? = null,
// 跳过WIP
var skipWip: Boolean? = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,32 @@

package com.tencent.devops.common.webhook.service.code.filter

class SkipCiFilter(
import org.slf4j.LoggerFactory

class KeywordSkipFilter(
private val pipelineId: String,
private val triggerOnMessage: String?
private val keyWord: List<String>,
private val enable: Boolean? = true,
private val triggerOnMessage: String?,
private val failedReason: String = ""
) : WebhookFilter {

companion object {
private const val SKIP_CI = "[skip ci]"
private val logger = LoggerFactory.getLogger(KeywordSkipFilter::class.java)
val KEYWORD_SKIP_CI = listOf("[skip ci]")
val KEYWORD_SKIP_WIP = listOf("[WIP]", "WIP")
}

override fun doFilter(response: WebhookFilterResponse): Boolean {
return triggerOnMessage?.contains(SKIP_CI) != true
logger.info("$pipelineId|triggerOnMessage:$triggerOnMessage|skipWord:$keyWord|enable:$enable")
return when {
enable == false -> true
keyWord.any { triggerOnMessage?.contains(it) == true } -> {
response.failedReason = failedReason
false
}

else -> true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ import com.tencent.devops.common.webhook.service.code.EventCacheService
import com.tencent.devops.common.webhook.service.code.filter.BranchFilter
import com.tencent.devops.common.webhook.service.code.filter.ContainsFilter
import com.tencent.devops.common.webhook.service.code.filter.PathFilterFactory
import com.tencent.devops.common.webhook.service.code.filter.SkipCiFilter
import com.tencent.devops.common.webhook.service.code.filter.KeywordSkipFilter
import com.tencent.devops.common.webhook.service.code.filter.KeywordSkipFilter.Companion.KEYWORD_SKIP_CI
import com.tencent.devops.common.webhook.service.code.filter.KeywordSkipFilter.Companion.KEYWORD_SKIP_WIP
import com.tencent.devops.common.webhook.service.code.filter.ThirdFilter
import com.tencent.devops.common.webhook.service.code.filter.UserFilter
import com.tencent.devops.common.webhook.service.code.filter.WebhookFilter
Expand Down Expand Up @@ -192,6 +194,13 @@ class TGitMrTriggerHandler(
webHookParams: WebHookParams
): List<WebhookFilter> {
with(webHookParams) {
val wipFilter = KeywordSkipFilter(
pipelineId = pipelineId,
enable = skipWip,
keyWord = KEYWORD_SKIP_WIP,
triggerOnMessage = getMessage(event),
failedReason = I18Variable(WebhookI18nConstants.MR_SKIP_WIP).toJsonStr()
)
val userId = getUsername(event)
val userFilter = UserFilter(
pipelineId = pipelineId,
Expand Down Expand Up @@ -237,8 +246,9 @@ class TGitMrTriggerHandler(
params = listOf(sourceBranch)
).toJsonStr()
)
val skipCiFilter = SkipCiFilter(
val skipCiFilter = KeywordSkipFilter(
pipelineId = pipelineId,
keyWord = KEYWORD_SKIP_CI,
triggerOnMessage = event.object_attributes.last_commit.message
)
val actionFilter = ContainsFilter(
Expand Down Expand Up @@ -309,7 +319,7 @@ class TGitMrTriggerHandler(
callbackCircuitBreakerRegistry = callbackCircuitBreakerRegistry
)
return listOf(
userFilter, targetBranchFilter,
wipFilter, userFilter, targetBranchFilter,
sourceBranchFilter, skipCiFilter, pathFilter,
commitMessageFilter, actionFilter, thirdFilter
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ import com.tencent.devops.common.webhook.service.code.GitScmService
import com.tencent.devops.common.webhook.service.code.filter.BranchFilter
import com.tencent.devops.common.webhook.service.code.filter.ContainsFilter
import com.tencent.devops.common.webhook.service.code.filter.PathFilterFactory
import com.tencent.devops.common.webhook.service.code.filter.SkipCiFilter
import com.tencent.devops.common.webhook.service.code.filter.KeywordSkipFilter
import com.tencent.devops.common.webhook.service.code.filter.KeywordSkipFilter.Companion.KEYWORD_SKIP_CI
import com.tencent.devops.common.webhook.service.code.filter.ThirdFilter
import com.tencent.devops.common.webhook.service.code.filter.UserFilter
import com.tencent.devops.common.webhook.service.code.filter.WebhookFilter
Expand Down Expand Up @@ -216,8 +217,9 @@ class TGitPushTriggerHandler(
params = listOf(triggerOnBranchName)
).toJsonStr()
)
val skipCiFilter = SkipCiFilter(
val skipCiFilter = KeywordSkipFilter(
pipelineId = pipelineId,
keyWord = KEYWORD_SKIP_CI,
triggerOnMessage = event.commits?.get(0)?.message ?: ""
)
val commits = event.commits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class GitWebhookElementParams : ScmWebhookElementParams<CodeGitWebHookTriggerEle
params.enableThirdFilter = element.enableThirdFilter
params.thirdUrl = EnvUtils.parseEnv(element.thirdUrl ?: "", variables)
params.thirdSecretToken = EnvUtils.parseEnv(element.thirdSecretToken ?: "", variables)
params.skipWip = element.skipWip
return params
}

Expand Down
1 change: 1 addition & 0 deletions support-files/i18n/process/message_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ bkNotSubPipelineExecutePermissionErrorMessage=Stage[{0}]-Job[{1}]-Step[{2}]:<a t
bkPipelineElementCheckFailedMessage=Pipeline plugin validity check failed
bkRepoTriggerMrActionNotMatch=Merge request action does not match
bkRepoTriggerPushActionNotMatch=Push action does not match
bkRepoTriggerSkipWipNotMatch=Do not trigger during WIP stage
bkPipelineRunConditionResult=The calculation result of custom condition [{0}] is: {1}
bkPipelineRunConditionNotMatch=, which is not met and will be skipped.
bkPipelineRunConditionWithError=The calculation of custom condition has error: {0}
1 change: 1 addition & 0 deletions support-files/i18n/process/message_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ bkNotSubPipelineExecutePermissionErrorMessage=Stage[{0}]-Job[{1}]-Step[{2}]:<a t
bkPipelineElementCheckFailedMessage=流水线插件有效性校验失败
bkRepoTriggerMrActionNotMatch=Merge request 动作不满足触发条件
bkRepoTriggerPushActionNotMatch=Push 动作不满足触发条件
bkRepoTriggerSkipWipNotMatch=WIP阶段不触发
bkPipelineRunConditionResult=自定义条件[{0}]的计算结果为:{1}
bkPipelineRunConditionNotMatch=,条件未满足即将跳过
bkPipelineRunConditionWithError=自定义条件计算出错:{0}

Large diffs are not rendered by default.

Loading