repo.rake 2.6 KB

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