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

pulibrary / orcid_princeton_hanami / fc91ec04-17e3-41a9-ab4b-9c4c1cc2d9bb

25 Jul 2025 05:17PM UTC coverage: 96.539% (-3.5%) from 100.0%
fc91ec04-17e3-41a9-ab4b-9c4c1cc2d9bb

push

circleci

carolyncole
Orcid url for the sandbox

1 of 1 new or added line in 1 file covered. (100.0%)

19 existing lines in 4 files now uncovered.

530 of 549 relevant lines covered (96.54%)

8.44 hits per line

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

96.55
/app/service/version_footer.rb
1
# frozen_string_literal: true
2

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

23
      # Returns a hash with version information.
24
      def self.info
1✔
25
        reset! if stale?
57✔
26
        { sha: git_sha, branch:, version:, stale: stale?, tagged_release: tagged_release? }
56✔
27
      rescue StandardError => e
28
        { error: "Error retrieving version information: #{e.message}" }
1✔
29
      end
30

31
      def self.reset!
1✔
32
        # Initalize these values so that they recalculated
33
        @@git_sha = nil
65✔
34
        @@branch = nil
65✔
35
        @@version = nil
65✔
36
      end
37

38
      def self.stale?
1✔
39
        return false if @@stale == false
115✔
40

41
        # Only read the file when version information is stale
42
        if File.exist?(revision_file)
113✔
43
          local_sha = File.read(revision_file).chomp.gsub(/\)$/, '')
24✔
44
          @@stale = local_sha != git_sha
24✔
45
        else
46
          @@stale = true
89✔
47
        end
48
      end
49

50
      def self.git_sha
1✔
51
        @@git_sha ||= if File.exist?(revisions_logfile)
82✔
52
                        log_line(revisions_logfile).chomp.split(' ')[3].gsub(/\)$/, '')
15✔
53
                      elsif Hanami.env?(:development) || Hanami.env?(:test)
46✔
UNCOV
54
                        `git rev-parse HEAD`.chomp
×
55
                      else
56
                        'Unknown SHA'
46✔
57
                      end
58
      end
59

60
      def self.tagged_release?
1✔
61
        # e.g. v0.8.0
62
        branch.match(/^v[\d+.+]+/) != nil
56✔
63
      end
64

65
      def self.branch
1✔
66
        @@branch ||= if File.exist?(revisions_logfile)
114✔
67
                       log_line(revisions_logfile).chomp.split(' ')[1]
12✔
68
                     elsif Hanami.env?(:development) || Hanami.env?(:test)
46✔
UNCOV
69
                       `git rev-parse --abbrev-ref HEAD`.chomp
×
70
                     else
71
                       'Unknown branch'
46✔
72
                     end
73
      end
74

75
      def self.version
1✔
76
        @@version ||= if File.exist?(revisions_logfile)
57✔
77
                        deployed = log_line(revisions_logfile).chomp.split(' ')[7]
12✔
78
                        Date.parse(deployed).strftime('%d %B %Y')
12✔
79
                      else
80
                        'Not in deployed environment'
45✔
81
                      end
82
      end
83

84
      def self.hanami_root_path
1✔
85
        # app path is the location of `config/app.rb`, so the root is up two
86
        Hanami.app_path.join('..', '..')
8✔
87
      end
88

89
      # This file is local to the application.
90
      # This file only has the git SHA of the version deployed (i.e. no date or branch)
91
      def self.revision_file
1✔
92
        @@revision_file ||= hanami_root_path.join('REVISION')
137✔
93
      end
94

95
      # Capistrano keeps this file a couple of levels up _outside_ the application.
96
      # This file includes all the information that we need (git SHA, branch name, date)
97
      def self.revisions_logfile
1✔
98
        @@revisions_logfile ||= hanami_root_path.join('..', '..', 'revisions.log')
215✔
99
      end
100

101
      # These assignment methods are needed to facilitate testing
102
      def self.revision_file=(filename)
1✔
103
        @@stale = true
6✔
104
        @@revision_file = filename
6✔
105
      end
106

107
      def self.revisions_logfile=(filename)
1✔
108
        @@stale = true
10✔
109
        @@revisions_logfile = filename
10✔
110
      end
111

112
      def self.log_line(revisions_logfile)
1✔
113
        log_line = `tail -1 #{revisions_logfile}`
38✔
114
        if log_line.include?('rolled back')
38✔
115
          grep_lines = `grep #{log_line.chomp.split(' ').last} #{revisions_logfile}`.split("\n")
3✔
116
          log_line = grep_lines.first
3✔
117
        end
118
        log_line
38✔
119
      end
120
    end
121
  end
122
end
123
# rubocop:enable 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