gitlab集成redmine后,代码push的pre-receive脚本

过去参考网上资料,做过svn的脚本。这次参考svn的脚本改写来的。另外还参考了网上其他资料。

由于ruby不熟悉,费了不少劲儿。脚本是以gitlab提供的默认脚本为基础改写的. 其中不好的地方

就是直接查询mysql数据库的地方,也没精力进一步优化了,凑合着用吧。

#!/usr/bin/env /opt/gitlab-8.5.7-0/ruby/bin/ruby

# This file was placed here by GitLab. It makes sure that your pushed commits
# will be processed properly.

refs = ARGF.read
key_id  = ENV['GL_ID']
repo_path = Dir.pwd

# added based on gitlab sample
paraArray = refs.split(' ')
$i=0
while $i < paraArray.size do
    oldrev = paraArray[$i]
    newrev = paraArray[$i+1]
    refnam = paraArray[$i+2]
    $i += 3

    # get the commit log message
    revStr = `git rev-list #{oldrev}..#{newrev}`
    revArray = revStr.split("\n")
    revArray.each do |rev|
        log=`git show -s --format="%s" #{rev}`
# from pre-commit.rb of svn
        comments = log
        if /[a-zA-Z0-9]/ !~ comments
            print "You must include a comment with your commit."
            ENV['GL_ID'] = nil
            exit 1
        end

        if /refs|fixes|closes\s#([0-9]+)/ !~ comments
            print  "You must reference a Redmine issue in your commit comments (e.g. 'refs #1234')."
            ENV['GL_ID'] = nil
            exit 1
        end
        issue_number = comments[/#([0-9]+)/][/([0-9]+)/]

        # Change the username, password, hostname, and dbname in the following line
        #    to match your settings
        command_line_output = `mysql -N -u root -proot123 \
            -h 127.0.0.1 bitnami_redmine -e \
            "SELECT COUNT(*) FROM issues I INNER JOIN issue_statuses S \
            ON S.id = I.status_id WHERE S.is_closed = 0 AND I.id = #{issue_number};" 2>/dev/null`

        redmine_issue_open = command_line_output[0,1]
        if '0' == redmine_issue_open
          print "Issue ##{issue_number} is not in an open state."
          ENV['GL_ID'] = nil
          exit 1
        end
    end
end

require_relative '../lib/gitlab_custom_hook'
require_relative '../lib/gitlab_access'

if GitlabAccess.new(repo_path, key_id, refs).exec &&
    GitlabCustomHook.new.pre_receive(refs, repo_path)
  exit 0
else
  # reset GL_ID env since we stop git push here
  ENV['GL_ID'] = nil
  exit 1
end


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值