Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add ruby script for uploading to github

  • Loading branch information...
commit ced634a9be569d5072699fbb97641a4957860ede 1 parent 5a9eb89
authored July 06, 2011
4  .gitignore
@@ -8,7 +8,5 @@ MaxCore.max
8 8
 *.ipr
9 9
 *.iml
10 10
 *.iws
11  
-build
12 11
 out
13  
-java.hprof.txt
14  
-
  12
+java.hprof.txt
211  build/github_upload.rb
... ...
@@ -0,0 +1,211 @@
  1
+# require
  2
+require 'rubygems'
  3
+
  4
+#### INLINE: fixed version of https://github.com/github/upload ####
  5
+
  6
+require 'tempfile'
  7
+require 'nokogiri'
  8
+require 'httpclient'
  9
+require 'stringio'
  10
+require 'json'
  11
+require 'faster_xml_simple'
  12
+
  13
+module Net
  14
+  module GitHub
  15
+    class Upload
  16
+      VERSION = '0.0.5'
  17
+      def initialize params=nil
  18
+        @login = params[:login]
  19
+        @token = params[:token]
  20
+
  21
+        if @login.empty? or @token.empty?
  22
+          raise "login or token is empty"
  23
+        end
  24
+      end
  25
+
  26
+      def upload info
  27
+        unless info[:repos]
  28
+          raise "required repository name"
  29
+        end
  30
+        info[:repos] = @login + '/' + info[:repos] unless info[:repos].include? '/'
  31
+
  32
+        if info[:file]
  33
+          file = info[:file]
  34
+          unless File.exist?(file) && File.readable?(file)
  35
+            raise "file does not exsits or readable"
  36
+          end
  37
+          info[:name] ||= File.basename(file)
  38
+        end
  39
+        unless  info[:file] || info[:data]
  40
+          raise "required file or data parameter to upload"
  41
+        end
  42
+
  43
+        unless info[:name]
  44
+          raise "required name parameter for filename with data parameter"
  45
+        end
  46
+
  47
+        if info[:replace]
  48
+          list_files(info[:repos]).each { |obj|
  49
+            next unless obj[:name] == info[:name]
  50
+            delete info[:repos], obj[:id]
  51
+          }
  52
+        elsif list_files(info[:repos]).any?{|obj| obj[:name] == info[:name]}
  53
+          raise "file '#{info[:name]}' is already uploaded. please try different name"
  54
+        end
  55
+
  56
+        info[:content_type] ||= 'application/octet-stream'
  57
+        stat = HTTPClient.post("https://github.com/#{info[:repos]}/downloads", {
  58
+          "file_size"    => info[:file] ? File.stat(info[:file]).size : info[:data].size,
  59
+          "content_type" => info[:content_type],
  60
+          "file_name"    => info[:name],
  61
+          "description"  => info[:description] || '',
  62
+          "login"        => @login,
  63
+          "token"        => @token
  64
+        })
  65
+
  66
+        unless stat.code == 200
  67
+          raise "Failed to post file info"
  68
+        end
  69
+
  70
+        upload_info = JSON.parse(stat.content)
  71
+        if info[:file]
  72
+          f = File.open(info[:file], 'rb')
  73
+        else
  74
+          f = Tempfile.open('net-github-upload')
  75
+          f << info[:data]
  76
+          f.flush
  77
+        end
  78
+        stat = HTTPClient.post("http://github.s3.amazonaws.com/", [
  79
+          ['Filename', info[:name]],
  80
+          ['policy', upload_info['policy']],
  81
+          ['success_action_status', 201],
  82
+          ['key', upload_info['path']],
  83
+          ['AWSAccessKeyId', upload_info['accesskeyid']],
  84
+          ['Content-Type', upload_info['content_type'] || 'application/octet-stream'],
  85
+          ['signature', upload_info['signature']],
  86
+          ['acl', upload_info['acl']],
  87
+          ['file', f]
  88
+        ])
  89
+        f.close
  90
+
  91
+        if stat.code == 201
  92
+          return FasterXmlSimple.xml_in(stat.content)['PostResponse']['Location']
  93
+        else
  94
+          raise 'Failed to upload' + extract_error_message(stat)
  95
+        end
  96
+      end
  97
+
  98
+      def replace info
  99
+         upload info.merge( :replace => true )
  100
+      end
  101
+
  102
+      def delete_all repos
  103
+        unless repos
  104
+          raise "required repository name"
  105
+        end
  106
