• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_discovery / d736ff91-d543-4530-a84d-5d91200b6783

03 Nov 2023 05:30PM UTC coverage: 93.662% (-3.3%) from 96.965%
d736ff91-d543-4530-a84d-5d91200b6783

push

circleci

jrgriffiniii
wip

10 of 11 new or added lines in 1 file covered. (90.91%)

91 existing lines in 15 files now uncovered.

2601 of 2777 relevant lines covered (93.66%)

178.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.82
/app/models/version_footer.rb
1
# frozen_string_literal: true
2

3
# Retrieves version information from Capistrano's files. The general approach is
4
# to read the version information (branch name, git SHA, and date deployed) out
5
# of Capistrano's revisions.log file.
6
#
7
# The code is a bit more complicated than it should because Capistrano does not
8
# always update the revision.log file before the application reads this information.
9
# Therefore there is logic in this class to detect if the version information is
10
# stale and re-read it until it is up to date. Because re-reading this file is
11
# an expensive operation we cache the information as soon as we are sure it's
12
# current.
13
#
14
# rubocop:disable Style/ClassVars
15
class VersionFooter
1✔
16
  @@stale = true
1✔
17
  @@git_sha = nil
1✔
18
  @@branch = nil
1✔
19
  @@version = nil
1✔
20

21
  # Returns a hash with version information.
22
  def self.info
1✔
23
    reset! if stale?
55✔
24
    { sha: git_sha, branch: branch, version: version, stale: stale?, tagged_release: tagged_release? }
55✔
25
  end
26

27
  def self.reset!
1✔
28
    # Initalize these values so that they recalculated
29
    @@git_sha = nil
3✔
30
    @@branch = nil
3✔
31
    @@version = nil
3✔
32
  end
33

34
  def self.stale?
1✔
35
    return false if @@stale == false
55✔
36
    # Only read the file when version information is stale
37
    if File.exist?(revision_file)
2✔
38
      local_sha = File.read(revision_file).chomp.gsub(/\)$/, '')
2✔
39
      @@stale = local_sha != git_sha
2✔
40
    else
41
      @@stale = true
×
42
    end
43
    @@stale
2✔
44
  end
45

46
  def self.git_sha
1✔
47
    @@git_sha ||= if File.exist?(revisions_logfile)
57✔
48
                    `tail -1 #{revisions_logfile}`.chomp.split(" ")[3].gsub(/\)$/, '')
3✔
49
                  elsif Rails.env.development? || Rails.env.test?
×
50
                    `git rev-parse HEAD`.chomp
×
51
                  else
52
                    "Unknown SHA"
×
53
                  end
54
  end
55

56
  def self.tagged_release?
1✔
57
    # e.g. v0.8.0
UNCOV
58
    branch.match(/^v[\d+\.+]+/) != nil
×
59
  end
60

61
  def self.branch
1✔
62
    @@branch ||= if File.exist?(revisions_logfile)
55✔
63
                   `tail -1 #{revisions_logfile}`.chomp.split(" ")[1]
2✔
64
                 elsif Rails.env.development? || Rails.env.test?
×
65
                   `git rev-parse --abbrev-ref HEAD`.chomp
×
66
                 else
67
                   "Unknown branch"
×
68
                 end
69
  end
70

71
  DEPLOYMENT_LOGFILE_COL_NUMBER = 7
1✔
72

73
  def self.find_deployment_date
1✔
74
    output = `tail -1 #{revisions_logfile}`
55✔
75
    entries = output.chomp.split(" ")
55✔
76
    return "(Deployment date could not be parsed from: #{output}.)" if entries.length <= DEPLOYMENT_LOGFILE_COL_NUMBER
55✔
77

78
    deployment_entry = entries[DEPLOYMENT_LOGFILE_COL_NUMBER]
55✔
79
    deployment_date = Date.parse(deployment_entry)
55✔
80
    return "(Deployment date could not be parsed from: #{deployment_entry}.)" if deployment_date.nil?
55✔
81

82
    formatted = parsed_date.strftime("%d %B %Y")
55✔
NEW
83
    formatted
×
84
  end
85

86
  def self.version
1✔
87
    @@version ||= if File.exist?(revisions_logfile)
55✔
88
                    find_deployment_date
55✔
89
                  else
90
                    "Not in deployed environment"
×
91
                  end
92
  end
93

94
  # This file is local to the application.
95
  # This file only has the git SHA of the version deployed (i.e. no date or branch)
96
  def self.revision_file
1✔
97
    @@revision_file ||= Rails.root.join("REVISION")
4✔
98
  end
99

100
  # Capistrano keeps this file a couple of levels up _outside_ the application.
101
  # This file includes all the information that we need (git SHA, branch name, date)
102
  def self.revisions_logfile
1✔
103
    @@revisions_logfile ||= Rails.root.join("..", "..", "revisions.log")
120✔
104
  end
105

106
  # These assignment methods are needed to facilitate testing
107
  def self.revision_file=(x)
1✔
108
    @@revision_file = x
2✔
109
  end
110

111
  def self.revisions_logfile=(x)
1✔
112
    @@revisions_logfile = x
2✔
113
  end
114
end
115
# rubocop:enable RuboCop::Cop::Style::ClassVars
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc