Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/onebox/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,29 @@ def self.engines
attr_reader :url
attr_reader :cache
attr_reader :timeout

DEFUALT = {}
def options
@options
end

def options=(opt)
return @options if opt.nil? #make sure options provided
if opt.instance_of? OpenStruct
@options = @options.merge(opt.to_h)
else
@options = @options.merge(opt)
end
@options
end


def initialize(link, cache = nil, timeout = nil)

@options = DEFUALT
class_name = self.class.name.split("::").last.to_s
self.options = Onebox.options[class_name] || {} #Set the engine options extracted from global options.

@url = link
@cache = cache || Onebox.options.cache
@timeout = timeout || Onebox.options.timeout
Expand Down Expand Up @@ -102,6 +123,7 @@ def onebox_name
require_relative "engine/json"
require_relative "engine/amazon_onebox"
require_relative "engine/classic_google_maps_onebox"
require_relative "engine/github_issue_onebox"
require_relative "engine/github_blob_onebox"
require_relative "engine/github_commit_onebox"
# broken
Expand Down
47 changes: 47 additions & 0 deletions lib/onebox/engine/github_pullrequest_onebox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,63 @@ def url

private

#Make an api JSON request, will attempt to authenticate if provided in the engine options
# Author: Lidlanca
#: self.options[:github_auth_method] = :basic | :oauth | nil
# :oauth is the recommend way for authentication. when generating token you can control privileges, and you do not expose your password
# :basic require username and password provided in options[:github_auth_user , :github_auth_pass]
# nil or false will make a request without any authentication. request rate limit are lower.

def api_json_request url
box_options = self.options
case box_options[:github_auth_method]
when :basic
auth = [box_options[:github_auth_user] , box_options[:github_auth_pass]] # user name and password
when :oauth
auth = [box_options[:github_auth_token] , "x-oauth-basic"] #oauth does not need password with token
else
#request without auth
return ::MultiJson.load(open(url,"Accept"=>"application/vnd.github.v3.text+json",read_timeout: timeout))

end
#Request with auth
return ::MultiJson.load(open(url,"Accept"=>"application/vnd.github.v3.text+json",http_basic_authentication:auth, read_timeout: timeout))
end

def raw
@raw ||= api_json_request url
end
def match
@match ||= @url.match(%r{github\.com/(?<owner>[^/]+)/(?<repository>[^/]+)/pull/(?<number>[^/]+)})
end

def data
box_options = self.options
result = raw.clone

pull_status = "Pull Status:" << {:closed=>"closed",:open=>"open"}[raw["state"].to_sym] << (raw["state"] == "closed" ? (raw["merged"] ? " & accepted" : " & declined") : "") #closed , open
result['pull_status_str'] = pull_status
if box_options[:get_build_status]
url2 = raw["statuses_url"]
raw2 = api_json_request url2 #2nd api request to get build status

result['pull_status'] = raw["state"]
result['pull_status_str'] = pull_status
result['pull_status_str_open'] = raw["state"]=="open"
result['pull_status_closed_accepted'] = raw["state"]=="closed" && raw["merged"]
result['pull_status_closed_declined'] = raw["state"]=="closed" && !raw["merged"]
unless raw2.empty?
result['build_status'] = "Build status: " + raw2[0]["state"].to_s.capitalize + " | " + raw2[0]["description"].to_s
end
end


result['link'] = link
result['created_at'] = Time.parse(result['created_at']).strftime("%I:%M%p - %d %b %y")
result
end
end
end
end


21 changes: 21 additions & 0 deletions templates/githubpullrequest.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@
<a href="{{html_url}}" target="_blank">{{title}}</a>
</h4>



<div class='github-commit-status' style='margin-bottom:3px;'>
{{#pull_status_str_open}}
<span style='background:#6cc644;color:#fff;display:inline-block;border-radius:3px;padding:2px;'>
{{/pull_status_str_open}}
{{#pull_status_closed_accepted}}
<span style='background:#6e5494;color:#fff;display:inline-block;border-radius:3px;padding:2px;'>
{{/pull_status_closed_accepted}}
{{#pull_status_closed_declined}}
<span style='background:#bd2c00;color:#fff;display:inline-block;border-radius:3px;padding:2px;'>
{{/pull_status_closed_declined}}

{{pull_status_str}} </span>
</br>

{{#build_status}}
<span style='padding: 2px;background: #cfcfcf;'>{{build_status}}</span>
{{/build_status}}
</div>

<div class="date">
by <a href="{{user.html_url}}" target="_blank">{{user.login}}</a>
on <a href="{{html_url}}" target="_blank">{{created_at}}</a>
Expand Down