Sha256: 3d88783db9325976b9908394eec8554d4d0e0781bee71342cb9e946b3a6a115d

Contents?: true

Size: 1.68 KB

Versions: 14

Compression:

Stored size: 1.68 KB

Contents

module Shipit
  class Team < ActiveRecord::Base
    REQUIRED_HOOKS = %i(membership).freeze

    has_many :memberships
    has_many :members, class_name: :User, through: :memberships, source: :user

    has_many :github_hooks,
      -> { where(event: REQUIRED_HOOKS) },
      foreign_key: :organization,
      primary_key: :organization,
      class_name: 'GithubHook::Organization',
      inverse_of: false

    class << self
      def find_or_create_by_handle(handle)
        organization, slug = handle.split('/').map(&:downcase)
        find_by(organization: organization, slug: slug) || fetch_and_create_from_github(organization, slug)
      end

      def fetch_and_create_from_github(organization, slug)
        if github_team = find_team_on_github(organization, slug)
          create!(github_team: github_team, organization: organization)
        end
      end

      def find_team_on_github(organization, slug)
        teams = Shipit::OctokitIterator.new { Shipit.github.api.org_teams(organization, per_page: 100) }
        teams.find { |t| t.slug == slug }
      rescue Octokit::NotFound
      end
    end

    def handle
      "#{organization}/#{slug}"
    end

    def add_member(member)
      members.append(member) unless members.include?(member)
    end

    def refresh_members!
      github_members = Shipit::OctokitIterator.new(Shipit.github.api.get(api_url).rels[:members])
      members = github_members.map { |u| User.find_or_create_from_github(u) }
      self.members = members
      save!
    end

    def github_team=(github_team)
      self.name = github_team.name
      self.slug = github_team.slug
      self.api_url = github_team.url
      self.github_id = github_team.id
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
shipit-engine-0.31.0 app/models/shipit/team.rb
shipit-engine-0.30.0 app/models/shipit/team.rb
shipit-engine-0.29.0 app/models/shipit/team.rb
shipit-engine-0.28.1 app/models/shipit/team.rb
shipit-engine-0.28.0 app/models/shipit/team.rb
shipit-engine-0.27.1 app/models/shipit/team.rb
shipit-engine-0.27.0 app/models/shipit/team.rb
shipit-engine-0.26.0 app/models/shipit/team.rb
shipit-engine-0.25.1 app/models/shipit/team.rb
shipit-engine-0.25.0 app/models/shipit/team.rb
shipit-engine-0.24.0 app/models/shipit/team.rb
shipit-engine-0.23.1 app/models/shipit/team.rb
shipit-engine-0.23.0 app/models/shipit/team.rb
shipit-engine-0.22.0 app/models/shipit/team.rb