caretaker-core 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+
6
+ This changelog was automatically generated using [Caretaker](https://github.com/DevelopersToolbox/caretaker) by [Wolf Software](https://github.com/WolfSoftware)
7
+
8
+ ### [v0.1.0](https://github.com/DevelopersToolbox/positronic-core/releases/v0.1.0)
9
+
10
+ > Released on March, 3rd 2021
11
+
12
+ - The initial commit [`[head]`](https://github.com/DevelopersToolbox/positronic-core/commit/)
13
+
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in slackit.gemspec
6
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,25 @@
1
+ The MIT License (MIT)
2
+ =====================
3
+
4
+ Copyright © `2009-2021` `Wolf Software Limited`
5
+
6
+ Permission is hereby granted, free of charge, to any person
7
+ obtaining a copy of this software and associated documentation
8
+ files (the “Software”), to deal in the Software without
9
+ restriction, including without limitation the rights to use,
10
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the
12
+ Software is furnished to do so, subject to the following
13
+ conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,207 @@
1
+ <h1 align="center">
2
+ <a href="https://github.com/WolfSoftware">
3
+ <img src="https://raw.githubusercontent.com/WolfSoftware/branding/master/images/general/banners/64/black-and-white.png" alt="Wolf Software Logo" />
4
+ </a>
5
+ <br>
6
+ caretaker-core
7
+ </h1>
8
+
9
+ <p align="center">
10
+ <a href="https://travis-ci.com/DevelopersToolbox/caretaker-core">
11
+ <img src="https://img.shields.io/travis/com/DevelopersToolbox/caretaker-core/master?style=for-the-badge&logo=travis" alt="Build Status">
12
+ </a>
13
+ <a href="https://github.com/DevelopersToolbox/caretaker-core/releases/latest">
14
+ <img src="https://img.shields.io/github/v/release/DevelopersToolbox/caretaker-core?color=blue&style=for-the-badge&logo=github&logoColor=white&label=Latest%20Release" alt="Release">
15
+ </a>
16
+ <a href="https://github.com/DevelopersToolbox/caretaker-core/releases/latest">
17
+ <img src="https://img.shields.io/github/commits-since/DevelopersToolbox/caretaker-core/latest.svg?color=blue&style=for-the-badge&logo=github&logoColor=white" alt="Commits since release">
18
+ </a>
19
+ <a href="LICENSE.md">
20
+ <img src="https://img.shields.io/badge/license-MIT-blue?style=for-the-badge&logo=read-the-docs&logoColor=white" alt="Software License">
21
+ </a>
22
+ <br>
23
+ <a href=".github/CODE_OF_CONDUCT.md">
24
+ <img src="https://img.shields.io/badge/Code%20of%20Conduct-blue?style=for-the-badge&logo=read-the-docs&logoColor=white" />
25
+ </a>
26
+ <a href=".github/CONTRIBUTING.md">
27
+ <img src="https://img.shields.io/badge/Contributing-blue?style=for-the-badge&logo=read-the-docs&logoColor=white" />
28
+ </a>
29
+ <a href=".github/SECURITY.md">
30
+ <img src="https://img.shields.io/badge/Report%20Security%20Concern-blue?style=for-the-badge&logo=read-the-docs&logoColor=white" />
31
+ </a>
32
+ <a href=".github/SUPPORT.md">
33
+ <img src="https://img.shields.io/badge/Get%20Support-blue?style=for-the-badge&logo=read-the-docs&logoColor=white" />
34
+ </a>
35
+ </p>
36
+
37
+ ## Overview
38
+
39
+ caretaker Core is the brain that makes [Sonny](https://github.com/DevelopersToolbox/sonny) work, it processes the git log for a given repository and returns all of the required information as a single JSON object.
40
+
41
+ [Sonny](https://github.com/DevelopersToolbox/sonny) makes use of this object in order to dynamically build a CHANGELOG.md file for the given project.
42
+
43
+ ## Installation
44
+
45
+ Add this line to your application's Gemfile:
46
+
47
+ ```ruby
48
+ gem 'caretaker-core'
49
+ ```
50
+
51
+ And then execute:
52
+
53
+ $ bundle install
54
+
55
+ Or install it yourself as:
56
+
57
+ $ gem install caretaker-core
58
+
59
+ ## Usage
60
+
61
+ The following is a very [simple snippet](testing/get-raw.rb) showing how to integrate the core into your own code. The key is the single call to [caretakerCore.run](lib/caretaker-core.rb#L20).
62
+
63
+ ```ruby
64
+ #!/usr/bin/env ruby
65
+
66
+ require 'json'
67
+ require 'caretaker-core'
68
+
69
+ begin
70
+ results = caretakerCore.run
71
+ rescue StandardError => e
72
+ puts e.message
73
+ exit
74
+ end
75
+
76
+ puts JSON.pretty_generate(JSON.parse(results))
77
+ ```
78
+
79
+ ### Output
80
+
81
+ The output from [caretakerCore.run](lib/caretaker-core.rb#L20) is a JSON formatted object. In the case of an error it will raise a `StandardError`.
82
+
83
+ The basic structure of the JSON is as follows:
84
+
85
+ ```yaml
86
+ {
87
+ "tags": [ ],
88
+ "commits": {
89
+ "chronological": { },
90
+ "categorised": { },
91
+ },
92
+ "repo": {
93
+ "url": "",
94
+ "slug": ""
95
+ }
96
+ }
97
+ ```
98
+ * tags - An array of tag names, it will also include 'untagged' for all commits that are not part of a tag.
99
+ * commits - A hash with 2 elements
100
+ * chronological - All commits in chronological order.
101
+ * categorised - All commits split by category.
102
+ * repo - A hash with information relating to the repository
103
+ * url - The full base url to the repo (e.g. https://github.com/DevelopersToolbox/caretaker-core)
104
+ * slug - The github organisation / repository name (e.g. DevelopersToolbox/caretaker-core)
105
+
106
+ A more full example (Showing a single commit)
107
+
108
+ ```yaml
109
+ {
110
+ "tags": [
111
+ "untagged"
112
+ ],
113
+ "commits": {
114
+ "chronological": {
115
+ "untagged": [
116
+ {
117
+ "hash": "11781d3",
118
+ "hash_full": "11781d3cbdac68a003492fc0d318d402dd241579",
119
+ "subject": "The initial commit",
120
+ "extra": false,
121
+ "commit_type": "commit",
122
+ "category": "Uncategorised:",
123
+ "date": "2021-03-03"
124
+ }
125
+ ]
126
+ },
127
+ "categorised": {
128
+ "untagged": {
129
+ "New Features:": [ ],
130
+ "Improvements:": [ ],
131
+ "Bug Fixes:": [ ],
132
+ "Security Fixes:": [ ],
133
+ "Refactor:": [ ],
134
+ "Style:": [ ],
135
+ "Deprecated:": [ ],
136
+ "Removed:": [ ],
137
+ "Tests:": [ ],
138
+ "Documentation:": [ ],
139
+ "Chores:": [ ],
140
+ "Experiments:": [ ],
141
+ "Miscellaneous:": [ ],
142
+ "Uncategorised:": [
143
+ {
144
+ "hash": "11781d3",
145
+ "hash_full": "11781d3cbdac68a003492fc0d318d402dd241579",
146
+ "subject": "The initial commit",
147
+ "extra": false,
148
+ "commit_type": "commit",
149
+ "category": "Uncategorised:",
150
+ "date": "2021-03-03"
151
+ }
152
+ ],
153
+ "Initial Commit:": [ ],
154
+ "Skip:": [ ]
155
+ }
156
+ }
157
+ },
158
+ "repo": {
159
+ "url": "https://github.com/DevelopersToolbox/caretaker-core",
160
+ "slug": "DevelopersToolbox/caretaker-core"
161
+ }
162
+ }
163
+ ```
164
+
165
+ #### Other Values
166
+
167
+ | Name | Purpose | Possible Values |
168
+ | ---- | ------- | --------------- |
169
+ | hash | Stores the short hash for the commit. | 7 character hexidecimal string |
170
+ | hash_full | Stores the full hash for the commit. | 40 character hexodecimal string|
171
+ | commit_message | The commit message message. | Anything alphanumberic |
172
+ | child_commit_messages | The commit messages of any commits that form part of a pull/merge request | Anything alphanumberic or false |
173
+ | commit_type | The type of commit | pr or commit |
174
+ | category | The category the commit belongs to | [Category List](lib/caretaker-core/config.rb#L13) |
175
+ | date | The date the commit was made | YYYY-MM-DD format |
176
+
177
+ > For more information about the use of categories - please refer to the sonny documentation.
178
+
179
+ ### Limitations with regards to Pull Requests
180
+
181
+ 1. Squished commits work as exepected and the contents of the child commmits is available
182
+ 2. Rebased commits show as local commits as there is no way to see where they came from
183
+ 3. Unsquished commits only show the PR commit_message as the child commits are ignored
184
+
185
+ > Our advice is to always use Squished commits.
186
+
187
+ ## Development
188
+
189
+ 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.
190
+
191
+ 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).
192
+
193
+ ## Contributors
194
+
195
+ <p>
196
+ <a href="https://github.com/TGWolf">
197
+ <img src="https://img.shields.io/badge/Wolf-black?style=for-the-badge" />
198
+ </a>
199
+ </p>
200
+
201
+ ## Show Support
202
+
203
+ <p>
204
+ <a href="https://ko-fi.com/wolfsoftware">
205
+ <img src="https://img.shields.io/badge/Ko%20Fi-blue?style=for-the-badge&logo=ko-fi&logoColor=white" />
206
+ </a>
207
+ </p>
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/VERSION.txt ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'caretaker-core'
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'caretaker-core/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'caretaker-core'
8
+ spec.version = CaretakerCore::VERSION
9
+ spec.authors = ['Tim Gurney aka Wolf']
10
+ spec.email = ['wolf@tgwolf.com']
11
+
12
+ spec.summary = 'caretaker-core'
13
+ spec.description = 'caretaker-core'
14
+ spec.homepage = 'https://github.com/DevelopersToolbox/caretaker-core'
15
+ spec.license = 'MIT'
16
+
17
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/DevelopersToolbox/caretaker-core'
21
+ spec.metadata['changelog_uri'] = 'https://github.com/DevelopersToolbox/caretaker-core/blob/master/CHANGELOG.md'
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ # rubocop:disable Metrics/Layout/ExtraSpacing, Layout/SpaceAroundOperators
26
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ end
29
+ # rubocop:enable Metrics/Layout/ExtraSpacing, Layout/SpaceAroundOperators
30
+
31
+ spec.bindir = 'exe'
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ['lib']
34
+
35
+ spec.add_development_dependency 'bundler', '~> 2'
36
+ spec.add_development_dependency 'json', '~> 2'
37
+ spec.add_development_dependency 'rake', '~> 12.3.3'
38
+ spec.add_development_dependency 'rspec', '~> 3.0'
39
+
40
+ spec.add_runtime_dependency 'json', '~> 2'
41
+ end
@@ -0,0 +1,40 @@
1
+ require 'json'
2
+
3
+ #
4
+ # We will need to change this when we restrcuture the code
5
+ #
6
+ require 'caretaker-core/core'
7
+ require 'caretaker-core/version'
8
+
9
+ #
10
+ # Docs to go here
11
+ #
12
+ class CaretakerCore
13
+ #
14
+ # Docs to go here
15
+ #
16
+ class << self
17
+ #
18
+ # Docs to go here
19
+ #
20
+ def run(options = {})
21
+ # Will throw an exception if not a git repo
22
+ check_for_git_repo
23
+
24
+ # Set the initial config and handle user supplied options
25
+ config = init_config(options)
26
+
27
+ # Get URL and slug for the repo, will throw and exception if no remote origin is set
28
+ repository, slug = git_repo_details
29
+
30
+ # Add to config for easy access
31
+ config[:repo_url] = repository
32
+
33
+ # Really do the processing
34
+ tags, chronological, categorised = real_process(config)
35
+
36
+ # Return the data to the calling function
37
+ { :tags => tags, :commits => { :chronological => chronological, :categorised => categorised }, :repo => { :url => repository, :slug => slug } }.to_json
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ #
2
+ # Description should go here
3
+ #
4
+ class CaretakerCore
5
+ #
6
+ # We want to create everything as a class/static method
7
+ #
8
+ class << self
9
+ # Class Constants
10
+ INITIAL_TAG = 'untagged'.freeze
11
+
12
+ DEFAULT_CATEGORY = 'Uncategorised:'.freeze
13
+ CATEGORIES = {
14
+ 'New Features:' => [ 'new feature:', 'new:', 'feature:' ],
15
+ 'Improvements:' => [ 'improvement:', 'improve:' ],
16
+ 'Bug Fixes:' => [ 'bug fix:', 'bug:', 'bugs:' ],
17
+ 'Security Fixes:' => [ 'security: '],
18
+ 'Refactor:' => [ ],
19
+ 'Style:' => [ ],
20
+ 'Deprecated:' => [ ],
21
+ 'Removed:' => [ 'deleted:' ],
22
+ 'Tests:' => [ 'test:', 'testing:' ],
23
+ 'Documentation:' => [ 'docs: ' ],
24
+ 'Chores:' => [ 'chore:' ],
25
+ 'Experiments:' => [ 'experiment:' ],
26
+ 'Miscellaneous:' => [ 'misc:' ],
27
+ 'Uncategorised:' => [ 'no category:' ],
28
+ 'Initial Commit:' => [ 'initial:' ],
29
+ 'Skip:' => [ 'ignore:' ]
30
+ }.freeze
31
+ DEFAULT_CONFIG = {
32
+ :enable_categories => false,
33
+ :remove_categories => true
34
+ }.freeze
35
+ end
36
+ end
@@ -0,0 +1,148 @@
1
+ require 'date'
2
+ require 'open3'
3
+ require 'uri'
4
+
5
+ require_relative 'config'
6
+ require_relative 'git'
7
+ require_relative 'process'
8
+ require_relative 'tags'
9
+ require_relative 'utils'
10
+
11
+ # This is the core file
12
+ # @author Wolf
13
+ class CaretakerCore
14
+ #
15
+ # We want to create everything as a class/static method
16
+ #
17
+ class << self
18
+ #
19
+ # Make everything else private so it cannot be accessed directly
20
+ #
21
+
22
+ private
23
+
24
+ #
25
+ # Setup the require global configuration
26
+ #
27
+ def init_config(options)
28
+ # Assign the default config
29
+ config = DEFAULT_CONFIG
30
+
31
+ # Merge in any user supplied options/config and return
32
+ config.merge(options)
33
+ end
34
+
35
+ def real_process(config)
36
+ log_entries = retrieve_git_log_entries
37
+
38
+ return [ [], [], [] ] unless log_entries
39
+
40
+ parsed = parse_git_log_entries(config, log_entries)
41
+
42
+ tags = generate_tag_list(parsed)
43
+
44
+ chronological, categorised = process_results(parsed)
45
+
46
+ [tags, chronological, categorised]
47
+ end
48
+
49
+ #
50
+ # Limitations with regards to Pull Requests
51
+ #
52
+ # 1. Squished commits work as exepected and the contents of the child commmits is available
53
+ # 2. Rebased commits show as local commits as there is no way to see where they came from
54
+ # 3. Unsquished commits only show the PR commit_message as the child commits are ignored
55
+ #
56
+ # This method reeks of :reek:TooManyStatements { max_statements: 6 }
57
+ def parse_git_log_entries(config, log_entries)
58
+ logs = {}
59
+ tag = INITIAL_TAG
60
+
61
+ log_entries.each do |line|
62
+ tag, data = process_single_line(config, line, tag)
63
+
64
+ (logs[tag] ||= []) << data if data
65
+ end
66
+ logs
67
+ end
68
+
69
+ def get_parts(line, tag)
70
+ date, hash, hash_full, commit_message, tag = split_log_entries(line, tag)
71
+ commit_message, child_commit_messages, commit_type = process_commit_message(commit_message, hash)
72
+ category = get_category(commit_message)
73
+
74
+ [hash, hash_full, commit_message, child_commit_messages, commit_type, category, date, tag]
75
+ end
76
+
77
+ def process_single_line(config, line, tag)
78
+ hash, hash_full, commit_message, child_commit_messages, commit_type, category, date, tag = get_parts(line, tag)
79
+
80
+ return false if category.downcase == 'skip:'
81
+
82
+ commit_message = commit_message.sub(/.*?:/, '').strip if config[:remove_categories]
83
+
84
+ data = { :hash => hash, :hash_full => hash_full, :commit_message => commit_message, :child_commit_messages => child_commit_messages, :commit_type => commit_type, :category => category, :date => date }
85
+
86
+ [tag, data]
87
+ end
88
+
89
+ # This method reeks of :reek:DuplicateMethodCall, :reek:NestedIterators
90
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
91
+ def get_category(commit_message)
92
+ commit_message_downcase = commit_message.downcase
93
+
94
+ # Does it match a primary category ?
95
+ category = CATEGORIES.select { |key, _values| commit_message_downcase.start_with?(key.downcase) }.map(&:first).join
96
+
97
+ # If not a primary category, does it match one of the aliases?
98
+ category = CATEGORIES.select { |_key, values| values.any? { |value| commit_message_downcase.start_with?(value.downcase) } }.map(&:first).join if category.empty?
99
+
100
+ # If in doubt set to the default
101
+ category = DEFAULT_CATEGORY if category.empty?
102
+
103
+ category
104
+ end
105
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
106
+
107
+ def get_pr_number(all_matches)
108
+ all_matches.to_a.compact.pop
109
+ end
110
+
111
+ def generate_pr_link(matches)
112
+ pr_number = get_pr_number(matches)
113
+
114
+ message = "Merge pull request ##{pr_number}"
115
+ [message, pr_number]
116
+ end
117
+
118
+ def process_merge_request(merge_message, hash, patterns)
119
+ matches = merge_message.match(patterns).captures
120
+
121
+ new_message = generate_pr_link(matches)
122
+
123
+ child_commit_messages = get_child_messages(hash)
124
+
125
+ [new_message, child_commit_messages]
126
+ end
127
+
128
+ def process_normal_commit(commit_message)
129
+ commit_type = :commit
130
+ [commit_message, commit_type]
131
+ end
132
+
133
+ def process_commit_message(commit_message, hash)
134
+ patterns_array = [/Merge pull request #(\d+).*/, /\.*\(#(\d+)\)*\)/]
135
+ patterns = Regexp.union(patterns_array)
136
+
137
+ child_commit_messages = false
138
+ commit_type = :pr
139
+
140
+ if commit_message.scan(/Merge pull request #(\d+).*/m).size.positive? || commit_message.scan(/\.*\(#(\d+)\)*\)/m).size.positive?
141
+ message, child_commit_messages = process_merge_request(commit_message, hash, patterns)
142
+ else
143
+ message, commit_type = process_normal_commit(commit_message)
144
+ end
145
+ [message, child_commit_messages, commit_type]
146
+ end
147
+ end
148
+ end