Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Backlog Webhook #58

Merged
merged 6 commits into from Nov 26, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/hooks/backlog/help.html.haml
@@ -0,0 +1,9 @@
%dl
%dt Usage
%dd
%p
You can add webhook URL to your team in backlog.
%br
See <a href='http://www.backlog.jp/help/adminsguide/webhook-setting/userguide2493.html' target='_blank'><i class='fa fa-external-link'></i>Webhook | Backlog</a> for more details.
%p Please append <code>space_id</code> parameter as below.
%pre {{endpoint}}?space_id=<code>hogehoge</code>
51 changes: 51 additions & 0 deletions lib/hooks/backlog/helper.rb
@@ -0,0 +1,51 @@
module Idobata::Hook
class Backlog < Base
module Helper
def type_label
case payload.type
when 1
label = 'created'
when 2
label = 'updated'
when 3
label = 'commented'
when 4
label = 'issue deleted'
when 17
label = 'noticed'
when 14
label = 'multiple issues updated'
end
label
end

def backlog_url_base
"https://#{space_id}.backlog.jp/view/#{payload.project.projectKey}" if space_id
end

def backlog_urls
urls = []
if backlog_url_base
if payload.content.key_id
url = "#{backlog_url_base}-#{payload.content.key_id}"
url += "#comment-#{payload.content.comment.id}" if payload.content.comment && payload.content.comment.id
Copy link
Contributor

Choose a reason for hiding this comment

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

We can use try here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. I'll fix it!

urls << url
else
payload.content.link.each do |link|
urls << "#{backlog_url_base}-#{link.key_id}" if link.key_id
end
end
end
urls
end

def md(source)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this necessary? It seems not used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The parameter called space_id is necessary to make a link to Backlog.
"help.html.haml" says it.

Is it not such a thing?

Copy link
Contributor

Choose a reason for hiding this comment

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

The parameter called space_id is necessary to make a link to Backlog.

Yep, you're right 😃 I think the space_id is necessary, too.

To be honest, I couldn't make out what you were getting at.
I meant to say the md helper is unnecessary since it is not used in this Pull Request. Is it right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I'm sorry. It was misunderstanding.:dizzy_face:
I remove the md helper.

HTML::Pipeline::MarkdownFilter.new(source, gfm: true).call.to_s.html_safe
end

def hbr(source)
html_escape(source).gsub(/\r\n|\r|\n/, "<br />").html_safe
end
end
end
end
23 changes: 23 additions & 0 deletions lib/hooks/backlog/hook.rb
@@ -0,0 +1,23 @@
module Idobata::Hook
class Backlog < Base
screen_name 'backlog'
icon_url hook_image_url('icon.png')
template_name { "#{type}.html.haml" }

helper Helper

private

def type
case payload.type
when 1, 2, 3, 4, 14, 17
'issue'
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you like to return nil explicitly on else clause?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, would you please skip processing when receiving unsupported event? Like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.Thank you!
I'll fix it.

end

def space_id
params[:space_id] || nil
end

end
end
Binary file added lib/hooks/backlog/images/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions lib/hooks/backlog/templates/issue.html.haml
@@ -0,0 +1,13 @@
%p
%b= payload.content.summary
Copy link
Contributor

Choose a reason for hiding this comment

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

How about making this a link to the issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will do so!

#{type_label} by #{payload.createdUser.name}.

- backlog_urls.each do |url|
%p
%a{href: url}= url

- if payload.type == 1
%p=hbr payload.content.description

- if payload.content.comment
%p=hbr payload.content.comment.content
173 changes: 173 additions & 0 deletions spec/backlog_spec.rb
@@ -0,0 +1,173 @@
describe Idobata::Hook::Backlog, type: :hook do
let(:payload) { fixture_payload("backlog/#{payload_type}.json") }

describe '#process_payload' do
subject { hook.process_payload }

context 'on issue create without space_id' do
let(:payload_type) { 'issue_create' }

before do
post payload, {'Content-Type' => 'application/json'}
end

its([:source]) { should eq(<<-HTML.strip_heredoc) }
<p>
<b>test issue</b>
created by ozamasa.
<p>test description</p>
</p>
HTML

its([:format]) { should eq(:html) }
end

context 'on issue create' do
let(:payload_type) { 'issue_create' }

before do
post payload, {'Content-Type' => 'application/json'}, {space_id: 'test'}
end

its([:source]) { should eq(<<-HTML.strip_heredoc) }
<p>
<b>test issue</b>
created by ozamasa.
<p>
<a href='https://test.backlog.jp/view/TEST-100'>https://test.backlog.jp/view/TEST-100</a>
</p>
<p>test description</p>
</p>
HTML

its([:format]) { should eq(:html) }
end

context 'on issue update' do
let(:payload_type) { 'issue_update' }

before do
post payload, {'Content-Type' => 'application/json'}, {space_id: 'test'}
end

