repo.rake 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # frozen_string_literal: true
  2. REPOSITORY_NAME = 'mastodon/mastodon'
  3. namespace :repo do
  4. desc 'Generate the AUTHORS.md file'
  5. task :authors do
  6. file = Rails.root.join('AUTHORS.md').open('w')
  7. file << <<~HEADER
  8. Authors
  9. =======
  10. Mastodon is available on [GitHub](https://github.com/#{REPOSITORY_NAME})
  11. and provided thanks to the work of the following contributors:
  12. HEADER
  13. url = "https://api.github.com/repos/#{REPOSITORY_NAME}/contributors?anon=1"
  14. HttpLog.config.compact_log = true
  15. while url.present?
  16. response = HTTP.get(url)
  17. contributors = Oj.load(response.body)
  18. contributors.each do |c|
  19. file << "* [#{c['login']}](#{c['html_url']})\n" if c['login']
  20. file << "* [#{c['name']}](mailto:#{c['email']})\n" if c['name']
  21. end
  22. url = LinkHeader.parse(response.headers['Link']).find_link(%w(rel next))&.href
  23. end
  24. file << <<~FOOTER
  25. This document is provided for informational purposes only. Since it is only updated once per release, the version you are looking at may be currently out of date. To see the full list of contributors, consider looking at the [git history](https://github.com/mastodon/mastodon/graphs/contributors) instead.
  26. FOOTER
  27. end
  28. desc 'Replace pull requests with authors in the CHANGELOG.md file'
  29. task :changelog do
  30. path = Rails.root.join('CHANGELOG.md')
  31. tmp = Tempfile.new
  32. HttpLog.config.compact_log = true
  33. begin
  34. File.open(path, 'r') do |file|
  35. file.each_line do |line|
  36. if line.start_with?('-')
  37. new_line = line.gsub(/#([[:digit:]]+)*/) do |pull_request_reference|
  38. pull_request_number = pull_request_reference[1..]
  39. response = nil
  40. loop do
  41. response = HTTP.headers('Authorization' => "token #{ENV['GITHUB_API_TOKEN']}").get("https://api.github.com/repos/#{REPOSITORY_NAME}/pulls/#{pull_request_number}")
  42. if response.code == 403
  43. sleep_for = (response.headers['X-RateLimit-Reset'].to_i - Time.now.to_i).abs
  44. puts "Sleeping for #{sleep_for} seconds to get over rate limit"
  45. sleep sleep_for
  46. else
  47. break
  48. end
  49. end
  50. pull_request = Oj.load(response.to_s)
  51. "[#{pull_request['user']['login']}](#{pull_request['html_url']})"
  52. end
  53. tmp.puts new_line
  54. else
  55. tmp.puts line
  56. end
  57. end
  58. end
  59. tmp.close
  60. FileUtils.mv(tmp.path, path)
  61. ensure
  62. tmp.close
  63. tmp.unlink
  64. end
  65. end
  66. task check_locales_files: :environment do
  67. pastel = Pastel.new
  68. missing_yaml_files = I18n.available_locales.reject { |locale| Rails.root.join('config', 'locales', "#{locale}.yml").exist? }
  69. missing_json_files = I18n.available_locales.reject { |locale| Rails.root.join('app', 'javascript', 'mastodon', 'locales', "#{locale}.json").exist? }
  70. locales_in_files = Dir[Rails.root.join('config', 'locales', '*.yml')].map do |path|
  71. file_name = File.basename(path, '.yml')
  72. file_name.gsub(/\A(doorkeeper|devise|activerecord|simple_form)\./, '').to_sym
  73. end.uniq.compact
  74. missing_available_locales = locales_in_files - I18n.available_locales
  75. supported_locale_codes = Set.new(LanguagesHelper::SUPPORTED_LOCALES.keys + LanguagesHelper::REGIONAL_LOCALE_NAMES.keys)
  76. missing_locale_names = I18n.available_locales.reject { |locale| supported_locale_codes.include?(locale) }
  77. critical = false
  78. unless missing_json_files.empty?
  79. critical = true
  80. puts pastel.red("You are missing JSON files for these locales: #{pastel.bold(missing_json_files.join(', '))}")
  81. puts pastel.red('This will lead to runtime errors for users who have selected those locales')
  82. puts pastel.red("Add the missing files or remove the locales from #{pastel.bold('I18n.available_locales')} in config/application.rb")
  83. end
  84. unless missing_yaml_files.empty?
  85. critical = true
  86. puts pastel.red("You are missing YAML files for these locales: #{pastel.bold(missing_yaml_files.join(', '))}")
  87. puts pastel.red('This will lead to runtime errors for users who have selected those locales')
  88. puts pastel.red("Add the missing files or remove the locales from #{pastel.bold('I18n.available_locales')} in config/application.rb")
  89. end
  90. unless missing_available_locales.empty?
  91. puts pastel.yellow("You have locale files that are not enabled: #{pastel.bold(missing_available_locales.join(', '))}")
  92. puts pastel.yellow("Add them to #{pastel.bold('I18n.available_locales')} in config/application.rb or remove them")
  93. end
  94. unless missing_locale_names.empty?
  95. puts pastel.yellow("You are missing human-readable names for these locales: #{pastel.bold(missing_locale_names.join(', '))}")
  96. puts pastel.yellow("Add them to app/helpers/languages_helper.rb or remove the locales from #{pastel.bold('I18n.available_locales')} in config/application.rb")
  97. end
  98. if critical
  99. exit(1)
  100. else
  101. puts pastel.green('OK')
  102. end
  103. end
  104. end