From fbd8207ff420929ded20b2ffb094a39fff8dd868 Mon Sep 17 00:00:00 2001 From: Dlani Mendes Date: Mon, 14 Sep 2015 01:02:52 -0300 Subject: [PATCH 1/2] Auto parse options from file .github_changelog_generator --- .gitignore | 1 + lib/github_changelog_generator.rb | 1 + lib/github_changelog_generator/parser.rb | 4 ++- lib/github_changelog_generator/parser_file.rb | 28 +++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 lib/github_changelog_generator/parser_file.rb diff --git a/.gitignore b/.gitignore index e69de29..1377554 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/lib/github_changelog_generator.rb b/lib/github_changelog_generator.rb index 3b1e60f..31d86ae 100755 --- a/lib/github_changelog_generator.rb +++ b/lib/github_changelog_generator.rb @@ -7,6 +7,7 @@ require "benchmark" require_relative "github_changelog_generator/helper" require_relative "github_changelog_generator/parser" +require_relative "github_changelog_generator/parser_file" require_relative "github_changelog_generator/generator/generator" require_relative "github_changelog_generator/version" require_relative "github_changelog_generator/reader" diff --git a/lib/github_changelog_generator/parser.rb b/lib/github_changelog_generator/parser.rb index d1b36b7..06b5c26 100644 --- a/lib/github_changelog_generator/parser.rb +++ b/lib/github_changelog_generator/parser.rb @@ -10,9 +10,11 @@ module GitHubChangelogGenerator options = get_default_options parser = setup_parser(options) - parser.parse! + parser_file = ParseFile.new options + parser_file.parse! + if options[:user].nil? || options[:project].nil? detect_user_and_project(options, ARGV[0], ARGV[1]) end diff --git a/lib/github_changelog_generator/parser_file.rb b/lib/github_changelog_generator/parser_file.rb new file mode 100644 index 0000000..068fc0f --- /dev/null +++ b/lib/github_changelog_generator/parser_file.rb @@ -0,0 +1,28 @@ +module GitHubChangelogGenerator + class ParseFile + def initialize(options) + @options = options + end + + def file + File.expand_path(".github_changelog_generator") + end + + def has_file? + File.exists?(file) + end + + def file_open + File.open(file) + end + + def parse! + return false unless has_file? + file_open.each do |line| + key, value = line.split("=") + key_sym = key.sub('-', '_').to_sym + @options[key_sym] = value.gsub(/[\n\r]+/, '') + end + end + end +end From 6c71cf439af0bf2ad866eb3cc8423b88fb6cb5af Mon Sep 17 00:00:00 2001 From: Dlani Mendes Date: Tue, 15 Sep 2015 15:38:41 -0300 Subject: [PATCH 2/2] Rspec and README --- .gitignore | 1 - README.md | 48 +++++++++++-------- lib/github_changelog_generator/parser.rb | 6 +-- lib/github_changelog_generator/parser_file.rb | 24 ++++++---- spec/files/github_changelog_params_empty | 0 spec/files/github_changelog_params_incorrect | 2 + spec/files/github_changelog_params_override | 2 + spec/unit/parse_file_spec.rb | 32 +++++++++++++ 8 files changed, 84 insertions(+), 31 deletions(-) create mode 100644 spec/files/github_changelog_params_empty create mode 100644 spec/files/github_changelog_params_incorrect create mode 100644 spec/files/github_changelog_params_override create mode 100644 spec/unit/parse_file_spec.rb diff --git a/.gitignore b/.gitignore index 1377554..e69de29 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +0,0 @@ -*.swp diff --git a/README.md b/README.md index 5acde66..9c23c4b 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ GitHub Changelog Generator ![GitHub Logo](../master/images/logo.jpg) - [Contributing](#contributing) - [License](#license) - + ### Changelog generation has never been so easy: **Fully automate changelog generation** - This gem generates change log file based on **tags**, **issues** and merged **pull requests** (and splits them into separate lists according labels) from :octocat: GitHub Issue Tracker. @@ -47,26 +47,26 @@ Because software tools are for people. If you don’t care, why are you contribu - In general it looks like this: > ## [1.2.5](https://github.com/skywinder/Github-Changelog-Generator/tree/1.2.5) (2015-01-15) -> +> > [Full Changelog](https://github.com/skywinder/Github-Changelog-Generator/compare/1.2.4...1.2.5) -> +> > **Implemented enhancements:** -> +> > - Use milestone to specify in which version bug was fixed [\#22](https://github.com/skywinder/Github-Changelog-Generator/issues/22) -> +> > **Fixed bugs:** -> +> > - Error when trying to generate log for repo without tags [\#32](https://github.com/skywinder/Github-Changelog-Generator/issues/32) -> +> > **Merged pull requests:** -> +> > - PrettyPrint class is included using lowercase 'pp' [\#43](https://github.com/skywinder/Github-Changelog-Generator/pull/43) ([schwing](https://github.com/schwing)) -> +> > - support enterprise github via command line options [\#42](https://github.com/skywinder/Github-Changelog-Generator/pull/42) ([glenlovett](https://github.com/glenlovett)) ## Usage -**It's really simple**: +**It's really simple**: - If your **git remote** `origin` refers to your GitHub repo, then just go to your project folder and run: @@ -75,7 +75,7 @@ Because software tools are for people. If you don’t care, why are you contribu - or from anywhere: - `github_changelog_generator -u github_username -p github_project` - `github_changelog_generator github_username/github_project` - + As output you will get `CHANGELOG.md` file with pretty *Markdown-formatted* changelog. ### Params @@ -83,6 +83,16 @@ Type `github_changelog_generator --help` for details. More detailed info about params you can find in Wiki page: [**Advanced change log generation examples**](https://github.com/skywinder/github-changelog-generator/wiki/Advanced-change-log-generation-examples) +### Params File +You can put Params in a .github_changelog_generator file in Project Root to override default params: + +Example: +``` +unreleased=false +future-release=5.0.0 +since-tag=1.0.0 +``` + ### GitHub token Since GitHub allows you to make only 50 requests without authentication it's recommended to run this script with a token (`-t, --token` option) @@ -91,9 +101,9 @@ Since GitHub allows you to make only 50 requests without authentication it's rec And: -- Run with key `-t [your-40-digit-token]` -- Or set environment variable `CHANGELOG_GITHUB_TOKEN` and specify there your token. - +- Run with key `-t [your-40-digit-token]` +- Or set environment variable `CHANGELOG_GITHUB_TOKEN` and specify there your token. + i.e. add to your `~/.bash_profile` or `~/.zshrc` or any other place to load ENV variables string : export CHANGELOG_GITHUB_TOKEN="your-40-digit-github-token" @@ -138,10 +148,10 @@ All command line options can be passed to the Rake task as `config` parameters. - Merged pull requests (all `merged` pull-requests) :twisted_rightwards_arrows: - Bug fixes (by label `bug` in issue) :beetle: - Enhancements (by label `enhancement` in issue) :star2: - - Issues (closed issues `w/o any labels`) :non-potable_water: + - Issues (closed issues `w/o any labels`) :non-potable_water: - You can manually set which labels should be included/excluded. :wrench: -- Apply a lot of other customisations, to fit changelog for your personal style :tophat: +- Apply a lot of other customisations, to fit changelog for your personal style :tophat: (*look `github_changelog_generator --help` for details)* @@ -152,7 +162,7 @@ Here is a [wikipage list of alternatives](https://github.com/skywinder/Github-Ch ### Projects using this library -[Wikipage with list of projects](https://github.com/skywinder/Github-Changelog-Generator/wiki/Projects-using-Github-Changelog-Generator) +[Wikipage with list of projects](https://github.com/skywinder/Github-Changelog-Generator/wiki/Projects-using-Github-Changelog-Generator) If you've used this project in a live app, please let me know! Nothing makes me happier than seeing someone else take my work and go wild with it. @@ -160,9 +170,9 @@ If you've used this project in a live app, please let me know! Nothing makes me ## Am I missing some essential feature? -- **Nothing is impossible!** +- **Nothing is impossible!** -- Open an [issue](https://github.com/skywinder/Github-Changelog-Generator/issues/new) and let's make generator better together! +- Open an [issue](https://github.com/skywinder/Github-Changelog-Generator/issues/new) and let's make generator better together! - *Bug reports, feature requests, patches, well-wishes are always welcome* :heavy_exclamation_mark: diff --git a/lib/github_changelog_generator/parser.rb b/lib/github_changelog_generator/parser.rb index 06b5c26..5001ad6 100644 --- a/lib/github_changelog_generator/parser.rb +++ b/lib/github_changelog_generator/parser.rb @@ -9,12 +9,12 @@ module GitHubChangelogGenerator def self.parse_options options = get_default_options + parser_file = ParserFile.new options + parser_file.parse! + parser = setup_parser(options) parser.parse! - parser_file = ParseFile.new options - parser_file.parse! - if options[:user].nil? || options[:project].nil? detect_user_and_project(options, ARGV[0], ARGV[1]) end diff --git a/lib/github_changelog_generator/parser_file.rb b/lib/github_changelog_generator/parser_file.rb index 068fc0f..74f3cd3 100644 --- a/lib/github_changelog_generator/parser_file.rb +++ b/lib/github_changelog_generator/parser_file.rb @@ -1,15 +1,15 @@ module GitHubChangelogGenerator - class ParseFile + class ParserFile def initialize(options) @options = options end def file - File.expand_path(".github_changelog_generator") + File.expand_path(@options[:params_file] || ".github_changelog_generator") end - def has_file? - File.exists?(file) + def file? + File.exist?(file) end def file_open @@ -17,12 +17,20 @@ module GitHubChangelogGenerator end def parse! - return false unless has_file? + return unless file? file_open.each do |line| - key, value = line.split("=") - key_sym = key.sub('-', '_').to_sym - @options[key_sym] = value.gsub(/[\n\r]+/, '') + begin + key, value = line.split("=") + key_sym = key.sub("-", "_").to_sym + value = value.gsub(/[\n\r]+/, "") + value = true if value =~ (/^(true|t|yes|y|1)$/i) + value = false if value =~ (/^(false|f|no|n|0)$/i) + @options[key_sym] = value + rescue + raise "File #{file} is incorrect in line \"#{line.gsub(/[\n\r]+/, '')}\"" + end end + @options end end end diff --git a/spec/files/github_changelog_params_empty b/spec/files/github_changelog_params_empty new file mode 100644 index 0000000..e69de29 diff --git a/spec/files/github_changelog_params_incorrect b/spec/files/github_changelog_params_incorrect new file mode 100644 index 0000000..35fa3d8 --- /dev/null +++ b/spec/files/github_changelog_params_incorrect @@ -0,0 +1,2 @@ +unreleased_label: staging +unreleased: false diff --git a/spec/files/github_changelog_params_override b/spec/files/github_changelog_params_override new file mode 100644 index 0000000..ff0a972 --- /dev/null +++ b/spec/files/github_changelog_params_override @@ -0,0 +1,2 @@ +unreleased_label=staging +unreleased=false diff --git a/spec/unit/parse_file_spec.rb b/spec/unit/parse_file_spec.rb new file mode 100644 index 0000000..55c743d --- /dev/null +++ b/spec/unit/parse_file_spec.rb @@ -0,0 +1,32 @@ +describe GitHubChangelogGenerator::ParserFile do + describe ".github_changelog_generator" do + context "when no has file" do + let(:options) { {} } + let(:parse) { GitHubChangelogGenerator::ParserFile.new(options) } + subject { parse.parse! } + it { is_expected.to be_nil } + end + + context "when file is empty" do + let(:options) { { params_file: "spec/files/github_changelog_params_empty" } } + let(:parse) { GitHubChangelogGenerator::ParserFile.new(options) } + subject { parse.parse! } + it { is_expected.to be_a(Hash) } + it { is_expected.to eq(options) } + end + + context "when file is incorrect" do + let(:options) { { params_file: "spec/files/github_changelog_params_incorrect" } } + let(:parse) { GitHubChangelogGenerator::ParserFile.new(options) } + it { expect { fail.raise! }.to raise_error RuntimeError } + end + + context "when override default values" do + let(:options) { { params_file: "spec/files/github_changelog_params_override" }.merge(GitHubChangelogGenerator::Parser.get_default_options) } + let(:parse) { GitHubChangelogGenerator::ParserFile.new(options) } + subject { parse.parse! } + it { is_expected.to be_a(Hash) } + it { is_expected.to eq(options.merge(unreleased_label: "staging", unreleased: false)) } + end + end +end