sem_ver_components 0.1.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c9da50f9edee211f731452cdfb03e376d2b895f75d04a5ebdbfe2d30c87dfb90
4
- data.tar.gz: 1ad69a75e32306303ae841aa9aecf633a7c90db25cd529d9c8c791837e154e04
3
+ metadata.gz: 387992ffe83f018c9096f517580fc810be55fce48f32ed519091d226aa480fed
4
+ data.tar.gz: 6b624945d92655aee943be4e1f380498b70e0fbb070917a1d1bd7eaacf00d353
5
5
  SHA512:
6
- metadata.gz: d4b54b6a41a4060da5171f2f809ca57aac59d75a72194da62b84d6e1a598f40e0ea29682221fdb0fda163db6dc494dcaf7dcd74b52fdbea6b20b167af3cc75b7
7
- data.tar.gz: 2111a729b803e12870b3df2d1e4efc2411d34deeff6882218b2313b17686748d8350f47f56a89a39f12b5c0d997b3caef7ec4134e7c57462dc05f0704605694b
6
+ metadata.gz: a4d1bf087e0922fe5eaa3439497279d8562fd865527c65f915d61ffe0fe5abd594af4ba6290f085297028dbf903e3c3d428bbb3ef885393ccabad3690b7107f2
7
+ data.tar.gz: dc626df24fc0c635c86aeed396e4eeed80ef11840d65008846dd1c40568f0f6955385d93aa5c2b9cef767dca3b65793e3acd76022dd930dce2fdcdfbe6ea6f55
@@ -4,17 +4,23 @@ require 'sem_ver_components/plugins'
4
4
  require 'sem_ver_components/local_git'
5
5
  require 'sem_ver_components/version'
6
6
  require 'sem_ver_components/output'
7
+ require 'sem_ver_components/git_hosting'
7
8
 
8
9
  # Default values
9
10
  git_repo = '.'
10
11
  git_from = nil
11
12
  git_to = 'HEAD'
13
+ git_hosting = :github
12
14
  output = :info
13
15
  output_plugins = SemVerComponents::Plugins.new(:outputs)
16
+ git_hosting_plugins = SemVerComponents::Plugins.new(:git_hostings)
14
17
  OptionParser.new do |opts|
15
18
  opts.banner = "Usage: #{File.basename($0)} [options]"
16
19
  opts.on('-f', '--from GIT_REF', 'Git reference from which commits are to be analyzed (defaults to first commit)') do |git_ref|
17
- git_from = git_ref
20
+ git_from = git_ref.empty? ? nil : git_ref
21
+ end
22
+ opts.on('-g', '--git_hosting GIT_HOSTING', "Specify which kind of git hosting is used. Used to format URLs to commits and comparisons. Possible values are #{git_hosting_plugins.list.sort.join(', ')}. (defaults to #{git_hosting})") do |git_hosting_str|
23
+ git_hosting = git_hosting_str.to_sym
18
24
  end
19
25
  opts.on('-h', '--help', 'Display this help') do
20
26
  puts opts
@@ -40,4 +46,4 @@ raise "Unknown parameters: #{ARGV.join(' ')}" unless ARGV.empty?
40
46
  local_git = SemVerComponents::LocalGit.new(git_repo, git_from, git_to)
41
47
  commits_info = local_git.analyze_commits
42
48
 
43
- output_plugins[output].new(local_git).process(commits_info)
49
+ output_plugins[output].new(local_git, git_hosting_plugins[git_hosting].new).process(commits_info)
@@ -0,0 +1,8 @@
1
+ module SemVerComponents
2
+
3
+ # Base class for all git hosting plugins
4
+ class GitHosting
5
+
6
+ end
7
+
8
+ end
@@ -0,0 +1,49 @@
1
+ module SemVerComponents
2
+
3
+ module GitHostings
4
+
5
+ class Bitbucket < GitHosting
6
+
7
+ # Get the URL to a given commit sha
8
+ #
9
+ # Parameters::
10
+ # * *git_url* (String): The git URL
11
+ # * *commit_sha* (String): The commit sha
12
+ def commit_url(git_url, commit_sha)
13
+ "#{public_url(git_url)}/commits/#{commit_sha}"
14
+ end
15
+
16
+ # Get the URL to compare 2 tags
17
+ #
18
+ # Parameters::
19
+ # * *git_url* (String): The git URL
20
+ # * *tag_1* (String): The first tag
21
+ # * *tag_2* (String): The second tag
22
+ def compare_url(git_url, tag_1, tag_2)
23
+ "#{public_url(git_url)}/compare/commits?targetBranch=refs%2Ftags%2F#{tag_1}&sourceBranch=refs%2Ftags%2F#{tag_2}"
24
+ end
25
+
26
+ private
27
+
28
+ # Convert the git remote URL to the public URL
29
+ #
30
+ # Parameters::
31
+ # * *git_url* (String): Git remote URL
32
+ # Result::
33
+ # * String: The corresponding public URL
34
+ def public_url(git_url)
35
+ if git_url =~ /^(.+)\/scm\/([^\/]+)\/(.+)$/
36
+ base_url = $1
37
+ project = $2
38
+ repo = $3
39
+ "#{base_url}/projects/#{project}/repos/#{repo}"
40
+ else
41
+ git_url
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,30 @@
1
+ module SemVerComponents
2
+
3
+ module GitHostings
4
+
5
+ class Github < GitHosting
6
+
7
+ # Get the URL to a given commit sha
8
+ #
9
+ # Parameters::
10
+ # * *git_url* (String): The git URL
11
+ # * *commit_sha* (String): The commit sha
12
+ def commit_url(git_url, commit_sha)
13
+ "#{git_url}/commit/#{commit_sha}"
14
+ end
15
+
16
+ # Get the URL to compare 2 tags
17
+ #
18
+ # Parameters::
19
+ # * *git_url* (String): The git URL
20
+ # * *tag_1* (String): The first tag
21
+ # * *tag_2* (String): The second tag
22
+ def compare_url(git_url, tag_1, tag_2)
23
+ "#{git_url}/compare/#{tag_1}...#{tag_2}"
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -60,6 +60,14 @@ module SemVerComponents
60
60
  end
