gitorious-merge-request 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source :rubygems
2
+
3
+ gem 'activesupport', :require => 'active_support/all'
4
+
5
+ gem 'trollop'
6
+
7
+ gem 'highline'
8
+
9
+ gem 'mechanize'
10
+
11
+ #gemspec
@@ -0,0 +1,25 @@
1
+ Create Gitorious' merge requests from command-line
2
+
3
+ Installation
4
+ ===========
5
+
6
+ bundle install
7
+
8
+ Examples
9
+ ========
10
+
11
+ ./gitorious-merge-request --help
12
+ ./gitorious-merge-request new --help
13
+ ./gitorious-merge-request show --help
14
+ ./gitorious-merge-request rm --help
15
+
16
+ ./gitorious-merge-request new -e brauliobo@gmail.com -s 'test' -r '~brauliobo/noosfero/brauliobos-noosfero' -a easysafeinstall -b master -t 'noosfero/noosfero'
17
+ ./gitorious-merge-request show -c noosfero/noosfero:248
18
+
19
+ ./gitorious-merge-request rm -c eita/test:3 -e brauliobo@gmail.com
20
+
21
+
22
+
23
+
24
+
25
+
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path('../lib/gitorious-merge-request', File.dirname(__FILE__))
4
+
5
+ SubCommands = %w(new show rm)
6
+ global_opts = Trollop::options do
7
+ version "gitorious-merge-request 1.0.0"
8
+ banner <<-EOS
9
+ Manage Gitorious' merge requests
10
+
11
+ Usage:
12
+ gitorious-merge-request command [options]
13
+ List of available commands: #{SubCommands.join ' '}.
14
+ Use 'command --help' for command usage.
15
+ EOS
16
+ stop_on SubCommands
17
+ end
18
+
19
+ def login_opts
20
+ opt :email, "Email for login", :short => 'e', :type => String
21
+ end
22
+ def code_opts
23
+ opt :code, "project/repo:id (e.g. noosfero/noosfero:248)", :type => String
24
+ end
25
+
26
+ def login_dies opts
27
+ Trollop::die :email, "must exist" unless opts[:email]
28
+ end
29
+ def code_dies opts
30
+ Trollop::die :code, "must exist" unless opts[:code]
31
+ end
32
+
33
+ cmd = ARGV.shift # get the subcommand
34
+ cmd_opts =
35
+ case cmd
36
+ when 'new'
37
+ Trollop::options do
38
+ banner <<-EOS
39
+ new: create new merge request
40
+
41
+ Usage:
42
+ gitorious-merge-request new [options]
43
+ where [options] are:
44
+ EOS
45
+ login_opts
46
+ opt :summary, "Summary", :short => 's', :type => String
47
+ opt :proposal, "Proposal", :short => 'p', :type => String
48
+ opt :forked_repo, "Forked repo id (e.g. ~brauliobo/noosfero/brauliobos-noosfero)", :short => 'f', :type => String
49
+ opt :forked_branch, "Forked branch", :short => 'a', :type => String
50
+ opt :target_repo, "Target repo id (e.g. noosfero/noosfero)", :short => 't', :type => String
51
+ opt :target_branch, "Target branch", :short => 'b', :type => String
52
+ end
53
+ when 'show'
54
+ Trollop::options do
55
+ code_opts
56
+ end
57
+ when 'rm'
58
+ Trollop::options do
59
+ login_opts
60
+ code_opts
61
+ end
62
+ else
63
+ Trollop::die "use one of the subcommands: #{SubCommands.join ' '}"
64
+ end
65
+
66
+ include GitoriousMergeRequest
67
+
68
+ send "cmd_#{cmd}", cmd_opts
69
+
70
+
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('./lib/gitorious-merge-request', File.dirname(__FILE__))
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.authors = ["Braulio Bhavamitra"]
7
+ gem.email = ["brauliobo@gmail.com"]
8
+ gem.summary = "Manage Gitorious' merge requests (create, remove, show, ...)"
9
+ gem.description = gem.summary
10
+ gem.homepage = "http://github.com/brauliobo/gitorious-merge-request"
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "gitorious-merge-request"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = GitoriousMergeRequest::Version
18
+ end
@@ -0,0 +1,161 @@
1
+ require 'rubygems'
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ require 'bundler/setup'
4
+ Bundler.require
5
+
6
+ require 'highline/import'
7
+
8
+ module GitoriousMergeRequest
9
+
10
+ Version = '1.0.0'
11
+
12
+ MRUrl = "https://gitorious.org/%{repo}/merge_requests/%{id}"
13
+ NewMRUrl = "https://gitorious.org/%{forked_repo}/merge_requests/new"
14
+ CommitListUrl = "https://gitorious.org/%{forked_repo}/merge_requests/commit_list"
15
+
16
+ def login opts
17
+ @email = opts[:email]
18
+
19
+ password = ask('Password:'){ |q| q.echo = false }
20
+
21
+ puts 'Login...'
22
+ page = @mechanize.get 'https://gitorious.org/login'
23
+ form = page.forms.first
24
+ fields = form.fields
25
+ fields.find{ |f| f.name == 'email' }.value = @email
26
+ fields.find{ |f| f.name == 'password' }.value = password
27
+ page = form.submit
28
+
29
+ raise "Can't login" unless page.content.include?('Logged in successfully')
30
+ end
31
+
32
+ def cmd_new opts
33
+ login_dies opts
34
+ Trollop::die :summary, "must exist" unless opts[:summary]
35
+ Trollop::die :forked, "must exist" unless opts[:forked_repo]
36
+ Trollop::die :forked_branch, "must exist" unless opts[:forked_branch]
37
+ Trollop::die :target, "must exist" unless opts[:target_repo]
38
+ Trollop::die :target_branch, "must exist" unless opts[:target_branch]
39
+
40
+ @summary = opts[:summary]
41
+ @proposal = opts[:proposal]
42
+ @forked_repo = opts[:forked_repo]
43
+ @forked_branch = opts[:forked_branch]
44
+ @target_repo = opts[:target_repo]
45
+ @target_branch = opts[:target_branch]
46
+
47
+ def ending_commit(target_id)
48
+ forked_repo = @forked_repo.split('/')[1,2].join('/')
49
+
50
+ puts "Selecting last commit"
51
+ page = @mechanize.post CommitListUrl % {:forked_repo => forked_repo}, {
52
+ 'authenticity_token' => @authenticity_token,
53
+ 'merge_request[target_repository_id]' => target_id,
54
+ 'merge_request[target_branch]' => @target_branch,
55
+ 'merge_request[source_branch]' => @forked_branch}
56
+
57
+ # TODO: give option to select
58
+ first_commit = page.parser.css('#commit_0 input').first
59
+ raise 'No commit to merge' unless first_commit
60
+ first_commit.attr 'value'
61
+ end
62
+
63
+ def merge_request
64
+ forked_repo = @forked_repo.split('/')[1,2].join('/')
65
+ target_repo = @target_repo.split('/').last
66
+
67
+ puts "Opening merge request page"
68
+ page = @mechanize.get NewMRUrl % {:forked_repo => forked_repo}
69
+ form = page.form_with :id => 'new_merge_request'
70
+
71
+ form.set_fields 'merge_request[summary]' => @summary
72
+ form.set_fields 'merge_request[proposal]' => @proposal
73
+
74
+ @authenticity_token = form.fields.find{ |f| f.name == 'authenticity_token' }.value
75
+
76
+ target_option = page.parser.css('#merge_request_target_repository_id option').find{ |o| o.text == target_repo }
77
+ raise "Can't find target repository" unless target_option
78
+ target_id = target_option.attr('value')
79
+ form.set_fields 'merge_request[target_repository_id]' => target_id
80
+
81
+ target_branch_option = page.parser.css("#merge_request_target_branch option[value=#{@target_branch}]").first
82
+ raise "Can't find target branch" unless target_branch_option
83
+ form.set_fields 'merge_request[target_branch]' => @target_branch
84
+
85
+ forked_branch_option = page.parser.css("#merge_request_source_branch option[value=#{@forked_branch}]").first
86
+ raise "Can't find forked branch" unless forked_branch_option
87
+ form.set_fields 'merge_request[source_branch]' => @forked_branch
88
+
89
+ commit = ending_commit target_id
90
+ form.add_field! 'merge_request[ending_commit]', commit
91
+
92
+ page = form.submit
93
+ raise "Error while creating merge request" if page.parser.css('.errorExplanation').first
94
+
95
+ puts "YOUR NEW MERGE REQUEST\n"
96
+ cmd_show_for page
97
+ end
98
+
99
+ @mechanize = Mechanize.new
100
+ login opts
101
+ merge_request
102
+ end
103
+
104
+ def parse_code opts
105
+ @code = opts[:code]
106
+
107
+ raise "can't parse code" unless @code =~ /(.+):(\d+)/
108
+ repo = $1
109
+ id = $2.to_i
110
+
111
+ [repo, id]
112
+ end
113
+
114
+ def cmd_rm opts
115
+ login_dies opts
116
+ code_dies opts
117
+
118
+ repo, id = parse_code opts
119
+
120
+ @mechanize = Mechanize.new
121
+ login opts
122
+ page = @mechanize.get MRUrl % {:repo => repo, :id => id}
123
+ form = page.form_with :action => "/#{repo}/merge_requests/#{id}"
124
+ page = form.submit
125
+ end
126
+
127
+ def cmd_show opts
128
+ code_dies opts
129
+
130
+ repo, id = parse_code opts
131
+
132
+ @mechanize = Mechanize.new
133
+ page = @mechanize.get MRUrl % {:repo => repo, :id => id}
134
+
135
+ cmd_show_for page
136
+ end
137
+
138
+ def cmd_show_for page
139
+ raise "can't grab summary" unless page.parser.css('#content h1').first.text.strip =~ /[^#]+ #(\d+): (.+)/
140
+ id, summary = $1, $2
141
+ proposal = page.parser.css('.proposal').first.text.strip
142
+
143
+ lis = page.parser.css('ul.meta li')
144
+ raise "can't get meta content" if lis.empty?
145
+
146
+ a = lis[0].css('a').last
147
+ requester = "#{a.text.strip} (http://gitorious.org#{a.attr('href')})"
148
+
149
+ puts <<-EOS
150
+ Requester: #{requester}
151
+
152
+ ID: #{id}
153
+ Summary: #{summary}
154
+ Proposal:
155
+ #{proposal}
156
+
157
+ EOS
158
+ end
159
+
160
+ end
161
+
File without changes
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitorious-merge-request
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Braulio Bhavamitra
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-11-09 00:00:00 +00:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Manage Gitorious' merge requests (create, remove, show, ...)
23
+ email:
24
+ - brauliobo@gmail.com
25
+ executables:
26
+ - gitorious-merge-request
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README.markdown
35
+ - Rakefile
36
+ - bin/gitorious-merge-request
37
+ - gitorious-merge-request.gemspec
38
+ - lib/gitorious-merge-request.rb
39
+ - lib/gitorious-merge-request/.gitkeep
40
+ has_rdoc: true
41
+ homepage: http://github.com/brauliobo/gitorious-merge-request
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.4.1
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Manage Gitorious' merge requests (create, remove, show, ...)
74
+ test_files: []
75
+