Skip to content
This repository has been archived by the owner on Apr 11, 2022. It is now read-only.

Commit

Permalink
Merge upstream patches, bit late
Browse files Browse the repository at this point in the history
  • Loading branch information
rmehner committed Dec 24, 2009
2 parents 538c128 + d137de9 commit 6787d59
Show file tree
Hide file tree
Showing 23 changed files with 334 additions and 257 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
pkg
*.gemspec
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2009 Alexander Lang

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -13,5 +13,4 @@ Note: You might need to add gemcutter.org to your gem sources if you haven't don
# Usage # Usage


Just run it from within the project you want to add mite entries for and follow the onscreen instructions Just run it from within the project you want to add mite entries for and follow the onscreen instructions

#project_path git2mite
` #project_path git2mite `
30 changes: 30 additions & 0 deletions Rakefile
@@ -0,0 +1,30 @@
require 'rubygems'
require 'rake'

begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "git2mite"
gem.summary = %Q{writes your git commit messages to your mite account}
gem.email = "alex@upstre.am"
gem.homepage = "http://github.com/upstream/git2mite"
gem.authors = ["Alexander Lang", 'Thilo Utke', 'Robin Mehner']
gem.files = FileList["[A-Z]*.*", "lib/**/*"]
gem.add_dependency 'json'
gem.add_dependency 'rest-client'
gem.add_dependency 'builder'
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end

rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
end

require 'spec/rake/spectask'
desc "Run all specs"
Spec::Rake::SpecTask.new(:spec) do |t|
t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""]
t.spec_files = FileList['spec/*_spec.rb']
end

task :default => :spec
1 change: 1 addition & 0 deletions VERSION
@@ -0,0 +1 @@
0.0.1
4 changes: 4 additions & 0 deletions bin/git2mite
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby

require 'git2mite'
Git2Mite::App.new.run
43 changes: 0 additions & 43 deletions lib/configuration.rb

This file was deleted.

70 changes: 7 additions & 63 deletions lib/git2mite.rb
Expand Up @@ -2,66 +2,10 @@
# * check if api key is valid # * check if api key is valid
# * run as post commit hook? # * run as post commit hook?


$LOAD_PATH << File.dirname(__FILE__) + '/../lib' $LOAD_PATH.unshift File.dirname(__FILE__)
require 'configuration' require 'git2mite/app'
require 'user' require 'git2mite/configuration'
require 'mite_client' require 'git2mite/user'
require 'gui' require 'git2mite/mite_client'
require 'git_repo' require 'git2mite/gui'

require 'git2mite/git_repo'
class Git2Mite
def configuration
@configuration ||= Configuration.new
end

def get_api_key
configuration.api_key ||= @gui.ask('What is your api key?').strip
end

def get_sub_domain
configuration.sub_domain ||= @gui.ask('What is your account subdomain?').strip
end

def check_if_git_repo!
@gui.error "Please change to a directory that is a git repository." unless @repo.is_git_repo?
end

def check_ruby_version!
@gui.error "Sorry you need Ruby 1.8.7+ for this." if RUBY_VERSION < '1.8.7'
end


def initialize
@gui = Gui.new
@repo = GitRepo.new

@gui.print_welcome
check_if_git_repo!
check_ruby_version!
client = MiteClient.new("http://#{get_sub_domain}.mite.yo.lk", get_api_key)
project_id = @gui.get_project_id(client.projects)
user_id = @gui.get_user_id(User.all(client))
start_date = @gui.get_date('start date')
end_date = @gui.get_date('end date')
commits = @repo.commits start_date, end_date
author = @gui.get_author commits.map{|date, message, _author| _author}.uniq

puts
puts 'Writing commits to mite'

commits.each do |date, message, _author|
next unless _author == author
entries = client.time_entries project_id, user_id, date
if entries.empty?
STDERR.puts "WARN no time entries for commit #{date.to_s}: #{message}"
else
entry = entries[rand(entries.size)]['time_entry']
client.add_message_to_entry entry, message
print "."
end
end
puts 'done'
end
end

Git2Mite.new
58 changes: 58 additions & 0 deletions lib/git2mite/app.rb
@@ -0,0 +1,58 @@
module Git2Mite
class App
def configuration
@configuration ||= Git2Mite::Configuration.new
end

def get_api_key
configuration.api_key ||= @gui.ask('What is your api key?').strip
end

def get_sub_domain
configuration.sub_domain ||= @gui.ask('What is your account subdomain?').strip
end

