kraken-build 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-18mode # JRuby in 1.8 mode
7
+ - jruby-19mode # JRuby in 1.9 mode
8
+ - rbx-18mode
9
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "httparty"
4
+
5
+ gem "rake"
6
+ gem "nokogiri"
7
+ #gem "github_api"
8
+ #gem "awesome_print"
9
+
10
+ group :test do
11
+ gem 'rspec'
12
+ gem 'autotest'
13
+ end
14
+
15
+
16
+ # Specify your gem's dependencies in kraken-build.gemspec
17
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 at
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ [![Build Status](https://secure.travis-ci.org/pxlpnk/kraken-build.png?branch=master)](http://travis-ci.org/pxlpnk/kraken-build)
2
+
3
+ # Kraken::Build
4
+
5
+ Important:
6
+ This Software contains absolutly no error handling and far by not enough tests!
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'kraken-build'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install kraken-build
21
+
22
+ ## Usage
23
+
24
+ `kraken-build config.yml`
25
+
26
+ ### Sample config
27
+ <pre>
28
+ -
29
+ repository : 'rails-project'
30
+ username : 'kraken'
31
+ password : '$uperSecret'
32
+ host : "https://jenkins.host"
33
+ port : '8080'
34
+ token : '123456789'
35
+ owner : 'pxlpnk'
36
+ </pre>
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ desc "runs the stuff"
5
+ require 'rspec/core/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new('spec')
8
+
9
+ # If you want to make this the default task
10
+ task :default => :spec
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+ require 'yaml'
3
+ require 'kraken-build'
4
+
5
+
6
+ ARGV.each do|argument|
7
+
8
+ File.open(argument) do |file|
9
+ config = YAML.load(file)
10
+
11
+ config.each do |project|
12
+
13
+ project = Hash[project.map{|(k,v)| [k.to_sym,v]}]
14
+ KrakenBuild.set_config(project)
15
+ KrakenBuild.run
16
+
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/kraken-build/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["at"]
6
+ gem.email = ["at@an-ti.eu"]
7
+ gem.description = %q{A simple tool that generates a job for each github branch in your Jenkins environment}
8
+ gem.summary = %q{Managing your feature branches on Jenkins and Github}
9
+ gem.homepage = "https://github.com/pxlpnk/kraken-build"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "kraken-build"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Kraken::Build::VERSION
17
+
18
+
19
+ gem.add_development_dependency "rspec"
20
+ gem.add_dependency "rake"
21
+ gem.add_dependency "httparty"
22
+ gem.add_dependency "nokogiri"
23
+ # Other attributes omitted
24
+
25
+ end
@@ -0,0 +1,62 @@
1
+ require "httparty"
2
+ require 'nokogiri'
3
+ require 'cgi'
4
+
5
+ require "kraken-build/version"
6
+ require "kraken-build/jenkins-api.rb"
7
+ require "kraken-build/github-api.rb"
8
+
9
+
10
+
11
+ module KrakenBuild
12
+
13
+ def self.set_config(options = {})
14
+ @config = options
15
+ @repository = options[:repository]
16
+ @github = GithubApi.new(@config)
17
+ @jenkins = JenkinsApi.new(@config)
18
+ @jobs = []
19
+ @branches = []
20
+
21
+ @config
22
+ end
23
+
24
+ def self.get_jenkins_branches
25
+ @jenkins.get_jobs.map{ |job| job =~ /^#{@repository}\.(.*)$/ && $1 }.compact
26
+ end
27
+
28
+ def self.get_github_branches
29
+ @github.get_branches
30
+ end
31
+
32
+ def self.run
33
+ @jobs = get_jenkins_branches
34
+ @branches = get_github_branches
35
+
36
+ create = compute_jobs_to_create
37
+ create.map do |job|
38
+ job_name = "#{@repository}.#{job}"
39
+ puts "creating => #{job_name}"
40
+ @jenkins.create_job(job_name)
41
+ @jenkins.build_job(job_name)
42
+ end
43
+
44
+
45
+ remove = compute_jobs_to_remove
46
+ remove.map do |job|
47
+ job_name = "#{@repository}.#{job}"
48
+ puts "removing => #{job_name}"
49
+ @jenkins.remove_job(job_name)
50
+ end
51
+
52
+ end
53
+
54
+ def self.compute_jobs_to_create
55
+ @branches - @jobs
56
+ end
57
+
58
+ def self.compute_jobs_to_remove
59
+ @jobs - @branches
60
+ end
61
+
62
+ end
@@ -0,0 +1,17 @@
1
+ class GithubApi
2
+ include HTTParty
3
+
4
+ def initialize(config)
5
+ @oauth_token = config[:token]
6
+ @owner = config[:owner]
7
+ @repository = config[:repository]
8
+
9
+ self.class.base_uri "https://api.github.com"
10
+ end
11
+
12
+ def get_branches(options = {})
13
+ response = self.class.get("/repos/#{@owner}/#{@repository}/branches?access_token=#{@oauth_token}", options)
14
+ response.map{|branch| branch["name"]}
15
+ end
16
+
17
+ end
@@ -0,0 +1,63 @@
1
+ class JenkinsApi
2
+ include HTTParty
3
+
4
+
5
+ def initialize(options = {})
6
+
7
+ if options[:port]
8
+ self.class.base_uri "#{options[:host]}:#{options[:port]}"
9
+ else
10
+ self.class.base_uri options[:host]
11
+ end
12
+
13
+ if(options[:username] && options[:password])
14
+ self.class.basic_auth options[:username] , options[:password]
15
+ end
16
+
17
+ end
18
+
19
+ def get_jobs(options = {})
20
+ response = self.class.get("/api/json/", options)
21
+
22
+ jobs = response["jobs"]
23
+
24
+ jobs.map{|job| job["name"]}
25
+ end
26
+
27
+ def create_job(job, options = {})
28
+ repo = job.split('.').first
29
+ branch_name = job.split('.').last
30
+ job_config = create_job_configuration(repo, branch_name)
31
+ options.merge!(
32
+ :body => job_config,
33
+ :format => :xml, :headers => { 'content-type' => 'application/xml' })
34
+
35
+ self.class.post("/createItem/api/xml?name=#{CGI.escape(job)}", options)
36
+ end
37
+
38
+ def create_job_configuration(repo, branch)
39
+
40
+ draft = get_job_configuration("#{repo}.master")
41
+
42
+ doc = Nokogiri.XML(draft)
43
+
44
+ doc.xpath('//branches//hudson.plugins.git.BranchSpec//name').first.content = "#{repo}.#{branch}"
45
+
46
+ doc.to_xml
47
+ end
48
+
49
+ def get_job_configuration(job, options = {})
50
+ response = self.class.get("/job/#{job}/config.xml", options)
51
+
52
+ response.body
53
+ end
54
+
55
+ def remove_job(job_name, options={})
56
+ self.class.post("/job/#{CGI.escape(job_name)}/doDelete")
57
+ end
58
+
59
+ def build_job(job_name, options={})
60
+ self.class.get("/job/#{CGI.escape(job_name)}/build")
61
+ end
62
+
63
+ end
@@ -0,0 +1,5 @@
1
+ module Kraken
2
+ module Build
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe GithubApi do
4
+
5
+ before(:each) do
6
+ @options = {:token => '1234',
7
+ :owner => "FooBert",
8
+ :repository => "uber-repo"
9
+ }
10
+
11
+ @api = GithubApi.new(@options)
12
+ end
13
+
14
+ it "returns the branches of a Github repository" do
15
+ response = []
16
+ response << {"name" => "foo"}
17
+ response << {"name" => "bar"}
18
+ @api.class.should_receive(:get).with("/repos/#{@options[:owner]}/#{@options[:repository]}/branches?access_token=#{@options[:token]}",{}).and_return(response)
19
+
20
+ branches = @api.get_branches
21
+ branches.should include "foo"
22
+ branches.should include "bar"
23
+ end
24
+
25
+ end
@@ -0,0 +1,115 @@
1
+ require 'spec_helper'
2
+
3
+ describe JenkinsApi do
4
+
5
+ context "configurations" do
6
+ before(:each) do
7
+ api = JenkinsApi.new
8
+ end
9
+
10
+ it "uses no basic_auth when username and password are not prived" do
11
+ pending("flapping")
12
+ asd = {:user_name => nil, :password =>"nil"}
13
+ api = asd
14
+ api = JenkinsApi.new(asd)
15
+
16
+ api.class.default_options[:basic_auth].should be(nil)
17
+ end
18
+
19
+ it "uses basic_auth when username and password prived" do
20
+ options = {:username => 'user', :password => 'password'}
21
+ api = JenkinsApi.new(options)
22
+
23
+ api.class.default_options[:basic_auth][:username] == options[:username].should
24
+ api.class.default_options[:basic_auth][:password] == options[:password].should
25
+ end
26
+
27
+ it "uses a port when provided" do
28
+ options = {:host => "host", :port => '1337'}
29
+ api = JenkinsApi.new(options)
30
+
31
+ api.class.default_options[:base_uri].should == 'http://host:1337'
32
+ end
33
+
34
+ end
35
+
36
+
37
+ context "when interacting with Jenkins" do
38
+ before(:each) do
39
+ @api = JenkinsApi.new
40
+ end
41
+
42
+ it "#get_jobs returns an array of jobs" do
43
+ j = []
44
+ j << {"name" => "Foo"}
45
+ j << {"name" => "Bar"}
46
+
47
+ results = {"jobs" => j}
48
+
49
+ @api.class.should_receive(:get).and_return(results)
50
+
51
+ jobs = @api.get_jobs
52
+
53
+ jobs.should include "Foo"
54
+ jobs.should include "Bar"
55
+ end
56
+
57
+ it "#remove_job returns true if a job was deleted successfully" do
58
+ job_name = "foo.test_branch"
59
+
60
+ @api.class.should_receive(:post).with("/job/#{CGI.escape(job_name)}/doDelete").and_return(true)
61
+
62
+ @api.remove_job(job_name)
63
+ end
64
+
65
+ it "#build_job returns true and triggers a build for a job" do
66
+ @api.class.should_receive(:get).with("/job/FooJob/build").and_return(true)
67
+
68
+ @api.build_job("FooJob")
69
+ end
70
+
71
+ it "#get_job_configuration returns the xml configuration for a job" do
72
+ a = <<XML
73
+ <xml>foo</xml>
74
+ XML
75
+ xml = double()
76
+ xml.should_receive(:body).and_return(a)
77
+
78
+ @api.class.should_receive(:get).with("/job/FooJob/config.xml", {}).and_return(xml)
79
+
80
+ @api.get_job_configuration("FooJob").should be(a)
81
+ end
82
+
83
+ end
84
+
85
+
86
+ context "#create_job returns true on the create a job" do
87
+ before(:each) do
88
+ @api = JenkinsApi.new
89
+ end
90
+
91
+ it "creates a new job with a given xml configuration" do
92
+
93
+
94
+ a = <<XML
95
+ <xml>foo</xml>
96
+ XML
97
+
98
+ repo = "FooRepo"
99
+ branch = "master"
100
+ job = "#{repo}.#{branch}"
101
+
102
+ job = "FooRepo.master"
103
+
104
+ @api.should_receive(:create_job_configuration).with(repo, branch).and_return(a)
105
+
106
+ @api.class.should_receive(:post).with("/createItem/api/xml?name=#{job}", {
107
+ :body => a,
108
+ :format => :xml,
109
+ :headers => {"content-type"=>"application/xml" }})
110
+
111
+ @api.create_job("FooRepo.master")
112
+ end
113
+ end
114
+
115
+ end
@@ -0,0 +1,23 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'rubygems'
9
+ require 'bundler/setup'
10
+
11
+ require 'kraken-build'
12
+
13
+ RSpec.configure do |config|
14
+ config.treat_symbols_as_metadata_keys_with_true_values = true
15
+ config.run_all_when_everything_filtered = true
16
+ config.filter_run :focus
17
+
18
+ # Run specs in random order to surface order dependencies. If you find an
19
+ # order dependency and want to debug it, you can fix the order by providing
20
+ # the seed, which is printed after each run.
21
+ # --seed 1234
22
+ config.order = 'random'
23
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kraken-build
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - at
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: httparty
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: nokogiri
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: A simple tool that generates a job for each github branch in your Jenkins
79
+ environment
80
+ email:
81
+ - at@an-ti.eu
82
+ executables:
83
+ - kraken-build
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - .gitignore
88
+ - .rspec
89
+ - .travis.yml
90
+ - Gemfile
91
+ - LICENSE
92
+ - README.md
93
+ - Rakefile
94
+ - bin/kraken-build
95
+ - kraken-build.gemspec
96
+ - lib/kraken-build.rb
97
+ - lib/kraken-build/github-api.rb
98
+ - lib/kraken-build/jenkins-api.rb
99
+ - lib/kraken-build/version.rb
100
+ - spec/lib/github_api_spec.rb
101
+ - spec/lib/jenins_api_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: https://github.com/pxlpnk/kraken-build
104
+ licenses: []
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ segments:
116
+ - 0
117
+ hash: 4316786241406826877
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ segments:
125
+ - 0
126
+ hash: 4316786241406826877
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 1.8.24
130
+ signing_key:
131
+ specification_version: 3
132
+ summary: Managing your feature branches on Jenkins and Github
133
+ test_files:
134
+ - spec/lib/github_api_spec.rb
135
+ - spec/lib/jenins_api_spec.rb
136
+ - spec/spec_helper.rb