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


你可能感兴趣的:(工具)