You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.5 KiB

  1. # frozen_string_literal: true
  2. namespace :repo do
  3. desc 'Generate the AUTHORS.md file'
  4. task :authors do
  5. file = File.open(Rails.root.join('AUTHORS.md'), 'w')
  6. file << <<~HEADER
  7. Mastodon is available on [GitHub](https://github.com/tootsuite/mastodon)
  8. and provided thanks to the work of the following contributors:
  9. HEADER
  10. url = 'https://api.github.com/repos/tootsuite/mastodon/contributors?anon=1'
  11. HttpLog.config.compact_log = true
  12. while url.present?
  13. response = HTTP.get(url)
  14. contributors = Oj.load(response.body)
  15. contributors.each do |c|
  16. file << "* [#{c['login']}](#{c['html_url']})\n" if c['login']
  17. file << "* [#{c['name']}](mailto:#{c['email']})\n" if c['name']
  18. end
  19. url = LinkHeader.parse(response.headers['Link']).find_link(%w(rel next))&.href
  20. end
  21. file << <<~FOOTER
  22. 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/tootsuite/mastodon/graphs/contributors) instead.
  23. FOOTER
  24. end
  25. desc 'Replace pull requests with authors in the CHANGELOG.md file'
  26. task :changelog do
  27. path = Rails.root.join('CHANGELOG.md')
  28. tmp = Tempfile.new
  29. HttpLog.config.compact_log = true
  30. begin
  31. File.open(path, 'r') do |file|
  32. file.each_line do |line|
  33. if line.start_with?('-')
  34. new_line = line.gsub(/#([[:digit:]]+)*/) do |pull_request_reference|
  35. pull_request_number = pull_request_reference[1..-1]
  36. response = nil
  37. loop do
  38. response = HTTP.headers('Authorization' => "token #{ENV['GITHUB_API_TOKEN']}").get("https://api.github.com/repos/tootsuite/mastodon/pulls/#{pull_request_number}")
  39. if response.code == 403
  40. sleep_for = (response.headers['X-RateLimit-Reset'].to_i - Time.now.to_i).abs
  41. puts "Sleeping for #{sleep_for} seconds to get over rate limit"
  42. sleep sleep_for
  43. else
  44. break
  45. end
  46. end
  47. pull_request = Oj.load(response.to_s)
  48. "[#{pull_request['user']['login']}](#{pull_request['html_url']})"
  49. end
  50. tmp.puts new_line
  51. else
  52. tmp.puts line
  53. end
  54. end
  55. end
  56. tmp.close
  57. FileUtils.mv(tmp.path, path)
  58. ensure
  59. tmp.close
  60. tmp.unlink
  61. end
  62. end
  63. end