Skip to content

Instantly share code, notes, and snippets.

@mm-git
Created February 8, 2014 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mm-git/026bde752ad2581a73d9 to your computer and use it in GitHub Desktop.
Save mm-git/026bde752ad2581a73d9 to your computer and use it in GitHub Desktop.
Git subcommand to access red mine by using Ruby ActiveResource class
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# git redmine
# - show all issue list
#
# git redmine n
# - show issue #n
#
# git redmine issue n status_id
# - change issue #n status to status_id
#
# git redmine version v [open|closed]
# - change version status to open or closed
require 'active_resource'
class Issue < ActiveResource::Base
# overriding element_path method for save
def element_path(options)
options = {:key => Issue.apiKey}
super(options)
end
class << self
attr_accessor :apiKey
attr_accessor :projectName
def checkGitConfig
result = true
if @site == ""
puts "[ERROR ] redmine.url is not set in the git config"
result = false
end
if @apiKey == ""
puts "[ERROR ] redmine.apikey is not set in the git config"
result = false
end
if @projectName == ""
puts "[ERROR ] redmine.project is not set in the git config"
result = false
end
return result
end
def showIssueList()
offset = 0
loop do
issues = find(:all, :from => "/projects/#{@projectName}/issues.xml", :params => {:limit => 100, :offset => offset, :status_id => "*", :key => @apiKey})
if issues == nil || issues.count() == 0
break
end
issues.each do |issue|
puts "#" + issue.id + " " + issue.subject
end
offset += 100
end
end
def showIssue(id)
issue = find(id, :params => {:key => @apiKey})
if issue == nil
return
end
puts "#" + issue.id + " " + issue.subject
end
def getTicketStatusList
statuses = find(:all, :from => "/issue_statuses.xml", :params => {:key => @apiKey})
if statuses == nil || statuses.count() == 0
puts "[ERROR] status not found."
return
end
return statuses
end
def setIssueStatus(id, status_id)
issue = find(id, :params => {:key => @apiKey})
if issue == nil
puts "[ERROR] Issue not found."
return
end
statuses = getTicketStatusList
statuses.each do |status|
if status.id.to_i == status_id
issue.status_id = status_id
issue.save()
break
end
end
end
end
self.site = %x(git config redmine.url).strip
self.apiKey = %x(git config redmine.apikey).strip
self.projectName = %x(git config redmine.project).strip
self.format = :xml
end
class Version < Issue
class << self
def getVersionList
versions = find(:all, :from => "/projects/#{Issue.projectName}/versions.xml", :params => {:key => Issue.apiKey})
if versions == nil || versions.count() == 0
puts "[ERROR] version not found."
return
end
return versions
end
def setVersionStatus(versionName, status)
versions = getVersionList()
versions.each do |version|
if version.name == versionName
versionObj = find(version.id, :params => {:key => Issue.apiKey})
versionObj.status = status
versionObj.save()
return
end
end
puts "[ERROR] Version not found."
end
end
end
def isNumeric?(s)
!!Float(s) rescue false
end
if ARGV.count() == 0
Issue.showIssueList()
else
case ARGV[0]
when "issue"
if ARGV.count() < 3
puts "[ERROR] Issue number or status id are not set in the parameters."
elsif isNumeric?(ARGV[1]) == false || isNumeric?(ARGV[2]) == false
puts "[ERROR] Issue number and status id must be a decimal."
else
Issue.setIssueStatus(ARGV[1].to_i, ARGV[2].to_i)
end
when "version"
if ARGV.count() < 3
puts "[ERROR] Version or status are not set in the parameters."
elsif ARGV[2] != "open" && ARGV[2] != "closed"
puts "[ERROR] Status must be 'open' or 'closed'."
else
Version.setVersionStatus(ARGV[1], ARGV[2])
end
else
if ARGV.count() < 1
puts "[ERROR] Issue number is not set in the parameters."
elsif isNumeric?(ARGV[0]) == false
puts "[ERROR] Issue number must be a decimal."
else
Issue.showIssue(ARGV[0].to_i)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment