Skip to content

Commit

Permalink
#331: Status changed rule for task alertif
Browse files Browse the repository at this point in the history
Fixed #331
  • Loading branch information
dgroup committed Feb 12, 2021
1 parent 63ae9f9 commit 5bee0e5
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/lazylead/task/alert/alertif.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ module Task
# - evaluate set of rules for each ticket
# - send an email
class AlertIf
# @todo #319/DEV Email template for alerts are not defined yet
def run(sys, postman, opts)
Requires.new(__dir__).load
opts[:rules] = opts.construct("rules")
sys.issues(opts["jql"], opts.jira_defaults)
sys.issues(opts["jql"], opts.jira_defaults.merge(expand: "changelog"))
.map { |i| When.new(i, opts) }
.select(&:matches)
.group_by(&:assignee)
Expand All @@ -58,9 +59,9 @@ def initialize(issue, opts)
@opts = opts
end

# @todo #319/DEV Verify that ticket matches expected conditions and set alert if matched.
def matches
!@issue.nil?
return false if @opts[:rules].nil?
@opts[:rules].all? { |r| r.passed(@issue, @opts) }
end

def assignee
Expand Down
58 changes: 58 additions & 0 deletions lib/lazylead/task/alert/changed_to.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

# The MIT License
#
# Copyright (c) 2019-2020 Yurii Dubinka
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom
# the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.

require_relative "../../opts"

module Lazylead
#
# Gives true when ticket status changed to expected status(es).
#
class ChangedTo
def initialize(field, expected)
@field = field
@expected = expected
end

def passed(issue, opts)
return false if issue.nil? || opts.nil? || opts[@expected].nil? || @field.nil?
last = last_change(issue)
opts.slice(@expected, ",").any? { |s| s.eql? last }
end

# Detect the last change of expected field in issue history
def last_change(issue)
issue.history
.reverse
.find { |h| h["items"].any? { |i| i["field"] == @field } }["items"]
.find { |h| h["field"] == @field }["toString"]
end
end

# Check that last status change has expected value.
class ToStatus < Lazylead::ChangedTo
def initialize
super "status", "to_status"
end
end
end
49 changes: 49 additions & 0 deletions test/lazylead/task/alert/alert_if_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

# The MIT License
#
# Copyright (c) 2019-2020 Yurii Dubinka
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom
# the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.

require_relative "../../../test"
require_relative "../../../../lib/lazylead/log"
require_relative "../../../../lib/lazylead/opts"
require_relative "../../../../lib/lazylead/smtp"
require_relative "../../../../lib/lazylead/postman"
require_relative "../../../../lib/lazylead/system/jira"
require_relative "../../../../lib/lazylead/task/alert/alertif"
require_relative "../../../../lib/lazylead/task/alert/changed_to"

module Lazylead
class AlertIfTest < Lazylead::Test
# @todo #319/DEV Implementation of unit testing required considering a new email template.
test "last change to Done" do
skip "Not implemented yet"
Smtp.new.enable
Lazylead::AlertIf.new.run(
NoAuthJira.new("https://jira.spring.io"),
Postman.new,
Opts.new(
"to_status" => "Done"
)
)
end
end
end
42 changes: 42 additions & 0 deletions test/lazylead/task/alert/changed_to_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

# The MIT License
#
# Copyright (c) 2019-2020 Yurii Dubinka
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom
# the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.

require_relative "../../../test"
require_relative "../../../../lib/lazylead/log"
require_relative "../../../../lib/lazylead/opts"
require_relative "../../../../lib/lazylead/system/jira"
require_relative "../../../../lib/lazylead/task/alert/changed_to"

module Lazylead
class ToStatusTest < Lazylead::Test
test "last change to Done" do
assert Lazylead::ToStatus.new.passed(
NoAuthJira.new("https://jira.spring.io")
.issues("key=XD-3064", Opts.new.jira_defaults.merge(expand: "changelog"))
.first,
Opts.new("to_status" => "Done")
)
end
end
end

0 comments on commit 5bee0e5

Please sign in to comment.