def check_if_git_repo!
@gui.error "Please change to a directory that is a git repository." unless @repo.is_git_repo?
end

def check_ruby_version!
@gui.error "Sorry you need Ruby 1.8.7+ for this." if RUBY_VERSION < '1.8.7'
end


def initialize
@gui = Gui.new
@repo = GitRepo.new
end

def run
@gui.print_welcome
check_if_git_repo!
check_ruby_version!
client = MiteClient.new("http://#{get_sub_domain}.mite.yo.lk", get_api_key)
project_id = @gui.get_project_id(client.projects)
user_id = @gui.get_user_id(User.all(client))
start_date = @gui.get_date('start date')
end_date = @gui.get_date('end date')
commits = @repo.commits start_date, end_date
author = @gui.get_author commits.map{|date, message, _author| _author}.uniq

puts
puts 'Writing commits to mite'

commits.each do |date, message, _author|
next unless _author == author
entries = client.time_entries project_id, user_id, date
if entries.empty?
STDERR.puts "WARN no time entries for commit #{date.to_s}: #{message}"
else
entry = entries[rand(entries.size)]['time_entry']
client.add_message_to_entry entry, message
print "."
end
end
puts 'done'
end
end
end
46 changes: 46 additions & 0 deletions lib/git2mite/configuration.rb
@@ -0,0 +1,46 @@
require 'yaml'

module Git2Mite
class Configuration
def initialize(config_file = nil)
@config_file = config_file || ENV['HOME'] + '/.git2mite.yml'
if File.exist?(@config_file)
@config = load_config
else
@config = {}
end
end

def api_key
@config[:api_key]
end

def sub_domain
@config[:sub_domain]
end

def sub_domain=(value)
@config[:sub_domain] = value.strip
store_config
@config[:sub_domain]
end

def api_key=(value)
@config[:api_key] = value.strip
store_config
@config[:api_key]
end

private

def store_config
File.open(@config_file, 'w') do |f|
f << @config.to_yaml
end
end

def load_config
YAML.load(File.read(@config_file))
end
end
end
19 changes: 19 additions & 0 deletions lib/git2mite/git_repo.rb
@@ -0,0 +1,19 @@
module Git2Mite
class GitRepo
def is_git_repo?
status = `git status 2>&1`
!status.downcase.include?('not a git repo')
end

def commits(start_date, end_date)
lines = []
IO.popen("git log --pretty=format:%ai\\|%s\\|%ae --no-merges --before=#{end_date + 1} --after=#{start_date}") do |io|
while line = io.gets
date, message, author = line.split('|')
lines.unshift [Date.parse(date), message.strip, author.strip]
end
lines
end
end
end
end
64 changes: 64 additions & 0 deletions lib/git2mite/gui.rb
@@ -0,0 +1,64 @@
module Git2Mite
class Gui
def print_welcome
puts <<-WELCOME
Welcome to git2mite
This tool allows to you to write the commit log of
your git repository to your mite account in order
to auto-fill your timesheets.
Brought to you by http://upstream-berlin.com
Question, Problems, Source Code: http://github.com/upstream/git2mite
WELCOME
end

def ask(question)
print "#{question}: "
$stdin.gets
end

def error(reason)
STDERR.puts reason
exit(-1)
end

def get_project_id(projects)
puts "=== Projects ==="
projects.each.with_index do |project, i|
puts "#{i+1}\t#{project['project']['name']}"
end
choice = ask('Which project do you want to write your commits to?')
(projects[choice.to_i - 1] || error('invalid project id'))['project']['id']
end

def get_user_id(users)
puts "\n=== Users ==="
users.each.with_index do |user, i|
puts "#{i+1}\t#{user.name}"
end
choice = ask('Which user do you want to write your commits to?')
(users[choice.to_i - 1] || error('invalid user id')).id
end

def get_author(authors)
puts "\n=== Git Authors ==="
authors.each.with_index do |name, i|
puts "#{i+1}\t#{name}"
end
choice = ask('Which author\'s commits to you want to use?')
authors[choice.to_i - 1] || error('Invalid author')
end

def get_date(label)
answer = ask("Enter the #{label} (yyyy-mm-dd) [#{Date.today}]")
begin
answer.strip.empty? ? Date.today : Date.parse(answer)
rescue(ArgumentError)
error('invalid date')
end
end

end
end

0 comments on commit 6787d59

Please sign in to comment.