|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module VersionHelper |
| 4 | + # Returns the current git commit SHA from REVISION file |
| 5 | + # Returns nil if file doesn't exist (development/test) |
| 6 | + def current_commit_sha |
| 7 | + version_info = read_version_file |
| 8 | + version_info[:sha] |
| 9 | + end |
| 10 | + |
| 11 | + # Returns the current git branch name |
| 12 | + def current_branch |
| 13 | + version_info = read_version_file |
| 14 | + version_info[:branch] |
| 15 | + end |
| 16 | + |
| 17 | + # Returns short version of commit SHA (first 7 characters) |
| 18 | + def short_commit_sha |
| 19 | + sha = current_commit_sha |
| 20 | + sha ? sha[0..6] : nil |
| 21 | + end |
| 22 | + |
| 23 | + # Returns GitHub URL for the current commit |
| 24 | + def commit_github_url |
| 25 | + sha = current_commit_sha |
| 26 | + return nil unless sha |
| 27 | + |
| 28 | + "https://github.com/RECTOR-LABS/core/commit/#{sha}" |
| 29 | + end |
| 30 | + |
| 31 | + # Returns GitHub URL for the current branch |
| 32 | + def branch_github_url |
| 33 | + branch = current_branch |
| 34 | + return nil unless branch |
| 35 | + |
| 36 | + "https://github.com/RECTOR-LABS/core/tree/#{branch}" |
| 37 | + end |
| 38 | + |
| 39 | + # Returns deployment timestamp from REVISION file modification time |
| 40 | + def deployment_timestamp |
| 41 | + revision_file = Rails.root.join("REVISION") |
| 42 | + return nil unless revision_file.exist? |
| 43 | + |
| 44 | + File.mtime(revision_file) |
| 45 | + end |
| 46 | + |
| 47 | + # Returns formatted deployment time (e.g., "2 hours ago") |
| 48 | + def deployment_time_ago |
| 49 | + timestamp = deployment_timestamp |
| 50 | + return nil unless timestamp |
| 51 | + |
| 52 | + time_ago_in_words(timestamp) |
| 53 | + end |
| 54 | + |
| 55 | + private |
| 56 | + |
| 57 | + # Reads and parses the REVISION file |
| 58 | + # Format: SHA|BRANCH (e.g., "abc123def456|main") |
| 59 | + def read_version_file |
| 60 | + return @version_info if defined?(@version_info) |
| 61 | + |
| 62 | + revision_file = Rails.root.join("REVISION") |
| 63 | + unless revision_file.exist? |
| 64 | + @version_info = { sha: nil, branch: nil } |
| 65 | + return @version_info |
| 66 | + end |
| 67 | + |
| 68 | + content = revision_file.read.strip |
| 69 | + parts = content.split("|") |
| 70 | + |
| 71 | + @version_info = { |
| 72 | + sha: parts[0]&.strip, |
| 73 | + branch: parts[1]&.strip || "unknown" |
| 74 | + } |
| 75 | + end |
| 76 | +end |
0 commit comments