Class: Crab::Rally

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/crab/rally.rb

Instance Method Summary collapse

Methods included from Utilities

#add_or_update_options, #credentials_file, #current_project_name, #dotcrab_file, #sanitize, #sanitize_options, #state_after, #state_before, #state_from, #valid_credentials_file, #valid_project_name

Methods included from Logging

#logger

Constructor Details

#initialize(dry_run) ⇒ Rally

Returns a new instance of Rally.



6
7
8
9
10
11
12
13
# File 'lib/crab/rally.rb', line 6

def initialize(dry_run)
  @dry_run = dry_run

  if block_given?
    connect
    yield self
  end
end

Instance Method Details

#connectObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/crab/rally.rb', line 15

def connect
  get_credentials
  logger.info "Connecting to Rally as #{@username}..."

  rally_logger = Logger.new(STDERR)
  rally_logger.formatter = Logger::Formatter.new
  rally_logger.progname = 'rally'
  rally_logger.level = ENV['CRAB_LOG_LEVEL'].present? ? ENV['CRAB_LOG_LEVEL'].to_i : Logger::WARN

  @rally = ::RallyRestAPI.new :username => @username, :password => @password, :logger => rally_logger
end

#create_story(opts) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/crab/rally.rb', line 135

def create_story(opts)
  logger.info "Creating story with #{opts.keys.inspect}"

  if @dry_run
    Crab::DryRun::Story.new opts
  else
    Crab::Story.new(@rally.create(:hierarchical_requirement, opts), @dry_run)
  end
end

#create_test_case(story_id, name, opts) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/crab/rally.rb', line 145

def create_test_case(story_id, name, opts)
  story = find_story_with_id story_id
  opts = {:name => name, :work_product => story.rally_object, :project => story.rally_object.project}.merge(opts)

  logger.info "Creating test case with #{opts.keys.inspect}"

  if @dry_run
    puts "Would create test case for story with ID #{story_id} with #{opts.inspect}"
  else
    tc = @rally.create(:test_case, opts)
    Crab::TestCase.new(tc, @dry_run)
  end
end

#find_defect_with_id(defect_id) ⇒ Object



39
40
41
42
43
44
# File 'lib/crab/rally.rb', line 39

def find_defect_with_id defect_id
  logger.info "Looking up defect with ID #{defect_id}"
  defect = @rally.find(:defect) { equal :formatted_i_d, defect_id }.first
  Trollop::die "Defect with ID #{defect_id.inspect} not found" if defect.nil?
  Crab::Defect.new(defect, @dry_run)
end

#find_defects(project, pattern, opts) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/crab/rally.rb', line 68

def find_defects(project, pattern, opts)
  logger.info "Looking for defects matching #{pattern.inspect} with options #{opts.keys.inspect} in #{project.name.inspect}"
  if pattern.join.empty? && opts.empty?
    return @rally.find_all(:defect, :fetch => true, :project => project).map {|s| Crab::Defect.new(s, @dry_run) }
  end

  rally_stories = @rally.find(:defect, :fetch => true, :project => project) do
    Crab::Rally.search_for_words_in pattern, self

    equal :iteration, opts[:iteration] if opts[:iteration]
    equal :release,   opts[:release]   if opts[:release]
    equal :parent,    opts[:parent].rally_object if opts[:parent]
  end

  rally_stories.map {|defect| Crab::Defect.new(defect, @dry_run) }.tap do |defects|
    logger.info "Found #{defects.size} defects"
  end
end

#find_iteration_by_name(name, project) ⇒ Object



116
117
118
119
120
121
# File 'lib/crab/rally.rb', line 116

def find_iteration_by_name(name, project)
  logger.info "Looking for iteration #{name.inspect} in #{project.name.inspect}"
  iteration = @rally.find(:iteration, :project => project) { equal :name, name }.first
  Trollop::die "Unknown iteration \"#{name}\"" if iteration.nil?
  iteration
end

#find_iterations(project) ⇒ Object



111
112
113
114
# File 'lib/crab/rally.rb', line 111

def find_iterations(project)
  logger.info "Looking for all iterations in #{project.name.inspect}"
  @rally.find_all(:iteration, :project => project)
