require_relative 'string.rb' module Helpers 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, rubygem:false) array = tag.strip.sub('v','').split(/[\.,-]/).select { |e| e.is_number? || (rubygem && e == 'pre') } if rubygem then array.join('.') else array.first(3).join('.') end 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.commit_author `git --no-pager show -s --format='%an' -1` end def self.commit_email `git --no-pager show -s --format='%ae' -1` end def self.installed? system 'git --version >>/dev/null 2>&1' end def self.parse_deploy(commitMessage) deploy = {} commands = %w(deploy DEPLOY force_deploy FORCE_DEPLOY).collect{ |command| "\\b#{command}\\b" }.join("|") re_command_and_message = Regexp.new(/<(#{commands}):?\s*(.*?)(?:>)/) result = commitMessage.scan(re_command_and_message).flatten.compact deploy[:match] = !result[0].nil? && !result[0].empty? re_channel = Regexp.new(/\s*([@#][A-z]+)\s*/) deploy[:message] = result[1] ? result[1].gsub(re_channel, '') : '' if result[1] && result[1].scan(re_channel) && result[1].scan(re_channel).first then deploy[:channel] = result[1].scan(re_channel).first.first || '' end deploy[:author] = commit_author || '' return deploy 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) #logic from https://github.com/sprintly/sprintly-commit-parser/blob/master/sprintly_commit_parser/commit_parser.py commandsRE = Regexp.new(/(?[A-Za-z]*) ?(task|issue|defect|bug|item|ticket|:)?.?(?(?:#|(?:ticket|issue|item|defect|bug)[: ]?)(\d+)(?:(?:[, &]*|[ ,]+?and[ ]?)(?:#|(?:ticket|issue|item|defect|bug)[: ]?)(\d+))*)/) ticketsRE = Regexp.new(/(?:#|(?:ticket|issue|item|defect|bug)[: ]?)(\d+)/) commands = commitMessage.downcase.scan(commandsRE) tickets = commands.collect { |command| command.last.scan(ticketsRE) unless command.first.empty? #only collect tickets with commands } return tickets.flatten.compact.collect{ |x| x.to_i }.uniq end def self.parse_commit_message(commitMessage) h = {} deploy = self.parse_deploy(commitMessage) sprintly_tickets = self.parse_sprintly(commitMessage) crashlytics_ids = self.parse_crashlytics(commitMessage) h[:deploy] = deploy 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 end