cwninja-auto_tagger 0.0.7.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [Jeff Dean]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,176 @@
1
+ # IMPORTANT NOTE
2
+
3
+ This gem is still in it's infancy, and lots of things might change. Since this creates and pushes tags to your git repository, please use with caution.
4
+
5
+ # AutoTagger
6
+
7
+ AutoTagger is a gem that helps you automatically create a date-stamped tag for each stage of your deployment, and deploy from the last tag from the previous environment.
8
+
9
+ Let's say you have the following workflow:
10
+
11
+ * Run all test on a Continuous Integration (CI) server
12
+ * Deploy to a staging server
13
+ * Deploy to a production server
14
+
15
+ You can use the `autotag` command to tag releases on your CI box, then use the capistrano tasks to auto-tag each release.
16
+
17
+ ## Installation
18
+
19
+ gem sources -a http://gems.github.com
20
+ sudo gem install zilkey-auto_tagger
21
+
22
+ ## Contribute
23
+
24
+ * [Tracker Project](http://www.pivotaltracker.com/projects/11988)
25
+ * [GitHub Repository](http://github.com/zilkey/auto_tagger/tree/master)
26
+
27
+ ## The autotag executable
28
+
29
+ Installing the gem creates an executable file named autotag, which takes two arguments: the stage, and optionally the path to the git repo:
30
+
31
+ $ autotag demo # => creates a tag like demo/200804041234 in the current directory
32
+ $ autotag demo . # => same as above
33
+ $ autotag demo /Users/me/foo # => cd's to /Users/me/foo before creating the tag
34
+
35
+ Running autotag does the following:
36
+
37
+ $ git fetch origin --tags
38
+ $ git tag <stage>/<timestamp>
39
+ $ git push origin --tags
40
+
41
+ ## Capistrano Integration
42
+
43
+ AutoTagger comes with 2 capistrano tasks:
44
+
45
+ * `release_tagger:set_branch` tries to set the branch to the last tag from the previous environment.
46
+ * `release_tagger:create_tag` runs autotag for the current stage
47
+
48
+ Example `config/deploy.rb` file:
49
+
50
+ require 'release_tagger'
51
+
52
+ # The :autotagger_stages variable is required
53
+ set :autotagger_stages, [:ci, :staging, :production]
54
+
55
+ # The :working_directory variable is optional, and defaults to Dir.pwd
56
+ # :working_directory can be an absolute or relative path
57
+ set :working_directory, "../../"
58
+
59
+ task :production do
60
+ # In each of your environments that need auto-branch setting, you need to set :stage
61
+ set :stage, :production
62
+ end
63
+
64
+ task :staging do
65
+ # If you do not set stage, it will not auto-set your branch
66
+ # set :stage, :staging
67
+ end
68
+
69
+ # You need to add the before/ater callbacks yourself
70
+ before "deploy:update_code", "release_tagger:set_branch"
71
+ after "deploy", "release_tagger:create_tag"
72
+ after "deploy", "release_tagger:write_tag_to_shared"
73
+ after "deploy", "release_tagger:print_latest_tags"
74
+
75
+ ### Cpistano-ext multistage support
76
+
77
+ If you use capistano-ext multistage, you can use auto_tagger.
78
+
79
+ set :autotagger_stages, [:ci, :staging, :production]
80
+ set :stages, [:staging, :production]
81
+ set :default_stage, :staging
82
+ require 'capistrano/ext/multistage'
83
+
84
+ When you deploy, autotagger will auto-detect your current stage.
85
+
86
+ ### release_tagger:set_branch
87
+
88
+ This task sets the git branch to the latest tag from the previous stage. Assume you have the following tags in your git repository:
89
+
90
+ * ci/01
91
+ * staging/01
92
+ * production/01
93
+
94
+ And the following stages in your capistrano file:
95
+
96
+ set :autotagger_stages, [:ci, :staging, :production]
97
+
98
+ The deployments would look like this:
99
+
100
+ cap staging release_tagger:set_branch # => sets branch to ci/01
101
+ cap production release_tagger:set_branch # => sets branch to staging/01
102
+
103
+ You can override with with the -Shead and -Stag options
104
+
105
+ cap staging release_tagger:set_branch -Shead=true # => sets branch to master
106
+ cap staging release_tagger:set_branch -Stag=staging/01 # => sets branch to staging/01
107
+
108
+ If you add `before "deploy:update_code", "release_tagger:set_branch"`, you can just deploy with:
109
+
110
+ cap staging deploy
111
+
112
+ and the branch will be set for you automatically.
113
+
114
+ ### release_tagger:create_tag
115
+
116
+ This cap task creates a new tag, based on the latest tag from the previous environment.
117
+
118
+ If there is no tag from the previous stage, it creates a new tag from the latest commit in your _working directory_.
119
+
120
+ ### release_tagger:print_latest_tags
121
+
122
+ This task reads the git version from the text file in shared:
123
+
124
+ cap staging release_tagger:read_tag_from_shared
125
+
126
+ ### release_tagger:print_latest_tags
127
+
128
+ This task takes the latest tag from each environment and prints it to the screen. You can add it to your deploy.rb like so:
129
+
130
+ after "deploy", "release_tagger:print_latest_tags"
131
+
132
+ Or call it directly, like:
133
+
134
+ cap production release_tagger:print_latest_tags
135
+
136
+ This will produce output like:
137
+
138
+ ** AUTO TAGGER: release tag history is:
139
+ ** ci ci/20090331045345 8031807feb5f4f99dd83257cdc07081fa6080cba some commit message
140
+ ** staging staging/20090331050908 8031807feb5f4f99dd83257cdc07081fa6080cba some commit message
141
+ ** production production/20090331050917 8031807feb5f4f99dd83257cdc07081fa6080cba some commit message
142
+
143
+ ## Running tests:
144
+
145
+ You must be able to ssh into your box via localhost (remote login). To make this easier, add your own key to your own account:
146
+
147
+ cat ~/.ssh/id_rsa.pub >>~/.ssh/authorized_keys
148
+
149
+ To ensure that this has worked, try this:
150
+
151
+ ssh localhost
152
+
153
+ If it asks you for a password, you've done something wrong.
154
+
155
+ To run the specs, execute:
156
+
157
+ spec spec/
158
+
159
+ To run the cucumber features, execute:
160
+
161
+ cucumber features/
162
+
163
+ ## Acknowledgments
164
+
165
+ Special thanks to
166
+
167
+ * Brian Takita for the original recipes
168
+ * Mike Dalessio for his git fu
169
+ * Chad Wooley for his feature ideas
170
+ * Tim Holahan for his QA
171
+
172
+ ## Links
173
+
174
+ * http://codeintensity.blogspot.com/2008/06/changelogs-and-deployment-notification.html
175
+
176
+ Copyright (c) 2009 [Jeff Dean], released under the MIT license
data/bin/autotag ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "lib", "auto_tagger"))
3
+ require 'getoptlong'
4
+
5
+ opts = GetoptLong.new(
6
+ ['--help', '-h', '-?', GetoptLong::NO_ARGUMENT]
7
+ )
8
+
9
+ def usage
10
+ puts
11
+ puts "USAGE: #{File.basename($0)} <stage> [<repository>]"
12
+ puts
13
+ puts ' where: stage sets the tag prefix'
14
+ puts ' repository sets the repository to act on - defualts to the current directory'
15
+ puts
16
+ puts ' examples: autotag'
17
+ puts ' autotag .'
18
+ puts ' autotag ../'
19
+ puts ' autotag /data/myrepo'
20
+ puts ' autotag demo'
21
+ puts
22
+ puts
23
+ exit 0
24
+ end
25
+
26
+ opts.each do |opt, arg|
27
+ case opt
28
+ when "--help"
29
+ usage
30
+ end
31
+ end
32
+
33
+ if ARGV[0]
34
+ AutoTagger.new(ARGV[0], ARGV[1]).create_tag
35
+ exit 0
36
+ else
37
+ usage
38
+ end
@@ -0,0 +1,10 @@
1
+ [
2
+ 'commander',
3
+ 'repository',
4
+ 'tag',
5
+ 'auto_tagger',
6
+ 'stage_manager',
7
+ 'capistrano_helper'
8
+ ].each do |file|
9
+ require File.expand_path(File.join(File.dirname(__FILE__), "auto_tagger", file))
10
+ end
@@ -0,0 +1,26 @@
1
+ class AutoTagger
2
+
3
+ class EnvironmentCannotBeBlankError < StandardError; end
4
+
5
+ attr_reader :stage, :repository, :working_directory
6
+
7
+ def initialize(stage, path = nil)
8
+ raise EnvironmentCannotBeBlankError if stage.to_s.strip == ""
9
+ @working_directory = File.expand_path(path ||= Dir.pwd)
10
+ @repository = Repository.new(@working_directory)
11
+ @stage = stage
12
+ end
13
+
14
+ def create_tag(commit = nil)
15
+ repository.tags.fetch
16
+ new_tag = repository.tags.create(stage, commit)
17
+ repository.tags.push
18
+ new_tag
19
+ end
20
+
21
+ def latest_tag
22
+ repository.tags.fetch
23
+ repository.tags.latest_from(stage)
24
+ end
25
+
26
+ end
@@ -0,0 +1,39 @@
1
+ class CapistranoHelper
2
+
3
+ attr_reader :variables, :stage, :working_directory
4
+
5
+ def initialize(variables)
6
+ @stage_manager = StageManager.new(variables[:autotagger_stages])
7
+ @variables = variables
8
+ @stage = variables[:stage]
9
+ @working_directory = variables[:working_directory] || Dir.pwd
10
+ end
11
+
12
+ def previous_stage
13
+ @stage_manager.previous_stage(stage)
14
+ end
15
+
16
+ def branch
17
+ if variables.has_key?(:head)
18
+ variables[:branch]
19
+ elsif variables.has_key?(:tag)
20
+ variables[:tag]
21
+ elsif stage && (latest = AutoTagger.new(stage, working_directory).latest_tag)
22
+ latest
23
+ else
24
+ variables[:branch]
25
+ end
26
+ end
27
+
28
+ def release_tag_entries
29
+ entries = []
30
+ @stage_manager.stages.each do |stage|
31
+ tagger = AutoTagger.new(stage, working_directory)
32
+ tag = tagger.latest_tag
33
+ commit = tagger.repository.commit_for(tag)
34
+ entries << "#{stage.to_s.ljust(10, " ")} #{tag.to_s.ljust(30, " ")} #{commit.to_s}"
35
+ end
36
+ entries
37
+ end
38
+
39
+ end
@@ -0,0 +1,15 @@
1
+ class Commander
2
+ class << self
3
+ def execute(path, cmd)
4
+ `#{command_in_context(path, cmd)}`
5
+ end
6
+
7
+ def execute!(path, cmd)
8
+ system command_in_context(path, cmd)
9
+ end
10
+
11
+ def command_in_context(path, cmd)
12
+ "cd #{path} && #{cmd}"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,42 @@
1
+ class Repository
2
+
3
+ class NoPathProvidedError < StandardError; end
4
+ class NoSuchPathError < StandardError; end
5
+ class InvalidGitRepositoryError < StandardError; end
6
+ class GitCommandFailedError < StandardError; end
7
+
8
+ attr_reader :path
9
+
10
+ def initialize(path)
11
+ if path.to_s.strip == ""
12
+ raise NoPathProvidedError
13
+ elsif ! File.exists?(path)
14
+ raise NoSuchPathError
15
+ elsif ! File.exists?(File.join(path, ".git"))
16
+ raise InvalidGitRepositoryError
17
+ else
18
+ @path = path
19
+ end
20
+ end
21
+
22
+ def ==(other)
23
+ other.is_a?(Repository) && other.path == path
24
+ end
25
+
26
+ def tags
27
+ @tags ||= Tag.new(self)
28
+ end
29
+
30
+ def commit_for(tag)
31
+ Commander.execute(path, "git --no-pager log #{tag} --pretty=oneline -1")
32
+ end
33
+
34
+ def run(cmd)
35
+ Commander.execute(path, cmd)
36
+ end
37
+
38
+ def run!(cmd)
39
+ Commander.execute!(path, cmd) || raise(GitCommandFailedError)
40
+ end
41
+
42
+ end
@@ -0,0 +1,23 @@
1
+ class StageManager
2
+
3
+ class NoStagesSpecifiedError < StandardError
4
+ def message
5
+ "You must set the :stages variable to an array, like set :stages, [:ci, :demo]"
6
+ end
7
+ end
8
+
9
+ attr_reader :stages
10
+
11
+ def initialize(stages)
12
+ raise NoStagesSpecifiedError unless stages && stages.is_a?(Array)
13
+ @stages = stages
14
+ end
15
+
16
+ def previous_stage(stage)
17
+ if stage
18
+ index = stages.index(stage) - 1
19
+ stages[index] if index > -1
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,41 @@
1
+ # git --no-pager log --pretty=oneline -1
2
+ # git tag -a -m 'Successful continuous integration build on #{timestamp}' #{tag_name}"
3
+ class Tag
4
+
5
+ attr_reader :repository
6
+
7
+ def initialize(repository)
8
+ @repository = repository
9
+ end
10
+
11
+ def find_all
12
+ repository.run("git tag").split("\n")
13
+ end
14
+
15
+ def fetch
16
+ repository.run! "git fetch origin --tags"
17
+ end
18
+
19
+ def latest_from(stage)
20
+ find_all.select{|tag| tag =~ /^#{stage}/}.sort.last
21
+ end
22
+
23
+ def push
24
+ repository.run! "git push origin --tags"
25
+ end
26
+
27
+ def create(stage, commit = nil)
28
+ tag_name = name_for(stage)
29
+ cmd = "git tag #{tag_name}"
30
+ cmd += " #{commit}" if commit
31
+ repository.run! cmd
32
+ tag_name
33
+ end
34
+
35
+ private
36
+
37
+ def name_for(stage)
38
+ "%s/%s" % [stage, Time.now.utc.strftime('%Y%m%d%H%M%S')]
39
+ end
40
+
41
+ end
@@ -0,0 +1,57 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "lib", "auto_tagger"))
2
+
3
+ Capistrano::Configuration.instance(:must_exist).load do
4
+ namespace :release_tagger do
5
+ desc %Q{
6
+ Sets the branch to the latest tag from the previous stage.
7
+ Use -Shead=true to set the branch to master, -Stag=<tag> to specify the tag explicitly.
8
+ }
9
+ task :set_branch do
10
+ if variables[:stage] and autotagger_stages.include?(stage)
11
+ if branch_name = CapistranoHelper.new(variables).branch
12
+ set :branch, branch_name
13
+ logger.info "setting branch to #{branch_name}"
14
+ else
15
+ logger.info "AUTO TAGGER: skipping auto-assignment of branch. Branch will remain the default."
16
+ end
17
+ end
18
+ end
19
+
20
+ desc %Q{Prints the most current tags from all stages}
21
+ task :print_latest_tags, :roles => :app do
22
+ logger.info "AUTO TAGGER: release tag history is:"
23
+ entries = CapistranoHelper.new(variables).release_tag_entries
24
+ entries.each do |entry|
25
+ logger.info entry
26
+ end
27
+ end
28
+
29
+ desc %Q{Reads the text file with the latest tag from the shared directory}
30
+ task :read_tag_from_shared, :roles => :app do
31
+ logger.info "AUTO TAGGER: latest tag deployed to this environment was:"
32
+ run "cat #{shared_path}/released_git_tag.txt"
33
+ end
34
+
35
+ desc %Q{Writes the tag name to a file in the shared directory}
36
+ task :write_tag_to_shared, :roles => :app do
37
+ if exists?(:branch)
38
+ logger.info "AUTO TAGGER: writing tag to shared text file on remote server"
39
+ run "echo '#{branch}' > #{shared_path}/released_git_tag.txt"
40
+ else
41
+ logger.info "AUTO TAGGER: no branch available. Text file was not written to server"
42
+ end
43
+ end
44
+
45
+ desc %Q{Creates a tag using the stage variable}
46
+ task :create_tag, :roles => :app do
47
+ if variables[:stage]
48
+ previous_stage = StageManager.new(autotagger_stages).previous_stage(stage)
49
+ previous_tag = previous_stage && AutoTagger.new(previous_stage, Dir.pwd).latest_tag
50
+ tag_name = AutoTagger.new(variables[:stage], variables[:working_directory]).create_tag(previous_tag)
51
+ logger.info "AUTO TAGGER created tag #{tag_name} from #{previous_tag.inspect}"
52
+ else
53
+ logger.info "AUTO TAGGER WARNING: skipping auto-creation of tag. Please specify :stage to enable auto-creation of tags (like set :stage, :ci)."
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,85 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe AutoTagger do
4
+ before(:each) do
5
+ stub(Dir).pwd { File.join(File.dirname(__FILE__), '..', '..') }
6
+ end
7
+
8
+ describe ".new" do
9
+ it "blows up if you don't pass an stage" do
10
+ proc do
11
+ AutoTagger.new(nil)
12
+ end.should raise_error(AutoTagger::EnvironmentCannotBeBlankError)
13
+ end
14
+
15
+ it "sets the stage when it's passed" do
16
+ AutoTagger.new("ci").stage.should == "ci"
17
+ end
18
+
19
+ it "sets the path to Dir.pwd when nil" do
20
+ mock(Dir).pwd { "/foo" }
21
+ mock(Repository).new("/foo")
22
+ AutoTagger.new("ci")
23
+ end
24
+
25
+ it "expands the path when the path is passed" do
26
+ mock(Repository).new(File.expand_path("."))
27
+ AutoTagger.new("ci", ".")
28
+ end
29
+
30
+ it "exposes the working directory" do
31
+ mock(Repository).new(File.expand_path("."))
32
+ AutoTagger.new("ci", ".").working_directory.should == File.expand_path(".")
33
+ end
34
+ end
35
+
36
+ describe "#create_tag" do
37
+ it "generates the correct commands" do
38
+ time = Time.local(2001,1,1)
39
+ mock(Time).now.once {time}
40
+ timestamp = time.utc.strftime('%Y%m%d%H%M%S')
41
+ mock(File).exists?(anything).twice { true }
42
+
43
+ mock(Commander).execute!("/foo", "git fetch origin --tags") {true}
44
+ mock(Commander).execute!("/foo", "git tag ci/#{timestamp}") {true}
45
+ mock(Commander).execute!("/foo", "git push origin --tags") {true}
46
+
47
+ AutoTagger.new("ci", "/foo").create_tag
48
+ end
49
+
50
+ it "allows you to base it off an existing tag or commit" do
51
+ time = Time.local(2001,1,1)
52
+ mock(Time).now.once {time}
53
+ timestamp = time.utc.strftime('%Y%m%d%H%M%S')
54
+ mock(File).exists?(anything).twice { true }
55
+
56
+ mock(Commander).execute!("/foo", "git fetch origin --tags") {true}
57
+ mock(Commander).execute!("/foo", "git tag ci/#{timestamp} guid") {true}
58
+ mock(Commander).execute!("/foo", "git push origin --tags") {true}
59
+
60
+ AutoTagger.new("ci", "/foo").create_tag("guid")
61
+ end
62
+
63
+ it "returns the tag that was created" do
64
+ time = Time.local(2001,1,1)
65
+ mock(Time).now.once {time}
66
+ timestamp = time.utc.strftime('%Y%m%d%H%M%S')
67
+ mock(File).exists?(anything).twice { true }
68
+ mock(Commander).execute!(anything, anything).times(any_times) {true}
69
+
70
+ AutoTagger.new("ci", "/foo").create_tag.should == "ci/#{timestamp}"
71
+ end
72
+ end
73
+
74
+ describe "#latest_tag" do
75
+ it "generates the correct commands" do
76
+ mock(File).exists?(anything).twice { true }
77
+
78
+ mock(Commander).execute!("/foo", "git fetch origin --tags") {true}
79
+ mock(Commander).execute("/foo", "git tag") { "ci_01" }
80
+
81
+ AutoTagger.new("ci", "/foo").latest_tag
82
+ end
83
+ end
84
+
85
+ end
@@ -0,0 +1,135 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe CapistranoHelper do
4
+
5
+ describe ".new" do
6
+ it "blows up if there are no stages" do
7
+ proc do
8
+ CapistranoHelper.new({})
9
+ end.should raise_error(StageManager::NoStagesSpecifiedError)
10
+ end
11
+ end
12
+
13
+ describe "#variables" do
14
+ it "returns all variables" do
15
+ CapistranoHelper.new({:autotagger_stages => [:bar]}).variables.should == {:autotagger_stages => [:bar]}
16
+ end
17
+ end
18
+
19
+ describe "#working_directory" do
20
+ it "returns the hashes' working directory value" do
21
+ CapistranoHelper.new({:autotagger_stages => [:bar], :working_directory => "/foo"}).working_directory.should == "/foo"
22
+ end
23
+
24
+ it "defaults to Dir.pwd if it's not set, or it's nil" do
25
+ mock(Dir).pwd { "/bar" }
26
+ CapistranoHelper.new({:autotagger_stages => [:bar]}).working_directory.should == "/bar"
27
+ end
28
+ end
29
+
30
+ describe "#stage" do
31
+ it "returns the hashes' current stage value" do
32
+ CapistranoHelper.new({:autotagger_stages => [:bar], :stage => :bar}).stage.should == :bar
33
+ CapistranoHelper.new({:autotagger_stages => [:bar]}).stage.should be_nil
34
+ end
35
+ end
36
+
37
+ describe "#release_tag_entries" do
38
+ it "returns a column-justifed version of all the commits" do
39
+ mock(Commander).execute("/foo", "git tag").times(any_times) { "ci_01\nstaging_01\nproduction_01" }
40
+ mock(Commander).execute("/foo", "git --no-pager log ci_01 --pretty=oneline -1") { "guid1" }
41
+ mock(Commander).execute("/foo", "git --no-pager log staging_01 --pretty=oneline -1") { "guid2" }
42
+ mock(Commander).execute("/foo", "git --no-pager log production_01 --pretty=oneline -1") { "guid3" }
43
+ mock(Commander).execute!("/foo", "git fetch origin --tags").times(any_times) { true }
44
+ mock(File).exists?(anything).times(any_times) {true}
45
+
46
+ variables = {
47
+ :working_directory => "/foo",
48
+ :autotagger_stages => [:ci, :staging, :production]
49
+ }
50
+ histories = CapistranoHelper.new(variables).release_tag_entries
51
+ histories.length.should == 3
52
+ histories[0].should include("ci_01", "guid1")
53
+ histories[1].should include("staging_01", "guid2")
54
+ histories[2].should include("production_01", "guid3")
55
+ end
56
+ end
57
+
58
+ describe "#branch" do
59
+ describe "with :head and :branch specified" do
60
+ it "returns master" do
61
+ variables = {
62
+ :autotagger_stages => [:bar],
63
+ :head => nil,
64
+ :branch => "foo"
65
+ }
66
+ CapistranoHelper.new(variables).branch.should == "foo"
67
+ end
68
+ end
69
+
70
+ describe "with :head specified, but no branch specified" do
71
+ it "returns master" do
72
+ variables = {
73
+ :autotagger_stages => [:bar],
74
+ :head => nil
75
+ }
76
+ CapistranoHelper.new(variables).branch.should == nil
77
+ end
78
+ end
79
+
80
+ describe "with :branch specified" do
81
+ it "returns the value of branch" do
82
+ variables = {
83
+ :autotagger_stages => [:bar],
84
+ :branch => "foo"
85
+ }
86
+ CapistranoHelper.new(variables).branch.should == "foo"
87
+ end
88
+ end
89
+
90
+ describe "with a stage with a tag" do
91
+ it "returns the latest tag for this stage" do
92
+ variables = {
93
+ :autotagger_stages => [:foo, :bar],
94
+ :stage => :bar,
95
+ :branch => "master",
96
+ :working_directory => "/foo"
97
+ }
98
+ tagger = Object.new
99
+ mock(tagger).latest_tag { "bar_01" }
100
+ mock(AutoTagger).new(:bar, "/foo") { tagger }
101
+ CapistranoHelper.new(variables).branch.should == "bar_01"
102
+ end
103
+ end
104
+
105
+ describe "with no branch and a previous stage with no tag" do
106
+ it "returns nil" do
107
+ variables = {
108
+ :autotagger_stages => [:foo, :bar],
109
+ :stage => :bar,
110
+ :working_directory => "/foo"
111
+ }
112
+ tagger = Object.new
113
+ mock(tagger).latest_tag { nil }
114
+ mock(AutoTagger).new(:bar, "/foo") { tagger }
115
+ CapistranoHelper.new(variables).branch.should == nil
116
+ end
117
+ end
118
+
119
+ describe "with no branch and previous stage" do
120
+ it "returns nil" do
121
+ variables = {
122
+ :autotagger_stages => [:bar],
123
+ :stage => :bar,
124
+ :working_directory => "/foo"
125
+ }
126
+ tagger = Object.new
127
+ mock(tagger).latest_tag { nil }
128
+ mock(AutoTagger).new(:bar, "/foo") { tagger }
129
+ CapistranoHelper.new(variables).previous_stage.should be_nil
130
+ CapistranoHelper.new(variables).branch.should == nil
131
+ end
132
+ end
133
+ end
134
+
135
+ end
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Commander do
4
+ describe ".execute" do
5
+ it "execute the command and returns the results" do
6
+ mock(Commander).`("cd /foo && ls") { "" } #`
7
+ Commander.execute("/foo", "ls")
8
+ end
9
+ end
10
+
11
+ describe "system" do
12
+ it "executes and doesn't return anything" do
13
+ mock(Commander).system("cd /foo && ls")
14
+ Commander.execute!("/foo", "ls")
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,72 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Repository do
4
+ describe ".new" do
5
+ it "sets the repo" do
6
+ mock(File).exists?(anything).twice { true }
7
+ repo = Repository.new("/foo")
8
+ repo.path.should == "/foo"
9
+ end
10
+
11
+ it "raises an error when the path is blank" do
12
+ proc do
13
+ Repository.new(" ")
14
+ end.should raise_error(Repository::NoPathProvidedError)
15
+ end
16
+
17
+ it "raises an error when the path is nil" do
18
+ proc do
19
+ Repository.new(nil)
20
+ end.should raise_error(Repository::NoPathProvidedError)
21
+ end
22
+
23
+ it "raises an error with a file that doesn't exist" do
24
+ mock(File).exists?("/foo") { false }
25
+ proc do
26
+ Repository.new("/foo")
27
+ end.should raise_error(Repository::NoSuchPathError)
28
+ end
29
+
30
+ it "raises an error with a non-git repository" do
31
+ mock(File).exists?("/foo") { true }
32
+ mock(File).exists?("/foo/.git") { false }
33
+ proc do
34
+ Repository.new("/foo")
35
+ end.should raise_error(Repository::InvalidGitRepositoryError)
36
+ end
37
+ end
38
+
39
+ describe "#==" do
40
+ it "compares paths" do
41
+ mock(File).exists?(anything).times(any_times) { true }
42
+ Repository.new("/foo").should_not == "/foo"
43
+ Repository.new("/foo").should_not == Repository.new("/bar")
44
+ Repository.new("/foo").should == Repository.new("/foo")
45
+ end
46
+ end
47
+
48
+ describe "#run" do
49
+ it "sends the correct command" do
50
+ mock(File).exists?(anything).twice { true }
51
+ mock(Commander).execute("/foo", "bar")
52
+ Repository.new("/foo").run("bar")
53
+ end
54
+ end
55
+
56
+ describe "run!" do
57
+ it "sends the correct command" do
58
+ mock(File).exists?(anything).twice { true }
59
+ mock(Commander).execute!("/foo", "bar") { true }
60
+ Repository.new("/foo").run!("bar")
61
+ end
62
+
63
+ it "raises an exception if it the command returns false" do
64
+ mock(File).exists?(anything).twice { true }
65
+ mock(Commander).execute!("/foo", "bar") { false }
66
+ proc do
67
+ Repository.new("/foo").run!("bar")
68
+ end.should raise_error(Repository::GitCommandFailedError)
69
+ end
70
+ end
71
+
72
+ end
@@ -0,0 +1,30 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe StageManager do
4
+
5
+ describe ".new" do
6
+ [nil, ""].each do |value|
7
+ it "blows up if there are stages == #{value.inspect}" do
8
+ proc do
9
+ StageManager.new(value)
10
+ end.should raise_error(StageManager::NoStagesSpecifiedError)
11
+ end
12
+ end
13
+ end
14
+
15
+ describe "#previous_stage" do
16
+ it "returns the previous stage if there is more than one stage, and there is a current stage" do
17
+ StageManager.new([:foo, :bar]).previous_stage(:bar).should == :foo
18
+ end
19
+
20
+ it "returns nil if there is no previous stage" do
21
+ StageManager.new([:bar]).previous_stage(:bar).should be_nil
22
+ end
23
+
24
+ it "returns nil if there is no current stage" do
25
+ StageManager.new([:bar]).previous_stage(nil).should be_nil
26
+ end
27
+ end
28
+
29
+
30
+ end
@@ -0,0 +1,66 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Tag do
4
+
5
+ before(:each) do
6
+ @repository = Object.new
7
+ end
8
+
9
+ describe ".new" do
10
+ it "sets the repository" do
11
+ Tag.new(@repository).repository.should == @repository
12
+ end
13
+ end
14
+
15
+ describe "#find_all" do
16
+ it "returns an array of tags" do
17
+ mock(@repository).run("git tag") { "ci_01\nci_02" }
18
+ Tag.new(@repository).find_all.should == ["ci_01", "ci_02"]
19
+ end
20
+
21
+ it "returns an empty array if there are none" do
22
+ mock(@repository).run("git tag") { "" }
23
+ Tag.new(@repository).find_all.should be_empty
24
+ end
25
+ end
26
+
27
+ describe "#latest_from" do
28
+ before do
29
+ @tag = Tag.new(@repository)
30
+ mock(@tag).find_all { ["ci/01", "ci/02"] }
31
+ end
32
+
33
+ it "returns the latest tag that starts with the specified stage" do
34
+ @tag.latest_from(:ci).should == "ci/02"
35
+ end
36
+
37
+ it "returns nil if none match" do
38
+ @tag.latest_from(:staging).should be_nil
39
+ end
40
+ end
41
+
42
+ describe "#fetch_tags" do
43
+ it "sends the correct command" do
44
+ mock(@repository).run!("git fetch origin --tags")
45
+ Tag.new(@repository).fetch
46
+ end
47
+ end
48
+
49
+ describe "#push" do
50
+ it "sends the correct command" do
51
+ mock(@repository).run!("git push origin --tags")
52
+ Tag.new(@repository).push
53
+ end
54
+ end
55
+
56
+ describe "#create" do
57
+ it "creates the right command and returns the name" do
58
+ time = Time.local(2001,1,1)
59
+ mock(Time).now.once {time}
60
+ tag_name = "ci/#{time.utc.strftime('%Y%m%d%H%M%S')}"
61
+ mock(@repository).run!("git tag #{tag_name}")
62
+ Tag.new(@repository).create("ci").should == tag_name
63
+ end
64
+ end
65
+
66
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec'
2
+ require 'rr'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "lib", "auto_tagger"))
4
+
5
+ Spec::Runner.configure do |config|
6
+ config.mock_with :rr
7
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cwninja-auto_tagger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7.1
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Dean
8
+ - Tom Lea
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-03-28 00:00:00 -07:00
14
+ default_executable: autotag
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: capistrano
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 2.5.3
25
+ version:
26
+ description:
27
+ email:
28
+ executables:
29
+ - autotag
30
+ extensions: []
31
+
32
+ extra_rdoc_files: []
33
+
34
+ files:
35
+ - lib/auto_tagger
36
+ - lib/auto_tagger/auto_tagger.rb
37
+ - lib/auto_tagger/capistrano_helper.rb
38
+ - lib/auto_tagger/commander.rb
39
+ - lib/auto_tagger/repository.rb
40
+ - lib/auto_tagger/stage_manager.rb
41
+ - lib/auto_tagger/tag.rb
42
+ - lib/auto_tagger.rb
43
+ - recipes/release_tagger.rb
44
+ - bin/autotag
45
+ - spec/auto_tagger
46
+ - spec/auto_tagger/auto_tagger_spec.rb
47
+ - spec/auto_tagger/capistrano_helper_spec.rb
48
+ - spec/auto_tagger/commander_spec.rb
49
+ - spec/auto_tagger/repository_spec.rb
50
+ - spec/auto_tagger/stage_manager_spec.rb
51
+ - spec/auto_tagger/tag_spec.rb
52
+ - spec/spec_helper.rb
53
+ - MIT-LICENSE
54
+ - README.md
55
+ has_rdoc: false
56
+ homepage: http://github.com/cwninja/auto_tagger/
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ - recipes
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.2.0
79
+ signing_key:
80
+ specification_version: 2
81
+ summary: Helps you automatically create tags for each stage in a multi-stage deploment and deploy from the latest tag from the previous environment
82
+ test_files: []
83
+