Skip to content

Commit

Permalink
#41 submit and close
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Feb 15, 2017
1 parent 10018b7 commit 361ad82
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
11 changes: 11 additions & 0 deletions objects/github_tickets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
32 changes: 25 additions & 7 deletions objects/puzzles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
"<issue href='#{p[:issue][:href]}'>#{p[:issue][:number]}</issue>"
)
end
end
xml
end
end
4 changes: 3 additions & 1 deletion test-assets/puzzles/ignores-unknown-issues.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@
<xpath>/puzzles[@date='2016-12-10T16:26:36Z']</xpath>
</assertions>
<submit/>
<close/>
<close>
<ticket></ticket>
</close>
</test>
1 change: 1 addition & 0 deletions test-assets/puzzles/simple.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
</submit>
<close>
<ticket>23-ae347a22</ticket>
<ticket>23-ffc97ad1</ticket>
</close>
</test>
4 changes: 4 additions & 0 deletions test/test__helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down

0 comments on commit 361ad82

Please sign in to comment.