Class: GSD::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/gsd/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(work_branch:, git_repo:, git_fork:, repo_path:) ⇒ Database

Returns a new instance of Database.



8
9
10
11
12
13
14
15
16
# File 'lib/gsd/database.rb', line 8

def initialize(work_branch:, git_repo:, git_fork:, repo_path:)
  @work_branch = work_branch
  @git_repo = git_repo
  @git_fork = git_fork
  @repo_path = repo_path
  # To be set by calling sync
  @git = nil
  @default_branch = nil
end

Instance Attribute Details

#default_branchObject (readonly)

Returns the value of attribute default_branch.



6
7
8
# File 'lib/gsd/database.rb', line 6

def default_branch
  @default_branch
end

#gitObject (readonly)

Returns the value of attribute git.



6
7
8
# File 'lib/gsd/database.rb', line 6

def git
  @git
end

#git_forkObject (readonly)

Returns the value of attribute git_fork.



6
7
8
# File 'lib/gsd/database.rb', line 6

def git_fork
  @git_fork
end

#git_repoObject (readonly)

Returns the value of attribute git_repo.



6
7
8
# File 'lib/gsd/database.rb', line 6

def git_repo
  @git_repo
end

#repo_pathObject (readonly)

Returns the value of attribute repo_path.



6
7
8
# File 'lib/gsd/database.rb', line 6

def repo_path
  @repo_path
end

#work_branchObject (readonly)

Returns the value of attribute work_branch.



6
7
8
# File 'lib/gsd/database.rb', line 6

def work_branch
  @work_branch
end

Instance Method Details

#add_file(file_path) ⇒ Object



63
64
65
# File 'lib/gsd/database.rb', line 63

def add_file(file_path)
  @git.add(file_path)
end

#commit(message) ⇒ Object



67
68
69
# File 'lib/gsd/database.rb', line 67

def commit(message)
  @git.commit(message)
end

#modify(file_path) {|new_gsd_entry| ... } ⇒ Object

Yields:

  • (new_gsd_entry)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gsd/database.rb', line 30

def modify(file_path, &block)
  raw_json_data = File.read(file_path)
  old_gsd_entry = JSON.parse(raw_json_data)
  new_gsd_entry = old_gsd_entry.deep_dup

  yield new_gsd_entry

  if new_gsd_entry != old_gsd_entry
    indent = json_indent_value(
      parsed_json: old_gsd_entry,
      raw_json: raw_json_data,
      gsd_id: new_gsd_entry['gsd']['osvSchema']['id']
    )
    # Sort by key and include a trailing newline
    contents = json_string(input: new_gsd_entry.sort.to_h, indent: indent) + "\n"
    File.write(file_path, contents)
    add_file(file_path)
    puts "Staged changes!"
  else
    puts "No changes!"
  end
end

#push!Object



59
60
61
# File 'lib/gsd/database.rb', line 59

def push!
  @git.push('fork', @work_branch, force: true)
end

#save!Object



53
54
55
56
57
# File 'lib/gsd/database.rb', line 53

def save!
  status = @git.status
  staged_files = status.changed.merge(status.added)
  commit("Sync Ruby Advisory DB\n\n#{staged_files.count} IDs have been updated.")
end

#sync!Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gsd/database.rb', line 18

def sync!
  if exists?
    open_repo
  else
    clone_repo
  end

  @default_branch = Git.default_branch(@git_repo)

  prepare_work_branch
end