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

update-changelog 2.0 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env ruby
  2. require 'octokit'
  3. class PullRequest
  4. attr_reader :gh, :num
  5. def initialize(gh, num)
  6. @gh = gh
  7. @num = num
  8. end
  9. def entry
  10. pr = gh.pull_request('cucumber/cucumber-ruby', num)
  11. username = pr['user']['login']
  12. url = pr['html_url']
  13. title = pr['title']
  14. "#{title} ([##{num}](#{url}) [@#{username}](https://github.com/#{username}))"
  15. end
  16. def labels
  17. gh.labels_for_issue('cucumber/cucumber-ruby', num).map { |label| label['name'] }
  18. end
  19. def type_label
  20. types = labels.select { |label| label =~ /^type: / }
  21. raise('This pull request does not have a \'type\' label on it. Please add one.') if types.empty?
  22. raise('This pull request has more than one \'type\' label on it. Please choose just one.') if types.length > 1
  23. types.first
  24. end
  25. end
  26. class Changelog
  27. def self.open(path, &block)
  28. full_path = File.expand_path("#{File.dirname(__FILE__)}/#{path}/CHANGELOG.md")
  29. log = new(File.read(full_path))
  30. log.instance_exec(&block)
  31. File.write(full_path, log.body)
  32. end
  33. attr_reader :lines
  34. def initialize(body)
  35. @lines = body.lines
  36. end
  37. def insert(label, entry)
  38. section_index = lines.index { |line| line.strip == heading_for(label) }
  39. raise("Unable to find matching heading in CHANGELOG.md for '#{label}' (#{heading_for(label)})") unless section_index
  40. lines.insert(section_index + 2, "* #{entry}\n")
  41. end
  42. def heading_for(label)
  43. text = {
  44. 'type: new feature' => 'Added',
  45. 'type: bug' => 'Fixed',
  46. 'type: refactoring / developer experience' => 'Changed'
  47. }.fetch(label)
  48. "### #{text}"
  49. end
  50. def body
  51. lines.join
  52. end
  53. end
  54. begin
  55. token = ENV['GITHUB_TOKEN'] || raise('You need to set GITHUB_TOKEN')
  56. num = (ARGV[0] || raise("syntax: #{$PROGRAM_NAME} <PULL-REQUEST-NUMBER>")).to_i
  57. gh = Octokit::Client.new(access_token: token)
  58. pr = PullRequest.new(gh, num)
  59. Changelog.open('..') do
  60. insert(pr.type_label, pr.entry)
  61. end
  62. rescue StandardError => e
  63. abort e.message
  64. end

No Description

Contributors (1)