Class: Gitorious::MergeRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/gitorious/merge_request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ MergeRequest

Returns a new instance of MergeRequest.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gitorious/merge_request.rb', line 10

def initialize *args
  repo = args[0]
  if args[1].is_a?(Nokogiri::XML::Node)
    page = args[1]
  else
    id = args[1]
    page = Nokogiri::HTML $mech.get(Gitorious::MRUrlXml % {:repo => repo, :id => id}).content
  end

  self.id = page.at("id").content
  self.summary = page.at("summary").content
  self.proposal = page.at("proposal").content
  self.username = page.at("username").content
  self.target_repo = repo
  self.target_branch = page.at('target_repository/branch').content
  self.forked_repo = "#{self.username}/#{page.at('source_repository/name').content}"
  self.forked_branch = page.at('source_repository/branch').content
  self.ending_commit = page.at("ending-commit").content
  version = page.xpath('//versions/version').last
  self.merge_base_sha = version.at('merge_base_sha').content if version
end

Instance Attribute Details

#ending_commit ⇒ Object

Returns the value of attribute ending_commit.



8
9
10
# File 'lib/gitorious/merge_request.rb', line 8

def ending_commit
  @ending_commit
end

#forked_branch ⇒ Object

Returns the value of attribute forked_branch.



7
8
9
# File 'lib/gitorious/merge_request.rb', line 7

def forked_branch
  @forked_branch
end

#forked_repo ⇒ Object

Returns the value of attribute forked_repo.



7
8
9
# File 'lib/gitorious/merge_request.rb', line 7

def forked_repo
  @forked_repo
end

#id ⇒ Object

Returns the value of attribute id.



3
4
5
# File 'lib/gitorious/merge_request.rb', line 3

def id
  @id
end

#merge_base_sha ⇒ Object

Returns the value of attribute merge_base_sha.



8
9
10
# File 'lib/gitorious/merge_request.rb', line 8

def merge_base_sha
  @merge_base_sha
end

#proposal ⇒ Object

Returns the value of attribute proposal.



5
6
7
# File 'lib/gitorious/merge_request.rb', line 5

def proposal
  @proposal
end

#summary ⇒ Object

Returns the value of attribute summary.



5
6
7
# File 'lib/gitorious/merge_request.rb', line 5

def summary
  @summary
end

#target_branch ⇒ Object

Returns the value of attribute target_branch.



6
7
8
# File 'lib/gitorious/merge_request.rb', line 6

def target_branch
  @target_branch
end

#target_repo ⇒ Object

Returns the value of attribute target_repo.



6
7
8
# File 'lib/gitorious/merge_request.rb', line 6

def target_repo
  @target_repo
end

#username ⇒ Object

Returns the value of attribute username.



4
5
6
# File 'lib/gitorious/merge_request.rb', line 4

def username
  @username
end

Class Method Details

.create(attrs = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gitorious/merge_request.rb', line 45

def self.create attrs = {}
  @summary = attrs[:summary]
  @proposal = attrs[:proposal]
  @forked_repo = attrs[:forked_repo]
  @forked_branch = attrs[:forked_branch]
  @target_repo = attrs[:target_repo]
  @target_branch = attrs[:target_branch]

  forked_repo = @forked_repo.split('/')[1,2].join('/')
  target_repo = @target_repo.split('/').last

  page = $mech.get Gitorious::NewMRUrl % {:forked_repo => forked_repo}
  form = page.form_with :id => 'new_merge_request'

  form.set_fields 'merge_request[summary]' => @summary
  form.set_fields 'merge_request[proposal]' => @proposal

  @authenticity_token = form.fields.find{ |f| f.name == 'authenticity_token' }.value

  target_option = page.parser.css('#merge_request_target_repository_id option').find{ |o| o.text == target_repo }
  raise "Can't find target repository" unless target_option
  target_id = target_option['value']
  form.set_fields 'merge_request[target_repository_id]' => target_id

  target_branch_option = page.parser.css("#merge_request_target_branch option[value=#{@target_branch}]").first
  raise "Can't find target branch" unless target_branch_option
  form.set_fields 'merge_request[target_branch]' => @target_branch

  forked_branch_option = page.parser.css("#merge_request_source_branch option[value=#{@forked_branch}]").first
  raise "Can't find forked branch" unless forked_branch_option
  form.set_fields 'merge_request[source_branch]' => @forked_branch

  commit = self.select_ending_commit target_id
  form.add_field! 'merge_request[ending_commit]', commit

  page = form.submit
  raise "Error while creating merge request" if page.parser.css('.errorExplanation').first

  repo = @target_repo
  id = page.uri.to_s.split('/').last
  mr = self.new repo, id
end

.delete(repo, id) ⇒ Object



39
40
41
42
43
# File 'lib/gitorious/merge_request.rb', line 39

def self.delete repo, id
  page = $mech.get Gitorious::MRUrl % {:repo => repo, :id => id}
  form = page.form_with :action => "/#{repo}/merge_requests/#{id}"
  page = form.submit
end

.list(repo, status = "Open") ⇒ Object



32
33
34
35
36
37
# File 'lib/gitorious/merge_request.rb', line 32

def self.list repo, status="Open"
  page = Nokogiri::HTML $mech.get(Gitorious::MRListUrl % {:repo => repo, :status => status}).content
  page.xpath('//merge-request').map do |node|
    mr = self.new repo, node
  end
end

Instance Method Details

#branch ⇒ Object



88
89
90
# File 'lib/gitorious/merge_request.rb', line 88

def branch
  "merge-requests/#{self.id}"
end

#checkout(remote = 'origin', options = {}) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/gitorious/merge_request.rb', line 92

def checkout remote='origin', options = {}
  if $current_branch != self.branch
    Git.branch "-D #{branch}", :quiet => true
    Git.checkout "-b #{branch} #{self.merge_base_sha}", options
  end
  Git.pull "#{remote} refs/#{branch}", :quiet => true
end

#diff(remote = 'origin') ⇒ Object



100
101
102
103
104
# File 'lib/gitorious/merge_request.rb', line 100

def diff remote='origin'
  self.checkout remote, :quiet => true
  Git.checkout $current_branch, :quiet => true
  Git.diff branch
end