Skip to content

Instantly share code, notes, and snippets.

@westonkd
Last active November 7, 2019 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonkd/f8edb8af2b42b5f6c7afc5473b5023c0 to your computer and use it in GitHub Desktop.
Save westonkd/f8edb8af2b42b5f6c7afc5473b5023c0 to your computer and use it in GitHub Desktop.
Update vague tool domains to a more specific domain (if it can be inferred)
class VagueDomainFixer
VAGUE_DOMAIN = 'instructure.com'.freeze
class << self
def run(plan: true)
# How many shards are there?
shard_count = Shard.count
# Which one are we on now?
shard_progress = -1
# Iterrate over every shard and update tools
Shard.with_each_shard do
update_progress(shard_progress += 1, shard_count)
fix_tools(ContextExternalTool.active.where(domain: VAGUE_DOMAIN), plan)
end.flatten
end
def fix_tools(tools, plan)
changes = []
tools.each do |t|
next if t.settings.blank?
specific_domain = specific_domain_for(t)
# Don't update to a blank domain
next if specific_domain.blank?
# Don't accidently set a specific domain that did not
# include the original, vague domain.
next unless specific_domain.include? t.domain
old_domain = t.domain
# update the tool (unless we are just doing a plan)
!plan && t.update!(domain: specific_domain)
changes << {
id: t.global_id,
old_domain: old_domain,
new_domain: specific_domain
}
end
changes
end
def specific_domain_for(tool)
# Pull the domain out of the placement-
# specific URLs. Return nil if there
# are no placement settings or the
# first placement setting does not
# contain a URL
# If the first placement does not
# have a URL, the tool is SOL
placement = tool.settings.keys&.first
return unless tool.settings[placement].is_a?(Hash)
url = tool.settings.dig(placement, 'url')
return if url.blank?
url = ContextExternalTool.standardize_url(url)
Addressable::URI.parse(url).normalize.host
end
def update_progress(shard_progress, shard_count)
puts "*Progress: #{(shard_progress.to_f / shard_count) * 100}%*" if (shard_progress % 50).zero?
end
end
end
tool_report = VagueDomainFixer.run(plan: true); ""
@derigible
Copy link

Why only check the first placement?

@westonkd
Copy link
Author

westonkd commented Nov 7, 2019

Thanks Marc! The tools we care about updating all of a url in the firs placement.

@westonkd
Copy link
Author

westonkd commented Nov 7, 2019

I'll add a comment for clarity (incase anyone else looks at this)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment