lib/thegarage/gitx/cli.rb in thegarage-gitx-1.1.0 vs lib/thegarage/gitx/cli.rb in thegarage-gitx-1.2.0.beta1
- old
+ new
@@ -3,20 +3,24 @@
require 'thegarage/gitx'
module Thegarage
module Gitx
class CLI < Thor
+ include Thor::Actions
+ add_runtime_options!
+
include Thegarage::Gitx
include Thegarage::Gitx::Git
include Thegarage::Gitx::Github
PULL_REQUEST_DESCRIPTION = "\n\n" + <<-EOS.dedent
# Use GitHub flavored Markdown http://github.github.com/github-flavored-markdown/
# Links to screencasts or screenshots with a desciption of what this is showcasing. For architectual changes please include diagrams that will make it easier for the reviewer to understand the change. Format is ![title](url).
# Link to ticket describing feature/bug (plantain, JIRA, bugzilla). Format is [title](url).
# Brief description of the change, and how it accomplishes the task they set out to do.
EOS
+ TAGGABLE_BRANCHES = %w(master staging)
method_option :trace, :type => :boolean, :aliases => '-v'
def initialize(*args)
super(*args)
RestClient.proxy = ENV['HTTPS_PROXY'] if ENV.has_key?('HTTPS_PROXY')
@@ -133,9 +137,44 @@
run_cmd "git pull origin #{Thegarage::Gitx::BASE_BRANCH}"
run_cmd "git pull . #{branch}"
run_cmd "git push origin HEAD"
integrate_branch('master', 'staging')
cleanup
+ end
+
+ desc 'buildtag', 'create a tag for the current Travis-CI build and push it back to origin'
+ def buildtag
+ travis_branch = ENV['TRAVIS_BRANCH']
+ pull_request = ENV['TRAVIS_PULL_REQUEST']
+
+ raise "Unknown branch. ENV['TRAVIS_BRANCH'] is required." unless travis_branch
+
+ if pull_request != 'false'
+ say "Skipping creation of tag for pull request: #{pull_request}"
+ elsif !TAGGABLE_BRANCHES.include?(travis_branch)
+ say "Cannot create build tag for branch: #{travis_branch}. Only #{TAGGABLE_BRANCHES} are supported."
+ else
+ timestamp = Time.now.utc.strftime '%Y-%m-%d-%H-%M-%S'
+ git_tag = "build-#{travis_branch}-#{timestamp}"
+ run_cmd "git tag #{git_tag} -a -m 'Generated tag from TravisCI build #{ENV['TRAVIS_BUILD_NUMBER']}'"
+ run_cmd "git push origin #{git_tag}"
+ end
+ end
+
+ private
+
+ # execute a shell command and raise an error if non-zero exit code is returned
+ # return the string output from the command
+ def run_cmd(cmd, options = {})
+ output = run(cmd, capture: true)
+ success = $CHILD_STATUS.to_i == 0
+ fail "#{cmd} failed" unless success || options[:allow_failure]
+ output
+ end
+
+ # check if --pretend or -p flag passed to CLI
+ def pretend?
+ options[:pretend]
end
end
end
end