Class: GitRedmineCommit

Inherits:
Object
  • Object
show all
Defined in:
lib/git-redmine-commit.rb

Constant Summary collapse

REDMINECOMMIT_RC =
File.join("#{ENV['HOME']}",".redmine_commit_rc")
GRC_CONFIG =
if File.exist?(REDMINECOMMIT_RC)
  defaults.merge(YAML.load_file(REDMINECOMMIT_RC))
else
  defaults
end

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ GitRedmineCommit

Returns a new instance of GitRedmineCommit.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/git-redmine-commit.rb', line 23

def initialize(args)
  @options = {}
  
  opts = OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename($0)} issue_id [options] -- [git-commit options]"
    opts.separator ""
    opts.separator "Specify the api key and url. You only need to do it once for each repo."
    
    opts.on( "--redmine-api-key [key]", String,
             "The api access key to access redmine." ) do |opt|
      @options[:key] = opt
    end
    opts.on( "--redmine-url [url]", String,
             "URL of your redmine." ) do |opt|
      @options[:url] = opt
    end
    opts.separator ""
    opts.on("-h", "--help", "Displays this help info") do
      puts opts
      exit 0
    end

    opts.separator ""
    opts.separator "Example:"
    opts.separator "\t#{File.basename($0)} 3125 -- -a"
  end
  
  opts.parse!(args)
  
  if args.empty?
    puts "Please specify the redmine issue id"
    puts opts
    exit 1
  end
  
  @options[:issue_id] = args.shift.to_i
  @options[:git_options] = args.join(' ')
  @options[:repo] = `git config --get remote.origin.url`.chomp
  @options[:repo] = Dir.pwd if @options[:repo].empty?
end

Instance Method Details

#get_config(git_repo) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/git-redmine-commit.rb', line 94

def get_config(git_repo)
  redmine_url = GRC_CONFIG.keys.select {|k| 
    rslt = false
    GRC_CONFIG[k][:repos].each {|repo|
      rslt = true and break if git_repo == repo
    }
    rslt
  }[0]
  
  {:url => redmine_url, :key => GRC_CONFIG[redmine_url][:key]} if redmine_url
end

#runObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/git-redmine-commit.rb', line 76

def run
  git_repo = @options[:repo]
  unless @options[:key] && @options[:url]
    config = get_config(git_repo) || get_config(Dir.pwd) || set_config(git_repo)
    @options[:key] ||= config[:key]
    @options[:url] ||= config[:url]
  end
  set_redmine_config(@options[:url], @options[:key], @options[:repo])
  
  url = File.join(@options[:url], "issues", "#{@options[:issue_id]}.xml?key=#{@options[:key]}")
  issue = XmlSimple.xml_in(open(url).read)
  title = "fix issue ##{issue['id']} : #{issue['subject']}" 
  temp = Tempfile.new('redmine_commit')
  temp << title
  temp.close
  puts `git commit #{@options[:git_options]} -F #{temp.path}`
end

#set_config(git_repo) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/git-redmine-commit.rb', line 106

def set_config(git_repo)
  redmine_url = @options[:url]
  until redmine_url && !redmine_url.empty?
    print "Redmine url: "
    redmine_url = STDIN.gets.chomp
  end
  
  GRC_CONFIG[redmine_url] ||= {}

  api_key = @options[:key] || GRC_CONFIG[redmine_url][:key]
  until api_key && !api_key.empty?
    print "Redmine api key: "
    api_key =  STDIN.gets.chomp
  end
  {:url => redmine_url, :key => api_key}
end

#set_redmine_config(redmine_url, api_key, repo) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/git-redmine-commit.rb', line 64

def set_redmine_config(redmine_url, api_key, repo)
  GRC_CONFIG[redmine_url] ||= {}
  GRC_CONFIG[redmine_url][:key] = api_key
  GRC_CONFIG[redmine_url][:repos] ||= []
  GRC_CONFIG[redmine_url][:repos] << repo
  GRC_CONFIG[redmine_url][:repos].uniq!
  
  File.open( REDMINECOMMIT_RC, 'w' ) do |out|
      YAML.dump( GRC_CONFIG, out )
  end    
end