Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #104 from github/statuses

Add Janky::GitHubStatus
  • Loading branch information...
commit ca23b54d63d6aa9c3f2cd7e489be9bbf1ffb8a85 2 parents 1c9438a + 223a38c
@rsanheim rsanheim authored
View
29 README.md
@@ -148,6 +148,17 @@ Using Janky with [GitHub Enterprise][ghe] requires one extra setting:
[ghe]: https://enterprise.github.com
+### GitHub Status API
+
+https://github.com/blog/1227-commit-status-api
+
+To update pull requests with the build status generate an OAuth token
+like so:
+
+ curl -u username:password -d '{ "scopes": [ "repo:status" ], "note": "janky" }' https://api.github.com/authorizations
+
+then set `JANKY_GITHUB_STATUS_TOKEN`.
+
### Chat Notification
#### Campfire
@@ -221,33 +232,33 @@ Hacking
Get your environment up and running:
- $ script/bootstrap
+ script/bootstrap
Create the databases:
- $ mysqladmin -uroot create janky_development
- $ mysqladmin -uroot create janky_test
+ mysqladmin -uroot create janky_development
+ mysqladmin -uroot create janky_test
Create the tables:
- $ RACK_ENV=development bin/rake db:migrate
- $ RACK_ENV=test bin/rake db:migrate
+ RACK_ENV=development bin/rake db:migrate
+ RACK_ENV=test bin/rake db:migrate
Seed some data into the development database:
- $ bin/rake db:seed
+ bin/rake db:seed
Start the server:
- $ script/server
+ script/server
Open the app:
- $ open http://localhost:9393/
+ open http://localhost:9393/
Run the test suite:
- $ script/test
+ script/test
Contributing
------------
View
1  janky.gemspec
@@ -25,6 +25,7 @@ Gem::Specification.new "janky", Janky::VERSION do |s|
# test
s.add_development_dependency "database_cleaner", "~>0.6"
+ s.add_development_dependency "mocha", "~>0.10.4"
s.files = %w[
CHANGES
View
10 lib/janky.rb
@@ -46,6 +46,7 @@
require "janky/notifier/chat_service"
require "janky/notifier/mock"
require "janky/notifier/multi"
+require "janky/notifier/github_status"
require "janky/app"
require "janky/views/layout"
require "janky/views/index"
@@ -198,7 +199,14 @@ def self.setup(settings)
end
ChatService.setup(chat_name, chat_settings, chat_room)
- Notifier.setup(Notifier::ChatService)
+ if token = settings["JANKY_GITHUB_STATUS_TOKEN"]
+ Notifier.setup([
+ Notifier::GithubStatus.new(token, api_url),
+ Notifier::ChatService
+ ])
+ else
+ Notifier.setup(Notifier::ChatService)
+ end
end
# List of settings required in production.
View
57 lib/janky/notifier/github_status.rb
@@ -0,0 +1,57 @@
+module Janky
+ module Notifier
+ # Create GitHub Status updates for builds.
+ #
+ # Note that Statuses are immutable - so we create one for
+ # "pending" status when a build starts, then create a new status for
+ # "success" or "failure" when the build is complete.
+ class GithubStatus
+ # Initialize with an OAuth token to POST Statuses with
+ def initialize(token, api_url)
+ @token = token
+ @api_url = URI(api_url)
+ end
+
+ # Create a Pending Status for the Commit when it starts.
+ def started(build)
+ repo = build.repo_nwo
+ path = "repos/#{repo}/statuses/#{build.sha1}"
+
+ post(path, "pending", build.web_url, "Build ##{build.number} started")
+ end
+
+ # Create a Success or Failure Status for the Commit.
+ def completed(build)
+ repo = build.repo_nwo
+ path = "repos/#{repo}/statuses/#{build.sha1}"
+ status = build.green? ? "success" : "failure"
+
+ desc = case status
+ when "success" then "Build ##{build.number} succeeded in #{build.duration}s"
+ when "failure" then "Build ##{build.number} failed in #{build.duration}s"
+ end
+
+ post(path, status, build.web_url, desc)
+ end
+
+ # Internal: POST the new status to the API
+ def post(path, status, url, desc)
+ http = Net::HTTP.new(@api_url.host, @api_url.port)
+ post = Net::HTTP::Post.new("#{@api_url.path}#{path}")
+
+ http.use_ssl = true
+
+ post["Content-Type"] = "application/json"
+ post["Authorization"] = "token #{@token}"
+
+ post.body = {
+ :state => status,
+ :target_url => url,
+ :description => desc,
+ }.to_json
+
+ http.request(post)
+ end
+ end
+ end
+end
View
2  script/test
@@ -1,2 +1,2 @@
#!/bin/sh
-testrb test/*_test.rb
+testrb ./test/*_test.rb
View
24 test/github_status_test.rb
@@ -0,0 +1,24 @@
+require File.expand_path("../test_helper", __FILE__)
+
+class GithubStatusTest < Test::Unit::TestCase
+ def stub_build
+ @stub_build = stub(:repo_nwo => "github/janky",
+ :sha1 => "xxxx",
+ :green? => true,
+ :number => 1,
+ :duration => 1,
+ :web_url => "http://example.com/builds/1")
+ end
+
+ def setup
+ # never allow any outgoing requests
+ Net::HTTP.any_instance.stubs(:request)
+ end
+
+ test "sending successful status uses the right path" do
+ post = stub_everything
+ Net::HTTP::Post.expects(:new).with("/repos/github/janky/statuses/xxxx").returns(post)
+ notifier = Janky::Notifier::GithubStatus.new("token", "http://example.com/")
+ notifier.completed(stub_build)
+ end
+end
View
1  test/test_helper.rb
@@ -2,6 +2,7 @@
require "janky"
require "test/unit"
+require "mocha"
require "database_cleaner"
class Test::Unit::TestCase
Please sign in to comment.
Something went wrong with that request. Please try again.