Class: XZGit::XZMr

Inherits:
Command
  • Object
show all
Defined in:
lib/mrbin/xzcommand/xzmr.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

read_private_token, read_remote_project, run, verify_git_repo

Constructor Details

#initialize(argv) ⇒ XZMr

Returns a new instance of XZMr.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

Class Method Details

.optionsObject



7
8
9
10
11
12
13
14
15
# 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

Instance Method Details

#check_conflictObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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_branchObject



107
108
109
110
# File 'lib/mrbin/xzcommand/xzmr.rb', line 107

def get_current_branch()
    branchname = `git branch | grep \\* | cut -d ' ' -f2`
    branchname
end

#inspect_mr_msgObject



104
105
106
# 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

#runObject



56
57
58
# File 'lib/mrbin/xzcommand/xzmr.rb', line 56

def run
    create_mr(@assigneeid)
end

#validate!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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