-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(puzzles): Better handle GenerateSlackThread job errors and incomp…
…lete puzzle data (#719) * feat(errors): create SlackError class * fix(puzzles): improve GenerateSlackThread * feat(tasks): create rake task to update puzzle thread * fix(puzzles): better handle missing puzzle data * fix(puzzles): wrap rake instruction in code block * fix: rubocop * Fix typo --------- Co-authored-by: Jérémie Bonal <jeremie.bonal@gmail.com>
- Loading branch information
Showing
5 changed files
with
67 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
class SlackError < StandardError; end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
desc "update_puzzle_thread" | ||
task :update_puzzle_thread, %i[day channel] => :environment do |_, args| | ||
next p "day param missing" if args[:day].nil? | ||
next p "channel missing or incorrect" unless args[:channel].in? %w[aoc aoc-dev] | ||
|
||
puzzle = Puzzle.by_date(Aoc.begin_time.change(day: args[:day])) | ||
next p "Puzzle not found" if puzzle.nil? | ||
|
||
html = URI.parse(@puzzle.url).open | ||
doc = Nokogiri::HTML(html) | ||
titles = doc.css("h2").map(&:text) | ||
raw_title = titles.find { |title| title.match?(/--- Day \d+:/) } | ||
next p "Title not found" if raw_title.nil? | ||
|
||
client = Slack::Web::Client.new | ||
text = "`SPOILER` <#{@puzzle.url}|#{raw_title.gsub('---', '').strip}>" | ||
response = client.chat_update(channel: args[:channel], ts: puzzle.thread_ts, text:) | ||
next p "Failed to update message" unless response["ok"] | ||
|
||
puzzle.update!(title: text) | ||
end |