510908220 / lintjenkins

简化jenkins使用pylint检查代码流程

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

lintjenkins

Documentation Status Updates

lintjenkins 是干什么的? 这里举一个使用场景的例子. 我又一个django项目需要使用jenkins进行持续代码检查, 需要做的操作是:

  1. 在jenkins上创建一个job, 配置job 和violations等信息.
  2. 在配置job时,添加svn代码时,需要输入账号和密码进行验证
  3. 查看检查结果
  4. 当需要统计历史结果时, 只能手动去看一次次检查结果.

lintjenkins 就是将上述场景操作封装为接口,便于二次开发.

Require

  1. 目标机器装有jenkins+violations

  2. 被检查的python代码所在目录符合包的形式,如:

    • __init__.py
    • requirements.txt
    • pylint.cfg
  3. pylint配置文件也需要修改.

    • 没有配置可以使用`pylint --generate-rcfile > pylint.cfg`生成配置.
    • 将`output-format`项的值改为`parseable`
    • init-hook`添加'import sys; sys.path.append("/var/lib/jenkins/workspace/xxx")',`xxx`为jenkins job名. 否则会出现`W0403 Relative import 'xxxx'警告

Example

In [2]: from lintjenkins import LintJenkins

In [3]: lint_jenkins = LintJenkins('http://x.x.x.x:8080', username='username', password='password')

In [4]: lint_jenkins.add_job(svn = 'svn', username='username', password='password',job_name='aliyun')

In [7]: lint_jenkins.get_build_numbers('aliyun')
Out[7]: [2, 1]

In [8]: lint_jenkins.get_build_info('aliyun',2)
Out[8]:
     {
         "violation_info": {
             "violation_num": 2027,
             "violation_file_num": 80
         },
         "commits": [
             {
                 "msg": "svn提交信息",
                 "paths": [
                     {
                         "editType": "add",
                         "file": "新增的文件"
                     },
                     {
                         "editType": "edit",
                         "file": "编辑的文件"
                     }
                 ],
                 "revision": 18830,
                 "datetime": "2017-06-12 09:44:38",
                 "author": "huzhongzhong"
             }
         ],
         "datetime": "2017-06-12 09:55:13",
         "result_url":'http://x.x.x.x:8080/job/job_name/7/violations/'
         "result": "UNSTABLE",
         "duration": 49,
         "revisions": [
             {
                 "module": "svn地址",
                 "revision": 18830
             }
         ]
     }

字段说明:

filed.png

对应的在jenkins上的表现是:

lintjenkins.png

注意

  • Credentials Plugin`使用的是版本`2.1.16
  • 如果报错`403 no valid crumb was included in the request , 简单可以这样处理:`setup jenkins's "global security settings": Uncheck "Prevent Cross Site Request Forgery exploits"

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

About

简化jenkins使用pylint检查代码流程

License:MIT License


Languages

Language:Python 91.3%Language:Makefile 8.7%