Skip to content

Commit

Permalink
Merge pull request TencentBlueKing#8068 from yongyiduan/issue_8067
Browse files Browse the repository at this point in the history
[stream]主动开启工蜂侧ci TencentBlueKing#8067
  • Loading branch information
bkci-bot authored Dec 1, 2022
2 parents 1391fef + 9d36d83 commit 4ed1a42
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,21 @@ interface ServiceGitResource {
tokenType: TokenTypeEnum
): Result<List<Commit>>

@ApiOperation("开启git仓库ci")
@GET
@Path("/stream/gitEnableCi")
fun enableCi(
@ApiParam(value = "仓库id或编码过的仓库path")
@QueryParam("projectName")
projectName: String,
@QueryParam("token")
token: String,
@QueryParam("tokenType")
tokenType: TokenTypeEnum,
@QueryParam("enable")
enable: Boolean? = true
): Result<Boolean>

@ApiOperation("工蜂创建文件")
@POST
@Path("/gitcode/create/file")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,17 @@ class ServiceGitResourceImpl @Autowired constructor(
)
}

override fun enableCi(
projectName: String,
token: String,
tokenType: TokenTypeEnum,
enable: Boolean?
): Result<Boolean> {
return gitService.enableCi(
projectName = projectName, token = token, tokenType = tokenType, enable = enable
)
}

override fun gitCreateFile(
gitProjectId: String,
token: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1110,10 +1110,10 @@ class GitService @Autowired constructor(
): GitMrChangeInfo {
val url = StringBuilder(
"${getApiUrl(repoUrl)}/projects/${
URLEncoder.encode(
id,
"UTF-8"
)
URLEncoder.encode(
id,
"UTF-8"
)
}/merge_request/$mrId/changes"
)
logger.info("get mr changes info url: $url")
Expand Down Expand Up @@ -1750,6 +1750,45 @@ class GitService @Autowired constructor(
}
}

@BkTimed(extraTags = ["operation", "enableCi"], value = "bk_tgit_api_time")
override fun enableCi(
projectName: String,
token: String,
tokenType: TokenTypeEnum,
enable: Boolean?
): Result<Boolean> {
logger.info(
"enableCi projectName:$projectName," +
"enable:$enable,tokenType:$tokenType"
)
val encodeProjectName = URLEncoder.encode(projectName, "utf-8")
val url = StringBuilder("${gitConfig.gitApiUrl}/projects/$encodeProjectName/ci/enable")
setToken(tokenType, url, token)
url.append("&enable_ci=$enable")
val request = Request.Builder()
.url(url.toString())
.put(
RequestBody.create(
MediaType.parse("application/json;charset=utf-8"), "{}"
)
)
.build()
OkhttpUtils.doHttp(request).use {
if (!it.isSuccessful) {
return Result(it.code(), "enableCi fail ${it.message()}")
}
val data = it.body()!!.string()
logger.info("enableCi response>> $data")
val dataMap = JsonUtil.toMap(data)
val code = dataMap["code"]
if (code != 200) {
// 把工蜂的错误提示抛出去
return Result(code as Int, "${dataMap["message"]}")
}
return Result(true)
}
}

@BkTimed(extraTags = ["operation", "git_create_file"], value = "bk_tgit_api_time")
override fun gitCreateFile(
gitProjectId: String,
Expand Down Expand Up @@ -1777,7 +1816,16 @@ class GitService @Autowired constructor(
}
}

override fun getGitCodeProjectList(accessToken: String, page: Int?, pageSize: Int?, search: String?, orderBy: GitCodeProjectsOrder?, sort: GitCodeBranchesSort?, owned: Boolean?, minAccessLevel: GitAccessLevelEnum?): Result<List<GitCodeProjectInfo>> {
override fun getGitCodeProjectList(
accessToken: String,
page: Int?,
pageSize: Int?,
search: String?,
orderBy: GitCodeProjectsOrder?,
sort: GitCodeBranchesSort?,
owned: Boolean?,
minAccessLevel: GitAccessLevelEnum?
): Result<List<GitCodeProjectInfo>> {
val pageNotNull = page ?: 1
val pageSizeNotNull = pageSize ?: 20
val url = "$gitCIUrl/api/v3/projects?access_token=$accessToken&page=$pageNotNull&per_page=$pageSizeNotNull"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ interface IGitService {
tokenType: TokenTypeEnum
): Result<List<Commit>>

fun enableCi(
projectName: String,
token: String,
tokenType: TokenTypeEnum,
enable: Boolean ? = true
): Result<Boolean>

fun gitCreateFile(
gitProjectId: String,
token: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ interface StreamGitTransferService {
refreshToken: Boolean?
): Result<AuthorizeResult>

/**
* 主动开启git侧 ci
*/
fun enableCi(
userId: String,
projectName: String,
enable: Boolean? = true
): Result<Boolean>

/**
* 获取当前项目的提交记录
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ class StreamGithubTransferService @Autowired constructor(
return Result(AuthorizeResult(HTTP_200))
}

override fun enableCi(userId: String, projectName: String, enable: Boolean?): Result<Boolean> {
// github 不支持
return Result(true)
}

override fun getCommits(
userId: String,
gitProjectId: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ class StreamTGitTransferService @Autowired constructor(
)
}

override fun enableCi(userId: String, projectName: String, enable: Boolean?): Result<Boolean> {
return client.get(ServiceGitResource::class).enableCi(
projectName = projectName,
token = getAndCheckOauthToken(userId).accessToken,
tokenType = TokenTypeEnum.OAUTH,
enable = enable
)
}

override fun getCommits(
userId: String,
gitProjectId: Long,
Expand Down

0 comments on commit 4ed1a42

Please sign in to comment.