#!/usr/bin/env python# -*- encoding: utf-8 -*-
import sys, re, datetimeimport fileinputimport shlex, subprocessimport dateutil.parserimport pytz
def datestring2timezone(datestring,timezone='Asia/Shanghai',dateformat='%Y-%m-%d %H:%M:%S'): """将带有时区的时间统一化规定的时区时间 :param datestring:svn/git时间,eg:2011-01-19T05:13:13.421543Z,2018-11-09 17:38:37 +0800 :param timezone:统一时区,默认是中国时区 :param dateformat:转换成时间格式 :return: """ local_time = dateutil.parser.parse(datestring).astimezone(pytz.timezone(timezone)) # 解析string 并转换为北京时区 # print(local_time , type(local_time)) # datetime 类型 da = datetime.datetime.strftime(local_time, dateformat) # 将datetime转换为string return da
print("Begin check your commit info")"""获取用户提交的信息"""origin_commit, curr_commit, branch = None, None, None# 读取用户试图更新的所有引用for line in fileinput.input(): line_list = line.strip().split() if len(line_list) >= 3: origin_commit, curr_commit, branch = line_list[:3] break
# TODO: 目前2.27.0版本的git有点问题,在部署的时候需要额外注意# git_version = subprocess.check_output(shlex.split('git --version'), shell=False)# print("git version: {}".format(str(git_version)))# which_git = subprocess.check_output(shlex.split('which git'), shell=False)# print("which git: {}".format(str(which_git)))
# 获取commit的信息,用户,邮箱,msg等commit_user = subprocess.check_output(shlex.split('git log --pretty=format:"%an" {} -1'.format(curr_commit)), shell=False)commit_date = subprocess.check_output(shlex.split('git log --pretty=format:"%cd" {} -1'.format(curr_commit)), shell=False)commit_msg = subprocess.check_output(shlex.split('git log --pretty=format:"%s" {} -1'.format(curr_commit)), shell=False)
# 针对merge request的请求,取最新一条非merge request的提交信息进行判断RULE_MERGE_REQUEST = r'^Merge branch .*(into|.*)'if re.search(RULE_MERGE_REQUEST, str(commit_msg), flags=0): # 获取最新一条非merge request的commit的信息,用户,邮箱,msg等 commit_user = subprocess.check_output(shlex.split('git log --no-merges --date-order --pretty=format:"%an" -1'), shell=False) commit_date = subprocess.check_output(shlex.split('git log --no-merges --date-order --pretty=format:"%cd" -1'), shell=False) commit_msg = subprocess.check_output(shlex.split('git log --no-merges --date-order --pretty=format:"%s" -1'), shell=False)
start_date = "2021-07-07 19:00:00"# 提交日期大于给定的开始时间才校验if start_date >= datestring2timezone(commit_date): sys.exit(0)
if not re.search(r'^JIRA-[0-9]{4,6}', str(commit_msg), flags=0): print("ERROR:Comment must start with DPT-<ID>. E.g.: DPT-1234") sys.exit(1)sys.exit(0)
评论