its([:source]) { should eq(<<-HTML.strip_heredoc) }
<p>
<b>test issue</b>
updated by ozamasa.
<p>
<a href='https://test.backlog.jp/view/TEST-100#comment-200'>https://test.backlog.jp/view/TEST-100#comment-200</a>
</p>
<p></p>
</p>
HTML

its([:format]) { should eq(:html) }
end

context 'on issue comment' do
let(:payload_type) { 'issue_comment' }

before do
post payload, {'Content-Type' => 'application/json'}, {space_id: 'test'}
end

its([:source]) { should eq(<<-HTML.strip_heredoc) }
<p>
<b>test issue</b>
commented by ozamasa.
<p>
<a href='https://test.backlog.jp/view/TEST-100#comment-200'>https://test.backlog.jp/view/TEST-100#comment-200</a>
</p>
<p>test comment</p>
</p>
HTML

its([:format]) { should eq(:html) }
end

context 'on issue comment' do
let(:payload_type) { 'issue_comment' }

before do
post payload, {'Content-Type' => 'application/json'}, {space_id: 'test'}
end

its([:source]) { should eq(<<-HTML.strip_heredoc) }
<p>
<b>test issue</b>
commented by ozamasa.
<p>
<a href='https://test.backlog.jp/view/TEST-100#comment-200'>https://test.backlog.jp/view/TEST-100#comment-200</a>
</p>
<p>test comment</p>
</p>
HTML

its([:format]) { should eq(:html) }
end

context 'on issue delete' do
let(:payload_type) { 'issue_delete' }

before do
post payload, {'Content-Type' => 'application/json'}, {space_id: 'test'}
end

its([:source]) { should eq(<<-HTML.strip_heredoc) }
<p>
<b></b>
issue deleted by ozamasa.
<p>
<a href='https://test.backlog.jp/view/TEST-100'>https://test.backlog.jp/view/TEST-100</a>
</p>
</p>
HTML

its([:format]) { should eq(:html) }
end

context 'on issue multipul update' do
let(:payload_type) { 'issue_multipul_update' }

before do
post payload, {'Content-Type' => 'application/json'}, {space_id: 'test'}
end

its([:source]) { should eq(<<-HTML.strip_heredoc) }
<p>
<b></b>
multiple issues updated by ozamasa.
<p>
<a href='https://test.backlog.jp/view/TEST-100'>https://test.backlog.jp/view/TEST-100</a>
</p>
<p>
<a href='https://test.backlog.jp/view/TEST-101'>https://test.backlog.jp/view/TEST-101</a>
</p>
</p>
HTML

its([:format]) { should eq(:html) }
end

context 'on issue notice' do
let(:payload_type) { 'issue_notice' }

before do
post payload, {'Content-Type' => 'application/json'}, {space_id: 'test'}
end

its([:source]) { should eq(<<-HTML.strip_heredoc) }
<p>
<b>test issue</b>
noticed by ozamasa.
<p>
<a href='https://test.backlog.jp/view/TEST-100#comment-200'>https://test.backlog.jp/view/TEST-100#comment-200</a>
</p>
<p>test comment</p>
</p>
HTML

its([:format]) { should eq(:html) }
end
end
end
32 changes: 32 additions & 0 deletions spec/fixtures/payload/backlog/issue_comment.json
@@ -0,0 +1,32 @@
{
"created":"2015-11-24T00:00:00Z",
"project":{
"archived":false,
"projectKey":"TEST",
"name":"TestProject",
"chartEnabled":false,
"id":100,
"subtaskingEnabled":false
},
"id":10,
"type":3,
"content":{
"summary":"test issue",
"key_id":100,
"description":"test description",
"comment":{
"id":200,
"content":"test comment"
},
"id":100
},
"notifications":[],
"createdUser":{
"name":"ozamasa",
"mailAddress":null,
"id":6336,
"roleType":1,
"lang":"ja",
"userId":null
}
}
28 changes: 28 additions & 0 deletions spec/fixtures/payload/backlog/issue_create.json
@@ -0,0 +1,28 @@
{
"created":"2015-11-24T00:00:00Z",
"project":{
"archived":false,
"projectKey":"TEST",
"name":"TestProject",
"chartEnabled":false,
"id":100,
"subtaskingEnabled":false
},
"id":10,
"type":1,
"content":{
"summary":"test issue",
"key_id":100,
"description":"test description",
"id":100
},
"notifications":[],
"createdUser":{
"name":"ozamasa",
"mailAddress":null,
"id":6336,
"roleType":1,
"lang":"ja",
"userId":null
}
}
26 changes: 26 additions & 0 deletions spec/fixtures/payload/backlog/issue_delete.json
@@ -0,0 +1,26 @@
{
"created":"2015-11-24T00:00:00Z",
"project":{
"archived":false,
"projectKey":"TEST",
"name":"TestProject",
"chartEnabled":false,
"id":100,
"subtaskingEnabled":false
},
"id":10,
"type":4,
"content":{
"key_id":100,
"id":100
},
"notifications":[],
"createdUser":{
"name":"ozamasa",
"mailAddress":null,
"id":6336,
"roleType":1,
"lang":"ja",
"userId":null
}
}