Skip to content

Commit

Permalink
feat: github触发器事件补充 #9372
Browse files Browse the repository at this point in the history
  • Loading branch information
hejieehe committed Oct 8, 2023
1 parent 1501b27 commit b1a4891
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ package com.tencent.devops.common.pipeline.pojo.element.trigger
import com.tencent.devops.common.api.enums.RepositoryType
import com.tencent.devops.common.pipeline.enums.StartType
import com.tencent.devops.common.pipeline.pojo.element.trigger.enums.CodeEventType
import com.tencent.devops.common.pipeline.pojo.element.trigger.enums.PathFilterType
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty

Expand All @@ -56,20 +55,6 @@ data class CodeGithubWebHookTriggerElement(
val repositoryType: RepositoryType? = null,
@ApiModelProperty("新版的github代码库名")
val repositoryName: String? = null,
@ApiModelProperty("路径过滤类型", required = true)
val pathFilterType: PathFilterType? = PathFilterType.NamePrefixFilter,
@ApiModelProperty("用于包含的路径", required = false)
val includePaths: String? = "",
@ApiModelProperty("用于排除的路径", required = false)
val excludePaths: String? = "",
@ApiModelProperty("用户白名单", required = false)
val includeUsers: String? = null,
@ApiModelProperty("用于排除的源分支名称", required = false)
val excludeSourceBranchName: String? = null,
@ApiModelProperty("用于包含的源分支名称", required = false)
val includeSourceBranchName: String? = null,
@ApiModelProperty("webhook队列", required = false)
val webhookQueue: Boolean? = false,
@ApiModelProperty("code review 状态", required = false)
val includeCrState: List<String>? = null,
@ApiModelProperty("code note comment", required = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,44 +54,6 @@ fun GithubPullRequestEvent.isPrForkNotMergeEvent() =

fun GithubPullRequestEvent.isPrForkEvent() = this.pullRequest.base.repo.id != this.pullRequest.head.repo.id

/**
* 以下情况需要同步触发GithubReview事件
* 1. 重新打开Pr
* 2. Pr请求评审
* 3. 源分支推送更新
*/
fun GithubPullRequestEvent.convertGithubReviewEvent(): GithubReviewEvent? {
val review = when (action) {
"opened", "reopened", "synchronize", "review_requested" -> {
GithubReview(
id = pullRequest.id.toLong(),
nodeId = "",
htmlUrl = pullRequest.htmlUrl,
createdAt = pullRequest.createdAt, // 2022-06-21T08:45:41Z
updatedAt = pullRequest.updatedAt, // 2022-06-21T08:45:41Z
user = sender,
body = pullRequest.body,
commitId = pullRequest.head.sha,
submittedAt = pullRequest.updatedAt ?: "",
state = GithubReviewState.APPROVING.value,
pullRequestUrl = pullRequest.htmlUrl,
authorAssociation = ""
)
}

else -> {
return null
}
}
return GithubReviewEvent(
repository = repository,
pullRequest = pullRequest,
review = review,
sender = sender,
action = "submitted"
)
}

@Suppress("ALL")
@JsonIgnoreProperties(ignoreUnknown = true)
data class GithubPullRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ data class GithubReviewEvent(
}

// 当存在多个必要评审人时,一个用户评审通过不算通过,需判断合并状态
private fun isApproved() = pullRequest.mergeable == true && pullRequest.mergeableState == "clean"
private fun isApproved() = pullRequest.mergeable == true

private fun isChangeRequired() = review.state == GithubReviewState.CHANGES_REQUESTED.value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,11 @@ package com.tencent.devops.common.webhook.service.code.param

import com.tencent.devops.common.pipeline.pojo.element.trigger.CodeGithubWebHookTriggerElement
import com.tencent.devops.common.pipeline.utils.PIPELINE_GIT_REPO_ID
import com.tencent.devops.common.webhook.pojo.code.BK_REPO_GIT_MANUAL_UNLOCK
import com.tencent.devops.common.webhook.pojo.code.BK_REPO_GIT_WEBHOOK_COMMIT_ID
import com.tencent.devops.common.webhook.pojo.code.BK_REPO_GIT_WEBHOOK_EVENT_TYPE
import com.tencent.devops.common.webhook.pojo.code.BK_REPO_GIT_WEBHOOK_EXCLUDE_BRANCHS
import com.tencent.devops.common.webhook.pojo.code.BK_REPO_GIT_WEBHOOK_EXCLUDE_PATHS
import com.tencent.devops.common.webhook.pojo.code.BK_REPO_GIT_WEBHOOK_EXCLUDE_USERS
import com.tencent.devops.common.webhook.pojo.code.BK_REPO_GIT_WEBHOOK_FINAL_INCLUDE_BRANCH
import com.tencent.devops.common.webhook.pojo.code.BK_REPO_GIT_WEBHOOK_FINAL_INCLUDE_PATH
import com.tencent.devops.common.webhook.pojo.code.BK_REPO_GIT_WEBHOOK_INCLUDE_BRANCHS
import com.tencent.devops.common.webhook.pojo.code.BK_REPO_GIT_WEBHOOK_INCLUDE_PATHS
import com.tencent.devops.common.webhook.pojo.code.BK_REPO_GIT_WEBHOOK_INCLUDE_USERS
import com.tencent.devops.common.webhook.pojo.code.MATCH_BRANCH
import com.tencent.devops.common.webhook.pojo.code.MATCH_PATHS
import com.tencent.devops.common.webhook.pojo.code.WebHookParams
import com.tencent.devops.common.webhook.service.code.matcher.ScmWebhookMatcher
import com.tencent.devops.repository.pojo.Repository
Expand Down Expand Up @@ -69,13 +61,6 @@ class GithubWebHookStartParam : ScmWebhookStartParams<CodeGithubWebHookTriggerEl
startParams[BK_REPO_GIT_WEBHOOK_INCLUDE_BRANCHS] = element.branchName ?: ""
startParams[BK_REPO_GIT_WEBHOOK_EXCLUDE_BRANCHS] = element.excludeBranchName ?: ""
startParams[BK_REPO_GIT_WEBHOOK_EXCLUDE_USERS] = element.excludeUsers ?: ""
startParams[BK_REPO_GIT_WEBHOOK_INCLUDE_USERS] = element.includeUsers ?: ""
startParams[BK_REPO_GIT_WEBHOOK_INCLUDE_PATHS] = element.includePaths ?: ""
startParams[BK_REPO_GIT_WEBHOOK_EXCLUDE_PATHS] = element.excludePaths ?: ""
startParams[BK_REPO_GIT_WEBHOOK_FINAL_INCLUDE_BRANCH] =
matchResult.extra[MATCH_BRANCH] ?: ""
startParams[BK_REPO_GIT_WEBHOOK_FINAL_INCLUDE_PATH] = matchResult.extra[MATCH_PATHS] ?: ""
startParams[BK_REPO_GIT_MANUAL_UNLOCK] = matcher.getEnv()[BK_REPO_GIT_MANUAL_UNLOCK] ?: false
startParams[PIPELINE_GIT_REPO_ID] = element.repositoryName ?: ""
startParams.putAll(matcher.retrieveParams())
return startParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ class GithubWebhookElementParams : ScmWebhookElementParams<CodeGithubWebHookTrig
params.eventType = element.eventType
params.excludeBranchName = EnvUtils.parseEnv(element.excludeBranchName ?: "", variables)
params.codeType = CodeType.GITHUB
params.includeUsers = includeUsers
params.excludeSourceBranchName = EnvUtils.parseEnv(excludeSourceBranchName ?: "", variables)
params.includeSourceBranchName = EnvUtils.parseEnv(includeSourceBranchName ?: "", variables)
params.webhookQueue = webhookQueue ?: false
params.includeCrState = joinToString(includeCrState)
params.includeNoteComment = includeNoteComment
params.includeNoteTypes = joinToString(includeNoteTypes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import com.tencent.devops.common.webhook.pojo.code.github.GithubPullRequestEvent
import com.tencent.devops.common.webhook.pojo.code.github.GithubPushEvent
import com.tencent.devops.common.webhook.pojo.code.github.GithubReviewCommentEvent
import com.tencent.devops.common.webhook.pojo.code.github.GithubReviewEvent
import com.tencent.devops.common.webhook.pojo.code.github.convertGithubReviewEvent
import com.tencent.devops.common.webhook.pojo.code.p4.P4Event
import com.tencent.devops.common.webhook.pojo.code.svn.SvnCommitEvent
import com.tencent.devops.common.webhook.service.code.loader.WebhookElementParamsRegistrar
Expand Down Expand Up @@ -192,8 +191,6 @@ abstract class PipelineBuildWebhookService : ApplicationContextAware {
return true
}
}
// Reopen pull request需同步触发Review事件
externalGithubReviewBuild(event)
val githubWebHookMatcher = scmWebhookMatcherBuilder.createGithubWebHookMatcher(event)
if (!githubWebHookMatcher.preMatch().isMatch) {
return true
Expand Down Expand Up @@ -224,21 +221,6 @@ abstract class PipelineBuildWebhookService : ApplicationContextAware {
return startProcessByWebhook(CodeGithubWebHookTriggerElement.classType, githubWebHookMatcher)
}

private fun externalGithubReviewBuild(event: GithubEvent) {
if (event is GithubPullRequestEvent && event.convertGithubReviewEvent() != null) {
val githubWebHookMatcher = scmWebhookMatcherBuilder.createGithubWebHookMatcher(event)
if (!githubWebHookMatcher.preMatch().isMatch) {
logger.info("fail to pre match github review event[$event]")
return
}
try {
startProcessByWebhook(CodeGithubWebHookTriggerElement.classType, githubWebHookMatcher)
} catch (ignored: Exception) {
logger.info("fail to start process by github review event[$event]", ignored)
}
}
}

fun externalP4Build(body: String): Boolean {
logger.info("Trigger p4 build($body)")

Expand Down

0 comments on commit b1a4891

Please sign in to comment.