Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/cyw3/CodeAnalysis
Browse files Browse the repository at this point in the history
  • Loading branch information
cyw3 committed Aug 2, 2024
2 parents 5b14be6 + 5a60e33 commit 1fe00bc
Show file tree
Hide file tree
Showing 29 changed files with 354 additions and 118 deletions.
4 changes: 2 additions & 2 deletions client/node/localtask/codecounttask.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class CodeCountTask(object):
@staticmethod
def run_count_line_task(request_list, task_name_id_maps, job_id, scm_auth_info, token, server_url, source_dir,
scm_info, origin_os_env):
scm_info, origin_os_env, create_from):
"""
统计代码行
:param request_list:
Expand All @@ -38,7 +38,7 @@ def run_count_line_task(request_list, task_name_id_maps, job_id, scm_auth_info,
RequestModify.modify_local_task_request(request, task_name_id_maps, job_id,
scm_auth_info.ssh_file,
token, server_url, source_dir, scm_info,
scm_auth_info)
scm_auth_info, create_from)
# 执行单个任务扫描
logger.info("启动 linecount 工具统计代码行...")
task = SingleTaskRuner(request, env=origin_os_env).run()
Expand Down
1 change: 1 addition & 0 deletions client/node/localtask/localrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ def _get_proj_config(self, repo_id, proj_id, org_sid, team_name):
def run(self):
"""执行本地项目扫描
"""
LogPrinter.info(f"start from {self._create_from}.")
self._job_start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))

try:
Expand Down
12 changes: 7 additions & 5 deletions client/node/localtask/requestmodify.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ def add_params(task_request, job_context, scm_info, pre_cmd, build_cmd, project_
task_params['ignore_merged_issue'] = True

@staticmethod
def modify_task_request(task_request, token, server_url, source_dir, scm_info, scm_auth_info):
def modify_task_request(task_request, token, server_url, source_dir, scm_info, scm_auth_info, create_from):
"""
向task_request中添加参数
:param task_request: dict, 任务参数
:return:
"""
# 添加task_scene信息,标记任务运行场景
task_request['task_params']['task_scene'] = TaskScene.LOCAL
# 添加create_from参数
task_request['task_params']['created_from'] = create_from
# 将token, server_url放到task_params中,供task进度上报和代码行上报使用
task_request['task_params']['token'] = Crypto(settings.PASSWORD_KEY).encrypt(token)
task_request['task_params']['server_url'] = server_url
Expand All @@ -70,13 +72,13 @@ def modify_task_request(task_request, token, server_url, source_dir, scm_info, s

@staticmethod
def modify_local_task_request(task_request, task_name_id_maps, job_id, ssh_file, token, server_url,
source_dir, scm_info, scm_auth_info):
source_dir, scm_info, scm_auth_info, create_from):
"""
本地任务,向task_request中添加参数
:param task_request:
:return:
"""
RequestModify.modify_task_request(task_request, token, server_url, source_dir, scm_info, scm_auth_info)
RequestModify.modify_task_request(task_request, token, server_url, source_dir, scm_info, scm_auth_info, create_from)
# 添加 task_dir 信息
task_name = task_request["task_name"]
task_id = task_name_id_maps.get(task_name)
Expand All @@ -90,13 +92,13 @@ def modify_local_task_request(task_request, task_name_id_maps, job_id, ssh_file,
task_request['task_params']['ssh_file'] = ssh_file

@staticmethod
def modify_pri_task_request(task_request, token, server_url, source_dir, scm_info, scm_auth_info):
def modify_pri_task_request(task_request, token, server_url, source_dir, scm_info, scm_auth_info, create_from):
"""
私有任务,向task_request中添加参数
:param task_request:
:return:
"""
RequestModify.modify_task_request(task_request, token, server_url, source_dir, scm_info, scm_auth_info)
RequestModify.modify_task_request(task_request, token, server_url, source_dir, scm_info, scm_auth_info, create_from)

# 在request中添加 task_dir 信息
task_id = task_request["id"]
Expand Down
8 changes: 4 additions & 4 deletions client/node/localtask/runlocaltask.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def scan_project(self, execute_request_list, proj_conf):
RequestModify.modify_local_task_request(task_request, self._task_name_id_maps, self._job_id,
self._scm_auth_info.ssh_file,
self._token, self._server_url, self._source_dir, self._scm_info,
self._scm_auth_info)
self._scm_auth_info, self._create_from)
codecount_handler = LocalCountLine(task_request)
codecount_handler.start_thread()
else:
Expand All @@ -88,14 +88,14 @@ def scan_project(self, execute_request_list, proj_conf):
self._scm_auth_info,
self._token, self._server_url,
self._source_dir, self._scm_info,
self._origin_os_env)
self._origin_os_env, self._create_from)

for task_request in execute_request_list:
# 完善task request字段
RequestModify.modify_local_task_request(task_request, self._task_name_id_maps, self._job_id,
self._scm_auth_info.ssh_file, self._token,
self._server_url, self._source_dir, self._scm_info,
self._scm_auth_info)
self._scm_auth_info, self._create_from)

# 通过环境变量获取并发执行开关
concurrent_task_env = os.getenv("TCA_CONCURRENT_SCAN")
Expand Down Expand Up @@ -189,7 +189,7 @@ def _wait_and_run_private_procs(self, project_id, job_id):
task_list = rt_data["tasks"]
for task_request in task_list:
RequestModify.modify_pri_task_request(task_request, self._token, self._server_url, self._source_dir,
self._scm_info, self._scm_auth_info)
self._scm_info, self._scm_auth_info, self._create_from)
# 执行单个任务分析
task = SingleTaskRuner(task_request, env=self._origin_os_env).run()
self._local_task_dirs.append(task_request["task_dir"])
Expand Down
5 changes: 3 additions & 2 deletions client/node/servertask/looprunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ def __init__(self, args):
self._token = args.token
self._tag = args.tag
self._org_sid = args.org_sid
self._create_from = args.create_from
self._create_from = args.create_from if args.create_from else "codedog_client"
self._server_url = LocalConfig.get_server_url()
# 打印连接的sever地址
# 打印启动渠道和连接的sever地址
LogPrinter.info(f"start from {self._create_from}.")
LogPrinter.info("using server: %s" % self._server_url)
# 初始化与codedog服务器通信的api server实例
self._server = RetryDogServer(self._server_url, self._token).get_api_server()
Expand Down
76 changes: 67 additions & 9 deletions doc/en/guide/插件/Jenkins_Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
### 获取Jenkins插件
Jenkins插件有以下两种获取方式:

**方式一**:在 TCA 源码的`plugin/jenkins_plugin`目录下,执行命令`mvm package -DskipTests`,打包完成后进入target目录会看到`Jenkins_plugin.hpi` 的安装包。
**方式一**:在 TCA 源码的`plugin/jenkins_plugin`目录下,执行命令`mvn package -DskipTests`,打包完成后进入target目录会看到`tca_jenkins_plugin.hpi` 的安装包。

**方式二**:从TCA release 安装包中,获取`jenkins_plugin.hpi`[https://github.com/Tencent/CodeAnalysis/releases](https://github.com/Tencent/CodeAnalysis/releases)


### 在Jenkins安装插件
在Jenkins中通过【Manage Plugin】-> 【Advanced】->【Deploy plugin】的方式选择 Jenkins_plugin.hpi文件上传安装,并重启Jenkins。
![](../../../images/jenkins_manage_plugins.png)
Expand All @@ -20,6 +19,12 @@ Jenkins插件有以下两种获取方式:
最终在【Installed】里搜索出【TCA】代表插件安装成功。
![](../../../images/jenkins_installed_plugins.png)

### 完善启动客户端的环境
在CodeAnalysis目录下执行代码
```shell
bash ./scripts/base/install_bin.sh
```
`client`目录下的`config.ini`文件中的`<Server IP地址>`替换为部署的开源版TCA的IP地址(可包含端口号)

## 使用插件
### 在 TCA 创建团队和项目
Expand All @@ -35,24 +40,23 @@ Value:`GITPATH` Value:xxxx(路径不包含git)
![](../../../images/jenkins_manage_jenkins.png)
<img src="../../../images/jenkins_environment_vars.png" width = "400"/>


### 创建一个构建任务,配置代码库信息
进入Jenkins,通过【New Item】创建一个空白任务,在任务配置中【Source Code Management】配置待分析的代码库地址和凭证。
### 配置 TCA 插件
#### 方式一:可视化界面配置 TCA 插件
创建一个构建任务,配置代码库信息,进入Jenkins,通过【New Item】创建一个空白任务,在任务配置中【Source Code Management】配置待分析的代码库地址和凭证。
`Repository URL`: 填入远端仓库地址
`Credentials`: 添加仓库的用户名和密码作为凭证,如果是公开仓库,可以不设置仓库凭证

<img src="../../../images/jenkins_new_item.png" width = "300"/>

![](../../../images/jenkins_git_config.png)

### 配置 TCA 插件
在构建任务的【Build】中选择【TCA】插件并配置以下参数:

`CodeAnalysis`: 拉取代码所在的绝对路径
`CodeAnalysis目录绝对路径`: 拉取到本地的CodeAnalysis开源仓库目录的绝对路径(例如:/data/CodeAnalysis/)
`团队ID`: 在 TCA 中创建的团队的标识ID,可在TCA【团队概览】中获取“团队唯一标识”
`项目名称`: 在 TCA 中创建的项目的标识ID,可在TCA【项目概览】中获取“项目唯一标识”
`Token`: 在 TCA 的【个人中心】->【个人令牌】中获取
`分支名称`: 需要扫描的代码分析名称
`分支名称`: 需要扫描的代码分支名称
`语言类别`: 项目需要扫描的语言
`分析方案模板ID`: 需要使用的分析方案模板ID,在分析方案模板的“基础属性”中获取,将根据此模板创建分析方案(选填)
`分析方案名称`: 指定创建出来的分析方案的名称(选填)
Expand All @@ -63,6 +67,33 @@ Value:`GITPATH` Value:xxxx(路径不包含git)

![](../../../images/jenkins_tca_plugin_config.png)

#### 方式二:pipeline语法配置 TCA 插件
在步骤中添加TCA插件参数配置语句,下面的配置语句可作为参考;注意:如果是release版本v1.11.0及之前的老版本(包含源代码构建生成和release获取)插件,语法参数略有差别,请参考[issue1150](https://github.com/Tencent/CodeAnalysis/issues/1150)
```pipeline
pipeline{
agent any
stages{
stage('Build'){
steps{
TCA(codeAnalysisPath: '/data/CodeAnalysis/', teamId: 'xxxx', projectName: 'demo', token: 'xxxxxxxxxxxx', branchName: 'master', languageType: 'Java', refSchemeID: '1', scanPlan: 'model', threshold: '90', total:true)
}
}
}
}
```
`codeAnalysisPath`: 拉取到本地的CodeAnalysis开源仓库目录的绝对路径(例如:/data/CodeAnalysis/)
`teamId`:团队ID
`projectName`: 项目名称
`token`: 在 TCA 的【个人中心】->【个人令牌】中获取
`branchName`: 需要扫描的代码分支名称
`languageType`: 项目需要扫描的语言
`refSchemeID`: 需要使用的分析方案模板ID,在分析方案模板的“基础属性”中获取,将根据此模板创建分析方案(选填)
`scanPlan`: 指定创建出来的分析方案的名称(选填)
`threshold`: 设置质量门禁值
`total`: 是否全量扫描,填`ture`为全量扫描,不填或填`false`为增量扫描

### 启动构建并查看结果
点击【Build Now】启动构建。
进入构建任务,在【Console Output】中查看执行过程。
Expand All @@ -74,6 +105,8 @@ Value:`GITPATH` Value:xxxx(路径不包含git)
## 设置质量门禁
在上述 TCA 插件配置部分填写`质量门禁`参数,需要填写一个整数,即当前分支的扫描问题量大于该质量门禁值时,判断为不通过;否则为通过。完成后会将TCA结果状态(`success`|`failure`)输出到工作空间下的`tca_threshold.txt`文件中,供后续步骤判断和终止流水线。

### 可视化界面使用质量门禁

在TCA插件后增加shell命令步骤,输入以下脚本内容:

![](../../../images/jenkins_shell.png)
Expand All @@ -87,5 +120,30 @@ else
exit 255
fi
```

当质量门禁不通过时,会终止流水线(退出码:255)。

### pipeline脚本使用质量门禁
以下是pipeline脚本使用质量门禁进行相应操作的示例,你可以在if和else部分写入你想要运行的脚本

```pipeline
pipeline{
agent any
stages{
stage('Build'){
steps{
TCA(codeAnalysisPath: '/data/CodeAnalysis/', teamId: 'xxxx', projectName: 'demo', token: 'xxxxxxxxxxxx', branchName: 'master', languageType: 'Java', refSchemeID: '1', scanPlan: 'model', threshold: '90', total:true)
script{
def tca_status = readFile('tca_threshold.txt')
if (tca_status == "success") {
echo ">> tca scan pass!"
} else {
echo ">> tca scan fail! exit code 255"
error("TCA scan failed. Terminating pipeline.")
}
}
}
}
}
}
```
Loading

0 comments on commit 1fe00bc

Please sign in to comment.