Class: Codeowners::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/codeowners/storage.rb,
lib/codeowners/storage/data.rb,
lib/codeowners/storage/collection.rb

Defined Under Namespace

Classes: Collection, Data

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Storage

Returns a new instance of Storage.



11
12
13
14
15
# File 'lib/codeowners/storage.rb', line 11

def initialize(path)
  @path = path
  @data = Data.new(load_data)
  @mutex = Mutex.new
end

Instance Method Details

#blacklisted_team?(team) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/codeowners/storage.rb', line 38

def blacklisted_team?(team)
  data[:teams].find do |record|
    record.fetch("slug") == team &&
      record.fetch("blacklisted")
  end
end

#team_exist?(team) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/codeowners/storage.rb', line 32

def team_exist?(team)
  data[:teams].find do |record|
    record.fetch("slug") == team
  end
end

#teams_for(user) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/codeowners/storage.rb', line 45

def teams_for(user)
  found = data[:users].find do |record|
    record.fetch("email") == user.email ||
      record.fetch("name") == user.name
  end

  return [] unless found

  memberships = data[:memberships].find_all do |record|
    record.fetch("user_id") == found.fetch("id")
  end
  memberships.map! { |hash| hash.fetch("team_id") }

  return [] if memberships.empty?

  teams = data[:teams].find_all do |record|
    !record.fetch("blacklisted") &&
      memberships.include?(record.fetch("id"))
  end.flatten

  teams
end

#transactionObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/codeowners/storage.rb', line 17

def transaction
  @mutex.synchronize do
    tmp_data = data.dup
    yield tmp_data

    Tempfile.open("codeowners-storage") do |tmp|
      tmp.binmode
      tmp.write(JSON.generate(data.dump))
      tmp.close

      FileUtils.mv(tmp.path, path, force: true)
    end
  end
end