使用Flask和Gitlab API

搭建预上线环境代码分支管理平台

车坤  2015.09 @西祠内部

Ops + Dev = DevOps
Ops+Dev=DevOpsOps + Dev = DevOps

前置知识

  • JSON
  • Python 基础
    • 基础语法
    • Package安装
  • Requests模块
  • Gitlab API

JSON

JavaScript Object Notation

一种轻量级的数据交换格式

{
    "key": "value",
    "list": [
        {
            "k1": "value1"
        },
        {
            "k2": 2
        },
        {
            "k3": true
        }
    ]
}
json = {
    "key": "value",
    "list": [
        {
            "k1": "value1"
        },
        {
            "k2": 2
        },
        {
            "k3": True
        }
    ]
}

JSON 演示

#encoding=utf-8

import json

json_string = '''
{
    "key": "value",
    "list": [
        {
            "k1": "value1"
        },
        {
            "k2": 2
        },
        {
            "k3": true
        }
    ]
}
'''
jsonObject = json.loads(json_string)

print json_string
print jsonObject
print jsonObject["key"]
print jsonObject["list"][2]["k3"]

Requests

>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
u'{"type":"User"...'
>>> r.json()
{u'private_gists': 419, u'total_private_repos': 77, ...}

Gitlab API

Automate GitLab via a simple and powerful API.

GET http://example.com/api/v3/projects?private_token=QVy1PB7sTxfy4pqfZM1U
curl --header "PRIVATE-TOKEN: QVy1PB7sTxfy4pqfZM1U" "http://example.com/api/v3/projects"

Auth Token

Response In JSON

{
  "message": "401 Unauthorized"
}
[
  {
    "commit": {
      "author_email": "john@example.com",
      "author_name": "John Smith",
      "authored_date": "2012-06-27T05:51:39-07:00"
    },
    "name": "master",
    "protected": true
  }
]

Requests 包请求Gitlab API演示

#encoding=utf-8

import requests

project = 'chekun%2fflask-dances-with-gitlab'

req = requests.get('http://git.xici.com/api/v3/projects/%s/repository/branches' 
    % (project)
)

print req.text

result = req.json()

print result

if req.status_code != 200:
    print result['message']

print '请求成功!'

Requests 包请求Gitlab API演示

#coding=utf-8

import os
import requests

project = 'chekun%2fflask-dances-with-gitlab'
token = 'xWYQJYaa6WpS2iwvbYEk'

req = requests.get('http://git.xici.com/api/v3/projects/%s/repository/branches?private_token=%s' 
    % (project, token)
)

result = req.json()

if req.status_code != 200:
    print result['message']
    os.exit(-1)

print 'Branches of chekun/flask-dances-with-gitlab\n'

for branch in result:
    print '* %s' % (branch['name'])

print '\n'

Preview 现状

1. 只能自动测试develop分支

2. 无法并行测试多个功能分支

3. 容易污染代码

Preview 改造

目的:独立分支测试

测试人员

Preview分支管理平台

选择分支

测试develop分支

测试选择的分支

Preview分支管理平台

web 界面

请求Gitlab API 显示分支

切换preview站点根目录代码分支

Web界面

#coding=utf-8

from flask import Flask
import requests

app = Flask(__name__)

@app.route("/")
def index():
    req = requests.get('...')
    result = req.json()
    options = ['<option value="%s">%s</option>' % (branch['name'], branch['name']) for branch in result]
    return '''
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>Preview 分支管理</title>
        </head>
        <body>
            <h3>Preview 分支管理</h3>
            <form method="post">
                <p>
                    分支:
                    <select name="branch">
                        %s
                    </select>
                </p>
                <p>
                    <input type="submit" value="切换分支" />
                </p>
            </form>
        </body>
    </html>
    ''' % (options)

if __name__ == "__main__":
    app.run()

获取提交的要切换的分支名

#coding=utf-8

from flask import Flask
from flask import request
import requests

app = Flask(__name__)

@app.route("/")
def index():
    ...
            <form method="post">
    ...

@app.route("/checkout", methods=["post"])
def checkout():
    return request.form['branch']

if __name__ == "__main__":
    app.run()

切换分支

...

@app.route("/checkout", methods=["post"])
def checkout():
    branch = request.form['branch']
    prewview_web_root = u'/demo/flask-dances-with-gitlab/'
    cmd = u"cd %s && git stash && git checkout %s" % (prewview_web_root, branch)
    subprocess.call(cmd, shell=True)
    return u'切换成功!<a href="/">返回</a>'

...

Q & A

谢谢

Made with Slides.com