class JiraCache::Sync

Performs the sync between JIRA and the local database where the issues are cached.

The issues are cached in the database through the Data::IssueRepository interface. It currently implements storage into a PostgreSQL database.

Attributes

client[R]
logger[R]

Public Class Methods

new(client) click to toggle source
# File lib/jira_cache/sync.rb, line 16
def initialize(client)
  @client = client
  @logger = client.logger
end

Public Instance Methods

cached_keys(project_key: nil) click to toggle source
# File lib/jira_cache/sync.rb, line 64
def cached_keys(project_key: nil)
  Data::IssueRepository.keys_in_project(project_key)
end
fetch_issue_keys(project_key: nil, updated_since: nil) click to toggle source

Fetch issue keys from JIRA using the specified `JiraCache::Client` instance, for the specified project, with an optional `updated_since` parameter.

@param project_key [String] @param updated_since [Time] @return [Array] array of issue keys as strings

# File lib/jira_cache/sync.rb, line 82
def fetch_issue_keys(project_key: nil, updated_since: nil)
  query_items = []
  query_items << "project = \"#{project_key}\"" unless project_key.nil?
  query_items << "updatedDate > \"#{updated_since.strftime('%Y-%m-%d %H:%M')}\"" unless updated_since.nil?
  query = query_items.join(" AND ")
  client.issue_keys_for_query(query)
end
fetch_issues(issue_keys, sync_time) click to toggle source

@param issue_keys [Array] array of strings representing the JIRA keys

# File lib/jira_cache/sync.rb, line 91
def fetch_issues(issue_keys, sync_time)
  issue_keys.each do |issue_key|
    sync_issue(issue_key, sync_time: sync_time)
  end
end
latest_sync_time(project_key) click to toggle source
# File lib/jira_cache/sync.rb, line 101
def latest_sync_time(project_key)
  Data::IssueRepository.latest_sync_time
end
log(message) click to toggle source
# File lib/jira_cache/sync.rb, line 105
def log(message)
  return if logger.nil?
  logger.info(message)
end
mark_deleted(issue_keys) click to toggle source
# File lib/jira_cache/sync.rb, line 97
def mark_deleted(issue_keys)
  Data::IssueRepository.update_where({ key: issue_keys }, deleted_from_jira_at: Time.now)
end
remote_keys(project_key: nil) click to toggle source

IMPLEMENTATION FUNCTIONS

# File lib/jira_cache/sync.rb, line 60
def remote_keys(project_key: nil)
  fetch_issue_keys(project_key: project_key)
end
sync_issue(key, sync_time: Time.now) click to toggle source
# File lib/jira_cache/sync.rb, line 49
def sync_issue(key, sync_time: Time.now)
  data = client.issue_data(key)
  Data::IssueRepository.insert(
    key: key,
    data: data,
    synced_at: sync_time
  )
end
sync_issues(project_key: nil) click to toggle source

Fetches new and updated raw issues, save them to the `issues` collection. Also mark issues deleted from JIRA as such.

@param project_key [String] the JIRA project key

# File lib/jira_cache/sync.rb, line 26
def sync_issues(project_key: nil)
  sync_start = Time.now

  log "Determining which issues to fetch..."
  remote = remote_keys(project_key: project_key)
  log "  - #{remote.count} remote issues"

  cached = cached_keys(project_key: project_key)
  log "  - #{cached.count} cached issues"

  missing = remote - cached
  log "  => #{missing.count} missing issues"

  updated = updated_keys(project_key: project_key)
  log "  - #{updated.count} updated issues"

  log "Fetching #{missing.count + updated.count} issues"
  fetch_issues(missing + updated, sync_start)

  deleted = cached - remote
  mark_deleted(deleted)
end
updated_keys(project_key: nil) click to toggle source
# File lib/jira_cache/sync.rb, line 68
def updated_keys(project_key: nil)
  time = latest_sync_time(project_key: project_key)
  fetch_issue_keys(project_key: project_key, updated_since: time)
end