lib/glman/commands/base.rb in glman-0.0.9 vs lib/glman/commands/base.rb in glman-0.1.0

- old
+ new

@@ -8,211 +8,159 @@ class Base include Executable # merge_request user_name/email message target_branch def mr(params) - return show_all_mrs if show? - user_name = params[0] - current_branch = git_repo.current_branch + return dp mr_command.get_all if params.length == 0 + user_name = params[0] + user_id = get_user_id(user_name) + target_branch = params[2] || 'master' + current_branch = git_repo.current_branch + msg = params[1] || git_repo.last_commit_message || current_branch - if current_branch == 'master' - p 'Merge request from master to master is not so good idea!' - return - end - - target_branch = params[2] || 'master' - user_id = get_user_id(user_name) - message = params[1] || git_repo.last_commit_message || current_branch - repository_name = git_repo.repository_name - - params = {assignee_id: user_id, title: message, source_branch: current_branch, target_branch: target_branch} - push_branch_first(origin, current_branch) unless origin.nil? - opts = projects_repo.create_merge_request(repository_name, params) - assignee = opts['assignee'] || {} - author = opts['author'] || {} - info = { - url: "#{configuration.load[:gitlab_url]}/#{repository_name}/merge_requests/#{opts['iid']}", - assignee: { - username: assignee['username'], - email: assignee['email'], - name: assignee['name'] - }, - author: { - username: author['username'], - email: author['email'], - name: author['name'] - }, - id: opts['id'], - iid: opts['iid'], - created_at: assignee['created_at'] - } - ap params.merge({repository_name: repository_name}.merge(info)) + result = mr_command.create(user_id: user_id, msg: msg, target_branch: target_branch) + dp result + irc_notify("Please review my merge request: #{result[:diff_url]}") if notify? + rescue Exception => e + dp e.message end def push=(origin=nil) @origin = origin || 'origin' end - def show=(bool) - @show = bool - end - - def show? - @show - end - def get_user_id(name) user = nil - email = (configuration.load[:aliases] || {})[name] - user = (configuration.load[:users] || {})[email] if email + email = (configuration.get(:aliases) || {})[name] + user = (configuration.get(:users) || {})[email] if email user = users_repo.find(email: name) unless user user[:id] if user end - # Set/Get configuration - def config(params=[]) - build_config(*params) if init? - ap configuration.load || "No configuration yet" - end + # Make Cache for user + def users_cache + return configuration.clear(:users) if clear? - # Set user alias - def user_alias(params) - if clear? - configuration.clear_user_aliases - else - params.empty? ? configuration.show_aliases : configuration.add_user_alias(email: params[0], alias: params[1]) + users = {}.tap do |h| + users_repo.list.each{ |u| h[u['email']] = u } end - config + + configuration.set(:users, users) end - # Make Cache for user - def cache - if clear? - configuration.set_user_list({}) - else - users = {}.tap do |h| - users_repo.list.each{ |u| h[u['email']] = u } - end - configuration.set_user_list(users) + # Set/Get configuration + def config(params=[]) + return configuration.show(params) unless [set?, del?, add?, clear?].any? + key = params.shift.to_sym + + configuration.delete(key, params[0]) if del? + configuration.clear(key) if clear? + + if set? || add? + opts = build_configuration_params(params) + configuration.set(key, opts) if set? + configuration.add(key, opts) if add? end - config - end - def clear=(bool) - @clear = bool - end + #TODO later + key = ['notify', 'irc'] if key == :notify_irc - def clear? - @clear + configuration.show(key) end + #flags + def s=(bool); @set = bool; end + def set=(bool); @set = bool; end + def set?; @set; end - #initialize configutation | cmd glman config [gitlab_url] [private_token] --init - def init=(bool) - @init = bool - end + def a=(bool); @add = bool; end + def add=(bool); @add = bool; end + def add?; @add; end - # - def init? - @init - end + def d=(bool); @del = bool; end + def del=(bool); @del = bool; end + def del?; @del; end - # Show help + def c=(bool); @clear = bool; end + def clear=(bool); @clear = bool; end + def clear?; @clear; end + + def n=(bool); @notify = bool; end + def notify=(bool); @notify = bool; end + def notify?; @notify; end + def help! - puts 'Need help :D' - puts help_page + puts Glman::Commands::HelpMessages.show exit end alias :h! :help! #Exec def call(name=nil, *params) - intro + puts Glman::Commands::HelpMessages.intro case name.to_s.strip - when 'config' then config(params) - when 'alias' then user_alias(params) - when 'cache' then cache - when 'mr' then mr(params) - when '' then puts '-' + when 'config' then config(params) + when 'alias' then user_alias(params) + when 'users_cache' then users_cache + when 'mr' then mr(params) + when '' then puts Glman::Commands::HelpMessages.unknown_command else puts "what ?" end end + private + attr_reader :origin + def build_configuration_params(params) + Hash.new.tap do |h| + params.each{ |e| e = e.split(':'); h[e.shift.to_sym] = e.join(':') } + end + end + def push_branch_first(origin, branch) p "push branch: #{branch} to origin: origin" git_repo.push('origin', branch) end def show_all_mrs ap projects_repo.get_merge_requests(git_repo.repository_name) end - def build_config(gitlab_url, private_token) - configuration.build_config(gitlab_url: gitlab_url, private_token: private_token) - end - def configuration - @configuration ||= Configuration.new + @configuration ||= Glman::Commands::Config.new(config_manager: ConfigManager.new) end def users_repo - @users_repo ||= Repos::UsersRepo.new(configuration.load) + @users_repo ||= Repos::UsersRepo.new(configuration.get(:gitlab)) end def projects_repo - @projects_repo ||= Repos::ProjectsRepo.new(configuration.load) + @projects_repo ||= Repos::ProjectsRepo.new(configuration.get(:gitlab)) end def git_repo @git_repo ||= Repos::GitRepo.new end - def notify(msg) - nick = configuration.load[:irc][:channel] || 'glman' - irc_client.register(nick) - irc_client.notify(configuration.load[:irc][:channel], msg) - client.quit + def mr_command + @mr_command ||= Glman::Commands::Mr.new(git_repo: git_repo, projects_repo: projects_repo, config: configuration.get(:gitlab)) end - def irc_client - @irc_client ||= ( - irc_config = configuration.load[:irc] - server = irc_config[:server] || "irc.freenode.net" - port = (irc_config[:port] || 6697).to_i - ssl = irc_config[:ssl] == true ? true : false - IrcNotify::Client.build(server, port, ssl: ssl) - ) - end - def help_page - %{ -commands: - -config # display current configuration -config <gitlab_url> <private_token> --init # init configuration -notify_config <server:port> <channel> <ssl> # setup irc configuration for notifications - -alias # display aliases -alias <user_email> <alias> # make alias for user email -alias --clear # clear all aliases - -cache # build user cache for better performance RECOMMENDED -cache --clear # clear user cache - -mr <user_email_or_alias> # create merge request for user for current branch to master with title as last commit message - -mr <user_email_or_alias> <message> <target_branch> --push <origin> # full options for merge request (default origin is a origin :D) - -Any questions pniemczyk@o2.pl or go to https://github.com/pniemczyk/glman - } + def irc_conf + @irc_conf ||= configuration.get(:notify_irc) end - def intro - puts "Glman ver: #{VERSION}" + def irc_notify(msg) + c = IrcNotify::Client.build(irc_conf[:server], irc_conf[:port], ssl: irc_conf[:ssl]) + nick = irc_conf[:nick] || "glman-#{git_repo.user_name.strip.downcase.gsub(' ','-')}" + channel = '#' + irc_conf[:channel].gsub('#', '') + c.register(nick) + c.notify(channel, msg) + c.quit end end end end \ No newline at end of file