make JiraAgent more testable and fix some minor issues detected during testing

Konstantin Nazarov 10 jaren geleden
bovenliggende
commit
0cecef2b20
1 gewijzigde bestanden met toevoegingen van 19 en 13 verwijderingen
  1. 19 13
      app/models/agents/jira_agent.rb

+ 19 - 13
app/models/agents/jira_agent.rb

@@ -49,6 +49,7 @@ module Agents
49 49
     end
50 50
 
51 51
     def validate_options
52
+      errors.add(:base, "you need to specify password if user name is set") if options['username'].present? and not options['password'].present?
52 53
       errors.add(:base, "you need to specify your jira URL") unless options['jira_url'].present?
53 54
       errors.add(:base, "you need to specify the expected update period") unless options['expected_update_period_in_days'].present?
54 55
       errors.add(:base, "you need to specify request timeout") unless options['timeout'].present?
@@ -70,7 +71,7 @@ module Agents
70 71
 
71 72
         # this check is more precise than in get_issues()
72 73
         # see get_issues() for explanation
73
-        if updated > last_run
74
+        if not last_run or updated > last_run
74 75
           create_event :payload => issue
75 76
         end
76 77
       end
@@ -93,6 +94,19 @@ module Agents
93 94
       ropts
94 95
     end
95 96
 
97
+    def get(url, options)
98
+        response = HTTParty.get(url, options)
99
+
100
+        if response.code == 400
101
+          raise RuntimeError.new("Jira error: #{response['errorMessages']}") 
102
+        elsif response.code == 403
103
+          raise RuntimeError.new("Authentication failed: Forbidden (403)")
104
+        elsif response.code != 200
105
+          raise RuntimeError.new("Request failed: #{response}")
106
+        end
107
+
108
+        response
109
+    end
96 110
 
97 111
     def get_issues(since)
98 112
       startAt = 0
@@ -103,7 +117,7 @@ module Agents
103 117
       # earlier and filter out unnecessary ones at a later
104 118
       # stage. Fortunately, the 'updated' field has GMT
105 119
       # offset
106
-      since -= 24*60*60
120
+      since -= 24*60*60 if since
107 121
 
108 122
       jql = ""
109 123
 
@@ -118,26 +132,18 @@ module Agents
118 132
 
119 133
       request_limit = 0
120 134
       loop do
121
-        response = HTTParty.get(request_url(jql, startAt), request_options)
122
-
123
-        if response.code == 400
124
-          raise RuntimeError.new("Jira error: #{response['errorMessages']}") 
125
-        elsif response.code == 403
126
-          raise RuntimeError.new("Authentication failed: Forbidden (403)")
127
-        elsif response.code != 200
128
-          raise RuntimeError.new("Request failed: #{response}")
129
-        end
135
+        response = get(request_url(jql, startAt), request_options)
130 136
 
131 137
         if response['issues'].length == 0
132 138
           request_limit+=1
133 139
         end
134 140
 
135 141
         if request_limit > MAX_EMPTY_REQUESTS
136
-          raise RuntimeError("There is no progress while fetching issues")
142
+          raise RuntimeError.new("There is no progress while fetching issues")
137 143
         end
138 144
 
139 145
         if Time.now > start_time + options['timeout'].to_i * 60
140
-          raise RuntimeError("Timeout exceeded while fetching issues")
146
+          raise RuntimeError.new("Timeout exceeded while fetching issues")
141 147
         end
142 148
 
143 149
         issues += response['issues']