Class: Database

Inherits:
Object
  • Object
show all
Includes:
Cache, Generate, Throttle
Defined in:
lib/issue_db/database.rb

Instance Method Summary collapse

Methods included from Generate

#generate

Methods included from Throttle

#fetch_rate_limit, #rate_limit_details, #update_rate_limit, #wait_for_rate_limit!

Methods included from Cache

#update_issue_cache!

Constructor Details

#initialize(log, client, repo, label, cache_expiry) ⇒ Database

:param: log [Logger] a logger object to use for logging :param: client [Octokit::Client] an Octokit::Client object to use for interacting with the GitHub API :param: repo [Repository] a Repository object that represents the GitHub repository to use as the datastore :param: label [String] the label to use for issues managed in the datastore by this library :param: cache_expiry [Integer] the number of seconds to cache issues in memory (default: 60) :return: A new Database object



21
22
23
24
25
26
27
28
29
30
# File 'lib/issue_db/database.rb', line 21

def initialize(log, client, repo, label, cache_expiry)
  @log = log
  @client = client
  @repo = repo
  @label = label
  @cache_expiry = cache_expiry
  @rate_limit_all = nil
  @issues = nil
  @issues_last_updated = nil
end

Instance Method Details

#create(key, data, options = {}) ⇒ Object

Create a new issue/record in the database This will return the newly created issue as a Record object (parsed) :param: key [String] the key (issue title) to create :param: data [Hash] the data to use for the issue body :param: options [Hash] a hash of options containing extra data such as body_before and body_after :return: The newly created issue as a Record object usage example: data = { color: “blue”, cool: true, popularity: 100, tags: [“tag1”, “tag2”] } options = { body_before: “some text before the data”, body_after: “some text after the data”, include_closed: true } db.create(“event123”, true, data: “here”, options)



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/issue_db/database.rb', line 42

def create(key, data, options = {})
  @log.debug("attempting to create: #{key}")
  issue = find_issue_by_key(key, options, create_mode: true)
  if issue
    @log.warn("skipping issue creation and returning existing issue - an issue already exists with the key: #{key}")
    return Record.new(issue)
  end

  # if we make it here, no existing issues were found so we can safely create one

  body = generate(data, body_before: options[:body_before], body_after: options[:body_after])

  # if we make it here, no existing issues were found so we can safely create one
  issue = Retryable.with_context(:default) do
    wait_for_rate_limit!
    @client.create_issue(@repo.full_name, key, body, { labels: @label })
  end

  # append the newly created issue to the issues cache
  @issues << issue

  @log.debug("issue created: #{key}")
  return Record.new(issue)
end

#delete(key, options = {}) ⇒ Object

Delete an issue/record from the database - in this context, “delete” means to close the issue as “completed” :param: key [String] the key (issue title) to delete :param: options [Hash] a hash of options to pass through to the search method :return: The deleted issue as a Record object (parsed) - it may contain useful data



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/issue_db/database.rb', line 111

def delete(key, options = {})
  @log.debug("attempting to delete: #{key}")
  issue = find_issue_by_key(key, options)

  deleted_issue = Retryable.with_context(:default) do
    wait_for_rate_limit!
    @client.close_issue(@repo.full_name, issue.number)
  end

  # remove the issue from the cache
  @issues.delete(issue)

  # return the deleted issue as a Record object as it may contain useful data
  return Record.new(deleted_issue)
end

#list(options = {}) ⇒ Object

List all issues/record in the database as Record objects (parsed) This will return an array of Record objects that represent the issues in the database :param: options [Hash] a hash of options to pass through to the search method :return: An array of Record objects that represent the issues in the database usage example: options = true records = db.list(options)



151
152
153
154
155
156
157
158
159
# File 'lib/issue_db/database.rb', line 151

def list(options = {})
  records = issues.select do |issue|
    options[:include_closed] || issue[:state] == "open"
  end.map do |issue|
    Record.new(issue)
  end

  return records
end

#list_keys(options = {}) ⇒ Object

List all keys in the database This will return an array of strings that represent the issue titles that are “keys” in the database :param: options [Hash] a hash of options to pass through to the search method :return: An array of strings that represent the issue titles that are “keys” in the database usage example: options = true keys = db.list_keys(options)



134
135
136
137
138
139
140
141
142
# File 'lib/issue_db/database.rb', line 134

def list_keys(options = {})
  keys = issues.select do |issue|
    options[:include_closed] || issue[:state] == "open"
  end.map do |issue|
    issue[:title]
  end

  return keys
end

#read(key, options = {}) ⇒ Object

Read an issue/record from the database This will return the issue as a Record object (parsed) :param: key [String] the key (issue title) to read :param: options [Hash] a hash of options to pass through to the search method :return: The issue as a Record object



72
73
74
75
76
77
# File 'lib/issue_db/database.rb', line 72

def read(key, options = {})
  @log.debug("attempting to read: #{key}")
  issue = find_issue_by_key(key, options)
  @log.debug("issue found: #{key}")
  return Record.new(issue)
end

#refresh!Object

Force a refresh of the issues cache This will update the issues cache with the latest issues from the repo :return: The updated issue cache as a list of issues (Hash objects not parsed)



164
165
166
# File 'lib/issue_db/database.rb', line 164

def refresh!
  update_issue_cache!
end

#update(key, data, options = {}) ⇒ Object

Update an issue/record in the database This will return the updated issue as a Record object (parsed) :param: key [String] the key (issue title) to update :param: data [Hash] the data to use for the issue body :param: options [Hash] a hash of options containing extra data such as body_before and body_after :return: The updated issue as a Record object usage example: data = { color: “blue”, cool: true, popularity: 100, tags: [“tag1”, “tag2”] } options = { body_before: “some text before the data”, body_after: “some text after the data”, include_closed: true } db.update(“event123”, true, data: “here”, options)



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/issue_db/database.rb', line 89

def update(key, data, options = {})
  @log.debug("attempting to update: #{key}")
  issue = find_issue_by_key(key, options)

  body = generate(data, body_before: options[:body_before], body_after: options[:body_after])

  updated_issue = Retryable.with_context(:default) do
    wait_for_rate_limit!
    @client.update_issue(@repo.full_name, issue.number, key, body)
  end

  # update the issue in the cache using the reference we have
  @issues[@issues.index(issue)] = updated_issue

  @log.debug("issue updated: #{key}")
  return Record.new(updated_issue)
end