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")
REDMINECOMMIT_TEMPLATE =
File.join("#{ENV['HOME']}",".redmine_commit_template")
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.



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
63
64
65
66
67
68
# File 'lib/git-redmine-commit.rb', line 26

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.on("-s", "--silent", "commit with default message silently without prompting the message editor") do
      @options[:silent] = true
    end
    opts.separator ""
    opts.separator "Example:"
    opts.separator "\t#{File.basename($0)} 3125 -s -- -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



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/git-redmine-commit.rb', line 120

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

#message_templateObject



82
83
84
85
86
87
88
89
# File 'lib/git-redmine-commit.rb', line 82

def message_template
  template_src = if File.file?(REDMINECOMMIT_TEMPLATE)
    open(REDMINECOMMIT_TEMPLATE) {|f| f.read}
  else
    " Fix #<%= issue_id %>:<%= issue_subject %>\n"
  end
  ERB.new template_src
end

#runObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/git-redmine-commit.rb', line 91

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)
  issue_id = issue['id']
  issue_subject = issue['subject']
  title = message_template.result(binding)
  temp = Tempfile.new('redmine_commit')
  temp << title
  temp.close

  if @options[:silent]
    puts `git commit #{@options[:git_options]} -F #{temp.path}`
  else
    commit_template = `git config --get commit.template`
    `git config commit.template #{temp.path}`    
    system "git commit #{@options[:git_options]}"
    system "git config --unset commit.template"
    `git config commit.template #{commit_template}` if commit_template && commit_template.size > 0
  end
end

#set_config(git_repo) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/git-redmine-commit.rb', line 132

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



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/git-redmine-commit.rb', line 70

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