end

#find_project(name) ⇒ Object



106
107
108
109
# File 'lib/crab/rally.rb', line 106

def find_project(name)
  logger.info "Looking for project #{name.inspect}"
  @rally.find(:project, :fetch => true) { equal :name, name }.first
end

#find_release_by_name(name, project) ⇒ Object



128
129
130
131
132
133
# File 'lib/crab/rally.rb', line 128

def find_release_by_name(name, project)
  logger.info "Looking for release #{name.inspect} in #{project.name.inspect}"
  release = @rally.find(:release, :project => project) { equal :name, name }.first
  Trollop::die "Unknown release \"#{name}\"" if release.nil?
  release
end

#find_releases(project) ⇒ Object



123
124
125
126
# File 'lib/crab/rally.rb', line 123

def find_releases(project)
  logger.info "Looking for all releases in #{project.name.inspect}"
  @rally.find_all(:release, :project => project, :fetch => true)
end

#find_stories(project, pattern, opts) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/crab/rally.rb', line 87

def find_stories(project, pattern, opts)
  logger.info "Looking for stories matching #{pattern.inspect} with options #{opts.keys.inspect} in #{project.name.inspect}"
  if pattern.join.empty? && opts.empty?
    return @rally.find_all(:hierarchical_requirement, :fetch => true, :project => project).map {|s| Crab::Story.new(s, @dry_run) }
  end

  rally_stories = @rally.find(:hierarchical_requirement, :fetch => true, :project => project) do
    Crab::Rally.search_for_words_in pattern, self

    equal :iteration, opts[:iteration] if opts[:iteration]
    equal :release,   opts[:release]   if opts[:release]
    equal :parent,    opts[:parent].rally_object if opts[:parent]
  end

  rally_stories.map {|story| Crab::Story.new(story, @dry_run) }.tap do |stories|
    logger.info "Found #{stories.size} stories"
  end
end

#find_story_with_id(story_id) ⇒ Object



32
33
34
35
36
37
# File 'lib/crab/rally.rb', line 32

def find_story_with_id story_id
  logger.info "Looking up story with ID #{story_id}"
  story = @rally.find(:hierarchical_requirement) { equal :formatted_i_d, story_id }.first
  Trollop::die "Story with ID #{story_id.inspect} not found" if story.nil?
  Crab::Story.new(story, @dry_run)
end

#find_test_case(tc_id) ⇒ Object



159
160
161
162
163
164
# File 'lib/crab/rally.rb', line 159

def find_test_case(tc_id)
  logger.info "Looking up test case with ID #{tc_id}"
  tc = @rally.find(:test_case) { equal :formatted_i_d, tc_id }.first
  Trollop::die "Test case with ID #{tc_id.inspect} not found" if tc.nil?
  Crab::TestCase.new(tc, @dry_run)
end

#find_testcases(project, pattern, opts) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/crab/rally.rb', line 46

def find_testcases(project, pattern, opts)
  logger.info "Looking for testcases matching #{pattern.inspect} with options #{opts.keys.inspect} in #{project.name.inspect}"

  if pattern.join.empty? && opts.empty?
    return @rally.find_all(:test_case, :fetch => true, :project => project).map {|tc| Crab::TestCase.new(tc, @dry_run) }
  end

  rally_testcases = @rally.find(:test_case, :fetch => true, :project => project) do
    Crab::Rally.search_for_words_in pattern, self

    equal :work_product, opts[:story].rally_object if opts[:story]
    equal :risk,     opts[:risk].capitalize     if opts[:risk]
    equal :method,   opts[:method].capitalize   if opts[:method]
    equal :priority, opts[:priority].capitalize if opts[:priority]
    equal :type,     opts[:type].capitalize     if opts[:type]
  end

  rally_testcases.map {|tc| Crab::TestCase.new(tc, @dry_run) }.tap do |testcases|
    logger.info "Found #{testcases.size} test cases"
  end
end

#get_credentialsObject



27
28
29
30
# File 'lib/crab/rally.rb', line 27

def get_credentials
  logger.info "Getting credentials..."
  @username, @password = File.read(valid_credentials_file).split /\n/
end