require 'cgi' require 'sinatra' require "json" require "json/add/core" require "P4" require "p4if" EMPTY_ARRAY = Array.new configure do set :bind, 'rest4p.das.perforce.com' set :port, 1999 end VALID_FORMS = Array[ 'branch', 'change', 'client', 'counter', 'depot', 'group', 'job', 'key', 'label', 'stream', 'user'] before do cache_control :no_store if request.path != "/_session" and request.path != "/env" cookie = request.cookies['PUP'] if !cookie halt 401, "Session not initialized" end end if (request.accept? 'text/plain') content_type 'text/plain' else content_type 'application/json' end end before '/p4/*' do request.path_info.downcase! end before '/p4/*/*' do | form, name | form.downcase! request.path_info = "/p4/#{form}/#{name}" end get '/_session' do content_type 'text/html' erb :session end delete '/_session' do response.delete_cookie 'PUP' body "Session deleted" end post '/_session' do charset = nil if params[:unicode] charset = "utf8" end result = p4_login( params[:port], params[:user], params[:pass], charset ) if result.length == 1 response.delete_cookie 'PUP' halt 401, result[0] end if result[0][0][0] == "'" response.delete_cookie 'PUP' halt 401, "Account must have a password set" end response.set_cookie 'PUP', result[0] body JSON.pretty_generate( result[1] ) end get '/p4/*s' do |form| if !VALID_FORMS.include?(form) and form != 'branche' halt 400, "Invalid or unsupport form: #{form}s" end cookie = request.cookies['PUP'] add_args = CGI.parse(request.query_string) args = p4_args( add_args ) result = p4_cmd( cookie, "#{form}s", args, nil ) if result.length == 1 halt 400, result[0] end if result.length == 0 body JSON.pretty_generate( [] ) else body JSON.pretty_generate( result[1] ) end end get '/p4/*/*' do |form, name| if !VALID_FORMS.include?(form) halt 400, "Invalid or unsupport form: #{form}" end if name.length == 0 halt 400, "Invalid request" end cookie = request.cookies['PUP'] add_args = CGI.parse(request.query_string) args = p4_args( add_args ) args << "-o" args << name result = p4_cmd( cookie, "#{form}", args, nil ) if result.length == 1 halt 400, result[0] end if result.length == 0 halt 200 end if result.length == 0 body JSON.pretty_generate( [] ) else body JSON.pretty_generate( result[1] ) end end get '/p4edit/*/*' do |form, name| if !VALID_FORMS.include?(form) halt 400, "Invalid or unsupport form: #{form}" end if name.length == 0 halt 400, "Invalid request" end cookie = request.cookies['PUP'] add_args = CGI.parse(request.query_string) args = p4_args( add_args ) args << "-o" args << name result = p4_cmd( cookie, "#{form}", args, nil ) if result.length == 1 halt 400, result[0] end if result.length == 0 halt 200 end if result.length == 0 body JSON.pretty_generate( [] ) else form_json = JSON.pretty_generate( result[1] ) content_type 'text/html' erb :edit, :locals => {:json_body => form_json, :post_url => request.path} end end post '/p4edit/*/*' do |form, name| begin if request.body.read.length == 0 data = JSON.parse(request.body.read) else data = JSON.parse( request["body"] ) end rescue halt 400, "Invalid JSON body" end cookie = request.cookies['PUP'] args = Array.new args << "-i" result = p4_cmd( cookie, "#{form}", args, data ) if result.length == 1 halt 400, result[0] end if result.length == 0 halt 200 end if result.length == 0 body JSON.pretty_generate( [] ) else body JSON.pretty_generate( result[1] ) end end get '/git-fusion/repos' do cookie = request.cookies['PUP'] args = Array.new args << "-e" args << "//.git-fusion/repos/*/p4gf_config" result = p4_cmd( cookie, "files", args, nil ) if result.length == 1 halt 400, result[0] end if result.length == 0 halt 200 end if result.length == 0 body JSON.pretty_generate( [] ) else repos = Array.new result[1].each { |r| repo = r['depotFile'] repo[0,20] = "" repo[-12,repo.length] = "" repos << repo } body JSON.pretty_generate( repos ) end end get '/git-fusion/repo/*' do |repo| cookie = request.cookies['PUP'] args = Array.new args << "-q" args << "//.git-fusion/repos/#{repo}/p4gf_config" result = p4_cmd( cookie, "print", args, nil, false ) if result.length == 1 halt 400, result[0] end if result.length == 0 halt 200 end if result.length == 0 body JSON.pretty_generate( [] ) else body JSON.pretty_generate( result ) end end get '/env' do a = Array.new a << request.accept # ['text/html', '*/*'] a << (request.accept? 'text/xml') # true a << request.body # request body sent by the client (see below) a << request.scheme # "http" a << request.script_name # "/example" a << request.path_info # "/foo" a << request.port # 80 a << request.request_method # "GET" a << request.query_string # "" a << request.content_length # length of request.body a << request.media_type # media type of request.body a << request.host # "example.com" a << request.get? # true (similar methods for other verbs) a << request.form_data? # false a << request["some_param"] # value of some_param parameter. [] is a shortcut to the params hash. a << request.referrer # the referrer of the client or '/' a << request.user_agent # user agent (used by :agent condition) a << request.cookies # hash of browser cookies a << request.xhr? # is this an ajax request? a << request.url # "http://example.com/example/foo" a << request.path # "/example/foo" a << request.ip # client IP address a << request.secure? # false (would be true over ssl) a << request.forwarded? # true (if running behind a reverse proxy) a << request.env # raw env hash handed in by Rack JSON.pretty_generate( a ) end