git_agent 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 768908461e4a78cb35911f5ffc66922cd9191877
4
+ data.tar.gz: fb58ff6e9514210651bc61eb66a1ab15cf6cb4ab
5
+ SHA512:
6
+ metadata.gz: c8e692289f753553e6854f9b5dcc23b44c0814a6b0aeaf2290bf63932ce1cc00b050e8325f7e3c35ec94f0f7c45aba6c72cdc9423458862f9880c8d827b63a06
7
+ data.tar.gz: 979066cbccdcd01f190e5633aeff6defb6bd699cde9b1d39ae6db8830c2b2a7b87f5cac7d0b5f05398d7b8d62604d85376770d66716d03f9978c4f806735f567
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ data/
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ test/data
19
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in git_agent.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Sophy Eung
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # GitAgent
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'git_agent'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install git_agent
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
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.pattern = './test/lib/**/*_test.rb'
6
+ end
7
+ task default: :test
data/bin/check_repo ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path('../../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'optparse'
7
+ require 'ostruct'
8
+ require 'git_agent'
9
+
10
+ options = OpenStruct.new
11
+
12
+ OptionParser.new do |opts|
13
+ opts.banner = 'Usage: monitor_git_repo [options]'
14
+
15
+ opts.on('-t', '--repository REPO', 'Repository to monitor') do |repo|
16
+ options.repo = repo
17
+ end
18
+
19
+ opts.on('-s', '--sender-email SENDER_EMAIL', 'Sender email address') do |sender_email|
20
+ options.sender_email = sender_email
21
+ end
22
+
23
+ opts.on('-p', '--sender-password SENDER_PASSWORD', 'Sender password') do |sender_password|
24
+ options.sender_password = sender_password
25
+ end
26
+
27
+ opts.on('-r', '--recipient-email RECIPIENT_EMAIL', 'Recipient email address') do |recipient_email|
28
+ options.recipient_email = recipient_email
29
+ end
30
+
31
+ opts.on('-h', '--homepage [HOMEPAGE]', 'Project home page') do |homepage|
32
+ options.homepage = homepage
33
+ end
34
+ end.parse!
35
+
36
+ GitAgent.data_directory = File.expand_path('../../data', __FILE__)
37
+ GitAgent::Notification.config = {
38
+ sender_username: options.sender_email,
39
+ sender_password: options.sender_password,
40
+ recipient_username: options.recipient_email
41
+ }
42
+
43
+ GitAgent.new(git: options.repo, homepage: options.homepage).notify_if_different!
data/git_agent.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'git_agent/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "git_agent"
8
+ spec.version = GitAgent::VERSION
9
+ spec.authors = ["Sophy Eung"]
10
+ spec.email = ["ungsophy@gmail.com"]
11
+ spec.description = "A command line utility to check git repositories"
12
+ spec.summary = "A command line utility to check git repositories"
13
+ spec.homepage = "https://github.com/ungsophy/git_agent"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "mail", "~> 2.5.4"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "minitest", "~> 5.2.0"
26
+ spec.add_development_dependency "mocha", "~> 1.0.0"
27
+ end
@@ -0,0 +1,73 @@
1
+ require 'mail'
2
+
3
+ class GitAgent
4
+ class Notification
5
+
6
+ class << self
7
+ attr_accessor :config
8
+
9
+ def config=(config)
10
+ @config = config
11
+
12
+ Mail.defaults do
13
+ delivery_method :smtp,
14
+ address: 'smtp.gmail.com',
15
+ port: 587,
16
+ authentication: 'plain',
17
+ user_name: config[:sender_username],
18
+ password: config[:sender_password]
19
+ end
20
+ end
21
+ end
22
+
23
+ attr_reader :diff, :title, :homepage
24
+
25
+ def initialize(title, homepage = nil, diff = {})
26
+ assert_config!
27
+
28
+ @title = title
29
+ @diff = diff
30
+ @homepage = homepage
31
+ end
32
+
33
+ def send!
34
+ mail = Mail.new
35
+ mail.from = config[:sender_username]
36
+ mail.to = config[:recipient_username]
37
+ mail.subject = title
38
+ mail.body = construct_body
39
+ mail.content_type = 'text/html; charset=UTF-8'
40
+
41
+ mail.deliver!
42
+ end
43
+
44
+ private
45
+
46
+ def construct_body
47
+ str = ''
48
+
49
+ diff.each do |key, values|
50
+ next if values.empty?
51
+
52
+ str += "<strong>#{key.capitalize}</strong><br/><ul>"
53
+ values.each { |value| str += "<li>#{value}</li>" }
54
+ str += "</ul><br/>"
55
+ end
56
+
57
+ str = "<a href='#{homepage}'>#{homepage}</a><br/>#{str}" if homepage
58
+ str
59
+ end
60
+
61
+ def assert_config!
62
+ return if Mail.delivery_method.instance_of?(Mail::TestMailer)
63
+
64
+ if !config.key?(:sender_username) || !config.key?(:sender_password) || !config.key?(:recipient_username)
65
+ raise ArgumentError, ':sender_username, :sender_password and :recipient_username must be defined.'
66
+ end
67
+ end
68
+
69
+ def config
70
+ self.class.config
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ class GitAgent
2
+ VERSION = '0.0.1'
3
+ end
data/lib/git_agent.rb ADDED
@@ -0,0 +1,100 @@
1
+ require 'yaml'
2
+ require 'fileutils'
3
+
4
+ require 'git_agent/version'
5
+ require 'git_agent/notification'
6
+
7
+ class GitAgent
8
+ class << self
9
+ attr_accessor :data_directory
10
+ end
11
+
12
+ attr_reader :url, :username, :project, :homepage
13
+
14
+ URLS = {
15
+ github: 'github.com',
16
+ bitbucket: 'bitbucket.org'
17
+ }
18
+
19
+ def initialize(args = {})
20
+ @homepage = args[:homepage]
21
+ @url, @username, @project = resolve_arguments(args)
22
+ end
23
+
24
+ def diff
25
+ old_hash, new_hash = read_from_record, ls_remote
26
+ old_keys, new_keys = old_hash.keys, new_hash.keys
27
+
28
+ same_keys = new_keys & old_keys
29
+ result = {
30
+ added: new_keys - same_keys,
31
+ deleted: old_keys - same_keys,
32
+ updated: []
33
+ }
34
+
35
+ same_keys.inject(result[:updated]) do |memo, key|
36
+ memo << key if old_hash[key] != new_hash[key]
37
+ memo
38
+ end
39
+
40
+ record(new_hash)
41
+
42
+ result
43
+ end
44
+
45
+ def notify_if_different!
46
+ result = diff
47
+
48
+ unless result.values.flatten.empty?
49
+ notification = GitAgent::Notification.new(project, homepage, result)
50
+ notification.send!
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def ls_remote
57
+ result = execute_git_ls_remote.split("\n")
58
+ result.inject({}) do |memo, line|
59
+ arr = line.split("\t")
60
+ memo[arr.last] = arr.first
61
+ memo
62
+ end
63
+ end
64
+
65
+ def read_from_record
66
+ return {} unless Dir.exist?(path)
67
+ File.open(File.join(path, project)) { |file| YAML.load(file.read) }
68
+ end
69
+
70
+ def record(hash)
71
+ FileUtils.mkdir_p(path) unless Dir.exist?(path)
72
+ File.open(File.join(path, project), 'w') { |file| file.write(YAML.dump(hash)) }
73
+ end
74
+
75
+ def path
76
+ @path ||= File.join(GitAgent.data_directory, username)
77
+ end
78
+
79
+ def execute_git_ls_remote
80
+ `git ls-remote #{url}`
81
+ end
82
+
83
+ def resolve_arguments(urls)
84
+ if urls.key?(:git)
85
+ username_and_project = urls[:git].split(':').last.split('/')
86
+ return [urls[:git], username_and_project.first, username_and_project.last]
87
+ end
88
+
89
+ urls = urls.first
90
+ raise ArgumentError, 'Need at least :git, :github or :bitbucket' unless URLS.key?(urls.first)
91
+
92
+ username_and_project = urls.last.split("/")
93
+
94
+ [
95
+ "git@#{URLS[urls.first]}:#{urls.last}.git",
96
+ username_and_project.first,
97
+ "#{username_and_project.last}.git"
98
+ ]
99
+ end
100
+ end
@@ -0,0 +1,33 @@
1
+ require './test/test_helper'
2
+
3
+ describe GitAgent::Notification do
4
+ describe '#initialize' do
5
+ it 'raises ArgumentError when :sender_username, :sender_password, or :recipient_username is not present' do
6
+ GitAgent::Notification.config = { sender_username: '', sender_password: '' }
7
+ -> { GitAgent::Notification.new('sample_project', {}) }.must_raise ArgumentError
8
+
9
+ GitAgent::Notification.config = { sender_password: '', recipient_username: '' }
10
+ -> { GitAgent::Notification.new('sample_project', {}) }.must_raise ArgumentError
11
+
12
+ GitAgent::Notification.config = { recipient_username: '', sender_username: '' }
13
+ -> { GitAgent::Notification.new('sample_project', {}) }.must_raise ArgumentError
14
+ end
15
+ end
16
+
17
+ describe '#send!' do
18
+ before do
19
+ Mail::TestMailer.deliveries.clear
20
+ GitAgent::Notification.config = { sender_username: 'foo@gmail.com',
21
+ sender_password: '123',
22
+ recipient_username: 'bar@gmail.com' }
23
+ Mail.defaults { delivery_method :test }
24
+ end
25
+
26
+ it 'sends an email to recipient' do
27
+ notification = GitAgent::Notification.new('sample_project', { updated: ['refs/heads/master', 'HEAD'] })
28
+ notification.send!
29
+
30
+ Mail::TestMailer.deliveries.length.must_equal 1
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,93 @@
1
+ require './test/test_helper'
2
+
3
+ describe GitAgent do
4
+ let(:path) { 'ungsophy/git_agent' }
5
+ let(:ssh_url) { "git@github.com:#{path}.git" }
6
+ let(:agent) { GitAgent.new(github: path) }
7
+
8
+ describe '#initialize' do
9
+ describe 'when :git is provided' do
10
+ it 'returns :git' do
11
+ agent = GitAgent.new(git: ssh_url)
12
+
13
+ agent.url.must_equal ssh_url
14
+ agent.username.must_equal 'ungsophy'
15
+ agent.project.must_equal 'git_agent.git'
16
+ end
17
+ end
18
+
19
+ describe 'when :github is provided' do
20
+ it 'returns github ssh url' do
21
+ agent.url.must_equal ssh_url
22
+ agent.username.must_equal 'ungsophy'
23
+ agent.project.must_equal 'git_agent.git'
24
+ end
25
+ end
26
+
27
+ describe 'when :bitbucket is provided' do
28
+ it 'returns bitbucket ssh url' do
29
+ agent = GitAgent.new(bitbucket: path)
30
+
31
+ agent.url.must_equal "git@bitbucket.org:#{path}.git"
32
+ agent.username.must_equal 'ungsophy'
33
+ agent.project.must_equal 'git_agent.git'
34
+ end
35
+ end
36
+
37
+ describe 'when invalid key is provided' do
38
+ it 'raises ArgumentError exception' do
39
+ -> { GitAgent.new(foo: '') }.must_raise ArgumentError
40
+ end
41
+ end
42
+ end
43
+
44
+ describe '#diff' do
45
+ it 'returns only different heads and new tags' do
46
+ agent.stubs(:read_from_record).returns({ 'HEAD' => '356a192b7913b04c54574d18c28d46e6395428ab',
47
+ 'refs/heads/master' => '356a192b7913b04c54574d18c28d46e6395428ab',
48
+ 'refs/heads/feature-1' => 'a87fc2afc9a9e219e2db4a16c28c3d3ef1b27b18',
49
+ 'refs/heads/feature-2' => '446ad0db9b22a1e3c0a42b6d8ab5bbd50a311f50' })
50
+
51
+ agent.stubs(:ls_remote).returns({ 'HEAD' => '17ba0791499db908433b80f37c5fbc89b870084b',
52
+ 'refs/heads/master' => '17ba0791499db908433b80f37c5fbc89b870084b',
53
+ 'refs/heads/feature-1' => '1c6637a8f2e1f75e06ff9984894d6bd16a3a36a9',
54
+ 'refs/heads/feature-3' => '755d3a4e0fec40669f760d6f47eac21ad92f846a' })
55
+
56
+ result = agent.diff
57
+
58
+ result[:added].must_equal ['refs/heads/feature-3']
59
+ result[:deleted].must_equal ['refs/heads/feature-2']
60
+ result[:updated].must_equal ['HEAD', 'refs/heads/master', 'refs/heads/feature-1']
61
+ File.exist?(File.join(GitAgent.data_directory, 'ungsophy', 'git_agent.git')).must_equal true
62
+ end
63
+
64
+ describe 'when nothing was changed' do
65
+ it 'returns empty hash' do
66
+ agent.stubs(:read_from_record).returns({ 'HEAD' => '356a192b7913b04c54574d18c28d46e6395428ab',
67
+ 'refs/heads/master' => '356a192b7913b04c54574d18c28d46e6395428ab' })
68
+
69
+ agent.stubs(:ls_remote).returns({ 'HEAD' => '356a192b7913b04c54574d18c28d46e6395428ab',
70
+ 'refs/heads/master' => '356a192b7913b04c54574d18c28d46e6395428ab' })
71
+
72
+ result = agent.diff
73
+
74
+ result[:added].must_be_empty
75
+ result[:deleted].must_be_empty
76
+ result[:updated].must_be_empty
77
+ end
78
+ end
79
+ end
80
+
81
+ describe '#notify_if_different!' do
82
+ it 'sends notify when there is any difference' do
83
+ agent.stubs(:read_from_record).returns({ 'HEAD' => '356a192b7913b04c54574d18c28d46e6395428ab',
84
+ 'refs/heads/master' => '356a192b7913b04c54574d18c28d46e6395428ab' })
85
+
86
+ agent.stubs(:ls_remote).returns({ 'HEAD' => '356a192b7913b04c54574d18c28d46e6395428ac',
87
+ 'refs/heads/master' => '356a192b7913b04c54574d18c28d46e6395428ac' })
88
+ GitAgent::Notification.any_instance.expects(:send!)
89
+
90
+ agent.notify_if_different!
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,21 @@
1
+ gem 'minitest'
2
+ require 'minitest/autorun'
3
+ gem 'mocha'
4
+ require 'mocha/mini_test'
5
+ require 'fileutils'
6
+ require 'pp'
7
+
8
+ require 'git_agent'
9
+
10
+ GitAgent.data_directory = './test/data'
11
+ FileUtils.mkdir(GitAgent.data_directory) unless Dir.exist?(GitAgent.data_directory)
12
+
13
+ Mail.defaults do
14
+ delivery_method :test
15
+ end
16
+
17
+ class Minitest::Spec
18
+ def teardown
19
+ FileUtils.rm_r(Dir.glob(File.join(GitAgent.data_directory, '**')))
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sophy Eung
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mail
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.5.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.5.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 5.2.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 5.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 1.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 1.0.0
83
+ description: A command line utility to check git repositories
84
+ email:
85
+ - ungsophy@gmail.com
86
+ executables:
87
+ - check_repo
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/check_repo
97
+ - git_agent.gemspec
98
+ - lib/git_agent.rb
99
+ - lib/git_agent/notification.rb
100
+ - lib/git_agent/version.rb
101
+ - test/lib/git_agent/notification_test.rb
102
+ - test/lib/git_agent_test.rb
103
+ - test/test_helper.rb
104
+ homepage: https://github.com/ungsophy/git_agent
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.0.6
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: A command line utility to check git repositories
128
+ test_files:
129
+ - test/lib/git_agent/notification_test.rb
130
+ - test/lib/git_agent_test.rb
131
+ - test/test_helper.rb