class XZGit::XZMr

Public Class Methods

new(argv) click to toggle source
Calls superclass method XZGit::Command::new
# File lib/mrbin/xzcommand/xzmr.rb, line 17
def initialize(argv)
    @targetbranch = argv.option('target','master')
    @sourcebranch = argv.option('source',get_current_branch())
    @mergetitle = argv.option('mrmsg','')
    @assignee = argv.option('assignee','')
    @remove = argv.flag?('remove',true)
     
    # if !check_conflict()
    #     puts "merge request has conflicts"
    #     exit(1)
    # end
    
    super
end
options() click to toggle source
Calls superclass method
# File lib/mrbin/xzcommand/xzmr.rb, line 7
def self.options 
    [
        ['--target','target branch name'],
        ['--source','source branch name'],
        ['--mrmsg','merge request message'],
        ['--assignee','who accept merge request'],
        ['--remove','remove source branch when accept mr']
    ].concat(super)
end

Public Instance Methods

check_conflict() click to toggle source
# File lib/mrbin/xzcommand/xzmr.rb, line 60
def check_conflict()
    gitworkspace = Dir.pwd
    `git -C #{gitworkspace} checkout #{@targetbranch}`
    `git -C #{gitworkspace} pull origin #{@targetbranch}`

    lastcommit = `git -C #{gitworkspace} rev-parse HEAD`
    mergemsg = `git merge #{@sourcebranch}`
    # `git -C #{gitworkspace} reset --hard #{lastcommit}`
    if mergemsg.include?('conflict')
        return false
    else
        return true
    end
end
create_mr(assineeid) click to toggle source
# File lib/mrbin/xzcommand/xzmr.rb, line 75
def create_mr(assineeid)
    project = XZGit.project
    if project.empty?
        puts "not exist remote url"
        exit(1)
    end
    token = XZGit.token()
    if token.empty?
        puts "not exist gitlab private token"
        exit(1)
    end
    url = "http://gitlab.idc.xiaozhu.com/api/v4/projects/#{project}/merge_requests"
    req_url = URI(url)
    req = Net::HTTP::Post.new(req_url)
    req['PRIVATE-TOKEN'] = token
    req.set_form_data('source_branch' => @sourcebranch,'target_branch' => @targetbranch,'title' => @mergetitle, 'assignee_id' => assineeid,'remove_source_branch' => @remove)
    res = Net::HTTP.start(req_url.hostname,req_url.port) do |http|
        http.request(req)
    end
    if res
        puts res.code
        if res.code == '201'
            puts "request mr suceess"
        else
            puts "request mr error #{JSON.parse(res.body)}"
        end
    end
end
get_current_branch() click to toggle source
# File lib/mrbin/xzcommand/xzmr.rb, line 107
def get_current_branch()
    branchname = `git branch | grep \\* | cut -d ' ' -f2`
    branchname
end
inspect_mr_msg() click to toggle source
# File lib/mrbin/xzcommand/xzmr.rb, line 104
def inspect_mr_msg()
    puts "create merge request sourcebranch => #{@sourcebranch} targetbranch => #{@targetbranch} mergetitle => #{@mergetitle} assignee => #{@assignee}"
end
run() click to toggle source
# File lib/mrbin/xzcommand/xzmr.rb, line 56
def run
    create_mr(@assigneeid)
end
validate!() click to toggle source
Calls superclass method
# File lib/mrbin/xzcommand/xzmr.rb, line 32
def validate!
    super
    if @mergetitle.empty?
        username = `id -un`
        username = username.strip
        @mergetitle = "create merge request by #{username}"
    end

    if @assignee.empty?
        puts "no specified assignee"
        exit(1)
    else
        assigneeid = XZGit.usermap[@assignee]
        if !assigneeid
            puts "can not found assignee"
            exit(1)
        end
        @assigneeid = assigneeid
    end
    @sourcebranch = @sourcebranch.strip
    inspect_mr_msg()
    
end