Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 50b8eb7dcc0007b6a60ffc00734dbb6af28175bd @pxlpnk committed Jul 8, 2012
Showing with 307 additions and 0 deletions.
  1. +17 −0 .gitignore
  2. +2 −0 .rspec
  3. +17 −0 Gemfile
  4. +22 −0 LICENSE
  5. +29 −0 README.md
  6. +10 −0 Rakefile
  7. +17 −0 kraken-build.gemspec
  8. +11 −0 lib/kraken-build.rb
  9. +54 −0 lib/kraken-build/jenkins-api.rb
  10. +5 −0 lib/kraken-build/version.rb
  11. +100 −0 spec/lib/jenins_api_spec.rb
  12. +23 −0 spec/spec_helper.rb
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
@@ -0,0 +1,2 @@
+--color
+--format progress
@@ -0,0 +1,17 @@
+source 'https://rubygems.org'
+
+gem "httparty"
+gem "xml-simple"
+gem "github_api"
+gem "rake"
+gem "awesome_print"
+
+group :test do
+ gemspec
+ gem 'rspec'
+ gem 'autotest'
+end
+
+
+# Specify your gem's dependencies in kraken-build.gemspec
+gemspec
@@ -0,0 +1,22 @@
+Copyright (c) 2012 at
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
+# Kraken::Build
+
+TODO: Write a gem description
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'kraken-build'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install kraken-build
+
+## Usage
+
+TODO: Write usage instructions here
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
@@ -0,0 +1,10 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
+
+desc "runs the stuff"
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new('spec')
+
+# If you want to make this the default task
+task :default => :spec
@@ -0,0 +1,17 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/kraken-build/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["at"]
+ gem.email = ["at@an-ti.eu"]
+ gem.description = %q{A simple tool that generates a job for each github branch in your Jenkins environment}
+ gem.summary = %q{Managing your feature branches on Jenkins and Github}
+ gem.homepage = ""
+
+ gem.files = `git ls-files`.split($\)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.name = "kraken-build"
+ gem.require_paths = ["lib"]
+ gem.version = Kraken::Build::VERSION
+end
@@ -0,0 +1,11 @@
+require "kraken-build/version"
+require "kraken-build/jenkins-api.rb"
+#require "kraken-build/github-api.rb"
+
+
+
+module Kraken
+ module Build
+ # Your code goes here...
+ end
+end
@@ -0,0 +1,54 @@
+require "HTTParty"
+require "awesome_print"
+require 'xmlsimple'
+
+
+class JenkinsApi
+ include HTTParty
+
+
+ def initialize(options = {})
+
+ if options[:port]
+ self.class.base_uri "#{options[:host]}:#{options[:port]}"
+ else
+ self.class.base_uri options[:host]
+ end
+
+ if(options[:username] && options[:password])
+ self.class.basic_auth options[:username] , options[:password]
+ end
+
+ end
+
+ def get_jobs(options = {})
+ response = self.class.get("/api/json/", options)
+
+ jobs = response["jobs"]
+
+ jobs.map{|job| job["name"]}
+ end
+
+ def create_job(job_name, job_config, options = {})
+ options.merge!(
+ :body => job_config,
+ :format => :xml, :headers => { 'content-type' => 'application/xml' })
+
+ self.class.post("/createItem/api/xml?name=#{CGI.escape(job_name)}", options)
+ end
+
+ def get_job_configuration(job, options = {})
+ response = self.class.get("/job/#{job}/config.xml", options)
+
+ response.body
+ end
+
+ def remove_job(job_name, options={})
+ self.class.post("/job/#{CGI.escape(job_name)}/doDelete")
+ end
+
+ def build_job(job_name, options={})
+ self.class.get("/job/#{CGI.escape(job_name)}/build")
+ end
+
+end
@@ -0,0 +1,5 @@
+module Kraken
+ module Build
+ VERSION = "0.0.1"
+ end
+end
@@ -0,0 +1,100 @@
+require 'spec_helper'
+
+describe JenkinsApi do
+
+ context "configurations" do
+ before(:each) do
+ @api = nil
+ end
+
+ it "uses basic_auth when username and password prived" do
+ options = {:username => 'user', :password => 'password'}
+ @api = JenkinsApi.new(options)
+
+ options[:username].should == @api.class.default_options[:basic_auth][:username]
+ options[:password].should == @api.class.default_options[:basic_auth][:password]
+ end
+
+ it "uses no basic_auth when username and password are not prived" do
+ pending("Flapping spec")
+ options = {}
+ @api = JenkinsApi.new(options)
+
+ options[:username].should_not == @api.class.default_options[:basic_auth][:username]
+ options[:password].should_not == @api.class.default_options[:basic_auth][:password]
+ end
+
+ it "uses a port when provided" do
+ options = {:host => "host", :port => '1337'}
+ @api = JenkinsApi.new(options)
+
+ @api.class.default_options[:base_uri].should == 'http://host:1337'
+ end
+
+ end
+
+
+ context "retriving data from jenkins" do
+ before(:each) do
+ @api = JenkinsApi.new
+ end
+
+ it "returns active jobs from jenkins" do
+ j = []
+ j << {"name" => "Foo"}
+ j << {"name" => "Bar"}
+
+ results = {"jobs" => j}
+
+ @api.class.should_receive(:get).and_return(results)
+ jobs = @api.get_jobs
+
+ jobs.should include "Foo"
+ jobs.should include "Bar"
+
+ end
+
+ it "removes a job" do
+ @api.class.should_receive(:post).with("/job/FooJob/doDelete").and_return(true)
+ @api.remove_job("FooJob")
+ end
+
+ it "triggers a build for a job" do
+ @api.class.should_receive(:get).with("/job/FooJob/build").and_return(true)
+ @api.build_job("FooJob")
+ end
+
+ it "returns the xml configuration for a job" do
+ a = <<XML
+<xml>foo</xml>
+XML
+ xml = double()
+ xml.should_receive(:body).and_return(a)
+
+ @api.class.should_receive(:get).with("/job/FooJob/config.xml", {}).and_return(xml)
+ @api.get_job_configuration("FooJob").should be(a)
+ end
+
+ end
+
+
+ context "create a job" do
+ before(:each) do
+ @api = JenkinsApi.new
+ end
+
+ it "creates a new job with a given xml configuration" do
+ a = <<XML
+<xml>foo</xml>
+XML
+ @api.class.should_receive(:post).with("/createItem/api/xml?name=FooJob", {
+ :body => a,
+ :format => :xml,
+ :headers => {"content-type"=>"application/xml" }})
+
+ @api.create_job("FooJob",a)
+
+ end
+ end
+
+end
@@ -0,0 +1,23 @@
+# This file was generated by the `rspec --init` command. Conventionally, all
+# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
+# Require this file using `require "spec_helper"` to ensure that it is only
+# loaded once.
+#
+# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
+
+require 'rubygems'
+require 'bundler/setup'
+
+require 'kraken-build'
+
+RSpec.configure do |config|
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+ config.run_all_when_everything_filtered = true
+ config.filter_run :focus
+
+ # Run specs in random order to surface order dependencies. If you find an
+ # order dependency and want to debug it, you can fix the order by providing
+ # the seed, which is printed after each run.
+ # --seed 1234
+ config.order = 'random'
+end

0 comments on commit 50b8eb7

Please sign in to comment.