github_changes 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 91e338d415d8597cc4d26affc9f90edee506b5a8
4
+ data.tar.gz: f161536c7da8b87c25d208ed3f313b7bf6100f35
5
+ SHA512:
6
+ metadata.gz: 12ad2188c031f14f1b028d12d02702d0adee8c714d4646240d9bc539ef7eeb89394adff81bfbf7f350d43d8515b5713a61a6e504c4640168501f8ae4f46176e2
7
+ data.tar.gz: 218d4597fde0c6c4d62ac0c351ff69f188593f9985cce98dc732ddabd9fd96ae5bfc4744fff13fdecd32ee902d60fb6755811cd0221ed107a36aa70f987121bb
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in changelog_generator.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # ChangelogGenerator
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/changelog_generator`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'changelog_generator'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install changelog_generator
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/changelog_generator.
36
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "changelog_generator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'changelog_generator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "github_changes"
8
+ spec.version = ChangelogGenerator::VERSION
9
+ spec.authors = ["Cody Rayment"]
10
+ spec.email = ["crayment16@gmail.com"]
11
+
12
+ spec.summary = %q{Generate Release Notes}
13
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ # spec.homepage = "https://robotsandpencils.com"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'thor'
22
+ spec.add_dependency 'github_api'
23
+ spec.add_dependency 'chronic'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec"
28
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby -wU
2
+
3
+ require 'changelog_generator'
4
+
5
+ ChangelogGenerator::CLI.start(ARGV)
@@ -0,0 +1,38 @@
1
+ require 'thor'
2
+ $thor_runner = false
3
+
4
+ module ChangelogGenerator
5
+ class CLI < Thor
6
+ desc "hello NAME", "This will greet you"
7
+ long_desc <<-EOS
8
+ `hello NAME` will print out a message to the person of your choosing.
9
+ EOS
10
+ option :upcase
11
+ def hello(name)
12
+ greeting = "Hello, #{name}!"
13
+ greeting.upcase! if options[:upcase]
14
+ puts greeting
15
+ end
16
+
17
+ desc "generate", "Generates release notes."
18
+ long_desc <<-EOS
19
+ Will generate release notes for the given repository information between
20
+ the given references (TO defaults to master) by querying github pull requests.
21
+ EOS
22
+ method_option :user, :aliases => "-u", :type => :string, :required => true
23
+ method_option :repo, :aliases => "-r", :type => :string, :required => true
24
+ method_option :from_ref, :aliases => "-f", :type => :string, :required => true
25
+ method_option :to_ref, :aliases => "-t", :type => :string, :default => "master"
26
+ method_option :label_filter, :aliases => "-l", :type => :string
27
+ def generate()
28
+ generator = Generator.new(options[:user], options[:repo], options[:from_ref], options[:to_ref], options[:label_filter])
29
+ puts generator.description
30
+ begin
31
+ puts generator.changelog
32
+ rescue Exception => e
33
+ STDERR.puts "#{e}"
34
+ STDERR.puts e.backtrace
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,213 @@
1
+ require 'github_api'
2
+ require 'chronic'
3
+ require 'changelog_generator/pull_request'
4
+ require 'changelog_generator/issue'
5
+
6
+ module ChangelogGenerator
7
+ class Generator
8
+ attr_accessor :user
9
+ attr_accessor :repo_name
10
+ attr_accessor :from_ref
11
+ attr_accessor :to_ref
12
+ attr_accessor :label_filter
13
+
14
+ def initialize(user, repo, from_ref, to_ref, label_filter = nil)
15
+ self.user = user
16
+ self.repo_name = repo
17
+ self.from_ref = from_ref
18
+ self.to_ref = to_ref || "master"
19
+ self.label_filter = label_filter
20
+ end
21
+
22
+ def description
23
+ description = "Changes for #{user}/#{repo_name} from #{from_ref} to #{to_ref}"
24
+ if label_filter
25
+ description << " with label '#{label_filter}'"
26
+ end
27
+ description
28
+ end
29
+
30
+ def changelog
31
+ generate_changelog(user, repo_name, from_ref, to_ref, label_filter, nil)
32
+ end
33
+
34
+ private
35
+
36
+ def generate_changelog(user, repo, start_ref, end_ref, label_filter = nil, exclude_sha = nil, changelog)
37
+ start_date, end_date = *date_range_for_references(start_ref, end_ref)
38
+ # puts "Start: #{start_ref}, End: #{end_ref}"
39
+ unless start_date && end_date
40
+ return "Failed to acquire date range from references"
41
+ end
42
+ pulls = pull_requests_for_date_range(start_date, end_date)
43
+ pull_nums = pulls.map { |p| p['number'] }
44
+
45
+ if pull_nums.empty?
46
+ return "No PRs found matching criteria"
47
+ else
48
+ # puts " PRs: #{pull_nums}"
49
+ # puts
50
+ end
51
+
52
+ if label_filter && !label_filter.empty?
53
+ # puts "Filtering PRs on label #{label_filter}:"
54
+ pull_nums = pull_nums.select do |n|
55
+ issue = github.issues.get user, repo, n
56
+ label_names = issue.labels.map { |l| l.name }
57
+ label_names.include?(label_filter)
58
+ end
59
+ # puts " PRs: #{pull_nums}"
60
+ # puts
61
+ end
62
+
63
+ if pull_nums.empty?
64
+ return "No PRs found matching criteria"
65
+ end
66
+
67
+ pulls = pull_nums.map { |p| github.pull_requests.get(user, repo, p) }
68
+ pulls = pulls.map { |p| PullRequest.from_github_pr(p) }
69
+ # puts JSON.pretty_generate(pulls.first.body.to_hash)
70
+
71
+ # puts "Finding connected issues:"
72
+ pulls.each do |pull|
73
+ issue_nums = connected_issues_from_pr(pull)
74
+ github_issues = issue_nums.map { |n| github.issues.get(user, repo, n) }
75
+ issues = github_issues.map { |i| Issue.from_github_issue(i) }
76
+ pull.issues = issues.compact
77
+ end
78
+
79
+ commits = pulls.map { |p| p.head_sha[0..7] }
80
+
81
+ connected_issue_nums = pulls.map(&:issues).flatten.map(&:number)
82
+
83
+ logs = pulls.map { |p| p.changelog_text }
84
+ logs = logs.reject(&:empty?)
85
+
86
+ string = "```\n"
87
+ string << logs.join("\n")
88
+ string << "\n```"
89
+ string << "\n\n"
90
+
91
+ # string << "<https://github.com/#{user}/#{repo}/pulls?q=is%3Apr+#{commits.join("+")}|PRs> | "
92
+ # string << "<https://github.com/#{user}/#{repo}/issues?q=is%3Aissue+#{connected_issue_nums.join("+")}|Issues> | "
93
+ # string << "<https://github.com/#{user}/#{repo}/issues?q=#{commits.join("+")}+#{connected_issue_nums.join("+")}|Both>"
94
+
95
+ string << "PRs: https://github.com/#{user}/#{repo}/pulls?q=is%3Apr+#{commits.join("+")}\n"
96
+ string << "Issues: https://github.com/#{user}/#{repo}/issues?q=is%3Aissue+#{connected_issue_nums.join("+")}\n"
97
+ string << "Both: https://github.com/#{user}/#{repo}/issues?q=#{commits.join("+")}+#{connected_issue_nums.join("+")}\n"
98
+
99
+ string << "\n"
100
+ end
101
+
102
+ def github
103
+ @github ||= Github.new({ oauth_token: ENV['GITHUB_TOKEN'] })
104
+ end
105
+
106
+ def check_github_response
107
+ begin
108
+ value = yield
109
+ rescue Github::Error::Unauthorized => e
110
+ abort "Error: wrong GitHub token"
111
+ rescue Github::Error::Forbidden => e
112
+ abort "Error: wrong GitHub token"
113
+ rescue Github::Error::NotFound => e
114
+ value = nil
115
+ end
116
+ value
117
+ end
118
+
119
+ def changelog_from_pr(pr)
120
+ default = "- #{pr['title']}"
121
+
122
+ body = pr.body.body
123
+ return default unless body
124
+
125
+ matcher = body.gsub(/\r\n?/, "\n").match(/.*#+\s+(changelog|release notes)(^\n)*\n(?<changes>(.*\n?(?!#))+)/i)
126
+ return default unless matcher && matcher.names.include?('changes')
127
+
128
+ matcher['changes'].strip
129
+ end
130
+
131
+ def connected_issues_from_pr(pr)
132
+ body = pr.body
133
+ return [] unless body
134
+
135
+ body.scan(/((connect|connects|fix|fixes|address|addresses):?\s*(to)?:?\s*#(?<number>\d+))/im).reject(&:nil?).map(&:last).flatten
136
+ end
137
+
138
+ def date_from_commit(commit)
139
+ return nil unless commit
140
+ date = commit["committer"]["date"]
141
+ date = Chronic.parse(date)
142
+ end
143
+
144
+ def find_commit_for_reference(ref)
145
+ return nil unless ref && ref["object"] && ref["object"]["sha"]
146
+ sha = ref["object"]["sha"]
147
+ commit = check_github_response() { github.git_data.commits.get user, repo_name, URI::encode(sha) }
148
+ unless commit
149
+ tag = check_github_response() { github.git_data.tags.get user, repo_name, URI::encode(sha) }
150
+ sha = (tag && tag.object) ? tag.object.sha : nil
151
+ commit = check_github_response() { github.git_data.commits.get user, repo_name, URI::encode(sha) } if sha
152
+ end
153
+ commit
154
+ end
155
+
156
+ def find_reference_for_string(ref)
157
+ reference = check_github_response() { github.git_data.references.get user, repo_name, URI::encode("heads/#{ref}") }
158
+ reference ||= check_github_response() { github.git_data.references.get user, repo_name, URI::encode("tags/#{ref}") }
159
+ reference
160
+ end
161
+
162
+ def find_commit_for_string(string)
163
+ reference = find_reference_for_string(string)
164
+ return find_commit_for_reference(reference) if reference
165
+ check_github_response() { github.git_data.commits.get user, repo_name, URI::encode(string) }
166
+ end
167
+
168
+ def date_range_for_references(ref_one, ref_two)
169
+ return [date_for_string(ref_one), date_for_string(ref_two)]
170
+ end
171
+
172
+ def date_for_string(string)
173
+ date_from_commit(find_commit_for_string(string)) || parse_string_as_date(string)
174
+ end
175
+
176
+ def parse_string_as_date(string)
177
+ Chronic.parse(string)
178
+ end
179
+
180
+ # Page through all pulls grabbing only those merged in date range
181
+ def pull_requests_for_date_range(start_date, end_date)
182
+ page = 1
183
+ pulls = []
184
+
185
+ # There is a chance that the commit is created slightly earlier than the PR is merged.
186
+ # Example: merged_at: 2015-11-25T17:31:24+00:00, commit_date: 2015-11-25T17:31:23+00:00
187
+ end_date += 5
188
+ start_date += 5
189
+
190
+ loop do
191
+ response_wrapper = check_github_response() { github.pull_requests.list user, repo_name, state: 'closed', sort: 'updated', direction: 'desc', base: 'master', page: page }
192
+ found_pulls = response_wrapper.to_a
193
+ found_pulls.select! { |p| p['merged_at'] }
194
+ break if found_pulls.count == 0
195
+ stop = false
196
+ found_pulls.each do |pull|
197
+ date = Chronic.parse(pull['merged_at'])
198
+ # puts "PR: #{pull.number} Date: #{date}, Start Date: #{start_date}, End Date: #{end_date}"
199
+ next if date > end_date
200
+ if date <= start_date
201
+ stop = true
202
+ break
203
+ end
204
+ pulls << pull
205
+ end
206
+ break if stop
207
+ page += 1
208
+ end
209
+
210
+ pulls
211
+ end
212
+ end
213
+ end
@@ -0,0 +1,20 @@
1
+ module ChangelogGenerator
2
+ class Issue
3
+ attr_accessor :title
4
+ attr_accessor :number
5
+
6
+ def self.from_github_issue(github_issue)
7
+ self.new({
8
+ title: github_issue['title'],
9
+ number: github_issue['number']
10
+ })
11
+ end
12
+
13
+ def initialize(args)
14
+ args.each do |k,v|
15
+ instance_variable_set("@#{k}", v) unless v.nil?
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,42 @@
1
+ module ChangelogGenerator
2
+ class PullRequest
3
+ attr_accessor :title
4
+ attr_accessor :body
5
+ attr_accessor :number
6
+ attr_accessor :changelog_text
7
+ attr_accessor :head_sha
8
+ attr_accessor :merged_at
9
+ attr_accessor :issues
10
+
11
+ def self.from_github_pr(github_pr)
12
+ self.new({
13
+ title: github_pr['title'],
14
+ body: github_pr.body.body,
15
+ number: github_pr['number'],
16
+ changelog_text: changelog_from_pr(github_pr),
17
+ head_sha: github_pr.head.sha,
18
+ merged_at: github_pr.merged_at
19
+ })
20
+ end
21
+
22
+ def initialize(args)
23
+ args.each do |k,v|
24
+ instance_variable_set("@#{k}", v) unless v.nil?
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def self.changelog_from_pr(pr)
31
+ default = "- #{pr['title']}"
32
+
33
+ body = pr.body.body
34
+ return default unless body
35
+
36
+ matcher = body.gsub(/\r\n?/, "\n").match(/.*#+\s+(changelog|release notes)(^\n)*\n(?<changes>(.*\n?(?!#))+)/i)
37
+ return default unless matcher && matcher.names.include?('changes')
38
+
39
+ matcher['changes'].strip
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module ChangelogGenerator
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "changelog_generator/version"
2
+ require "changelog_generator/cli"
3
+ require "changelog_generator/generator"
4
+
5
+ module ChangelogGenerator
6
+ # Your code goes here...
7
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: github_changes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Cody Rayment
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: github_api
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: chronic
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - crayment16@gmail.com
100
+ executables:
101
+ - github_changes
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - changelog_generator.gemspec
114
+ - exe/github_changes
115
+ - lib/changelog_generator.rb
116
+ - lib/changelog_generator/cli.rb
117
+ - lib/changelog_generator/generator.rb
118
+ - lib/changelog_generator/issue.rb
119
+ - lib/changelog_generator/pull_request.rb
120
+ - lib/changelog_generator/version.rb
121
+ homepage:
122
+ licenses: []
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.2.2
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Generate Release Notes
144
+ test_files: []