diff --git a/lib/oneboxer/github_pullrequest_onebox.rb b/lib/oneboxer/github_pullrequest_onebox.rb new file mode 100644 index 00000000000..e3b374be07f --- /dev/null +++ b/lib/oneboxer/github_pullrequest_onebox.rb @@ -0,0 +1,26 @@ +require_dependency 'oneboxer/handlebars_onebox' + +module Oneboxer + class GithubPullrequestOnebox < HandlebarsOnebox + + matcher /^https?:\/\/(?:www\.)?github\.com\/[^\/]+\/[^\/]+\/pull\/.+/ + favicon 'github.png' + + def translate_url + @url.match( + /github\.com\/(?[^\/]+)\/(?[^\/]+)\/pull\/(?[^\/]+)/mi + ) do |match| + "https://api.github.com/repos/#{match[:owner]}/#{match[:repo]}/pulls/#{match[:number]}" + end + end + + def parse(data) + result = JSON.parse(data) + + result['created_at'] = + Time.parse(result['created_at']).strftime("%I:%M%p - %d %b %y") + + result + end + end +end diff --git a/lib/oneboxer/templates/github_pullrequest_onebox.hbrs b/lib/oneboxer/templates/github_pullrequest_onebox.hbrs new file mode 100644 index 00000000000..cd4be7d6c8f --- /dev/null +++ b/lib/oneboxer/templates/github_pullrequest_onebox.hbrs @@ -0,0 +1,39 @@ +
+ {{#host}} + + {{/host}} + +
+ {{#user.avatar_url}} + + {{user.login}} + + {{/user.avatar_url}} + +

+ {{title}} +

+ + + +
+ {{commits}} commits + changed {{changed_files}} files + with {{additions}} additions + and {{deletions}} deletions. +
+
+
+
diff --git a/spec/components/oneboxer/github_pullrequest_onebox_spec.rb b/spec/components/oneboxer/github_pullrequest_onebox_spec.rb new file mode 100644 index 00000000000..378b8531609 --- /dev/null +++ b/spec/components/oneboxer/github_pullrequest_onebox_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' +require 'oneboxer' +require 'oneboxer/github_pullrequest_onebox' + +describe Oneboxer::GithubPullrequestOnebox do + describe '#translate_url' do + it 'returns the api url for the given pull request' do + onebox = described_class.new( + 'https://github.com/discourse/discourse/pull/988' + ) + expect(onebox.translate_url).to eq( + 'https://api.github.com/repos/discourse/discourse/pulls/988' + ) + end + end +end +