require_relative 'string.rb' class Git def self.remote begin `git remote -v show`.lines.first.strip.match(/github\.com[\/|:](.+)\.git/)[1] rescue $stderr.puts 'Unable to retrieve slug from >> git remote -v show' exit 1 end end def self.repo remote end def self.branch `git rev-parse --abbrev-ref HEAD`.strip end def self.tag (`git describe --tags --match 'v*' --abbrev=0 2>/dev/null` || 'HEAD').strip end def self.clean_tag(tag=self.tag) tag.strip.sub('v','').split(/[\.,-]/).select { |e| e.is_number?}.first(3).join('.') end def self.commit_count `git rev-list --count HEAD`.to_i end def self.commit_count_since_tag(tag) `git rev-list --count #{tag}.. 2>/dev/null`.to_i end def self.installed? system 'git --version >>/dev/null 2>&1' end def self.parse_deploy(commitMessage) commands = %w(deploy DEPLOY force_deploy FORCE_DEPLOY).collect{ |x| "\\b#{x}\\b" }.join("|") re = Regexp.new(/<(#{commands}):?\s*(.*?)(?:>)/) deploy = commitMessage.scan(re).flatten.compact return !deploy[0].nil? && !deploy[0].empty?, deploy[1] || '' end def self.parse_crashlytics(commitMessage) return commitMessage.scan(/(c|C):(?\d+)/).flatten.collect { |x| x.to_i }.uniq end def self.parse_sprintly(commitMessage) commands = %w(close closed closes finish finished finishes fix fixed fixes breaks unfixes reopen reopens re-open re-opens addresses re ref references refs start starts see).collect{ |x| "\\b#{x}\\b" }.join("|") prefixes = %w(task issue defect bug item ticket).collect{ |x| "\\b#{x}:\\b" }.join("|") + "|#" re = Regexp.new(/(?:#{commands})\s(?:#{prefixes})(\d+)/) return commitMessage.downcase.scan(re).flatten.collect{ |x| x.to_i }.uniq end def self.parse_commit_message(commitMessage) h = Hash.new deploy, message = self.parse_deploy(commitMessage) sprintly_tickets = self.parse_sprintly(commitMessage) crashlytics_ids = self.parse_crashlytics(commitMessage) h["deploy?"] = deploy h["message"] = message h["sprintly"] = sprintly_tickets h["crashlytics"] = crashlytics_ids return h end def self.commit_sha `git rev-parse HEAD`.strip end def self.commit_short_sha `git rev-parse --short HEAD`.strip end end