Class: Janky::Notifier::GithubStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/janky/notifier/github_status.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(token, api_url, context = nil) ⇒ GithubStatus

Initialize with an OAuth token to POST Statuses with



10
11
12
13
14
# File 'lib/janky/notifier/github_status.rb', line 10

def initialize(token, api_url, context = nil)
  @token = token
  @api_url = URI(api_url)
  @default_context = context
end

Instance Method Details

#completed(build) ⇒ Object

Create a Success or Failure Status for the Commit.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/janky/notifier/github_status.rb', line 41

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, context(build))
end

#context(build) ⇒ Object



16
17
18
# File 'lib/janky/notifier/github_status.rb', line 16

def context(build)
  repository_context(build.repository) || @default_context
end

#post(path, status, url, desc, context = nil) ⇒ Object

Internal: POST the new status to the API



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/janky/notifier/github_status.rb', line 55

def post(path, status, url, desc, context = nil)
  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}"

  body = {
    :state => status,
    :target_url => url,
    :description => desc,
  }

  unless context.nil?
    post["Accept"] = "application/vnd.github.she-hulk-preview+json"
    body[:context] = context
  end

  post.body = body.to_json

  http.request(post)
end

#queued(build) ⇒ Object

Create a Pending Status for the Commit when it is queued.



25
26
27
28
29
30
# File 'lib/janky/notifier/github_status.rb', line 25

def queued(build)
  repo   = build.repo_nwo
  path  = "repos/#{repo}/statuses/#{build.sha1}"

  post(path, "pending", build.web_url, "Build ##{build.number} queued", context(build))
end

#repository_context(repository) ⇒ Object



20
21
22
# File 'lib/janky/notifier/github_status.rb', line 20

def repository_context(repository)
  repository && repository.context
end

#started(build) ⇒ Object

Create a Pending Status for the Commit when it starts.



33
34
35
36
37
38
# File 'lib/janky/notifier/github_status.rb', line 33

def started(build)
  repo   = build.repo_nwo
  path  = "repos/#{repo}/statuses/#{build.sha1}"

  post(path, "pending", build.web_url, "Build ##{build.number} started", context(build))
end