From 361ad82ba9d07e7b06e3166faca9f3de148af30b Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Wed, 15 Feb 2017 20:16:22 +0200 Subject: [PATCH] #41 submit and close --- objects/github_tickets.rb | 11 +++++++ objects/puzzles.rb | 32 +++++++++++++++---- .../puzzles/ignores-unknown-issues.xml | 4 ++- test-assets/puzzles/simple.xml | 1 + test/test__helper.rb | 4 +++ 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/objects/github_tickets.rb b/objects/github_tickets.rb index 476dbdea..2741ba40 100644 --- a/objects/github_tickets.rb +++ b/objects/github_tickets.rb @@ -24,6 +24,7 @@ # # Tickets in Github +# API: http://octokit.github.io/octokit.rb/method_list.html # class GithubTickets def initialize(repo, login, pwd, sources) @@ -33,6 +34,11 @@ def initialize(repo, login, pwd, sources) @sources = sources end + # Is it safe to do something right now or it's better to wait a bit? + def safe + client.rate_limit.remaining > 2000 + end + def submit(puzzle) # @todo #3:20min This mechanism of body abbreviation is rather # primitive and doesn't produce readable texts very often. Instead @@ -72,6 +78,7 @@ def submit(puzzle) def close(puzzle) issue = puzzle.xpath('issue').text + return if client.issue(@repo, issue)['state'] == 'closed' client.close_issue(@repo, issue) client.add_comment( @repo, @@ -112,6 +119,10 @@ def truncate(text, limit = 40, separator = '...') def client if ENV['RACK_ENV'] == 'test' client = Object.new + def client.issue(_, _) + { 'state' => 'open' } + end + def client.close_issue(_, _) # nothing to do here end diff --git a/objects/puzzles.rb b/objects/puzzles.rb index 8245760e..88a75a6e 100644 --- a/objects/puzzles.rb +++ b/objects/puzzles.rb @@ -37,14 +37,17 @@ def deploy(tickets) # be much better to check whether any modifications have been made # and skip that SAVE() operation. @storage.save( - group( - close( - submit( - join(@storage.load, @repo.xml), + update_all( + group( + close( + submit( + join(@storage.load, @repo.xml), + tickets + ), tickets - ), - tickets - ) + ) + ), + tickets ) ) end @@ -88,4 +91,19 @@ def close(xml, tickets) .each { |p| tickets.close(p) } xml end + + def update_all(xml, tickets) + if tickets.safe + xml.xpath('//puzzle[@alive="false" and issue and issue!="unknown"]') + .each { |p| tickets.close(p) } + xml.xpath('//puzzle[@alive="true" and not(issue)]') + .map { |p| { issue: tickets.submit(p), id: p.xpath('id').text } } + .each do |p| + xml.xpath("//puzzle[id='#{p[:id]}']")[0].add_child( + "#{p[:issue][:number]}" + ) + end + end + xml + end end diff --git a/test-assets/puzzles/ignores-unknown-issues.xml b/test-assets/puzzles/ignores-unknown-issues.xml index 6abf59c8..77b22713 100644 --- a/test-assets/puzzles/ignores-unknown-issues.xml +++ b/test-assets/puzzles/ignores-unknown-issues.xml @@ -23,5 +23,7 @@ /puzzles[@date='2016-12-10T16:26:36Z'] - + + + diff --git a/test-assets/puzzles/simple.xml b/test-assets/puzzles/simple.xml index ff33e08a..020f7087 100644 --- a/test-assets/puzzles/simple.xml +++ b/test-assets/puzzles/simple.xml @@ -58,5 +58,6 @@ 23-ae347a22 + 23-ffc97ad1 diff --git a/test/test__helper.rb b/test/test__helper.rb index 1f2d57e1..8bc6f633 100644 --- a/test/test__helper.rb +++ b/test/test__helper.rb @@ -51,6 +51,10 @@ def initialize @closed = [] end + def safe + true + end + def submit(puzzle) @submitted << puzzle.xpath('id').text { number: '123', href: 'http://0pdd.com' }