Skip to content

Commit

Permalink
Merge pull request #4756 from mingshewhe/feature_4567
Browse files Browse the repository at this point in the history
feature:OpenApi:支持编辑代码库 #4567
  • Loading branch information
irwinsun authored Jul 29, 2021
2 parents 76a06c3 + 8f4833a commit fe02892
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,12 @@ object GitUtils {
}
return branchName.startsWith(PRE_PUSH_BRANCH_NAME_PREFIX)
}

fun isLegalHttpUrl(url: String): Boolean {
return Regex("http[s]?://([-.a-z0-9A-Z]+)(:[0-9]+)?/(.*).git").matches(url)
}

fun isLegalSshUrl(url: String): Boolean {
return Regex("git@([-.a-z0-9A-Z]+):(.*).git").matches(url)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package com.tencent.devops.scm.utils.code.git

import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Test

Expand Down Expand Up @@ -86,4 +87,23 @@ class GitUtilsTest {
projectName = GitUtils.getProjectName("http://github.com/Tencent/bk-ci.git")
assertEquals(repoName, projectName)
}

@Test
fun isLegalHttpUrl() {
Assert.assertTrue(GitUtils.isLegalHttpUrl("https://github.com/Tencent/bk-ci.git"))
Assert.assertTrue(GitUtils.isLegalHttpUrl("http://github.com/Tencent/bk-ci.git"))
Assert.assertTrue(GitUtils.isLegalHttpUrl("http://github.com:8080/Tencent/bk-ci.git"))
Assert.assertFalse(GitUtils.isLegalHttpUrl("http://github.com:8080/Tencent/bk-ci"))
Assert.assertFalse(GitUtils.isLegalHttpUrl("git@git.xxx.com:Tencent/bk-ci.git"))
}

@Test
fun isLegalSshUrl() {
Assert.assertFalse(GitUtils.isLegalSshUrl("https://github.com/Tencent/bk-ci.git"))
Assert.assertFalse(GitUtils.isLegalSshUrl("http://github.com/Tencent/bk-ci.git"))
Assert.assertFalse(GitUtils.isLegalSshUrl("http://github.com:8080/Tencent/bk-ci.git"))
Assert.assertFalse(GitUtils.isLegalSshUrl("http://github.com:8080/Tencent/bk-ci"))
Assert.assertTrue(GitUtils.isLegalSshUrl("git@git.xxx.com:Tencent/bk-ci.git"))
Assert.assertFalse(GitUtils.isLegalHttpUrl("git@git.xxx.com:Tencent/bk-ci"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import javax.ws.rs.DELETE
import javax.ws.rs.GET
import javax.ws.rs.HeaderParam
import javax.ws.rs.POST
import javax.ws.rs.PUT
import javax.ws.rs.Path
import javax.ws.rs.PathParam
import javax.ws.rs.Produces
Expand All @@ -54,6 +55,7 @@ import javax.ws.rs.core.MediaType
@Path("/{apigwType:apigw-user|apigw-app|apigw}/v3/repositories")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@SuppressWarnings("All")
interface ApigwRepositoryResourceV3 {

@ApiOperation("代码库列表")
Expand Down Expand Up @@ -117,4 +119,27 @@ interface ApigwRepositoryResourceV3 {
@PathParam("repositoryHashId")
repositoryHashId: String
): Result<Boolean>

@ApiOperation("编辑关联代码库")
@PUT
@Path("/{projectId}/{repositoryHashId}/")
fun edit(
@ApiParam(value = "appCode", required = true, defaultValue = AUTH_HEADER_DEVOPS_APP_CODE_DEFAULT_VALUE)
@HeaderParam(AUTH_HEADER_DEVOPS_APP_CODE)
appCode: String?,
@ApiParam(value = "apigw Type", required = true)
@PathParam("apigwType")
apigwType: String?,
@ApiParam(value = "用户ID", required = true, defaultValue = AUTH_HEADER_DEVOPS_USER_ID_DEFAULT_VALUE)
@HeaderParam(AUTH_HEADER_DEVOPS_USER_ID)
userId: String,
@ApiParam("项目ID", required = true)
@PathParam("projectId")
projectId: String,
@ApiParam("代码库哈希ID", required = true)
@PathParam("repositoryHashId")
repositoryHashId: String,
@ApiParam(value = "代码库模型", required = true)
repository: Repository
): Result<Boolean>
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ class ApigwRepositoryResourceV3Impl @Autowired constructor(private val client: C
)
}

override fun edit(
appCode: String?,
apigwType: String?,
userId: String,
projectId: String,
repositoryHashId: String,
repository: Repository
): Result<Boolean> {
logger.info(
"edit repostitories in project:userId=$userId,projectId=$projectId,repositoryHashId:$repositoryHashId"
)
return client.get(ServiceRepositoryResource::class).edit(
userId = userId,
projectId = projectId,
repositoryHashId = repositoryHashId,
repository = repository
)
}

companion object {
private val logger = LoggerFactory.getLogger(ApigwRepositoryResourceV3Impl::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import javax.ws.rs.DELETE
import javax.ws.rs.GET
import javax.ws.rs.HeaderParam
import javax.ws.rs.POST
import javax.ws.rs.PUT
import javax.ws.rs.Path
import javax.ws.rs.PathParam
import javax.ws.rs.Produces
Expand Down Expand Up @@ -180,4 +181,21 @@ interface ServiceRepositoryResource {
@PathParam("repositoryHashId")
repositoryHashId: String
): Result<Boolean>

@ApiOperation("编辑关联代码库")
@PUT
@Path("/{projectId}/{repositoryHashId}/")
fun edit(
@ApiParam(value = "用户ID", required = true, defaultValue = AUTH_HEADER_USER_ID_DEFAULT_VALUE)
@HeaderParam(AUTH_HEADER_USER_ID)
userId: String,
@ApiParam("项目ID", required = true)
@PathParam("projectId")
projectId: String,
@ApiParam("代码库哈希ID", required = true)
@PathParam("repositoryHashId")
repositoryHashId: String,
@ApiParam(value = "代码库模型", required = true)
repository: Repository
): Result<Boolean>
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package com.tencent.devops.repository.pojo

import com.tencent.devops.repository.pojo.enums.RepoAuthType
import com.tencent.devops.scm.utils.code.git.GitUtils
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty

Expand Down Expand Up @@ -65,10 +66,11 @@ data class CodeGitRepository(
}

override fun isLegal(): Boolean {
if (authType == RepoAuthType.HTTP) {
return url.startsWith("http://") ||
url.startsWith("https://")
return when (authType) {
RepoAuthType.HTTP, RepoAuthType.OAUTH, RepoAuthType.HTTPS ->
GitUtils.isLegalHttpUrl(url)
else ->
GitUtils.isLegalSshUrl(url)
}
return url.startsWith(getStartPrefix())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package com.tencent.devops.repository.pojo

import com.tencent.devops.repository.pojo.enums.RepoAuthType
import com.tencent.devops.scm.utils.code.git.GitUtils
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty

Expand Down Expand Up @@ -64,10 +65,11 @@ data class CodeGitlabRepository(
}

override fun isLegal(): Boolean {
if (authType == RepoAuthType.HTTP) {
return url.startsWith("http://") ||
url.startsWith("https://")
return when (authType) {
RepoAuthType.HTTP, RepoAuthType.OAUTH, RepoAuthType.HTTPS ->
GitUtils.isLegalHttpUrl(url)
else ->
GitUtils.isLegalSshUrl(url)
}
return url.startsWith(getStartPrefix())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package com.tencent.devops.repository.pojo

import com.tencent.devops.repository.pojo.enums.RepoAuthType
import com.tencent.devops.scm.utils.code.git.GitUtils
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty

Expand Down Expand Up @@ -63,4 +64,13 @@ data class CodeTGitRepository(
else -> "git@"
}
}

override fun isLegal(): Boolean {
return when (authType) {
RepoAuthType.HTTP, RepoAuthType.OAUTH, RepoAuthType.HTTPS ->
GitUtils.isLegalHttpUrl(url)
else ->
GitUtils.isLegalSshUrl(url)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,14 @@ class ServiceRepositoryResourceImpl @Autowired constructor(
repositoryService.userDelete(userId, projectId, repositoryHashId)
return Result(true)
}

override fun edit(
userId: String,
projectId: String,
repositoryHashId: String,
repository: Repository
): Result<Boolean> {
repositoryService.userEdit(userId, projectId, repositoryHashId, repository)
return Result(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,6 @@ class UserRepositoryResourceImpl @Autowired constructor(
repositoryHashId: String,
repository: Repository
): Result<Boolean> {
if (userId.isBlank()) {
throw ParamBlankException("Invalid userId")
}
if (projectId.isBlank()) {
throw ParamBlankException("Invalid projectId")
}
if (repositoryHashId.isBlank()) {
throw ParamBlankException("Invalid repositoryHashId")
}
if (repository.aliasName.isBlank()) {
throw ParamBlankException("Invalid repository aliasName")
}
if (repository.url.isBlank()) {
throw ParamBlankException("Invalid repository url")
}
repositoryService.userEdit(userId, projectId, repositoryHashId, repository)
return Result(true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.tencent.devops.common.api.enums.RepositoryType
import com.tencent.devops.common.api.enums.ScmType
import com.tencent.devops.common.api.exception.ErrorCodeException
import com.tencent.devops.common.api.exception.OperationException
import com.tencent.devops.common.api.exception.ParamBlankException
import com.tencent.devops.common.api.exception.PermissionForbiddenException
import com.tencent.devops.common.api.exception.RemoteServiceException
import com.tencent.devops.common.api.model.SQLPage
Expand Down Expand Up @@ -75,6 +76,7 @@ import com.tencent.devops.scm.enums.CodeSvnRegion
import com.tencent.devops.scm.pojo.GitCommit
import com.tencent.devops.scm.pojo.GitRepositoryDirItem
import com.tencent.devops.scm.pojo.GitRepositoryResp
import com.tencent.devops.scm.utils.code.git.GitUtils
import com.tencent.devops.ticket.api.ServiceCredentialResource
import org.jooq.DSLContext
import org.jooq.impl.DSL
Expand Down Expand Up @@ -528,7 +530,7 @@ class RepositoryService @Autowired constructor(
repositoryCodeGitDao.create(
dslContext = transactionContext,
repositoryId = repositoryId,
projectName = repository.projectName,
projectName = GitUtils.getProjectName(repository.url),
userName = repository.userName,
credentialId = repository.credentialId,
authType = repository.authType
Expand All @@ -547,7 +549,7 @@ class RepositoryService @Autowired constructor(
repositoryCodeGitDao.create(
dslContext = transactionContext,
repositoryId = repositoryId,
projectName = repository.projectName,
projectName = GitUtils.getProjectName(repository.url),
userName = repository.userName,
credentialId = repository.credentialId,
authType = repository.authType
Expand All @@ -566,7 +568,7 @@ class RepositoryService @Autowired constructor(
repositoryCodeGitLabDao.create(
dslContext = transactionContext,
repositoryId = repositoryId,
projectName = repository.projectName,
projectName = GitUtils.getProjectName(repository.url),
userName = repository.userName,
privateToken = repository.credentialId
)
Expand Down Expand Up @@ -710,6 +712,21 @@ class RepositoryService @Autowired constructor(
}

fun userEdit(userId: String, projectId: String, repositoryHashId: String, repository: Repository) {
if (userId.isBlank()) {
throw ParamBlankException("Invalid userId")
}
if (projectId.isBlank()) {
throw ParamBlankException("Invalid projectId")
}
if (repositoryHashId.isBlank()) {
throw ParamBlankException("Invalid repositoryHashId")
}
if (repository.aliasName.isBlank()) {
throw ParamBlankException("Invalid repository aliasName")
}
if (repository.url.isBlank()) {
throw ParamBlankException("Invalid repository url")
}
val repositoryId = HashUtil.decodeOtherIdToLong(repositoryHashId)
validatePermission(
user = userId,
Expand Down Expand Up @@ -771,7 +788,7 @@ class RepositoryService @Autowired constructor(
repositoryCodeGitDao.edit(
dslContext = transactionContext,
repositoryId = repositoryId,
projectName = repository.projectName,
projectName = GitUtils.getProjectName(repository.url),
userName = repository.userName,
credentialId = repository.credentialId,
authType = repository.authType
Expand All @@ -790,7 +807,7 @@ class RepositoryService @Autowired constructor(
repositoryCodeGitDao.edit(
dslContext = transactionContext,
repositoryId = repositoryId,
projectName = repository.projectName,
projectName = GitUtils.getProjectName(repository.url),
userName = repository.userName,
credentialId = repository.credentialId,
authType = repository.authType
Expand Down Expand Up @@ -831,7 +848,7 @@ class RepositoryService @Autowired constructor(
repositoryCodeGitLabDao.edit(
dslContext = transactionContext,
repositoryId = repositoryId,
projectName = repository.projectName,
projectName = GitUtils.getProjectName(repository.url),
userName = repository.userName,
credentialId = repository.credentialId
)
Expand Down

0 comments on commit fe02892

Please sign in to comment.