利用Python和webhook實現自動提交代碼

最近在爲公司書寫項目的api文檔,計劃利用碼雲的wiki管理整個項目,公司自有git做爲項目內容依託,這樣全員均可參與,而我按期向碼雲推送就能夠了。python

問題

根據需求碰見了這樣一個問題:我每次從git上拉取更新再向碼雲wiki推送,最後再經過釘釘向你們通知的過程,過於繁瑣。因而計劃用Python實現部分流程自動化

代碼

1.引入的頭文件

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/9/25 上午10:52
# @Author  : liuyonghu
# @Site    : https://www.lyonghu.com
# @File    : autoGitPush.py
# @Software: PyCharm


import subprocess
import time
from tools.sendMessageToDD import sendMessage

2.add 方法

def add():
    cmd = "git add ."
    process = subprocess.Popen(cmd, shell=True)
    process.wait()
    returnCode = process.returncode

    if returnCode != 0:
        print(" add returnCode", returnCode)
    else:
        commit()

3.commit 方法

commitMessage = ""
def commit():
    global commitMessage
    commitMessage = time.strftime("%Y/%m/%d %H:%M")
    cmd = "git commit -m  '{}'".format(commitMessage)

    # print("cmd = " + cmd)
    process = subprocess.Popen(cmd, shell=True)
    process.wait()
    push()

4.push 方法

def push():
    cmd = "git push origin master"
    process = subprocess.Popen(cmd, shell=True)
    process.wait()
    returnCode = process.returncode
    if returnCode != 0:
        print("push returnCode", returnCode)
    else:
        sendMessage({
            "fileName": "api文檔 : \n\n已更新,請注意查看! \n" +"\n更新信息: {}".format(
                commitMessage),
            "text": time.strftime("%Y/%m/%d %H:%M"),
            "error": False
        })

5.最後調用

add()

注意
代碼調用順序git

由此除了項目成員向wiki推送狀態沒法獲取外其餘事件基本能夠省去不少手動操做。若是你們知道更好的辦法歡迎討論指導shell

相關文章
相關標籤/搜索