Skip to content

Commit

Permalink
Add owner synchronization to Collective model
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew committed Jan 18, 2024
1 parent e7a3015 commit fe6313a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
28 changes: 28 additions & 0 deletions app/models/collective.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class Collective < ApplicationRecord
has_many :transactions, dependent: :destroy

scope :with_repository, -> { where.not(repository: nil) }

scope :with_owner, -> { where.not(owner: nil) }
scope :with_org_owner, -> { with_owner.where("owner ->> 'kind' = 'organization'") }
scope :with_user_owner, -> { with_owner.where("owner ->> 'kind' = 'user'") }


def self.sync_least_recently_synced
Collective.where(last_synced_at: nil).or(Collective.where("last_synced_at < ?", 1.day.ago)).order('last_synced_at asc nulls first').limit(500).each do |collective|
Expand Down Expand Up @@ -94,6 +99,7 @@ def sync
update(updated_attrs) if updated_attrs.present?
load_projects
sync_transactions
sync_owner
ping_owner
rescue
puts "Error syncing #{slug}"
Expand All @@ -106,6 +112,28 @@ def ping_owner
Faraday.get(ping_owner_url) rescue nil
end

def fetch_owner
return unless project_owner.present?

conn = Faraday.new(url: "https://repos.ecosyste.ms") do |faraday|
faraday.response :follow_redirects
faraday.adapter Faraday.default_adapter
end

resp = conn.get("/api/v1/hosts/#{project_host}/owners/#{project_owner}")
return unless resp.status == 200
JSON.parse(resp.body)
rescue => e
puts "Error fetching owner for #{slug}: #{e.message}"
nil
end

def sync_owner
owner = fetch_owner
return unless owner.present?
update(owner: owner)
end

def website
return read_attribute(:website) if read_attribute(:website).present?
(social_links || {}).select{|x| ['WEBSITE'].include? x['type']}.first.try(:[], 'url') || ''
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240118140953_add_owner_to_collectives.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddOwnerToCollectives < ActiveRecord::Migration[7.1]
def change
add_column :collectives, :owner, :json
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fe6313a

Please sign in to comment.