From 2471c87a12503b37d0723fb6257b213c8eeb08a6 Mon Sep 17 00:00:00 2001 From: Ayomide Bakare Date: Tue, 23 Aug 2022 09:48:47 +0100 Subject: [PATCH] put back code to rank puzzles --- Gemfile.lock | 34 +++++++++++----------- objects/puzzles.rb | 59 ++++++++++++++++++++++++++++++++++++++ objects/tickets/tickets.rb | 3 ++ test/fake_log.rb | 10 +++++++ 4 files changed, 88 insertions(+), 18 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 4f8cc95..5c06769 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,14 +6,14 @@ GEM i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) - addressable (2.8.0) - public_suffix (>= 2.0.2, < 5.0) + addressable (2.8.1) + public_suffix (>= 2.0.2, < 6.0) ast (2.4.2) atlassian-jwt (0.2.1) jwt (~> 2.1) aws-eventstream (1.2.0) - aws-partitions (1.613.0) - aws-sdk-core (3.131.5) + aws-partitions (1.621.0) + aws-sdk-core (3.133.0) aws-eventstream (~> 1, >= 1.0.2) aws-partitions (~> 1, >= 1.525.0) aws-sigv4 (~> 1.1) @@ -38,10 +38,10 @@ GEM rexml differ (0.1.2) docile (1.4.0) - faraday (2.4.0) - faraday-net_http (~> 2.0) + faraday (2.5.2) + faraday-net_http (>= 2.0, < 3.1) ruby2_keywords (>= 0.0.4) - faraday-net_http (2.1.0) + faraday-net_http (3.0.0) ffi (1.15.5) gitlab (4.19.0) httparty (~> 0.20) @@ -70,23 +70,23 @@ GEM mime-types-data (~> 3.2015) mime-types-data (3.2022.0105) mini_mime (1.1.2) - minitest (5.16.2) + mini_portile2 (2.8.0) + minitest (5.16.3) mocha (1.14.0) multi_json (1.15.0) multi_xml (0.6.0) multipart-post (2.2.3) mustermann (2.0.2) ruby2_keywords (~> 0.0.1) - nokogiri (1.13.8-x86_64-darwin) - racc (~> 1.4) - nokogiri (1.13.8-x86_64-linux) + nokogiri (1.13.8) + mini_portile2 (~> 2.8.0) racc (~> 1.4) oauth (0.5.10) octokit (5.1.0) faraday (>= 1, < 3) sawyer (~> 0.9) parallel (1.22.1) - parser (3.1.2.0) + parser (3.1.2.1) ast (~> 2.4.1) pdd (0.21.3) nokogiri (~> 1.10) @@ -94,7 +94,7 @@ GEM ruby-filemagic (~> 0.7.2) slop (~> 4.6) power_assert (2.0.1) - public_suffix (4.0.7) + public_suffix (5.0.0) racc (1.6.0) rack (2.2.4) rack-protection (2.2.2) @@ -118,7 +118,7 @@ GEM rubocop-ast (>= 1.19.1, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 1.4.0, < 3.0) - rubocop-ast (1.19.1) + rubocop-ast (1.21.0) parser (>= 3.1.1.0) rubocop-rspec (2.12.1) rubocop (~> 1.31) @@ -173,9 +173,7 @@ GEM slop (~> 4.4) PLATFORMS - x86_64-darwin-19 - x86_64-darwin-21 - x86_64-linux + ruby DEPENDENCIES atlassian-jwt (~> 0.2.1) @@ -207,4 +205,4 @@ DEPENDENCIES xcop (= 0.7.1) BUNDLED WITH - 2.3.17 + 2.1.2 diff --git a/objects/puzzles.rb b/objects/puzzles.rb index f919630..e4de9ae 100644 --- a/objects/puzzles.rb +++ b/objects/puzzles.rb @@ -78,6 +78,16 @@ def group(xml) # Take some puzzles from the XML and either close their tickets in GitHub # or create new tickets. def expose(xml, tickets) + close(xml, tickets) + skip_model = xml.xpath('/puzzles[@model="true"]').empty? + if skip_model + submit(xml, tickets) + else + submit_ranked(xml, tickets) + end + end + + def close(xml, tickets) seen = [] Kernel.loop do puzzles = xml.xpath( @@ -93,6 +103,9 @@ def expose(xml, tickets) puzzle.search('issue')[0]['closed'] = Time.now.iso8601 if tickets.close(puzzle) save(xml) end + end + + def submit(xml, tickets) seen = [] Kernel.loop do puzzles = xml.xpath( @@ -115,4 +128,50 @@ def expose(xml, tickets) save(xml) end end + + # Reads the list of all puzzles from the XML in the storage and then + # sorts them in the right order, in which they should be present in the + # backlog. + def rank(puzzles) + LinearModel.new(@repo.name, @storage).predict( + puzzles.map { |puzzle| JSON.parse(Crack::XML.parse(puzzle.to_s).to_json)['puzzle'] } + ) + end + + def submit_ranked(xml, tickets) + seen = [] + unique_puzzles = [] + Kernel.loop do + puzzles = xml.xpath( + [ + '//puzzle[@alive="true" and (not(issue) or issue="unknown")', + seen.map { |i| "and id != '#{i}'" }.join(' '), + ']' + ].join(' ') + ) + break if puzzles.empty? + puzzle = puzzles[0] + id = puzzle.xpath('id')[0].text + unique_puzzles.append(puzzle.dup) + seen << id + end + submitted = 0 + ranked_idx = rank(unique_puzzles) + Kernel.loop do + puzzles = xml.xpath( + '//puzzle[@alive="true" and (not(issue) or issue="unknown")]' + ) + break if puzzles.empty? || ranked_idx.empty? || submitted >= @threshold + next_idx = ranked_idx.shift + puzzle = puzzles.find { |p| p.xpath('id')[0].text == unique_puzzles[next_idx].xpath('id')[0].text } + issue = tickets.submit(puzzle) + next if issue.nil? + puzzle.search('issue').remove + puzzle.add_child( + "#{issue[:number]}" + ) + save(xml) + submitted += 1 + end + end end diff --git a/objects/tickets/tickets.rb b/objects/tickets/tickets.rb index 5d59f2c..e23181d 100644 --- a/objects/tickets/tickets.rb +++ b/objects/tickets/tickets.rb @@ -19,6 +19,9 @@ # SOFTWARE. require 'haml' +require 'gitlab' +require 'octokit' +require 'jira-ruby' require_relative '../truncated' require_relative '../maybe_text' diff --git a/test/fake_log.rb b/test/fake_log.rb index 5d19c98..78f0af8 100644 --- a/test/fake_log.rb +++ b/test/fake_log.rb @@ -29,4 +29,14 @@ def put(tag, text) @title = text @tag = tag end + + def get(tag) + end + + def delete(time, tag) + end + + def list(since = Time.now.to_i) + [] + end end