Sha256: 345768c075687a6d7e2311df076e0b18dc3a306fe6eb1e68eea3bc0a440a50c8

Contents?: true

Size: 1.75 KB

Versions: 38

Compression:

Stored size: 1.75 KB

Contents

module Bozo::Hooks

  # Hooks for notifying GitHub of the build
  #
  # The following env variables are required
  # - BUILD_URL
  # - BUILD_NUMBER
  #
  # with_hook :git_hub do |h|
  #   h.token '.....'
  #   h.owner 'zopaUK'
  #   h.repo  'bozo-scripts'
  # end
  #
  class GitHub
    require 'net/http'
    require 'openssl'
    require 'json'

    def pre_build
      submit_notification(:pending, "Build #{build_number} pending")
    end

    def post_build
      submit_notification(:success, "Build #{build_number} succeeded")
    end

    def failed_build
      submit_notification(:failure, "Build #{build_number} failed")
    end

    def token(token)
      @token = token
    end

    def owner(owner)
      @owner = owner
    end

    def repo(repo)
      @repo = repo
    end

    private

    def build_url
      env['BUILD_URL']
    end

    def build_number
      env['BUILD_NUMBER']
    end

    def submit_notification(state, description)
      return unless build_server?

      log_info "Notifying GitHub of #{state} - #{description} - #{build_url}"

      commit = `git rev-parse HEAD`

      uri = URI("https://api.github.com/repos/#{@owner}/#{@repo}/statuses/#{commit}")
      header = {
        'Content-Type'  => 'application/json',
        'Authorization' => "token #{@token}",
        'User-Agent'    => 'Bozo GitHub notifier'
      }
      data = { state: state, description: description, target_url: build_url}.to_json

      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      http.post(uri.request_uri, data, header)

      log_info "Notified GitHub of #{state} - #{description} - #{build_url}"
    end

  end

end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
bozo-scripts-0.18.5 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.18.4 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.18.3 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.18.2 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.18.1 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.18.0 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.17.0 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.16.3 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.16.2 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.16.1 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.16.0 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.15.0 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.14.0 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.13.0 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.12.0 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.11.0 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.10.6 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.10.5 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.10.4 lib/bozo/hooks/git_hub.rb
bozo-scripts-0.10.3 lib/bozo/hooks/git_hub.rb