Sha256: a01e8d8e829589f5d8e71b53bd4de4c900f93a08038d332fc98b04cece03dbf7

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

require 'uri'
require 'json'
require 'net/http'

module Jenkins
  module Build
    class Client
      def initialize(configuration)
        @configuration = configuration

        @base_uri = URI(@configuration.server).freeze

        @connection = Net::HTTP.new(@base_uri.host, @base_uri.port)
        @connection.use_ssl = (@base_uri.scheme == 'https')

        @connection
      end

      def trigger(branch)
        response = post("/job/#{@configuration.project}/build", parameter: { name: :sha1, value: branch })
        case response
          when Net::HTTPCreated then true
          else
            raise InvalidResponse, response
        end
      end

      private

      class InvalidResponse < StandardError
        attr_reader :code, :message

        def initialize(response)
          @code = response.code
          @message = response.message
          @response = response
          super("Unexpected HTTP Response: #{@message}")
        end
      end

      def post(path, params)
        uri = URI.join(@base_uri, path)
        body = JSON.generate(params)

        post = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
        post.set_form_data json: body

        request(post)
      end

      def request(request)
        request.basic_auth(@configuration.user, @configuration.api_key)

        case response = @connection.request(request)
          when Net::HTTPSuccess then return response
          else raise InvalidResponse, response
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jenkins-build-0.1.3 lib/jenkins/build/client.rb
jenkins-build-0.1.2 lib/jenkins/build/client.rb