+        repos = @login + '/' + repos unless repos.include? '/'
  107
+        list_files(repos).each { |obj|
  108
+          delete repos, obj[:id]
  109
+        }
  110
+      end
  111
+
  112
+      private
  113
+
  114
+      def extract_error_message(stat)
  115
+        # @see http://docs.amazonwebservices.com/AmazonS3/2006-03-01/ErrorResponses.html
  116
+        error = FasterXmlSimple.xml_in(stat.content)['Error']
  117
+        " due to #{error['Code']} (#{error['Message']})"
  118
+      rescue
  119
+        ''
  120
+      end
  121
+
  122
+      def delete repos, id
  123
+        HTTPClient.post("https://github.com/#{repos}/downloads/#{id.gsub( "download_", '')}", {
  124
+          "_method"      => "delete",
  125
+          "login"        => @login,
  126
+          "token"        => @token
  127
+        })
  128
+      end
  129
+
  130
+      def list_files repos
  131
+        raise "required repository name" unless repos
  132
+        res = HTTPClient.get_content("https://github.com/#{repos}/downloads", {
  133
+          "login" => @login,
  134
+          "token" => @token
  135
+        })
  136
+        Nokogiri::HTML(res).xpath('id("manual_downloads")/li').map do |fileinfo|
  137
+          obj = {
  138
+            :description => fileinfo.at_xpath('descendant::h4').text,
  139
+            :date        => fileinfo.at_xpath('descendant::p/time').attribute('title').text,
  140
+            :size        => fileinfo.at_xpath('descendant::p/strong').text,
  141
+            :id          => /\d+$/.match(fileinfo.at_xpath('a').attribute('href').text)[0]
  142
+          }
  143
+          anchor = fileinfo.at_xpath('descendant::h4/a')
  144
+          obj[:link] = anchor.attribute('href').text
  145
+          obj[:name] = anchor.text
  146
+          obj
  147
+        end
  148
+      end
  149
+    end
  150
+  end
  151
+end
  152
+
  153
+#### END INLINE ####
  154
+
  155
+# setup
  156
+login = `git config github.user`.chomp  # your login for github
  157
+token = `git config github.token`.chomp # your token for github
  158
+repos = 'KentBeck/junit'               # your repos name (like 'taberareloo')
  159
+gh = Net::GitHub::Upload.new(
  160
+  :login => login,
  161
+  :token => token
  162
+)
  163
+
  164
+version = ARGV[0]
  165
+
  166
+def upload(gh, version, repos, filename, description)
  167
+  gh.upload(:repos => repos, 
  168
+            :file => "junit#{version}/#{filename}", 
  169
+            :description => description)
  170
+end
  171
+
  172
+upload(gh, version, repos, "junit-#{version}-src.jar", 'Source jar')
  173
+upload(gh, version, repos, "junit-#{version}.jar", 'Basic jar')
  174
+upload(gh, version, repos, "junit-dep-#{version}.jar", 'Jar without hamcrest')
  175
+upload(gh, version, repos, "junit#{version}.zip", 'Source zip')
  176
+
  177
+# # file upload
  178
+# direct_link = gh.upload(
  179
+#   :repos => repos,
  180
+#   :file  => 'test/test',
  181
+#   :description => "test file"
  182
+# )
  183
+# # direct link is link to Amazon S3.
  184
+# # Because GitHub refrection for file changing is async,
  185
+# # if you get changed file synchronously, you use this "direct_link"
  186
+# 
  187
+# # data upload
  188
+# # you can define content_type => Amazon S3 Content-Type
  189
+# time = Time.now.to_i
  190
+# direct_link = gh.upload(
  191
+#   :repos => repos,
  192
+#   :data  => 'test',
  193
+#   :name  => "test_#{time}.txt",
  194
+#   :content_type => 'text/plain',
  195
+#   :description => "test file2"
  196
+# )
  197
+# 
  198
+# # replace file or data
  199
+# # thx id:rngtng !
  200
+# direct_link = gh.replace(
  201
+#   :repos => repos,
  202
+#   :file  => 'test/test',
  203
+#   :description => "test file"
  204
+# )
  205
+# direct_link = gh.replace(
  206
+#   :repos => repos,
  207
+#   :data  => 'test',
  208
+#   :name  => "test_#{time}.txt",
  209
+#   :content_type => 'text/plain',
  210
+#   :description => "test file2"
  211
+# )

0 notes on commit ced634a

Please sign in to comment.
Something went wrong with that request. Please try again.