@@ -4,17 +4,21 @@ import com.tencent.bk.codecc.defect.dao.mongotemplate.CodeRepoInfoDao
4
4
import com.tencent.bk.codecc.defect.service.PipelineScmService
5
5
import com.tencent.bk.codecc.task.api.ServiceTaskRestResource
6
6
import com.tencent.devops.common.api.CodeRepoVO
7
+ import com.tencent.devops.common.api.codecc.util.JsonUtil
7
8
import com.tencent.devops.common.api.enums.RepositoryType
8
9
import com.tencent.devops.common.api.exception.CodeCCException
9
10
import com.tencent.devops.common.api.pojo.Result
10
- import com.tencent.devops.common.api.codecc.util.JsonUtil
11
11
import com.tencent.devops.common.client.Client
12
+ import com.tencent.devops.common.client.proxy.DevopsProxy
12
13
import com.tencent.devops.common.constant.ComConstants
13
14
import com.tencent.devops.common.constant.CommonMessageCode
14
15
import com.tencent.devops.common.util.HttpPathUrlUtil
16
+ import com.tencent.devops.common.util.OkhttpUtils
15
17
import com.tencent.devops.repository.api.ExternalCodeccRepoResource
18
+ import com.tencent.devops.repository.api.ServiceOauthResource
16
19
import com.tencent.devops.repository.api.ServiceRepositoryResource
17
20
import com.tencent.devops.repository.api.scm.ServiceGitResource
21
+ import com.tencent.devops.repository.pojo.enums.RepoAuthType
18
22
import org.apache.commons.collections.CollectionUtils
19
23
import org.apache.commons.lang.math.NumberUtils
20
24
import org.apache.commons.lang3.RandomStringUtils
@@ -32,6 +36,7 @@ class PipelineScmServiceImpl @Autowired constructor(
32
36
33
37
companion object {
34
38
private val logger = LoggerFactory .getLogger(PipelineScmServiceImpl ::class .java)
39
+ private val FILE_TOO_LARGE_CONTENT = " 当前告警代码文件大小超过1M,不能在平台查看代码详情,可以根据告警行号在IDE查看" ;
35
40
}
36
41
37
42
@Value(" \$ {codecc.privatetoken:#{null}}" )
@@ -203,4 +208,92 @@ class PipelineScmServiceImpl @Autowired constructor(
203
208
return client.getDevopsService(ServiceGitResource ::class .java).getAuthUrl(authParamJsonStr = authParamJsonStr).data
204
209
? : " "
205
210
}
206
- }
211
+
212
+ override fun getStreamFileContent (
213
+ projectId : String ,
214
+ userId : String ,
215
+ repoUrl : String ,
216
+ filePath : String ,
217
+ reversion : String? ,
218
+ branch : String?
219
+ ): String? {
220
+ if (projectId.startsWith(" github_" )) {
221
+ return getGithubFileContent(repoUrl, reversion ? : branch ? : " " , filePath)
222
+ }
223
+ val token = try {
224
+ val tokenResult = client.getDevopsService(ServiceOauthResource ::class .java, projectId).gitGet(userId)
225
+ if (tokenResult.data == null || tokenResult.isNotOk()) {
226
+ logger.error(" can not get user repository token: $userId $repoUrl $filePath $reversion $branch " )
227
+ throw CodeCCException (errorCode = CommonMessageCode .OAUTH_TOKEN_IS_INVALID )
228
+ }
229
+ tokenResult.data!! .accessToken
230
+ } catch (e: CodeCCException ) {
231
+ if (e.errorCode == CommonMessageCode .OAUTH_TOKEN_IS_INVALID ) {
232
+ throw e
233
+ } else {
234
+ " "
235
+ }
236
+ } finally {
237
+ DevopsProxy .projectIdThreadLocal.remove()
238
+ }
239
+ if (token.isBlank()) {
240
+ return " "
241
+ }
242
+
243
+ val fileContent = try {
244
+ logger.info(" get file content: $repoUrl | $filePath | $reversion | $branch | $token " )
245
+ val result = client.getDevopsService(ExternalCodeccRepoResource ::class .java, projectId)
246
+ .getGitFileContentCommon(
247
+ repoUrl = repoUrl,
248
+ filePath = filePath.removePrefix(" /" ),
249
+ ref = if (! reversion.isNullOrBlank()) reversion else branch,
250
+ token = token,
251
+ authType = RepoAuthType .OAUTH
252
+ )
253
+ if (result.isNotOk()) {
254
+ logger.error(" get file content fail!" )
255
+ throw CodeCCException (CommonMessageCode .CODE_NORMAL_CONTENT_ERROR )
256
+ }
257
+ result.data
258
+ } catch (e: CodeCCException ) {
259
+ return if (e.errorCode == CommonMessageCode .FILE_CONTENT_TOO_LARGE ) {
260
+ FILE_TOO_LARGE_CONTENT
261
+ } else {
262
+ throw e
263
+ }
264
+ } catch (e: Exception ) {
265
+ logger.error(
266
+ " get git file content fail!, repoUrl: {}, filePath: {}, token: {}" ,
267
+ repoUrl,
268
+ filePath,
269
+ token,
270
+ e
271
+ )
272
+ throw CodeCCException (CommonMessageCode .CODE_CONTENT_ERROR )
273
+ } finally {
274
+ DevopsProxy .projectIdThreadLocal.remove()
275
+ }
276
+
277
+
278
+ return fileContent
279
+ }
280
+
281
+ /* *
282
+ * 获取 Github 文本内容
283
+ * 等待蓝盾支持后,切换到蓝盾的版本
284
+ */
285
+ private fun getGithubFileContent (repoUrl : String , ref : String , filePath : String ): String {
286
+ val headerIndex = if (repoUrl.startsWith(" https://" )) {
287
+ 8
288
+ } else if (repoUrl.startsWith(" http://" )) {
289
+ 7
290
+ } else {
291
+ 0
292
+ }
293
+ val startIndex = repoUrl.indexOf(" /" , headerIndex)
294
+ val endIndex = repoUrl.lastIndexOf(" .git" )
295
+ val projectName = repoUrl.substring(startIndex + 1 , endIndex)
296
+ val url = " https://raw.githubusercontent.com/$projectName /$ref /$filePath "
297
+ return OkhttpUtils .doGet(url)
298
+ }
299
+ }
0 commit comments