1
0

repo.rake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 if defined?(HttpLog)
  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 if defined?(HttpLog)
  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:]]+)(, #([[:digit:]]+))*\)\Z/) do |pull_requests_string|
  38. pull_requests = pull_requests_string[1...-1].split(',').map { |pr_id| pr_id.strip[1...] }
  39. response = nil
  40. authors = pull_requests.map do |pull_request_number|
  41. response = nil
  42. loop do
  43. response = HTTP.headers('Authorization' => "token #{ENV['GITHUB_API_TOKEN']}").get("https://api.github.com/repos/#{REPOSITORY_NAME}/pulls/#{pull_request_number}")
  44. if response.code == 403
  45. sleep_for = (response.headers['X-RateLimit-Reset'].to_i - Time.now.to_i).abs
  46. puts "Sleeping for #{sleep_for} seconds to get over rate limit"
  47. sleep sleep_for
  48. else
  49. break
  50. end
  51. end
  52. pull_request = Oj.load(response.to_s)
  53. pull_request['user']['login']
  54. end
  55. authors.sort!.uniq!
  56. "(#{pull_requests.map { |pr| "##{pr}" }.to_sentence} by #{authors.map { |author| "@#{author}" }.to_sentence})"
  57. end
  58. tmp.puts new_line
  59. else
  60. tmp.puts line
  61. end
  62. end
  63. end
  64. tmp.close
  65. FileUtils.mv(tmp.path, path)
  66. ensure
  67. tmp.close
  68. tmp.unlink
  69. end
  70. end
  71. task check_locales_files: :environment do
  72. pastel = Pastel.new
  73. missing_yaml_files = I18n.available_locales.reject { |locale| Rails.root.join('config', 'locales', "#{locale}.yml").exist? }
  74. missing_json_files = I18n.available_locales.reject { |locale| Rails.root.join('app', 'javascript', 'mastodon', 'locales', "#{locale}.json").exist? }
  75. locales_in_files = Rails.root.glob('config/locales/*.yml').map do |path|
  76. file_name = File.basename(path, '.yml')
  77. file_name.gsub(/\A(doorkeeper|devise|activerecord|simple_form)\./, '').to_sym
  78. end.uniq.compact
  79. missing_available_locales = locales_in_files - I18n.available_locales
  80. supported_locale_codes = Set.new(LanguagesHelper::SUPPORTED_LOCALES.keys + LanguagesHelper::REGIONAL_LOCALE_NAMES.keys)
  81. missing_locale_names = I18n.available_locales.reject { |locale| supported_locale_codes.include?(locale) }
  82. critical = false
  83. unless missing_json_files.empty?
  84. critical = true
  85. puts pastel.red("You are missing JSON files for these locales: #{pastel.bold(missing_json_files.join(', '))}")
  86. puts pastel.red('This will lead to runtime errors for users who have selected those locales')
  87. puts pastel.red("Add the missing files or remove the locales from #{pastel.bold('I18n.available_locales')} in config/application.rb")
  88. end
  89. unless missing_yaml_files.empty?
  90. critical = true
  91. puts pastel.red("You are missing YAML files for these locales: #{pastel.bold(missing_yaml_files.join(', '))}")
  92. puts pastel.red('This will lead to runtime errors for users who have selected those locales')
  93. puts pastel.red("Add the missing files or remove the locales from #{pastel.bold('I18n.available_locales')} in config/application.rb")
  94. end
  95. unless missing_available_locales.empty?
  96. puts pastel.yellow("You have locale files that are not enabled: #{pastel.bold(missing_available_locales.join(', '))}")
  97. puts pastel.yellow("Add them to #{pastel.bold('I18n.available_locales')} in config/application.rb or remove them")
  98. end
  99. unless missing_locale_names.empty?
  100. puts pastel.yellow("You are missing human-readable names for these locales: #{pastel.bold(missing_locale_names.join(', '))}")
  101. 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")
  102. end
  103. if critical
  104. exit(1)
  105. else
  106. puts pastel.green('OK')
  107. end
  108. end
  109. end