天天看點

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
           

繼續閱讀