Class: GitHubChangelogGenerator::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/github_changelog_generator/parser.rb

Class Method Summary collapse

Class Method Details

.detect_user_and_project(options) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/github_changelog_generator/parser.rb', line 137

def self.detect_user_and_project(options)
  if ARGV[0] && !ARGV[1]
    github_site = options[:github_site] ? options[:github_site] : "github.com"
    # this match should parse  strings such "https://github.com/skywinder/Github-Changelog-Generator" or "skywinder/Github-Changelog-Generator" to user and name
    match = /(?:.+#{Regexp.escape(github_site)}\/)?(.+)\/(.+)/.match(ARGV[0])

    begin
      param = match[2].nil?
    rescue
      puts "Can't detect user and name from first parameter: '#{ARGV[0]}' -> exit'"
      exit
    end
    if param
      exit
    else
      options[:user] = match[1]
      options[:project] = match[2]
    end

  end

  if !options[:user] && !options[:project]
    if ENV["RUBYLIB"] =~ /ruby-debug-ide/
      options[:user] = "skywinder"
      options[:project] = "changelog_test"
    else
      remote = `git config --get remote.#{options[:branch]}.url`
      # try to find repo in format:
      # origin	[email protected]:skywinder/Github-Changelog-Generator.git (fetch)
      # [email protected]:skywinder/Github-Changelog-Generator.git
      match = /.*(?:[:\/])((?:-|\w|\.)*)\/((?:-|\w|\.)*)(?:\.git).*/.match(remote)

      if match && match[1] && match[2]
        puts "Detected user:#{match[1]}, project:#{match[2]}"
        options[:user], options[:project] = match[1], match[2]
      else
        # try to find repo in format:
        # origin	https://github.com/skywinder/ChangelogMerger (fetch)
        # https://github.com/skywinder/ChangelogMerger
        match = /.*\/((?:-|\w|\.)*)\/((?:-|\w|\.)*).*/.match(remote)
        if match && match[1] && match[2]
          puts "Detected user:#{match[1]}, project:#{match[2]}"
          options[:user], options[:project] = match[1], match[2]
        end
      end
    end
  end
end

.parse_optionsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/github_changelog_generator/parser.rb', line 8

def self.parse_options
  options = {
    tag1: nil,
    tag2: nil,
    dateformat: "%Y-%m-%d",
    output: "CHANGELOG.md",
    issues: true,
    add_issues_wo_labels: true,
    add_pr_wo_labels: true,
    pulls: true,
    filter_issues_by_milestone: true,
    author: true,
    unreleased: true,
    unreleased_label: "Unreleased",
    compare_link: true,
    include_labels: %w(bug enhancement),
    exclude_labels: %w(duplicate question invalid wontfix),
    max_issues: nil,
    simple_list: false,
    verbose: true,

    merge_prefix: "**Merged pull requests:**",
    issue_prefix: "**Closed issues:**",
    bug_prefix: "**Fixed bugs:**",
    enhancement_prefix: "**Implemented enhancements:**",
    branch: "origin"
  }

  parser = OptionParser.new do |opts|
    opts.banner = "Usage: github_changelog_generator [options]"
    opts.on("-u", "--user [USER]", "Username of the owner of target GitHub repo") do |last|
      options[:user] = last
    end
    opts.on("-p", "--project [PROJECT]", "Name of project on GitHub") do |last|
      options[:project] = last
    end
    opts.on("-t", "--token [TOKEN]", "To make more than 50 requests per hour your GitHub token is required. You can generate it at: https://github.com/settings/tokens/new") do |last|
      options[:token] = last
    end
    opts.on("-f", "--date-format [FORMAT]", "Date format. Default is %Y-%m-%d") do |last|
      options[:dateformat] = last
    end
    opts.on("-o", "--output [NAME]", "Output file. Default is CHANGELOG.md") do |last|
      options[:output] = last
    end
    opts.on("--[no-]issues", "Include closed issues in changelog. Default is true") do |v|
      options[:issues] = v
    end
    opts.on("--[no-]issues-wo-labels", "Include closed issues without labels in changelog. Default is true") do |v|
      options[:add_issues_wo_labels] = v
    end
    opts.on("--[no-]pr-wo-labels", "Include pull requests without labels in changelog. Default is true") do |v|
      options[:add_pr_wo_labels] = v
    end
    opts.on("--[no-]pull-requests", "Include pull-requests in changelog. Default is true") do |v|
      options[:pulls] = v
    end
    opts.on("--[no-]filter-by-milestone", "Use milestone to detect when issue was resolved. Default is true") do |last|
      options[:filter_issues_by_milestone] = last
    end
    opts.on("--[no-]author", "Add author of pull-request in the end. Default is true") do |author|
      options[:author] = author
    end
    opts.on("--unreleased-only", "Generate log from unreleased closed issues only.") do |v|
      options[:unreleased_only] = v
    end
    opts.on("--[no-]unreleased", "Add to log unreleased closed issues. Default is true") do |v|
      options[:unreleased] = v
    end
    opts.on("--unreleased-label [label]", "Add to log unreleased closed issues. Default is true") do |v|
      options[:unreleased_label] = v
    end
    opts.on("--[no-]compare-link", "Include compare link (Full Changelog) between older version and newer version. Default is true") do |v|
      options[:compare_link] = v
    end
    opts.on("--include-labels  x,y,z", Array, 'Only issues with the specified labels will be included in the changelog. Default is \'bug,enhancement\'') do |list|
      options[:include_labels] = list
    end
    opts.on("--exclude-labels  x,y,z", Array, 'Issues with the specified labels will be always excluded from changelog. Default is \'duplicate,question,invalid,wontfix\'') do |list|
      options[:exclude_labels] = list
    end
    opts.on("--max-issues [NUMBER]", Integer, "Max number of issues to fetch from GitHub. Default is unlimited") do |max|
      options[:max_issues] = max
    end
    opts.on("--github-site [URL]", "The Enterprise Github site on which your project is hosted.") do |last|
      options[:github_site] = last
    end
    opts.on("--github-api [URL]", "The enterprise endpoint to use for your Github API.") do |last|
      options[:github_endpoint] = last
    end
    opts.on("--simple-list", "Create simple list from issues and pull requests. Default is false.") do |v|
      options[:simple_list] = v
    end
    opts.on("--[no-]verbose", "Run verbosely. Default is true") do |v|
      options[:verbose] = v
    end
    opts.on("-v", "--version", "Print version number") do |_v|
      puts "Version: #{GitHubChangelogGenerator::VERSION}"
      exit
    end
    opts.on("-h", "--help", "Displays Help") do
      puts opts
      exit
    end
  end

  parser.parse!

  detect_user_and_project(options)

  if !options[:user] || !options[:project]
    puts parser.banner
    exit
  end

  if ARGV[1]
    options[:tag1] = ARGV[0]
    options[:tag2] = ARGV[1]
  end

  if options[:verbose]
    puts "Performing task with options:"
    pp options
    puts ""
  end

  options
end