61
61
  end
62
62
 
63
+ # Is the git to ref part of a release branch?
64
+ #
65
+ # Result::
66
+ # * Boolean: Is the git to ref part of a release branch?
67
+ def on_release_branch?
68
+ @git_to == 'master'
69
+ end
70
+
63
71
  end
64
72
 
65
73
  end
@@ -1,13 +1,16 @@
1
1
  module SemVerComponents
2
2
 
3
+ # Base class for all output plugins
3
4
  class Output
4
5
 
5
6
  # Constructor
6
7
  #
7
8
  # Parameters::
8
9
  # * *local_git* (LocalGit): The git repository
9
- def initialize(local_git)
10
+ # * *git_hosting* (GitHosting): The git hosting to be used for URLs
11
+ def initialize(local_git, git_hosting)
10
12
  @local_git = local_git
13
+ @git_hosting = git_hosting
11
14
  end
12
15
 
13
16
  end
@@ -1,3 +1,5 @@
1
+ require 'sem_ver_components/semver'
2
+
1
3
  module SemVerComponents
2
4
 
3
5
  module Outputs
@@ -12,11 +14,12 @@ module SemVerComponents
12
14
  # * *commit* (Git::Object::Commit): Corresponding git commit
13
15
  def process(commits_info)
14
16
  # Display bump levels per component
15
- commits_info.inject({}) do |components_bump_levels, commit_info|
17
+ bumps_per_component = commits_info.inject({}) do |components_bump_levels, commit_info|
16
18
  components_bump_levels.merge(commit_info[:components_bump_levels]) do |_component, bump_level_1, bump_level_2|
17
19
  [bump_level_1, bump_level_2].max
18
20
  end
19
- end.each do |component, bump_level|
21
+ end
22
+ bumps_per_component.each do |component, bump_level|
20
23
  puts "#{component.nil? ? 'Global' : component}: Bump #{
21
24
  case bump_level
22
25
  when 0
@@ -30,6 +33,15 @@ module SemVerComponents
30
33
  end
31
34
  } version"
32
35
  end
36
+ # Compute new version
37
+ global_bump_level = bumps_per_component.values.max
38
+ if global_bump_level.nil?
39
+ puts 'No next version'
40
+ else
41
+ puts "Next global version#{@local_git.on_release_branch? ? '' : ' (not on release branch)'}: #{
42
+ Semver.next_version_from(Semver.version_from_git_ref(@local_git.git_from), global_bump_level, pre_release: !@local_git.on_release_branch?)
43
+ }"
44
+ end
33
45
  end
34
46
 
35
47
  end
@@ -1,4 +1,5 @@
1
1
  require 'time'
2
+ require 'sem_ver_components/semver'
2
3
 
3
4
  module SemVerComponents
4
5
 
@@ -14,31 +15,10 @@ module SemVerComponents
14
15
  # * *commit* (Git::Object::Commit): Corresponding git commit
15
16
  def process(commits_info)
16
17
  # Compute new version
17
- new_version =
18
- if @local_git.git_from.nil?
19
- '0.0.1'
20
- elsif @local_git.git_from =~ /^v(\d+)\.(\d+)\.(\d+)$/
21
- major = Integer($1)
22
- minor = Integer($2)
23
- patch = Integer($3)
24
- bump_level = commits_info.map { |commit_info| commit_info[:components_bump_levels].values }.flatten(1).max
25
- case bump_level
26
- when 0
27
- patch += 1
28
- when 1
29
- minor += 1
30
- patch = 0
31
- when 2
32
- major += 1
33
- minor = 0
34
- patch = 0
35
- else
36
- raise "Invalid bump level: #{bump_level}"
37
- end
38
- "#{major}.#{minor}.#{patch}"
39
- else
40
- raise "Can't generate release notes from a git ref that is not a semantic release (#{@local_git.git_from})"
41
- end
18
+ new_version = Semver.next_version_from(
19
+ Semver.version_from_git_ref(@local_git.git_from),
20
+ commits_info.map { |commit_info| commit_info[:components_bump_levels].values }.flatten(1).max
21
+ )
42
22
  git_url = @local_git.git.remote('origin').url
