Class: JiraHook

Inherits:
Object
  • Object
show all
Defined in:
lib/svn_jira_hook.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url = nil) ⇒ JiraHook

Returns a new instance of JiraHook.



7
8
9
# File 'lib/svn_jira_hook.rb', line 7

def initialize(base_url = nil)
  @jira = ::Jira4R::JiraTool.new(2, base_url) unless base_url.nil?
end

Class Method Details

.check(keyprefix, argv, config = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/svn_jira_hook.rb', line 24

def self.check(keyprefix, argv, config = nil)
  #self.init(nil)
  repo_path = argv[0]
  transaction = argv[1]
  svnlook = 'svnlook'

  #commit_dirs_changed = `#{svnlook} dirs-changed #{repo_path} -t #{transaction}`
  #commit_changed = `#{svnlook} changed #{repo_path} -t #{transaction}`
  commit_author = `#{svnlook} author #{repo_path} -t #{transaction}`.chop
  commit_log = `#{svnlook} log #{repo_path} -t #{transaction}`
  #commit_diff = `#{svnlook} diff #{repo_path} -t #{transaction}`
  #commit_date = `#{svnlook} date #{repo_path} -t #{transaction}`

  if commit_log.nil? || commit_log.empty?
    STDERR.puts("'#{commit_log}' doesn't exist as a issue on Jira")
    exit(1)
  end

  check_log(commit_author, commit_log, keyprefix, config)
end

.check_log(commit_author, commit_log, keyprefix, config = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/svn_jira_hook.rb', line 45

def self.check_log(commit_author, commit_log, keyprefix, config = nil)
  if config
    jira_configuration = config
  else
    # TODO die if configuration file is missing
    jira_configuration = configuration()
  end

  jira = JiraHook.new(jira_configuration['jira_url'])
  jira.(jira_configuration['jira_username'], jira_configuration['jira_password'])

  unless jira.check_right(commit_author, keyprefix, commit_log)
    STDERR.puts("Doesn't exist as a issue on Jira!\n: #{keyprefix}-10: 修改说明")
    exit(1)
  end

end

.configurationObject



102
103
104
105
106
107
108
109
110
# File 'lib/svn_jira_hook.rb', line 102

def self.configuration
  if defined?(SVN_HOOKS_CONFIG_PATH)
    config_file = SVN_HOOKS_CONFIG_PATH
  else
    config_file = '/etc/svn_hooks.yml'
  end

  YAML::load(IO.read(config_file))
end

.init(config = nil) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/svn_jira_hook.rb', line 15

def self.init(config = nil)
  if config
    @@config = config
  else
    @@config = nil
  end
end

Instance Method Details

#check_right(username, issue_key_regex, message) ⇒ Object

check the message is match the issue_key_regex and assign to the user



65
66
67
68
69
70
71
72
73
74
# File 'lib/svn_jira_hook.rb', line 65

def check_right(username, issue_key_regex, message)
  if message.nil? || message.empty?
    return false
  end
  issue_id = get_issue_number(message, issue_key_regex)
  if issue_id.nil?
    return false
  end
  has_right(username, issue_id)
end

#get_issue_number(message, keyprefix) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/svn_jira_hook.rb', line 89

def get_issue_number(message, keyprefix)
  # key="ab" => key=[Aa][Bb]
  keyreg = []
  keyprefix.each_char do |c|
    keyreg << "[#{c.upcase}#{c.downcase}]"
  end

  re = Regexp.new("#{keyreg.join("")}-[0-9]+")
  if message =~ re
    "#{$&}".upcase
  end
end

#has_right(username, issue_id) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/svn_jira_hook.rb', line 76

def has_right(username, issue_id)
  begin
    issue = @jira.getIssue(issue_id)
    if issue.assignee == username && issue.status == '3'
      return true
    else
      return false
    end
  rescue SOAP::FaultError
    return false
  end
end

#login(user, pass) ⇒ Object



11
12
13
# File 'lib/svn_jira_hook.rb', line 11

def (user, pass)
  @jira.(user, pass)
end