tracker_git_hook 1.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0c2eada90f17a42b0debf03936a46cb83c033b7f
4
+ data.tar.gz: 6b4e425eccc6cb3bd2364531b038230cd9de58cf
5
+ SHA512:
6
+ metadata.gz: 20487b9a032c334e005b9f2daaf1d8260f7b7711e69a026b231d7f25b1aa9bd7c82d6acdb58582c69a22b7e7f66ed409f10283454de8a92ad27a28a847517023
7
+ data.tar.gz: dfc9b2b1f58c2127d596a648ae823b8ce4176e596069d5eeffd31d0a98fafe1e98f028da5d113f88ddb5e9b3aaba65eabdf64b449c6bb034439243b05e0c600f
data/bin/story ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'tracker_git_hook/installer'
4
+ require 'tracker_git_hook/repo'
5
+ require 'tracker_git_hook/cli'
6
+
7
+ repo = TrackerGitHook::Repo.discover(path: Dir.pwd)
8
+ TrackerGitHook::Installer.new(repo: repo).install
9
+
10
+ TrackerGitHook::Cli.new(repo: repo).process_arguments(*ARGV)
@@ -0,0 +1,63 @@
1
+ module TrackerGitHook
2
+ class Cli
3
+ def initialize(repo:)
4
+ @repo = repo
5
+ end
6
+
7
+ attr_reader :repo
8
+
9
+ def process_arguments(*args)
10
+ argument = args[0]
11
+
12
+ if argument
13
+ process_argument(argument)
14
+ else
15
+ puts repo.current_story_id
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def process_argument(argument)
22
+ if story_id?(argument)
23
+ repo.current_story_id = argument
24
+ elsif argument == 'finish'
25
+ repo.clear_story_id
26
+ elsif argument == 'whale'
27
+ puts whale
28
+ else
29
+ puts usage
30
+ end
31
+ end
32
+
33
+ def usage
34
+ 'Usage: story [ <story_id> | finish ]'
35
+ end
36
+
37
+ def whale
38
+ <<-'WHALE'
39
+
40
+ __________...----..____..-'``-..___
41
+ ,'. ```--.._
42
+ : ``._
43
+ | -- ``.
44
+ | ಠ -. - -. `.
45
+ : __ -- . \
46
+ `._____________ ( `. -.- -- - . ` \
47
+ `-----------------\ \_.--------..__..--.._ `. `. :
48
+ `--' `-._ . |
49
+ `.` |
50
+ \` |
51
+ \ |
52
+ / \`.
53
+ / _\-'
54
+ /_,'
55
+
56
+ WHALE
57
+ end
58
+
59
+ def story_id?(string)
60
+ !!/^\d+$/.match(string)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,14 @@
1
+ require 'tracker_git_hook/repo'
2
+ require 'tracker_git_hook/story_status_checker'
3
+
4
+ module TrackerGitHook
5
+ class CommitMsg
6
+ def run(message_file_path:)
7
+ message = File.read(message_file_path)
8
+
9
+ repo = Repo.discover(path: Dir.pwd)
10
+
11
+ StoryStatusChecker.new(repo: repo).check(message)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,38 @@
1
+ module TrackerGitHook
2
+ class Installer
3
+ def initialize(repo:)
4
+ @repo = repo
5
+ end
6
+
7
+ def install
8
+ @repo.install_hook(AddStoryIdHook.new)
9
+ @repo.install_hook(CheckStoryIdHook.new)
10
+ end
11
+ end
12
+
13
+ class AddStoryIdHook
14
+ def script
15
+ <<-BASH
16
+ /usr/bin/env ruby -e "require 'tracker_git_hook/prepare_commit_msg';
17
+ TrackerGitHook::PrepareCommitMsg.new.prepare message_file_path: ARGV[0]" $1
18
+ BASH
19
+ end
20
+
21
+ def filename
22
+ 'prepare-commit-msg'
23
+ end
24
+ end
25
+
26
+ class CheckStoryIdHook
27
+ def script
28
+ <<-BASH
29
+ /usr/bin/env ruby -e "require 'tracker_git_hook/commit_msg';
30
+ TrackerGitHook::CommitMsg.new.run(message_file_path: ARGV[0])" $1
31
+ BASH
32
+ end
33
+
34
+ def filename
35
+ 'commit-msg'
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ module TrackerGitHook
2
+ class MessageGenerator
3
+ def generate(original_message:, story_id:)
4
+ if message_is_tagged?(original_message)
5
+ original_message
6
+ else
7
+ message_lines = original_message.split("\n")
8
+
9
+ new_message = insert_into_message(message_lines, "[##{story_id}]")
10
+
11
+ new_message.join("\n")
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def message_is_tagged?(original_message)
18
+ original_message.match(/\[.*#\d*\]/)
19
+ end
20
+
21
+ def insert_into_message(message_lines, story_line)
22
+ split_index = message_lines.find_index(&method(:info_line?))
23
+
24
+ if split_index
25
+ message_body = message_lines[0..split_index - 1]
26
+ message_info = message_lines[split_index..-1]
27
+ message_body << [story_line, ''] << message_info << ['']
28
+ else
29
+ message_lines << [story_line, '']
30
+ end
31
+ end
32
+
33
+ def info_line?(line)
34
+ line.start_with?('#')
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,27 @@
1
+ require 'tracker_git_hook/message_generator'
2
+ require 'tracker_git_hook/repo'
3
+
4
+ module TrackerGitHook
5
+ class PrepareCommitMsg
6
+ def prepare(message_file_path:)
7
+ return unless story_id
8
+
9
+ original_message = File.read(message_file_path)
10
+
11
+ message = MessageGenerator.new.generate(
12
+ original_message: original_message,
13
+ story_id: story_id
14
+ )
15
+
16
+ File.open(message_file_path, 'w') do |f|
17
+ f.write(message)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def story_id
24
+ Repo.discover(path: Dir.pwd).current_story_id
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,56 @@
1
+ module TrackerGitHook
2
+ class Repo
3
+ def self.discover(path:)
4
+ git_directory = File.join(path, '.git')
5
+
6
+ if File.exist?(git_directory)
7
+ Repo.new(root_path: path)
8
+ else
9
+ parent_path = File.dirname(path)
10
+
11
+ fail 'Not in a git repo' if parent_path == path
12
+
13
+ discover(path: parent_path)
14
+ end
15
+ end
16
+
17
+ attr_reader :root_path
18
+
19
+ def initialize(root_path:)
20
+ @root_path = root_path
21
+ end
22
+
23
+ def current_story_id=(story_id)
24
+ File.open(story_file_path, 'w') do |f|
25
+ f.write(story_id)
26
+ end
27
+ end
28
+
29
+ def current_story_id
30
+ return unless File.exist?(story_file_path)
31
+
32
+ story_id = File.read(story_file_path)
33
+
34
+ story_id unless story_id.empty?
35
+ end
36
+
37
+ def clear_story_id
38
+ self.current_story_id = nil
39
+ end
40
+
41
+ def install_hook(hook)
42
+ hook_path = File.join(root_path, '.git', 'hooks', hook.filename)
43
+
44
+ File.open(hook_path, 'w') do |f|
45
+ f.write(hook.script)
46
+ f.chmod(0755)
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def story_file_path
53
+ File.join(root_path, '.git', 'tracker_story_id')
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,41 @@
1
+ module TrackerGitHook
2
+ class StoryStatusChecker
3
+ def initialize(repo:)
4
+ @repo = repo
5
+ end
6
+
7
+ def check(message)
8
+ if contains_completion_keyword?(message) && contains_current_story_id?(message)
9
+ repo.clear_story_id
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ attr_reader :repo
16
+
17
+ def contains_completion_keyword?(message)
18
+ word = parse_message(message)[1]
19
+ return false unless word
20
+
21
+ completion_keywords.any? do |keyword|
22
+ word.downcase.include? keyword
23
+ end
24
+ end
25
+
26
+ def completion_keywords
27
+ %w(finish fix complete)
28
+ end
29
+
30
+ def contains_current_story_id?(message)
31
+ message_story_ids = parse_message(message)[2]
32
+
33
+ message_story_ids.include? repo.current_story_id
34
+ end
35
+
36
+ def parse_message(message)
37
+ regex = /\[\s*(\w*)\s*((#\d+\s*)+)\]/
38
+ regex.match(message) || []
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tracker_git_hook
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - August Toman-Yih
8
+ - Matt Hess
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-03-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '3'
28
+ description: Simple command-line tool to tag stories from Pivotal Tracker in git commits.
29
+ Uses git hooks.
30
+ email: august.toman.yih@gmail.com
31
+ executables:
32
+ - story
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - bin/story
37
+ - lib/tracker_git_hook/cli.rb
38
+ - lib/tracker_git_hook/commit_msg.rb
39
+ - lib/tracker_git_hook/installer.rb
40
+ - lib/tracker_git_hook/message_generator.rb
41
+ - lib/tracker_git_hook/prepare_commit_msg.rb
42
+ - lib/tracker_git_hook/repo.rb
43
+ - lib/tracker_git_hook/story_status_checker.rb
44
+ homepage: https://github.com/atomanyih/tracker
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '2'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.4.5
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Simple command-line tool to tag stories from Pivotal Tracker in git commits
68
+ test_files: []