Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmith committed Sep 5, 2014
1 parent 8700ebf commit a26399a
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format progress documentation
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: ruby
#bundler_args: --without development

env:
- CI=true
rvm:
- 1.9.2
- 1.9.3
gemfile:
- Gemfile
before_install:
- bundle install
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "http://rubygems.org/"

gem 'rake'
gemspec
24 changes: 24 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "bundler/gem_tasks"

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.rspec_opts = [ '-I', 'lib', '-I', 'spec' ]
spec.pattern = FileList['spec/**/*_spec.rb']
end

RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.rspec_opts = [ '-I', 'lib', '-I', 'spec' ]
spec.pattern = FileList['spec/**/*_spec.rb']
spec.rcov = true
end

task :default => :spec

begin
require 'yard'
YARD::Rake::YardocTask.new
rescue LoadError
task :yardoc do
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
end
end
22 changes: 22 additions & 0 deletions chef-handler-slack.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
$:.push File.expand_path("../lib", __FILE__)

Gem::Specification.new do |s|
s.name = "chef-handler-slack"
s.version = "0.1.0"
s.authors = ["Derek Smith"]
s.email = ["[email protected]"]
s.homepage = "https://github.com/tinyspeck/chef-handler-slack"
s.summary = %q{Chef reports generated to a channel in Slack}
s.description = %q{Chef reports generated to a channel in Slack}
s.license = "MIT"


s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

# specify any dependencies here; for example:
s.add_development_dependency "rspec"
s.add_runtime_dependency "chef", '>= 0'
end
65 changes: 65 additions & 0 deletions lib/chef/chef_handler_slack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#
# Author:: Derek Smith <[email protected]>
# Copyright:: Copyright (c) 2014, Derek Smith
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "rubygems"
Gem.clear_paths
require "chef"
require "chef/handler"

require "net/http"
require "uri"
require "json"

class SlackReporting < Chef::Handler
attr_accessor :source, :team, :icon_emoj, :channel, :token, :username

def initialize(options = {})
@source = options[:source] || "#{Chef::Config[:node_name]}"
@channel = options[:channel] || "#test"
@api_key = options[:token] || "token"
@team = options[:team] || "doesnotexist"
@username = options[:username] || "chef"
end

def report
gemspec = if Gem::Specification.respond_to? :find_by_name
Gem::Specification.find_by_name('chef-handler-slack')
else
Gem.source_index.find_name('chef-handler-slack').last
end

Chef::Log.debug("#{gemspec.full_name} loaded as a handler.")

params = {
:username => @username,
:icon_emoji => @icon_emoj,
:channel => @channel,
:text => "HELLO",
}

begin
http = Net::HTTP.new
uri = URI.parse("https://#{@team}.slack.com/services/hooks/incoming-webhook?token=#{@token}")
req = Net::HTTP::Post.new(uri)
req.set_form_data(params)
http.request(req)
rescue Exception => e
puts "#{e}"
end
end
end

0 comments on commit a26399a

Please sign in to comment.