43
23
  git_url = git_url[0..-5] if git_url.end_with?('.git')
44
24
  # Reference merge commits: merged commits will not be part of the changelog, but their bump level will be taken into account when reporting the merge commit.
@@ -80,7 +60,7 @@ module SemVerComponents
80
60
  commits_per_component[component][bump_level] << git_commit
81
61
  end
82
62
  end
83
- puts "# [v#{new_version}](#{git_url}/compare/#{@local_git.git_from}...v#{new_version}) (#{Time.now.utc.strftime('%F %T')})"
63
+ puts "# [v#{new_version}](#{@git_hosting.compare_url(git_url, @local_git.git_from, "v#{new_version}")}) (#{Time.now.utc.strftime('%F %T')})"
84
64
  puts
85
65
  commits_per_component.sort_by { |component, _component_info| component || '' }.each do |(component, component_info)|
86
66
  puts "## #{component.nil? ? 'Global changes' : "Changes for #{component}"}\n" if commits_per_component.size > 1 || !component.nil?
@@ -112,7 +92,7 @@ module SemVerComponents
112
92
  commit_lines[commit_line] = commit.sha
113
93
  end
114
94
  commit_lines.each do |commit_line, commit_sha|
115
- puts "* [#{commit_line}](#{git_url}/commit/#{commit_sha})"
95
+ puts "* [#{commit_line}](#{@git_hosting.commit_url(git_url, commit_sha)})"
116
96
  end
117
97
  puts
118
98
  end
@@ -0,0 +1,66 @@
1
+ module SemVerComponents
2
+
3
+ # Helpers giving ways to adapt to semantic versioning conventions
4
+ module Semver
5
+
6
+ # Compute next version from an existing one and a bump level
7
+ #
8
+ # Parameters::
9
+ # * *version* (String): Existing version
10
+ # * *bump_level* (Integer): The bump level (0: patch, 1: minor, 2: major)
11
+ # * *pre_release* (Boolean): Should we get a pre-release version (adding a unique metadata)? [default: false]
12
+ # Result::
13
+ # * String: The next version
14
+ def self.next_version_from(version, bump_level, pre_release: false)
15
+ if version =~ /^(\d+)\.(\d+)\.(\d+)$/
16
+ major = Integer($1)
17
+ minor = Integer($2)
18
+ patch = Integer($3)
19
+ case bump_level
20
+ when 0
21
+ patch += 1
22
+ when 1
23
+ minor += 1
24
+ patch = 0
25
+ when 2
26
+ major += 1
27
+ minor = 0
28
+ patch = 0
29
+ else
30
+ raise "Invalid bump level: #{bump_level}"
31
+ end
32
+ "#{major}.#{minor}.#{patch}#{pre_release ? "-#{pre_release_metadata}" : ''}"
33
+ else
34
+ raise "Invalid version: #{version}"
35
+ end
36
+ end
37
+
38
+ # Get a version from a git ref
39
+ #
40
+ # Parameters::
41
+ # * *git_ref* (String): The git ref
42
+ # Result::
43
+ # * String: Corresponding version
44
+ def self.version_from_git_ref(git_ref)
45
+ if git_ref.nil?
46
+ '0.0.0'
47
+ elsif git_ref =~ /^v(\d+\.\d+\.\d+)$/
48
+ $1
49
+ else
50
+ raise "Can't assume version from git ref: #{git_ref}"
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ # Get a pre-release metadata to be appended to a version
57
+ #
58
+ # Result::
59
+ # * String: The pre-release metadata
60
+ def self.pre_release_metadata
61
+ "#{`whoami`.strip}-#{Time.now.utc.strftime('%Y%m%d%H%M%S')}-SNAPSHOT"
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -1,5 +1,5 @@
1
1
  module SemVerComponents
2
2
 
3
- VERSION = '0.1.2'
3
+ VERSION = '0.3.0'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sem_ver_components
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Muriel Salvan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-15 00:00:00.000000000 Z
11
+ date: 2021-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: git
@@ -62,12 +62,16 @@ extensions: []
62
62
  extra_rdoc_files: []
63
63
  files:
64
64
  - bin/sem_ver_git
65
+ - lib/sem_ver_components/git_hosting.rb
66
+ - lib/sem_ver_components/git_hostings/bitbucket.rb
67
+ - lib/sem_ver_components/git_hostings/github.rb
65
68
  - lib/sem_ver_components/local_git.rb
66
69
  - lib/sem_ver_components/output.rb
67
70
  - lib/sem_ver_components/outputs/info.rb
68
71
  - lib/sem_ver_components/outputs/semantic_release_analyze.rb
69
72
  - lib/sem_ver_components/outputs/semantic_release_generate_notes.rb
70
73
  - lib/sem_ver_components/plugins.rb
74
+ - lib/sem_ver_components/semver.rb
71
75
  - lib/sem_ver_components/version.rb
72
76
  homepage: https://github.com/Muriel-Salvan/sem_ver_components
73
77
  licenses: