Class: GithubOrganisation

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/github_organisation.rb

Constant Summary collapse

API_FIELDS =
[:name, :login, :blog, :email, :location, :description]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_from_github(login_or_id) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/github_organisation.rb', line 74

def self.create_from_github()
  begin
    r = AuthToken.client.org().to_hash
    return false if r.blank?
    g = GithubOrganisation.find_or_initialize_by(github_id: r[:id])
    g.github_id = r[:id]
    g.assign_attributes r.slice(*GithubOrganisation::API_FIELDS)
    g.save
    g
  rescue Octokit::RepositoryUnavailable, Octokit::NotFound, Octokit::Forbidden, Octokit::InternalServerError, Octokit::BadGateway => e
    p e
    false
  end
end

Instance Method Details

#async_syncObject


89
90
91
# File 'app/models/github_organisation.rb', line 89

def async_sync
  GithubUpdateOrgWorker.perform_async(self.)
end

#avatar_url(size = 60) ⇒ Object


50
51
52
# File 'app/models/github_organisation.rb', line 50

def avatar_url(size = 60)
  "https://avatars.githubusercontent.com/u/#{github_id}?size=#{size}"
end

#companyObject


66
67
68
# File 'app/models/github_organisation.rb', line 66

def company
  nil
end

#download_from_githubObject


99
100
101
102
103
# File 'app/models/github_organisation.rb', line 99

def download_from_github
  update_attributes(github_client.org(github_id).to_hash.slice(:login, :name, :email, :blog, :location, :description))
rescue Octokit::RepositoryUnavailable, Octokit::NotFound, Octokit::Forbidden, Octokit::InternalServerError, Octokit::BadGateway => e
  nil
end

#download_reposObject


105
106
107
108
109
110
111
# File 'app/models/github_organisation.rb', line 105

def download_repos
  github_client.org_repos().each do |repo|
    GithubCreateWorker.perform_async(repo.full_name)
  end
rescue Octokit::Unauthorized, Octokit::RepositoryUnavailable, Octokit::NotFound, Octokit::Forbidden, Octokit::InternalServerError, Octokit::BadGateway => e
  nil
end

#github_clientObject


70
71
72
# File 'app/models/github_organisation.rb', line 70

def github_client
  AuthToken.client
end

#github_contributionsObject


22
23
24
# File 'app/models/github_organisation.rb', line 22

def github_contributions
  GithubContribution.none
end

#github_urlObject


54
55
56
# File 'app/models/github_organisation.rb', line 54

def github_url
  "https://github.com/#{}"
end

#org?Boolean

Returns:

  • (Boolean)

46
47
48
# File 'app/models/github_organisation.rb', line 46

def org?
  true
end

#syncObject


93
94
95
96
97
# File 'app/models/github_organisation.rb', line 93

def sync
  download_from_github
  download_repos
  update_attributes(last_synced_at: Time.now)
end

#to_paramObject


62
63
64
# File 'app/models/github_organisation.rb', line 62

def to_param
  
end

#to_sObject


58
59
60
# File 'app/models/github_organisation.rb', line 58

def to_s
  name.presence || 
end

#top_contributor_idsObject


40
41
42
43
44
# File 'app/models/github_organisation.rb', line 40

def top_contributor_ids
  Rails.cache.fetch "org:#{self.id}:top_contributor_ids", :expires_in => 1.week do
    contributors.visible.limit(50).pluck(:id)
  end
end

#top_contributorsObject


36
37
38
# File 'app/models/github_organisation.rb', line 36

def top_contributors
  GithubUser.where(id: top_contributor_ids).order("position(','||github_users.id::text||',' in '#{top_contributor_ids.join(',')}')")
end

#top_favourite_project_idsObject


30
31
32
33
34
# File 'app/models/github_organisation.rb', line 30

def top_favourite_project_ids
  Rails.cache.fetch "org:#{self.id}:top_favourite_project_ids:v2", :expires_in => 1.week do
    favourite_projects.limit(10).pluck(:id)
  end
end

#top_favourite_projectsObject


26
27
28
# File 'app/models/github_organisation.rb', line 26

def top_favourite_projects
  Project.where(id: top_favourite_project_ids).maintained.order("position(','||projects.id::text||',' in '#{top_favourite_project_ids.join(',')}')")
end