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

Add Janky::GitHubStatus #104

Merged
merged 10 commits into from Sep 12, 2012
29 changes: 20 additions & 9 deletions README.md
Expand Up @@ -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

then set `JANKY_GITHUB_STATUS_TOKEN`.

### Chat Notification

#### Campfire
Expand Down Expand Up @@ -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
------------
Expand Down
10 changes: 9 additions & 1 deletion lib/janky.rb
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

er was this a mistake @sr?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I guess I understand it. Man that setup method is a beast.

])
else
Notifier.setup(Notifier::ChatService)
end
end

# List of settings required in production.
Expand Down
57 changes: 57 additions & 0 deletions 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}")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extra / causes the URL to be //repos which returns a 404.


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