Skip to content

Commit

Permalink
Refactor project loading in Collective model
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
andrew committed Jan 17, 2024
1 parent 7ea0a1a commit f3e5967
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions app/models/collective.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,23 @@ def project_owner
def load_projects
return if project_url.nil?
if project_org?
resp = Faraday.get("https://repos.ecosyste.ms/api/v1/hosts/#{project_host}/owners/#{project_owner}/repositories?per_page=100")
# TODO pagination
if resp.status == 200
page = 1
loop do
resp = Faraday.get("https://repos.ecosyste.ms/api/v1/hosts/#{project_host}/owners/#{project_owner}/repositories?per_page=100&page=#{page}")
break unless resp.status == 200

data = JSON.parse(resp.body)
break if data.empty? # Stop if there are no more repositories

urls = data.map{|p| p['html_url'] }.uniq.reject(&:blank?)
urls.each do |url|
puts url
project = projects.find_or_create_by(url: url)
project.sync_async unless project.last_synced_at.present?
collective_project = collective_projects.find_or_create_by(project_id: project.id)
end

page += 1
end
else
project = Project.find_or_create_by(url: project_url)
Expand All @@ -186,7 +192,7 @@ def load_projects
end
rescue
puts "Error loading projects for #{slug}"
end
end

def self.discover
first_page = load_osc_projects
Expand Down

0 comments on commit f3e5967

Please sign in to comment.