git_reporting 1.0.0

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: 91b9f49ba3d2b69928b8dba4e18fbf53f8c9ef8d
4
+ data.tar.gz: 18e165d4fc7d5ca67f7a7e29fc5ae55c522d7ec3
5
+ SHA512:
6
+ metadata.gz: c5f9c345e79db0b9af8e791efb652fc8b7a8938d32dbcc85b1e2768aa5d1cdc26eadca95a5dce39dc0ad4a8905a4a963a6d4302b47ef03453e92c8bdcfda771c
7
+ data.tar.gz: b4e7799f72df6ae9d125ed0f011fd0c4eb3f8d318a49e56de76a463243f06cc0583b192105e3f4f2ab4ce440eb41bf943d07c1842031ed0e035bcab3900043f7
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in git_reporting.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 bsboris
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # GitReporting
2
+
3
+ [![Code Climate](https://codeclimate.com/github/bsboris/git_reporting/badges/gpa.svg)](https://codeclimate.com/github/bsboris/git_reporting)
4
+
5
+ reporter = GitReporting::Reporter.new(source: :github, login: "...", password: "...", repo: "my/repo")
6
+
7
+ reporter.build_report #=> simple report for all time
8
+ reporter.build_report(type: :timesheet) #=> report per user for all time
9
+ reporter.build_report(type: :timesheet, from: "2015-01-01", to: "2015-01-31") #=> report per user for given period
10
+ reporter.build_report(type: :timesheet, group: :week, from: "2015-01-01", to: "2015-01-31") #=> report per user for given period groupped by week
11
+
12
+ More docs are coming.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.pattern = "test/**/*_test.rb"
7
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'git_reporting/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "git_reporting"
8
+ spec.version = GitReporting::VERSION
9
+ spec.authors = ["bsboris"]
10
+ spec.email = ["bsboris@gmail.com"]
11
+
12
+ spec.summary = %q{Git reporitng.}
13
+ spec.description = %q{Git reporitng.}
14
+ spec.homepage = "https://github.com/bsboris/git_reporting"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test)/}) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "activesupport", "~> 4.2"
21
+ spec.add_dependency "chronic_duration", "~> 0.10"
22
+ spec.add_dependency "octokit", "~> 4.0"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.8"
25
+ spec.add_development_dependency "faker", "~> 1.4"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "vcr", "~> 2.9.3"
28
+ spec.add_development_dependency "webmock", "~> 1.21"
29
+ end
@@ -0,0 +1,64 @@
1
+ require "active_support"
2
+ require "active_support/core_ext"
3
+ require "git_reporting/version"
4
+ require "git_reporting/commit"
5
+ require "git_reporting/report"
6
+ require "git_reporting/source"
7
+
8
+ module GitReporting
9
+ def self.configure
10
+ yield(configuration) if block_given?
11
+ end
12
+
13
+ def self.configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+
17
+ def self.reset_configuration!
18
+ @configuration = Configuration.new
19
+ end
20
+
21
+ class Configuration
22
+ attr_accessor :prefix
23
+
24
+ def initialize
25
+ @prefix = nil
26
+ end
27
+ end
28
+
29
+ class Reporter
30
+ attr_reader :source
31
+
32
+ def initialize(source_options)
33
+ @source = case source_options
34
+ when Source then source_options
35
+ when Array then Source::Array.new(source_options)
36
+ when Hash then Source.from_options(source_options)
37
+ else
38
+ raise ArgumentError, "You should initialize GitReporting::Reporter with array, hash of source options or GitReporting::Source instance."
39
+ end
40
+ end
41
+
42
+ def build_report(options = {})
43
+ type = options[:type] || :simple
44
+ group = options[:group] || :none
45
+ from_time = options[:from].present? ? parse_time(options[:from]) : Time.at(0)
46
+ to_time = options[:to].present? ? parse_time(options[:to]) : Time.current
47
+
48
+ commits = source.fetch(from_time..to_time)
49
+
50
+ root = Report.new("Report")
51
+ root << Report::Group.const_get(group.to_s.capitalize).build(commits) do |group_commits|
52
+ Report::Type.const_get(type.to_s.capitalize).build(group_commits)
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def parse_time(time)
59
+ DateTime.parse(time.to_s)
60
+ rescue ArgumentError
61
+ raise ArgumentError, "#{time} is not a valid date"
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,26 @@
1
+ require "git_reporting/message_parser"
2
+
3
+ module GitReporting
4
+
5
+ class Commit
6
+ attr_accessor :sha, :author, :message, :time, :timestamp
7
+
8
+ def initialize(attrs = {})
9
+ @time = 0
10
+ attrs.each do |key, value|
11
+ send "#{key}=", value
12
+ end
13
+ end
14
+
15
+ def message=(value)
16
+ parsed = MessageParser.parse(value)
17
+ @message = parsed.first
18
+ @time = parsed.last if parsed.last.present?
19
+ end
20
+
21
+ def to_s
22
+ "Commit #{sha}"
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,43 @@
1
+ require "chronic_duration"
2
+
3
+ module GitReporting
4
+ class MessageParser
5
+ attr_reader :input, :regexp
6
+
7
+ def self.parse(*args)
8
+ new(*args).parse
9
+ end
10
+
11
+ def initialize(input)
12
+ @input = input.to_s
13
+ prefix = GitReporting.configuration.prefix
14
+ @regexp = /\[\s*#{prefix && prefix + "\s*"}\s*(\d[^\[\]]*)\]/m
15
+ end
16
+
17
+ def parse
18
+ message = parse_message
19
+ time = parse_time
20
+ [message, time]
21
+ end
22
+
23
+ private
24
+
25
+ def parse_message
26
+ input.gsub(regexp, "").strip
27
+ end
28
+
29
+ def parse_time
30
+ times = input.scan(regexp)
31
+ time = if times[0]
32
+ times[0][0] # we want to take only first time marker
33
+ else
34
+ nil
35
+ end
36
+ return nil unless time.present?
37
+
38
+ time = "#{time}:00" if time =~ /^\d{1,2}:\d{1,2}$/
39
+
40
+ ChronicDuration.parse(time, keep_zero: true)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,61 @@
1
+ require "git_reporting/report/commit"
2
+ require "git_reporting/report/type"
3
+ require "git_reporting/report/group"
4
+
5
+ module GitReporting
6
+ class Report
7
+ attr_reader :key, :children
8
+
9
+ def self.new_from_collection(collection)
10
+ raise ArgumentError, "#{collection.inspect} is not a collection" unless collection.respond_to?(:each)
11
+
12
+ collection.map { |item| new(item) }
13
+ end
14
+
15
+ def self.build(commits)
16
+ hash = Hash.new { |h, k| h[k] = [] }
17
+ commits.each { |commit| hash[key_for_commit(commit)] << commit }
18
+ hash.map do |key, commits|
19
+ new(key) << (block_given? ? yield(commits) : Commit.new_from_collection(commits))
20
+ end
21
+ end
22
+
23
+ def self.key_for_commit(commit)
24
+ nil
25
+ end
26
+
27
+ def initialize(key = nil)
28
+ @key = key
29
+ @children = []
30
+ end
31
+
32
+ def append_child(child)
33
+ if child.respond_to?(:each)
34
+ child.each { |c| self.append_child(c) }
35
+ else
36
+ raise ArgumentError, "Attempting to add a nil node" unless child
37
+ raise ArgumentError, "Only Report and its descendants can be added as a children" unless child.is_a?(Report)
38
+ raise ArgumentError, "Attempting add node to itself" if self.equal?(child)
39
+
40
+ children << child
41
+ end
42
+ self
43
+ end
44
+ alias << append_child
45
+
46
+ def time
47
+ @time ||= children.inject(0) { |time, child| time + child.time }
48
+ end
49
+
50
+ def to_s
51
+ "#{self.class.name} : #{key} : #{time}"
52
+ end
53
+
54
+ def print
55
+ puts to_s
56
+ children.each do |child|
57
+ child.print
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,11 @@
1
+ module GitReporting
2
+ class Report
3
+
4
+ class Commit < Report
5
+ def time
6
+ key.time
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,37 @@
1
+ module GitReporting
2
+ class Report
3
+
4
+ module Group
5
+ class Base < Report
6
+ end
7
+
8
+ class None < Base
9
+ end
10
+
11
+ class Day < Base
12
+ def self.key_for_commit(commit)
13
+ commit.timestamp.to_date
14
+ end
15
+ end
16
+
17
+ class Week < Base
18
+ def self.key_for_commit(commit)
19
+ commit.timestamp.beginning_of_week
20
+ end
21
+ end
22
+
23
+ class Month < Base
24
+ def self.key_for_commit(commit)
25
+ commit.timestamp.beginning_of_month
26
+ end
27
+ end
28
+
29
+ class Year < Base
30
+ def self.key_for_commit(commit)
31
+ commit.timestamp.beginning_of_year
32
+ end
33
+ end
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,19 @@
1
+ module GitReporting
2
+ class Report
3
+
4
+ module Type
5
+ class Base < Report
6
+ end
7
+
8
+ class Simple < Base
9
+ end
10
+
11
+ class Timesheet < Base
12
+ def self.key_for_commit(commit)
13
+ commit.author
14
+ end
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+ require "git_reporting/source/array"
2
+ require "git_reporting/source/github"
3
+
4
+ module GitReporting
5
+ module Source
6
+ def self.from_options(options = {})
7
+ source = options.delete(:source)
8
+ const_get(source.to_s.capitalize).new(options)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,32 @@
1
+ module GitReporting
2
+ module Source
3
+
4
+ class Array
5
+ attr_reader :commits
6
+
7
+ def initialize(commits_array)
8
+ @commits = extract_commits_from_array(commits_array)
9
+ sort_commits!
10
+ end
11
+
12
+ def fetch(period)
13
+ commits.select { |commit| period === commit.timestamp }
14
+ end
15
+
16
+ def fetch_all
17
+ commits
18
+ end
19
+
20
+ private
21
+
22
+ def extract_commits_from_array(commits_array)
23
+ commits_array.map { |commit_hash| Commit.new(commit_hash) }
24
+ end
25
+
26
+ def sort_commits!
27
+ @commits = @commits.sort_by { |commit| commit.timestamp }
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,41 @@
1
+ require "octokit"
2
+
3
+ module GitReporting
4
+ module Source
5
+
6
+ class Github
7
+ attr_reader :client, :repo, :branch
8
+
9
+ def initialize(options = {})
10
+ if options[:repo]
11
+ @repo = options.delete(:repo)
12
+ else
13
+ raise ArgumentError, "You must specify repo for GitHub source"
14
+ end
15
+ @branch = options.delete(:branch) || :master
16
+ options[:auto_paginate] ||= true
17
+ @client = Octokit::Client.new(options)
18
+ end
19
+
20
+ def fetch_all
21
+ extract_commits_from_array client.commits(repo, branch)
22
+ end
23
+
24
+ def fetch(period)
25
+ extract_commits_from_array client.commits_between(repo, period.begin, period.end, sha: branch)
26
+ end
27
+
28
+ private
29
+
30
+ def extract_commits_from_array(commits_array)
31
+ commits_array.map do |data|
32
+ Commit.new sha: data.sha,
33
+ author: data.author.login,
34
+ message: data.commit.message,
35
+ timestamp: data.commit.author.date
36
+ end
37
+ end
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module GitReporting
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git_reporting
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - bsboris
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: chronic_duration
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.10'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: octokit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.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.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faker
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: vcr
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 2.9.3
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 2.9.3
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.21'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.21'
125
+ description: Git reporitng.
126
+ email:
127
+ - bsboris@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - Gemfile
134
+ - LICENSE.txt
135
+ - README.md
136
+ - Rakefile
137
+ - git_reporting.gemspec
138
+ - lib/git_reporting.rb
139
+ - lib/git_reporting/commit.rb
140
+ - lib/git_reporting/message_parser.rb
141
+ - lib/git_reporting/report.rb
142
+ - lib/git_reporting/report/commit.rb
143
+ - lib/git_reporting/report/group.rb
144
+ - lib/git_reporting/report/type.rb
145
+ - lib/git_reporting/source.rb
146
+ - lib/git_reporting/source/array.rb
147
+ - lib/git_reporting/source/github.rb
148
+ - lib/git_reporting/version.rb
149
+ homepage: https://github.com/bsboris/git_reporting
150
+ licenses:
151
+ - MIT
152
+ metadata: {}
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 2.4.5
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: Git reporitng.
173
+ test_files: []