Skip to content

Commit

Permalink
Merge branch 'codeship'
Browse files Browse the repository at this point in the history
* codeship:
  bump version
  flag as beta
  bump version
  Support Codeship for buildtag command
  • Loading branch information
wireframe committed Aug 27, 2014
2 parents f60b331 + 587bccc commit 7297991
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
29 changes: 16 additions & 13 deletions lib/thegarage/gitx/cli/buildtag_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@ module Cli
class BuildtagCommand < BaseCommand
TAGGABLE_BRANCHES = %w( master staging )

desc 'buildtag', 'create a tag for the current Travis-CI build and push it back to origin'
desc 'buildtag', 'create a tag for the current build and push it back to origin (supports Travis CI and Codeship)'
def buildtag
branch = ENV['TRAVIS_BRANCH']
pull_request = ENV['TRAVIS_PULL_REQUEST']
fail "Unknown branch. Environment variables TRAVIS_BRANCH or CI_BRANCH are required" unless branch_name
fail "Branch must be one of the supported taggable branches: #{TAGGABLE_BRANCHES}" unless TAGGABLE_BRANCHES.include?(branch_name)

raise "Unknown branch. ENV['TRAVIS_BRANCH'] is required." unless branch

if pull_request != 'false'
say "Skipping creation of tag for pull request: #{pull_request}"
elsif !TAGGABLE_BRANCHES.include?(branch)
say "Cannot create build tag for branch: #{branch}. Only #{TAGGABLE_BRANCHES} are supported."
else
label = "Generated tag from TravisCI build #{ENV['TRAVIS_BUILD_NUMBER']}"
create_build_tag(branch, label)
end
label = "buildtag generated by build #{build_number}"
create_build_tag(branch_name, label)
end

private

# pull the current branch name from environment variables
# supports Travis CI or Codeship variables
# see https://www.codeship.io/documentation/continuous-integration/set-environment-variables/
def branch_name
ENV['TRAVIS_BRANCH'] || ENV['CI_BRANCH']
end

def build_number
ENV['TRAVIS_BUILD_NUMBER'] || ENV['CI_BUILD_NUMBER']
end

def create_build_tag(branch, label)
timestamp = Time.now.utc.strftime '%Y-%m-%d-%H-%M-%S'
git_tag = "build-#{branch}-#{timestamp}"
Expand Down
2 changes: 1 addition & 1 deletion lib/thegarage/gitx/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Thegarage
module Gitx
VERSION = '2.3.0'
VERSION = '2.4.0'
end
end
47 changes: 23 additions & 24 deletions spec/thegarage/gitx/cli/buildtag_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,46 @@

describe '#buildtag' do
let(:env_travis_branch) { nil }
let(:env_travis_pull_request) { nil }
let(:env_travis_build_number) { nil }
let(:env_ci_branch) { nil }
let(:env_ci_build_number) { nil }
before do
ENV['TRAVIS_BRANCH'] = env_travis_branch
ENV['TRAVIS_PULL_REQUEST'] = env_travis_pull_request
ENV['TRAVIS_BUILD_NUMBER'] = env_travis_build_number
ENV['CI_BRANCH'] = env_ci_branch
ENV['CI_BUILD_NUMBER'] = env_ci_build_number
end
context 'when ENV[\'TRAVIS_BRANCH\'] is nil' do
context 'when TRAVIS_BRANCH is nil' do
it 'raises Unknown Branch error' do
expect { cli.buildtag }.to raise_error "Unknown branch. ENV['TRAVIS_BRANCH'] is required."
expect { cli.buildtag }.to raise_error(/Unknown branch/)
end
end
context 'when the travis branch is master and the travis pull request is not false' do
let(:env_travis_branch) { 'master' }
let(:env_travis_pull_request) { '45' }
before do
expect(cli).to receive(:say).with("Skipping creation of tag for pull request: #{ENV['TRAVIS_PULL_REQUEST']}")
cli.buildtag
end
it 'tells us that it is skipping the creation of the tag' do
should meet_expectations
context 'when TRAVIS_BRANCH is NOT master or staging' do
let(:env_travis_branch) { 'random-branch' }
it 'raises unsupported branch error' do
expect { cli.buildtag }.to raise_error(/Branch must be one of the supported taggable branches/)
end
end
context 'when the travis branch is NOT master and is not a pull request' do
let(:env_travis_branch) { 'random-branch' }
let(:env_travis_pull_request) { 'false' }
context 'when TRAVIS_BRANCH is master' do
let(:env_travis_branch) { 'master' }
let(:env_travis_build_number) { '24' }
before do
expect(cli).to receive(:say).with(/Cannot create build tag for branch: #{ENV['TRAVIS_BRANCH']}/)
cli.buildtag
Timecop.freeze(Time.utc(2013, 10, 30, 10, 21, 28)) do
expect(cli).to receive(:run_cmd).with("git tag build-master-2013-10-30-10-21-28 -a -m 'buildtag generated by build 24'").ordered
expect(cli).to receive(:run_cmd).with("git push origin build-master-2013-10-30-10-21-28").ordered
cli.buildtag
end
end
it 'tells us that the branch is not supported' do
it 'creates a tag for the branch and push it to github' do
should meet_expectations
end
end
context 'when the travis branch is master and not a pull request' do
let(:env_travis_branch) { 'master' }
let(:env_travis_pull_request) { 'false' }
let(:env_travis_build_number) { '24' }
context 'when CI_BRANCH is master' do
let(:env_ci_branch) { 'master' }
let(:env_ci_build_number) { '24' }
before do
Timecop.freeze(Time.utc(2013, 10, 30, 10, 21, 28)) do
expect(cli).to receive(:run_cmd).with("git tag build-master-2013-10-30-10-21-28 -a -m 'Generated tag from TravisCI build 24'").ordered
expect(cli).to receive(:run_cmd).with("git tag build-master-2013-10-30-10-21-28 -a -m 'buildtag generated by build 24'").ordered
expect(cli).to receive(:run_cmd).with("git push origin build-master-2013-10-30-10-21-28").ordered
cli.buildtag
end
Expand Down

0 comments on commit 7297991

Please sign in to comment.