glman 0.0.9 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,53 @@
1
+ module Glman
2
+ module Commands
3
+ module Configs
4
+ class AliasesConfig
5
+ class AliasesConfigurationError < StandardError; end
6
+
7
+ include InitRequired
8
+
9
+ attr_required :config_manager
10
+
11
+ DEFAULT = {}
12
+
13
+ def add(hash={})
14
+ email = hash[:email]
15
+ alias_name = hash[:alias_name]
16
+ hash = alias_name.nil? ? symbolize_keys(hash) : { alias_name.to_sym => email }
17
+ aliases = get
18
+ aliases = aliases.merge(hash)
19
+ set(aliases)
20
+ end
21
+
22
+ def delete(alias_name)
23
+ aliases = config_manager.get[:aliases] || {}
24
+ aliases.delete_if{ |k, _| k==alias_name.to_s }
25
+ set(aliases)
26
+ end
27
+
28
+ def set(hash={})
29
+ raise AliasesConfigurationError.new('incorrect aliases data') unless hash.kind_of? Hash
30
+ hash = nil if hash.empty?
31
+ config_manager.set(aliases: hash)
32
+ end
33
+
34
+ def get
35
+ (config_manager.get || {})[:aliases] || DEFAULT
36
+ end
37
+
38
+ def clear
39
+ set(DEFAULT)
40
+ end
41
+
42
+ private
43
+
44
+ def symbolize_keys(hash)
45
+ hash.inject({}) do |result, (key, value)|
46
+ result[key.to_sym] = value
47
+ result
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,42 @@
1
+ require 'uri'
2
+
3
+ module Glman
4
+ module Commands
5
+ module Configs
6
+ class GitlabConfig
7
+ class GitlabConfigurationError < StandardError; end
8
+
9
+ include InitRequired
10
+
11
+ attr_required :config_manager
12
+
13
+ DEFAULT = { url: '', private_token: '' }
14
+
15
+ def set(hash={})
16
+ raise GitlabConfigurationError.new('incorrect data') unless hash.kind_of? Hash
17
+ raise GitlabConfigurationError.new "url is incorrect" unless valid_url?(hash[:url])
18
+ raise GitlabConfigurationError.new "private_token missing" if hash[:private_token].to_s.strip.empty?
19
+
20
+ gitlab = {url: hash[:url], private_token: hash[:private_token]}
21
+
22
+ config_manager.set(gitlab: gitlab)
23
+ end
24
+
25
+ def get
26
+ (config_manager.get || {})[:gitlab] || DEFAULT
27
+ end
28
+
29
+ def clear
30
+ config_manager.set(gitlab: DEFAULT)
31
+ end
32
+
33
+ private
34
+
35
+ def valid_url?(url)
36
+ url = URI.parse(url) rescue false
37
+ url.kind_of?(URI::HTTP) || url.kind_of?(URI::HTTPS)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,48 @@
1
+ module Glman
2
+ module Commands
3
+ module Configs
4
+ class NotifyIrcConfig
5
+ class NotifyIrcConfigurationError < StandardError; end
6
+
7
+ include InitRequired
8
+
9
+ attr_required :config_manager
10
+
11
+ DEFAULT = {
12
+ nick: 'glman',
13
+ channel: 'glman_notify',
14
+ server: 'irc.freenode.net',
15
+ port: 6697,
16
+ ssl: true
17
+ }
18
+
19
+ def set(opts={})
20
+ raise NotifyIrcConfigurationError.new('incorrect data') unless opts.kind_of? Hash
21
+ notify = config_manager.get[:notify] || {}
22
+ irc = notify[:irc] || {}
23
+
24
+ irc[:server] = opts[:server] || irc[:server] || DEFAULT[:server]
25
+ irc[:nick] = opts[:nick] || irc[:nick] || DEFAULT[:nick]
26
+ irc[:port] = (opts[:port] || irc[:port] || DEFAULT[:port]).to_i
27
+ irc[:channel] = opts[:channel] || irc[:channel] || DEFAULT[:channel]
28
+ irc[:ssl] = if opts[:ssl].nil?
29
+ irc[:ssl].nil? ? true : irc[:ssl]
30
+ else
31
+ opts[:ssl].to_s == 'true'
32
+ end
33
+
34
+ notify[:irc] = irc
35
+ config_manager.set(notify: notify)
36
+ end
37
+
38
+ def get
39
+ (config_manager.get[:notify] || {})[:irc] || DEFAULT
40
+ end
41
+
42
+ def clear
43
+ set(DEFAULT)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,29 @@
1
+ module Glman
2
+ module Commands
3
+ module Configs
4
+ class UsersConfig
5
+ class UsersConfigurationError < StandardError; end
6
+
7
+ include InitRequired
8
+
9
+ attr_required :config_manager
10
+
11
+ DEFAULT = {}
12
+
13
+ def set(users={})
14
+ raise UsersConfigurationError.new('incorrect data') unless users.kind_of? Hash
15
+ hash = nil if hash.empty?
16
+ config_manager.set(users: users)
17
+ end
18
+
19
+ def get
20
+ config_manager.get[:users] || DEFAULT
21
+ end
22
+
23
+ def clear
24
+ set(DEFAULT)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,56 @@
1
+ module Glman
2
+ module Commands
3
+ class HelpMessages
4
+ def self.intro
5
+ "Glman ver: #{VERSION}"
6
+ end
7
+
8
+ def self.unknown_command
9
+ "Command unknown"
10
+ end
11
+
12
+ def self.show
13
+ %{
14
+ Need help :D
15
+
16
+ commands:
17
+
18
+ config # display current configuration
19
+ config gitlab # show gitlab configuration
20
+ config users # show users configuration
21
+ config aliases # show aliases configuration
22
+ config notify_irc # show notify_irc configuration
23
+
24
+ you can show specific config details:
25
+
26
+ config gitlab url # specific attribute in gitlab config
27
+ config users pniemczyk@o2 id # specific attribute in users config
28
+
29
+ config glman url:http://site private_token:123 --set
30
+ config glman --clear
31
+
32
+ config notify_irc server:irc.org channel:free ssl:true port:10 nick:test --set
33
+ config notify_irc --clear
34
+
35
+ config aliases pn:pawel@o2.pl --add
36
+ config aliases pn --del
37
+ config aliases --clear
38
+
39
+ cache_users # build user cache for better performance RECOMMENDED
40
+ cache_users --clear # clear user cache
41
+
42
+ mr <user_email_or_alias> # create merge request for user for current branch to master with title as last commit message
43
+
44
+ mr <user_email_or_alias> --push # push before make a merge request
45
+ mr <user_email_or_alias> --notify # notify on irc after merge request
46
+
47
+ mr <user_email_or_alias> --push --notify # :D
48
+
49
+ mr <user_email_or_alias> <message> <target_branch> --push <origin> # full options for merge request (default origin is a origin :D)
50
+
51
+ Any questions pniemczyk@o2.pl or go to https://github.com/pniemczyk/glman
52
+ }
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,70 @@
1
+ module Glman
2
+ module Commands
3
+ class Mr
4
+ class IncorrectMrOptionsError < StandardError; end
5
+ attr_reader :projects_repo, :git_repo, :config
6
+
7
+ def initialize(opts={})
8
+ @git_repo = opts.fetch(:git_repo)
9
+ @config = opts.fetch(:config)
10
+ @projects_repo = opts.fetch(:projects_repo)
11
+ end
12
+
13
+ def create(params={})
14
+ user_id, msg, target_branch = params[:user_id], params[:msg], params.fetch(:target_branch, 'master')
15
+ raise IncorrectMrOptionsError.new('Merge request branch target and source are the same.') if target_branch == current_branch
16
+ mr_opts = {assignee_id: user_id, title: msg, source_branch: current_branch, target_branch: target_branch}
17
+ prepare_details(create_merge_request(mr_opts))
18
+ end
19
+
20
+ def get_all
21
+ projects_repo.get_merge_requests(git_repo.repository_name)
22
+ end
23
+
24
+ private
25
+
26
+ def create_merge_request(opts)
27
+ projects_repo.create_merge_request(repository_name, opts)
28
+ end
29
+
30
+ def repository_name
31
+ @repository_name ||= git_repo.repository_name
32
+ end
33
+
34
+ def current_branch
35
+ @current_branch ||= git_repo.current_branch
36
+ end
37
+
38
+ def gitlab_url
39
+ @gitlab_url ||= config[:url]
40
+ end
41
+
42
+ def prepare_details(create_result)
43
+ assignee = create_result.fetch('assignee', {}) || {}
44
+ author = create_result.fetch('author', {}) || {}
45
+ url = "#{gitlab_url}/#{repository_name}/merge_requests/#{create_result['iid']}"
46
+ {
47
+ repository_name: repository_name,
48
+ current_branch: current_branch,
49
+ target_branch: create_result['target_branch'],
50
+ message: create_result['title'],
51
+ url: url,
52
+ diff_url: "#{url}/diffs",
53
+ assignee: {
54
+ username: assignee['username'],
55
+ email: assignee['email'],
56
+ name: assignee['name']
57
+ },
58
+ author: {
59
+ username: author['username'],
60
+ email: author['email'],
61
+ name: author['name']
62
+ },
63
+ id: create_result['id'],
64
+ iid: create_result['iid'],
65
+ created_at: assignee['created_at']
66
+ }
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,48 @@
1
+ require "irc-notify"
2
+
3
+ module Glman
4
+ module Commands
5
+ class Notify
6
+ attr_reader :config, :irc_config, :irc_client, :nick, :server, :port, :ssl
7
+
8
+ def initialize(opts={})
9
+ @config = opts.fetch(:config)
10
+ end
11
+
12
+ def send(msg)
13
+ irc_client.register(nick)
14
+ irc_client.notify(channel, msg)
15
+ client.quit
16
+ end
17
+
18
+ def nick
19
+ @nick ||= irc_config.fetch(:nick)
20
+ end
21
+
22
+ def channel
23
+ @channel ||= irc_config.fetch(:channel)
24
+ end
25
+
26
+ def server
27
+ @server ||= irc_config.fetch(:server)
28
+ end
29
+
30
+ def port
31
+ @port ||= irc_config.fetch(:port)
32
+ end
33
+
34
+ def ssl
35
+ @ssl ||= irc_config.fetch(:ssl)
36
+ end
37
+
38
+ def irc_config
39
+ @irc_config ||= config.fetch(:notify_cfg).fetch(:irc)
40
+ end
41
+
42
+ def irc_client
43
+ @irc_client ||= IrcNotify::Client.build(server, port, ssl: ssl)
44
+ end
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,39 @@
1
+ require 'yaml'
2
+ require 'hashie'
3
+
4
+ module Glman
5
+ class ConfigManager
6
+ class SetConfigError < StandardError; end
7
+
8
+ def get
9
+ File.exist?(config_file) ? load_configuration : {}
10
+ end
11
+
12
+ def set(hash)
13
+ raise SetConfigError, 'argument is not kind of hash' unless hash.kind_of?(Hash)
14
+ updated_config = get.merge(hash)
15
+
16
+ save_configuration(updated_config)
17
+ end
18
+
19
+ def clear
20
+ File.write(config_file, {}.to_yaml)
21
+ end
22
+
23
+ private
24
+
25
+ def save_configuration(config)
26
+ File.write(config_file, config.to_yaml)
27
+ end
28
+
29
+ def load_configuration
30
+ Hashie::Mash.new(YAML.load_file(config_file))
31
+ rescue
32
+ {}
33
+ end
34
+
35
+ def config_file
36
+ File.expand_path('.glmanrc',Dir.home)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,9 @@
1
+ require 'awesome_print'
2
+
3
+ module Glman
4
+ class DataPresenter
5
+ def self.show(object, options)
6
+ ap object, options
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,40 @@
1
+ module Glman
2
+ module InitRequired
3
+ class InitializationError < StandardError; end
4
+
5
+ module ClassMethods
6
+ def attr_required(*attrs)
7
+ attrs.each do |attr|
8
+ self.send(:attr_accessor, attr)
9
+ end
10
+ @__required_attributes = (@__required_attributes || []) + attrs
11
+ end
12
+ end
13
+
14
+ module InstanceMethods
15
+ def initialize(params={})
16
+ params.each do |attr, value|
17
+ self.send("#{attr}=", value)
18
+ end if params
19
+ required_attrs_valid?(params)
20
+ end
21
+
22
+ private
23
+
24
+ def required_attrs_valid?(params)
25
+ required_attrs = self.class.instance_variable_get(:@__required_attributes) || []
26
+ return if required_attrs.empty?
27
+
28
+ required_attrs.each do |key|
29
+ sym_key = key.to_sym
30
+ raise Glman::InitRequired::InitializationError.new("Missing #{sym_key} param during initiaization #{self.class}") unless params.has_key?(sym_key)
31
+ end
32
+ end
33
+ end
34
+
35
+ def self.included(base)
36
+ base.send(:include, InstanceMethods)
37
+ base.extend(ClassMethods)
38
+ end
39
+ end
40
+ end