@@ -125,7 +125,6 @@ def remote_sync(options={})
125125 def update_from_github ( options = { } )
126126 # read synced_at before updating
127127 if_modified_since = options [ :force ] ? nil : self . synced_at . try ( :httpdate )
128- update_attribute ( :synced_at , Time . now )
129128
130129 # add access token
131130 params = {
@@ -139,7 +138,10 @@ def update_from_github(options={})
139138 headers : { 'If-Modified-Since' => if_modified_since }
140139 )
141140 # trigger an update on the underlying object if modified
142- self . class . update_attributes_from_github_data ( api_response . data , obj : self ) if api_response . modified?
141+ if api_response . modified?
142+ update_attribute ( :synced_at , Time . now )
143+ self . class . update_attributes_from_github_data ( api_response . data , obj : self )
144+ end
143145 end
144146
145147 # Update the repo languages from GitHub
@@ -188,29 +190,38 @@ def find_or_create_issues_from_github(options={})
188190 # TODO: optimization: github api returns open_issues_count which we could compare to issues.where(state=open)
189191
190192 if options [ :state ]
193+ # IMPROVEMENT: Add a 10-minute buffer to account for clock drift/indexing delays
194+ since_timestamp = options [ :since ]
195+ since_timestamp -= 10 . minutes if since_timestamp
196+
191197 # make a call to github API
192198 api_response = Github ::API . call (
193199 url : File . join ( self . github_api_path , "/issues" ) ,
194200 params : {
195201 access_token : options [ :person ] . try ( :github_account ) . try ( :oauth_token ) ,
196- since : options [ :since ] . try ( :iso8601 ) ,
202+ since : since_timestamp . try ( :iso8601 ) ,
197203 state : options [ :state ] ,
204+ per_page : 100 ,
198205 page : options [ :page ] || 1
199- } . reject { |k , v | v . nil? } ,
200-
201- # NOTE: this doesn't have any affect... and we don't want to store etags, so no API optimization here.
202- #headers: {
203- # 'If-Modified-Since' => options[:since].try(:httpdate)
204- #}.reject { |k,v| v.nil? }
206+ } . reject { |k , v | v . nil? }
205207 )
206208
207- # Edge case: tracker is deleted, just raise for now
208- if !api_response . success? && api_response . status == 404
209- raise "Github::Repository(#{ id } ) not found at GitHub(#{ url } ). Maybe it was deleted?"
209+ # IMPROVEMENT: Robust error logging and status check
210+ unless api_response . success?
211+ Rails . logger . error "Github::Repository(#{ id } ) Issue Sync Failed: #{ api_response . status } - #{ api_response . data . inspect } "
212+ if api_response . status == 404
213+ raise "Github::Repository(#{ id } ) not found at GitHub(#{ url } ). Maybe it was deleted?"
214+ end
215+ return false # Stop processing this branch
210216 end
211217
212- # process these 100 issues
213- Github ::Issue . update_attributes_from_github_array ( api_response . data )
218+ # IMPROVEMENT: Type validation before processing
219+ if api_response . data . is_a? ( Array )
220+ Github ::Issue . update_attributes_from_github_array ( api_response . data )
221+ else
222+ Rails . logger . error "Github::Repository(#{ id } ) Expected Array from API, got: #{ api_response . data . class } "
223+ return false
224+ end
214225
215226 # if a page param wasn't passed in, load the rest of the pages
216227 if !options [ :page ] && api_response . link [ :last ]
0 commit comments