From bbcc14a9f7ca9a56caffcfc4d0d664c95b1b117f Mon Sep 17 00:00:00 2001 From: lidlanca Date: Wed, 24 Sep 2014 01:50:42 -0400 Subject: [PATCH 1/3] Template to support new pull statuses --- templates/githubpullrequest.mustache | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/templates/githubpullrequest.mustache b/templates/githubpullrequest.mustache index 9b406cf5..de23120b 100644 --- a/templates/githubpullrequest.mustache +++ b/templates/githubpullrequest.mustache @@ -8,6 +8,27 @@ {{title}} + + +
+{{#pull_status_str_open}} + +{{/pull_status_str_open}} +{{#pull_status_closed_accepted}} + +{{/pull_status_closed_accepted}} +{{#pull_status_closed_declined}} + +{{/pull_status_closed_declined}} + +{{pull_status_str}} +
+ +{{#build_status}} +{{build_status}} +{{/build_status}} +
+
by {{user.login}} on {{created_at}} From 3dc1d50c68b4ab4d23c3e18214b90a7e92b99f67 Mon Sep 17 00:00:00 2001 From: lidlanca Date: Wed, 24 Sep 2014 01:54:40 -0400 Subject: [PATCH 2/3] implement api authentications and get build status oauth is the recommend authentication method. when generating token you can control privileges, and you do not expose your password authentication method and crediential is provided in the engine options. see comments of the method api_json_request() --- .../engine/github_pullrequest_onebox.rb | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/lib/onebox/engine/github_pullrequest_onebox.rb b/lib/onebox/engine/github_pullrequest_onebox.rb index b12fdd6e..d0bd3b58 100644 --- a/lib/onebox/engine/github_pullrequest_onebox.rb +++ b/lib/onebox/engine/github_pullrequest_onebox.rb @@ -13,12 +13,57 @@ 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/(?[^/]+)/(?[^/]+)/pull/(?[^/]+)}) 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 @@ -26,3 +71,5 @@ def data end end end + + From b11be88f0a6205933ea1d8e359c89dde359ddd43 Mon Sep 17 00:00:00 2001 From: lidlanca Date: Wed, 24 Sep 2014 01:59:13 -0400 Subject: [PATCH 3/3] Update engine.rb --- lib/onebox/engine.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/onebox/engine.rb b/lib/onebox/engine.rb index 5025a397..7335341a 100644 --- a/lib/onebox/engine.rb +++ b/lib/onebox/engine.rb @@ -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 @@ -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