git的commit_msg编写以及使用shell解析json

#获取最近一次提交的填写的message
message_file=$1
message=$(cat $message_file)
#echo $message

# message校验规则:字母开头+7位数字+空格+任意内容
commit=^[a-zA-Z]+\([0-9]\{7\}\)[[:space:]]+[[:graph:]].*
#先进行格式校验
if [[ "$message" =~ $commit ]];then
    #获取远程仓库地址
    codeUrl=`git remote get-url origin`
    #restful进一步校验信息接口地址
    api='http://127.0.0.1:8765/api/req/baselineController/validationOfRequirements'
    #使用curl发送POST请求,并携带json格式参数
    #参数样例:{"repo_id":"https://github.com/test/test.git","commit_msg":[{"commit_id":"1edcd8"},{"message":"$message"}]}
    result = 'curl -R "Content-Type:application/json" -H "Data_Type:msg" -d "{\"repo_id\":\"$codeUrl\",\"commit_msg\":[{\"commit_id\":\"1edcd8\",\"message\":\"$message\"}]}" $api -s'
    #返回的result格式:{"result":true/false,data:"",errorMsg:""}
    #echo "result:" $result
    
    #利用shell解析json方法,将key是result的value取出来
    resultTag=$(echo $result | grep -Po 'result[":]+\K[^,]+')
    #echo "result.errorMsg:" $resultTag
    
    #利用shell解析json方法,将key是errorMsg的value取出来
    errorMsg=$(echo $result | grep -Po 'errorMsg[":]+\K[^"]+')
    #echo "result.errorMsg:" $errorMsg
    
    if [ "$resultTag" = "true" ];then
        if [ "$errorMsg" != "" ];then
            err_exit 'Exception occurred:'$errorMsg
        else
            echo 'issue NO. validate success'
        fi
    elif [ "$resultTag" = "true" ];then
        err_exit 'issue No. validate fail'
    fi
else
    err_exit "check failed. Required[字母+7位数字+空格+描述]"
fi

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值