Class: JiraCache::Sync

Inherits:
Object
  • Object
show all
Defined in:
lib/jira_cache/sync.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Sync

Returns a new instance of Sync.



16
17
18
19
# File 'lib/jira_cache/sync.rb', line 16

def initialize(client)
  @client = client
  @logger = client.logger
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



14
15
16
# File 'lib/jira_cache/sync.rb', line 14

def client
  @client
end

#loggerObject (readonly)

Returns the value of attribute logger.



14
15
16
# File 'lib/jira_cache/sync.rb', line 14

def logger
  @logger
end

Instance Method Details

#cached_keys(project_key: nil) ⇒ Object



64
65
66
# 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) ⇒ Array

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

Parameters:

  • project_key (String) (defaults to: nil)
  • updated_since (Time) (defaults to: nil)

Returns:

  • (Array)

    array of issue keys as strings



82
83
84
85
86
87
88
# 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) ⇒ Object

Parameters:

  • issue_keys (Array)

    array of strings representing the JIRA keys



91
92
93
94
95
# 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) ⇒ Object



101
102
103
# File 'lib/jira_cache/sync.rb', line 101

def latest_sync_time(project_key)
  Data::IssueRepository.latest_sync_time
end

#log(message) ⇒ Object



105
106
107
108
# File 'lib/jira_cache/sync.rb', line 105

def log(message)
  return if logger.nil?
  logger.info(message)
end

#mark_deleted(issue_keys) ⇒ Object



97
98
99
# 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) ⇒ Object

IMPLEMENTATION FUNCTIONS



60
61
62
# 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) ⇒ Object



49
50
51
52
53
54
55
56
# 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) ⇒ Object

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

Parameters:

  • project_key (String) (defaults to: nil)

    the JIRA project key



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object



68
69
70
71
# 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