Skip to content

Commit

Permalink
Added authenticated commands, but GitHub API is giving 500 errors for…
Browse files Browse the repository at this point in the history
… opening issues.
  • Loading branch information
fcoury committed Apr 21, 2009
1 parent 917b6ea commit 545fff1
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
examples/github.yml
9 changes: 9 additions & 0 deletions examples/authenticated.rb
@@ -0,0 +1,9 @@
require File.join(File.dirname(__FILE__), '..', 'lib', 'octopi')

include Octopi

authenticated_with :config => "github.yml" do |g|
repo = g.repository("api-labrat")
repo.open_issue :title => "Sample issue",
:body => "This issue was opened using GitHub API and Octopi"
end
2 changes: 2 additions & 0 deletions examples/github.yml.default
@@ -0,0 +1,2 @@
# login: github-username
# token: github-token
4 changes: 2 additions & 2 deletions examples/issues.rb
Expand Up @@ -2,10 +2,10 @@

include Octopi

user = User.find("bcalloway")
user = User.find("fcoury")
puts user.name

repo = user.repository("myproject")
repo = user.repository("octopi")
puts repo.description

issue = Issue.find_all(user.login, repo.name).first
Expand Down
54 changes: 48 additions & 6 deletions lib/octopi.rb
@@ -1,12 +1,24 @@
require 'rubygems'
require 'httparty'
require 'yaml'
require 'pp'

module Octopi
class Api; end
ANONYMOUS_API = Api.new

def connect(login, token, &block)
def authenticated_with(*args, &block)
opts = args.last.is_a?(Hash) ? args.last : {}
if opts[:config]
config = File.open(opts[:config]) { |yf| YAML::load(yf) }
raise "Missing config #{opts[:config]}" unless config

login = config["login"]
token = config["token"]
else
login, token = *args
end

yield Api.new(login, token)
end

Expand All @@ -19,11 +31,14 @@ class Api
}
base_uri "http://github.com/api/v2"

attr_accessor :format
attr_accessor :format, :login, :token

def initialize(login = nil, token = nil, format = "xml")
self.class.default_params(:login => login, :token => token) if login
@format = format
if login
@login = login
@token = token
end
end

%w[keys emails].each do |action|
Expand All @@ -33,11 +48,22 @@ def initialize(login = nil, token = nil, format = "xml")
end

def user
user_data = get("/user/show/#{self.class.default_params[:login]}")
user_data = get("/user/show/#{login}")
raise "Unexpected response for user command" unless user_data and user_data['user']
User.new(self, user_data['user'])
end

def open_issue(user, repo, params)
Issue.open(user, repo, params, self)
end

def repository(name)
repo = Repository.find(login, name)
repo.api = self
repo
end
alias_method :repo, :repository

def save(resource_path, data)
traslate resource_path, data
#still can't figure out on what format values are expected
Expand All @@ -55,13 +81,23 @@ def find_all(path, result_key, query)
def get_raw(path, params)
get(path, params, 'plain')
end

def post(path, params = {}, format = "yaml")
submit(path, params, format) do |path, params, format|
puts "POST: /#{format}#{path} with: #{params.inspect}"
resp = self.class.post "/#{format}#{path}", :query => params
pp resp
resp
end
end

private
def get(path, params = {}, format = "yaml")
def submit(path, params = {}, format = "yaml", &block)
params.each_pair do |k,v|
path = path.gsub(":#{k.to_s}", v)
end
resp = self.class.get("/#{format}#{path}")
query = login ? { :login => login, :token => token } : {}
resp = yield(path, query.merge(params), format)
# FIXME: This fails for showing raw Git data because that call returns
# text/html as the content type. This issue has been reported.
ctype = resp.headers['content-type'].first
Expand All @@ -74,6 +110,12 @@ def get(path, params = {}, format = "yaml")
end
resp
end

def get(path, params = {}, format = "yaml")
submit(path, params, format) do |path, params, format|
self.class.get "/#{format}#{path}"
end
end
end

%w{error base resource user tag repository issue file_object blob commit}.
Expand Down
3 changes: 3 additions & 0 deletions lib/octopi/base.rb
Expand Up @@ -21,6 +21,9 @@ class Base
:pat => /^[a-f0-9]{40}$/,
:msg => "%s is an invalid SHA hash"}
}

attr_accessor :api

def initialize(api, hash)
@api = api
@keys = []
Expand Down
5 changes: 5 additions & 0 deletions lib/octopi/issue.rb
Expand Up @@ -58,5 +58,10 @@ def self.find(*args)
validate_args(user => :user, repo => :repo)
super user, repo, number
end

def self.open(user, repo, params, api = ANONYMOUS_API)
data = api.post("/issues/open/#{user}/#{repo}", params)
new(api, data)
end
end
end
10 changes: 9 additions & 1 deletion lib/octopi/repository.rb
Expand Up @@ -26,7 +26,7 @@ def self.find(user, name)
user = user.login if user.is_a? User
name = repo.name if name.is_a? Repository
self.validate_args(user => :user, name => :repo)
super [user,name]
super [user, name]
end

def self.find_all(*args)
Expand All @@ -35,6 +35,14 @@ def self.find_all(*args)
super args.join(" ").gsub(/ /,'+')
end

def self.open_issue(args)
Issue.open(args[:user], args[:repo], args)
end

def open_issue(args)
Issue.open(self.name, self.owner, args, @api)
end

def commits(branch = "master")
Commit.find_all(self, :branch => branch)
end
Expand Down

0 comments on commit 545fff1

Please sign in to comment.