Permalink
Browse files

Retrieving ticket titles successfully

  • Loading branch information...
1 parent a0eaa25 commit 8280c0f5dc7047f4b34f419e64f8fa2f6957c3ee @winton committed Nov 28, 2011
Showing with 133 additions and 3 deletions.
  1. +1 −1 README.md
  2. +14 −1 bin/gitcycle
  3. +2 −0 gitcycle.gemspec
  4. +20 −0 lib/ext/string.rb
  5. +96 −1 lib/gitcycle.rb
View
@@ -1,4 +1,4 @@
-GitCycle
+Gitcycle
========
Tame your development cycle.
View
@@ -1,3 +1,16 @@
#!/usr/bin/env ruby
-require File.expand_path("../../lib/gitcycle", __FILE__)
+require File.expand_path("../../lib/gitcycle", __FILE__)
+
+gitcycle = Gitcycle.new
+command = ARGV.shift
+
+if command.nil?
+ puts "\nNo command specified\n".red
+elsif gitcycle.respond_to?(command)
+ gitcycle.send(command, *ARGV)
+elsif ARGV.empty?
+ gitcycle.pull_request(command)
+else
+ puts "\nCommand \"#{command}\" not found\n".red
+end
View
@@ -20,4 +20,6 @@ Gem::Specification.new do |s|
s.test_files = `cd #{root} && git ls-files -- {features,test,spec}/*`.split("\n")
s.add_development_dependency "rspec", "~> 1.0"
+
+ s.add_dependency "yajl-ruby", "= 1.1.0"
end
View
@@ -0,0 +1,20 @@
+class String
+
+ # Colors
+
+ def blue
+ "\e[34m#{self}\e[0m"
+ end
+
+ def green
+ "\e[32m#{self}\e[0m"
+ end
+
+ def red
+ "\e[31m#{self}\e[0m"
+ end
+
+ def yellow
+ "\e[33m#{self}\e[0m"
+ end
+end
View
@@ -1,4 +1,99 @@
+require 'open-uri'
+require 'uri'
+require 'yaml'
+
+gem 'yajl-ruby', '= 1.1.0'
+require 'yajl'
+
$:.unshift File.dirname(__FILE__)
-module Gitcycle
+require "ext/string"
+
+class Gitcycle
+
+ API =
+ if ENV['ENV'] == 'development'
+ "http://127.0.0.1:8080/api"
+ else
+ "http://gitcycle.com/api"
+ end
+
+ def initialize
+ @config_path = File.expand_path("~/.gitcycle.yml")
+ load_config
+ load_git
+ end
+
+ def pull_request(url_or_desc)
+ require_git && require_config
+ if url_or_desc.strip[0..3] == 'http'
+ ticket = get('ticket',
+ :login => @git_login,
+ :token => @token,
+ :url => url_or_desc
+ )
+ title = ticket['ticket']['title']
+ else
+ title = url_or_desc
+ end
+ puts title
+ end
+
+ def setup(login, repo, token)
+ @config[login] ||= {}
+ @config[login][repo] = token
+ save_config
+ end
+
+ private
+
+ def get(path, hash)
+ params = ''
+ hash.each do |k, v|
+ params << "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}&"
+ end
+ params.chop! # trailing &
+
+ json = open("#{API}/#{path}.json?#{params}").read
+ Yajl::Parser.parse(json)
+ end
+
+ def load_config
+ if File.exists?(@config_path)
+ @config = YAML.load(File.read(@config_path))
+ else
+ @config = {}
+ end
+ end
+
+ def load_git
+ path = "#{Dir.pwd}/.git/config"
+ if File.exists?(path)
+ @git_url = File.read(path).match(/\[remote "origin"\].*url = ([^\n]+)/m)[1]
+ @git_repo = @git_url.match(/\/(.+)\./)[1]
+ @git_login = @git_url.match(/:(.+)\//)[1]
+ @token = @config[@git_login][@git_repo]
+ end
+ end
+
+ def require_config
+ unless @token
+ puts "\ngitcycle configuration not found.".red
+ puts "Are you in the right repository?"
+ puts "Have you set up this repository at http://gitcycle.com?\n\n"
+ end
+ end
+
+ def require_git
+ unless @git_url && @git_repo && @git_login
+ puts "\n.git/config origin entry not found!".red
+ puts "Are you sure you are in a git repository?\n\n"
+ end
+ end
+
+ def save_config
+ File.open(@config_path, 'w') do |f|
+ f.write(YAML.dump(@config))
+ end
+ end
end

0 comments on commit 8280c0f

Please sign in to comment.