glman 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ Y2Y1MmEwOTA1NDllMGM3YWUxMmUyM2FjNmM0Nzk4YzU1YzE1MDliMw==
5
+ data.tar.gz: !binary |-
6
+ ZDE5YjRlZjdhZTgxZjUxZWZlYTM1MDUxYjQ5NTcwZjViZDllODkxOA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MjM2OWFlN2JkNzU3NjA4MjRiZGQxZDgwNzY3ODBhN2ZhNmRkMTMwNjgyODgx
10
+ ZGY4M2JkOGYwZDllMTYxYjYwMDE2MzMzZTA0N2VhYmQyNjZiNzU4OTMxMmQ3
11
+ ZjNiZDQ5ZmMyN2I2MDNlYTU1MzA2NmI1ODU3YjNjZTE5NmVkZDk=
12
+ data.tar.gz: !binary |-
13
+ ZjcyYTU2NTlmOWQzZGIyZTdhNWY4ZWJlMDc0ZjE1ODU4YWUwNGFkZjYyM2Ez
14
+ Zjg5Y2IyNDQ1Nzc5MWEzNjdlMjdkNjZjNDk5ZmNjYTI3ZjUyNWI2MjFkYmEw
15
+ NzZjYTlhOTk1M2JjNGZhZTRiNTliY2VjNWYwNDUzMTNmMmJmYmM=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in glman.gemspec
4
+ gemspec
5
+
6
+ gem "httpclient"
7
+ gem "executable"
8
+ gem "json"
9
+ gem "active_support"
10
+ gem "awesome_print"
11
+ gem "hashie"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Paweł Niemczyk
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Glman
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'glman'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install glman
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/glman ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'glman'
4
+
5
+ Glman::Commands::Base.run
data/glman.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ # coding: utf-8
4
+ lib = File.expand_path('../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'glman/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = "glman"
10
+ spec.version = Glman::VERSION
11
+ spec.authors = ["Paweł Niemczyk"]
12
+ spec.email = ["pniemczyk@o2.pl"]
13
+ spec.description = %q{Git Lab Manager}
14
+ spec.summary = %q{Git Lab Manager}
15
+ spec.homepage = "https://github.com/pniemczyk/glman"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files`.split($/)
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_dependency "httpclient"
26
+ spec.add_dependency "executable"
27
+ spec.add_dependency "json"
28
+ spec.add_dependency "active_support"
29
+ spec.add_dependency "awesome_print"
30
+ spec.add_dependency "hashie"
31
+ end
data/lib/glman.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "glman/version"
2
+ require "active_support/dependencies"
3
+
4
+ module Glman
5
+ extend ActiveSupport::Dependencies
6
+ autoload_paths << File.expand_path('../', __FILE__)
7
+ end
@@ -0,0 +1,136 @@
1
+ require "glman/version"
2
+ require 'executable'
3
+ require 'awesome_print'
4
+
5
+ module Glman
6
+ module Commands
7
+ class Base
8
+ include Executable
9
+
10
+ # merge_request user_name/email message target_branch
11
+ def mr(params)
12
+ return show_all_mrs if show?
13
+ user_name = params[0]
14
+ current_branch = git_repo.current_branch
15
+ p 'You realy want to create merge request master to master ?' if current_branch == 'master'
16
+ target_branch = params[2] || 'master'
17
+ user_id = get_user_id(user_name)
18
+ message = params[1] || git_repo.last_commit_message
19
+ repository_name = git_repo.repository_name
20
+
21
+ params = {assignee_id: user_id, title: message, source_branch: current_branch, target_branch: target_branch}
22
+
23
+ projects_repo.create_merge_request(repository_name, params)
24
+ ap params.merge({user_name: user_name, repository_name: repository_name})
25
+ end
26
+
27
+ def show=(bool)
28
+ @show = bool
29
+ end
30
+
31
+ def show?
32
+ @show
33
+ end
34
+
35
+ def get_user_id(name)
36
+ user = nil
37
+ email = (configuration.load[:aliases] || {})[name]
38
+ user = (configuration.load[:users] || {})[email] if email
39
+ user = users_repo.find(email: name) unless user
40
+ user[:id] if user
41
+ end
42
+
43
+ # Set/Get configuration
44
+ def config(params=[])
45
+ build_config(*params) if init?
46
+ ap configuration.load || "No configuration yet"
47
+ end
48
+
49
+ # Set user alias
50
+ def user_alias(params)
51
+ if clear?
52
+ configuration.clear_user_aliases
53
+ else
54
+ configuration.add_user_alias(email: params[0], alias: params[1])
55
+ end
56
+ config
57
+ end
58
+
59
+ # Make Cache for user
60
+ def cache
61
+ if clear?
62
+ configuration.set_user_list({})
63
+ else
64
+ users = {}.tap do |h|
65
+ users_repo.list.each{ |u| h[u['email']] = u }
66
+ end
67
+ configuration.set_user_list(users)
68
+ end
69
+ config
70
+ end
71
+
72
+ def clear=(bool)
73
+ @clear = bool
74
+ end
75
+
76
+ def clear?
77
+ @clear
78
+ end
79
+
80
+
81
+ #initialize configutation | cmd glman config [gitlab_url] [private_token] --init
82
+ def init=(bool)
83
+ @init = bool
84
+ end
85
+
86
+ #
87
+ def init?
88
+ @init
89
+ end
90
+
91
+ # Show help
92
+ def help?
93
+ puts 'help me :D'
94
+ exit
95
+ end
96
+ alias :h? :help?
97
+
98
+ #Exec
99
+ def call(name=nil, *params)
100
+ case name.to_s.strip
101
+ when 'config' then config(params)
102
+ when 'alias' then user_alias(params)
103
+ when 'cache' then cache
104
+ when 'mr' then mr(params)
105
+ when '' then puts '-'
106
+ else puts "what ?"
107
+ end
108
+ end
109
+ private
110
+
111
+ def show_all_mrs
112
+ ap projects_repo.get_merge_requests(git_repo.repository_name)
113
+ end
114
+
115
+ def build_config(gitlab_url, private_token)
116
+ configuration.build_config(gitlab_url: gitlab_url, private_token: private_token)
117
+ end
118
+
119
+ def configuration
120
+ @configuration ||= Configuration.new
121
+ end
122
+
123
+ def users_repo
124
+ @users_repo ||= Repos::UsersRepo.new(configuration.load)
125
+ end
126
+
127
+ def projects_repo
128
+ @projects_repo ||= Repos::ProjectsRepo.new(configuration.load)
129
+ end
130
+
131
+ def git_repo
132
+ @git_repo ||= Repos::GitRepo.new
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,54 @@
1
+ require 'yaml'
2
+ require 'hashie'
3
+
4
+ module Glman
5
+ class Configuration
6
+
7
+ def load
8
+ File.exist?(config_file) ? Hashie::Mash.new(YAML.load_file(config_file)) : nil
9
+ end
10
+
11
+ def build_config(config={})
12
+ validare_config(config)
13
+ File.write(config_file, config.to_yaml)
14
+ end
15
+
16
+ def add_user_alias(opts={})
17
+ config = load
18
+ aliases = config[:aliases] || {}
19
+ aliases[opts.fetch(:alias)] = opts.fetch(:email)
20
+ config[:aliases] = aliases
21
+ File.write(config_file, config.to_yaml)
22
+ end
23
+
24
+ def clear_user_aliases
25
+ config = load
26
+ config[:aliases] = {}
27
+ File.write(config_file, config.to_yaml)
28
+ end
29
+
30
+ def set_user_list(users)
31
+ config = load
32
+ config[:users] = users
33
+ File.write(config_file, config.to_yaml)
34
+ end
35
+
36
+ private
37
+
38
+ def validare_config(hash)
39
+ raise ArgumentError, 'config is not hash' unless hash.kind_of?(Hash)
40
+ raise ArgumentError, 'private_token missing in configuration' unless hash.has_key?(:private_token)
41
+ raise ArgumentError, 'gitlab_url missing in configuration' unless hash.has_key?(:gitlab_url)
42
+ end
43
+
44
+ def config_load
45
+ Hashie::Mash.new(YAML.load_file(config_file)).tap do |config|
46
+ validare_config(config)
47
+ end
48
+ end
49
+
50
+ def config_file
51
+ File.expand_path('.glmanrc',Dir.home)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,29 @@
1
+ module Glman
2
+ module Repos
3
+ class GitRepo
4
+ def remote_origin_ssh_url
5
+ %x[git remote -v].split("\t").select{ |c| c.include?('(push)')}.first.gsub('(push)', '').gsub("\n", '').strip
6
+ rescue
7
+ nil
8
+ end
9
+
10
+ def repository_name
11
+ remote_origin_ssh_url.split(':').last.gsub('.git','')
12
+ rescue
13
+ nil
14
+ end
15
+
16
+ def current_branch
17
+ %x[git br].split("\n").select{ |c| c.include?('*')}.first.gsub('*','').strip
18
+ rescue
19
+ nil
20
+ end
21
+
22
+ def last_commit_message
23
+ %x[git log -1].split("\n").select{|c| c.include?('Subject:')}.first.gsub('Subject:','').strip
24
+ rescue
25
+ nil
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,107 @@
1
+ require 'httpclient'
2
+ require 'uri'
3
+ require 'cgi'
4
+ require 'json'
5
+
6
+ module Glman
7
+ module Repos
8
+ class ProjectsRepo
9
+ PER_PAGE = 10000
10
+ def initialize(opts={})
11
+ @gitlab_url = opts.fetch(:gitlab_url)
12
+ @private_token = opts.fetch(:private_token)
13
+ end
14
+ def list
15
+ all
16
+ end
17
+
18
+ def get(id)
19
+ get_base(id)
20
+ end
21
+
22
+ def get_events(id)
23
+ get_base(id, 'events')
24
+ end
25
+
26
+ def get_team_members(id, member_id=nil)
27
+ path = ['members', member_id].compact.join('/')
28
+ get_base(id, path)
29
+ end
30
+
31
+ def get_hooks(id, hook_id=nil)
32
+ path = ['hooks', hook_id].compact.join('/')
33
+ get_base(id, path)
34
+ end
35
+
36
+ def get_branches(id, branch_id=nil)
37
+ path = ['repository/branches', branch_id].compact.join('/')
38
+ get_base(id, path)
39
+ end
40
+
41
+ def get_merge_requests(id, mk_id=nil)
42
+ path = ['merge_requests', mk_id].compact.join('/')
43
+ get_base(id, path)
44
+ end
45
+
46
+ def create_merge_request(id, opts={})
47
+ merge_request_params_validate(opts)
48
+ #source_branch, target_branch, assignee_id, title
49
+ client.post(url(id: id, path: 'merge_requests'), opts)
50
+ end
51
+
52
+ def find(opts={})
53
+ opts = Hash[opts.map{ |k, v| [k.to_s, v] }]
54
+ all.each do |project|
55
+ return project if project.eql?(project.merge(opts))
56
+ end
57
+ nil
58
+ end
59
+
60
+ private
61
+ attr_reader :gitlab_url, :private_token
62
+
63
+ def merge_request_params_validate(params)
64
+ raise ArgumentError, 'source_branch missing in configuration' unless params.has_key?(:source_branch)
65
+ raise ArgumentError, 'target_branch missing in configuration' unless params.has_key?(:target_branch)
66
+ raise ArgumentError, 'title missing in configuration' unless params.has_key?(:title)
67
+ end
68
+
69
+ def get_base(id, path=nil)
70
+ JSON.parse(client.get(url(id: id, path: path)).body)
71
+ end
72
+
73
+ def all
74
+ JSON.parse(client.get(url).body)
75
+ end
76
+
77
+ def url(opts={})
78
+ id = CGI.escape(opts[:id].to_s) if opts[:id]
79
+ path = opts[:path]
80
+ uri = [gitlab_url, 'api', 'v3', 'projects', id, path, "?private_token=#{private_token}&per_page=#{PER_PAGE}"].compact.join('/')
81
+ URI.join(uri)
82
+ end
83
+
84
+ def client
85
+ @client ||= HTTPClient.new
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+
92
+ # USAGE
93
+
94
+
95
+ # glman mk rafal.klimek "to co" source_branch target_branch
96
+
97
+ # glman mr rafal
98
+
99
+ # glman setconfig gl_url token
100
+
101
+ # glman cache users
102
+
103
+ # glman user alias rafal rafal.klimek
104
+
105
+
106
+ # TODO
107
+
@@ -0,0 +1,46 @@
1
+ require 'httpclient'
2
+ require 'uri'
3
+ require 'json'
4
+
5
+ module Glman
6
+ module Repos
7
+ class UsersRepo
8
+ PER_PAGE = 10000
9
+ def initialize(opts={})
10
+ @gitlab_url = opts.fetch(:gitlab_url)
11
+ @private_token = opts.fetch(:private_token)
12
+ end
13
+ def list
14
+ all
15
+ end
16
+
17
+ def get(id)
18
+ JSON.parse(client.get(url(id)).body)
19
+ end
20
+
21
+ def find(opts={})
22
+ opts = Hash[opts.map{ |k, v| [k.to_s, v] }]
23
+ all.each do |user|
24
+ return user if user.eql?(user.merge(opts))
25
+ end
26
+ nil
27
+ end
28
+
29
+ private
30
+ attr_reader :gitlab_url, :private_token
31
+
32
+ def all
33
+ JSON.parse(client.get(url).body)
34
+ end
35
+
36
+ def url(id=nil)
37
+ url = [gitlab_url, 'api', 'v3', 'users', id, "?private_token=#{private_token}&per_page=#{PER_PAGE}"].compact.join('/')
38
+ URI.join(url)
39
+ end
40
+
41
+ def client
42
+ @client ||= HTTPClient.new
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Glman
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: glman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Paweł Niemczyk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httpclient
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: executable
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: active_support
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: awesome_print
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: hashie
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Git Lab Manager
126
+ email:
127
+ - pniemczyk@o2.pl
128
+ executables:
129
+ - glman
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - bin/glman
139
+ - glman.gemspec
140
+ - lib/glman.rb
141
+ - lib/glman/commands/base.rb
142
+ - lib/glman/configuration.rb
143
+ - lib/glman/repos/git_repo.rb
144
+ - lib/glman/repos/projects_repo.rb
145
+ - lib/glman/repos/users_repo.rb
146
+ - lib/glman/version.rb
147
+ homepage: https://github.com/pniemczyk/glman
148
+ licenses:
149
+ - MIT
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ! '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ! '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.1.10
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: Git Lab Manager
171
+ test_files: []