github_uploader 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2-p290
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rest-client"
4
+ gem "github_api", "0.4.11"
5
+
6
+ group :development do
7
+ gem "shoulda", ">= 0"
8
+ gem "rdoc", "~> 3.12"
9
+ gem "bundler", "~> 1.1.0"
10
+ gem "jeweler", "~> 1.8.3"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,51 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.2.7)
5
+ faraday (0.7.6)
6
+ addressable (~> 2.2)
7
+ multipart-post (~> 1.1)
8
+ rack (~> 1.1)
9
+ git (1.2.5)
10
+ github_api (0.4.11)
11
+ faraday (~> 0.7.6)
12
+ hashie (~> 1.2.0)
13
+ multi_json (~> 1.3)
14
+ nokogiri (~> 1.5.2)
15
+ oauth2 (~> 0.5.2)
16
+ hashie (1.2.0)
17
+ jeweler (1.8.3)
18
+ bundler (~> 1.0)
19
+ git (>= 1.2.5)
20
+ rake
21
+ rdoc
22
+ json (1.7.0)
23
+ mime-types (1.18)
24
+ multi_json (1.3.4)
25
+ multipart-post (1.1.5)
26
+ nokogiri (1.5.2)
27
+ oauth2 (0.5.2)
28
+ faraday (~> 0.7)
29
+ multi_json (~> 1.0)
30
+ rack (1.4.1)
31
+ rake (0.9.2.2)
32
+ rdoc (3.12)
33
+ json (~> 1.4)
34
+ rest-client (1.6.7)
35
+ mime-types (>= 1.16)
36
+ shoulda (3.0.1)
37
+ shoulda-context (~> 1.0.0)
38
+ shoulda-matchers (~> 1.0.0)
39
+ shoulda-context (1.0.0)
40
+ shoulda-matchers (1.0.0)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ bundler (~> 1.1.0)
47
+ github_api (= 0.4.11)
48
+ jeweler (~> 1.8.3)
49
+ rdoc (~> 3.12)
50
+ rest-client
51
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ # Copyright (c) 2011 Yehuda Katz, Tom Dale, Charles Jolley and Ember.js contributors
2
+
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ # this software and associated documentation files (the "Software"), to deal in
5
+ # the Software without restriction, including without limitation the rights to
6
+ # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ # of the Software, and to permit persons to whom the Software is furnished to do
8
+ # so, subject to the following conditions:
9
+
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
data/README.mdown ADDED
@@ -0,0 +1,26 @@
1
+ # github_uploader
2
+
3
+ Found this in the Rakefile for Ember.js. Looked pretty damn useful.
4
+
5
+ Use it:
6
+
7
+ ```ruby
8
+ uploader = GithubUploader.setup_uploader
9
+ GithubUploader.upload_file(uploader, FILENAME_ON_GITHUB, DESCRIPTION, "relative/path/to/file")
10
+ ```
11
+
12
+ ## Contributing to github_uploader
13
+
14
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
15
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
16
+ * Fork the project.
17
+ * Start a feature/bugfix branch.
18
+ * Commit and push until you are happy with your contribution.
19
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
20
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
21
+
22
+ ## Copyright
23
+
24
+ Copyright (c) 2011 Yehuda Katz, Tom Dale, Charles Jolley and Ember.js contributors.
25
+ See LICENSE.txt for further details.
26
+
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "github_uploader"
18
+ gem.homepage = "http://github.com/collin/github_uploader"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Upload files to github.}
21
+ gem.description = %Q{Upload files to github, useful in your Rakefile.}
22
+ gem.email = "collintmiller@gmail.com"
23
+ gem.authors = ["Yehuda Katz", "Tom Dale", "Charles Jolley", "Ember.js contributors"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "github_uploader #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,66 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "github_uploader"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Yehuda Katz", "Tom Dale", "Charles Jolley", "Ember.js contributors"]
12
+ s.date = "2012-05-01"
13
+ s.description = "Upload files to github, useful in your Rakefile."
14
+ s.email = "collintmiller@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.mdown"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rvmrc",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.mdown",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "github_uploader.gemspec",
29
+ "lib/github_uploader.rb",
30
+ "test/helper.rb",
31
+ "test/test_github_uploader.rb"
32
+ ]
33
+ s.homepage = "http://github.com/collin/github_uploader"
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "1.8.10"
37
+ s.summary = "Upload files to github."
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<rest-client>, [">= 0"])
44
+ s.add_runtime_dependency(%q<github_api>, ["= 0.4.11"])
45
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
46
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
47
+ s.add_development_dependency(%q<bundler>, ["~> 1.1.0"])
48
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
49
+ else
50
+ s.add_dependency(%q<rest-client>, [">= 0"])
51
+ s.add_dependency(%q<github_api>, ["= 0.4.11"])
52
+ s.add_dependency(%q<shoulda>, [">= 0"])
53
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
54
+ s.add_dependency(%q<bundler>, ["~> 1.1.0"])
55
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<rest-client>, [">= 0"])
59
+ s.add_dependency(%q<github_api>, ["= 0.4.11"])
60
+ s.add_dependency(%q<shoulda>, [">= 0"])
61
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
62
+ s.add_dependency(%q<bundler>, ["~> 1.1.0"])
63
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
64
+ end
65
+ end
66
+
@@ -0,0 +1,119 @@
1
+ require "rest-client"
2
+ require "github_api"
3
+
4
+ class GithubUploader
5
+ AUTH_NOTE = "Github Uploader (gem)"
6
+
7
+ def self.setup_uploader
8
+ # get the github user name
9
+ login = `git config github.user`.chomp
10
+
11
+ # get repo from git config's origin url
12
+ origin = `git config remote.origin.url`.chomp # url to origin
13
+ # extract USERNAME/REPO_NAME
14
+ # sample urls: https://github.com/emberjs/ember.js.git
15
+ # git://github.com/emberjs/ember.js.git
16
+ # git@github.com:emberjs/ember.js.git
17
+ # git@github.com:emberjs/ember.js
18
+
19
+ repoUrl = origin.match(/github\.com[\/:]((.+?)\/(.+?))(\.git)?$/)
20
+ username = repoUrl[2] # username part of origin url
21
+ repo = repoUrl[3] # repository name part of origin url
22
+
23
+ uploader = GithubUploader.new(login, username, repo)
24
+ uploader.authorize
25
+
26
+ uploader
27
+ end
28
+
29
+ def self.upload_file(uploader, filename, description, file)
30
+ print "Uploading #{filename}..."
31
+ if uploader.upload_file(filename, description, file)
32
+ puts "Success"
33
+ else
34
+ puts "Failure"
35
+ end
36
+ end
37
+
38
+ def initialize(login, username, repo, root=Dir.pwd)
39
+ @login = login
40
+ @username = username
41
+ @repo = repo
42
+ @root = root
43
+ @token = check_token
44
+ end
45
+
46
+ def authorized?
47
+ !!@token
48
+ end
49
+
50
+ def token_path
51
+ File.expand_path(".github-upload-token", @root)
52
+ end
53
+
54
+ def check_token
55
+ File.exist?(token_path) ? File.open(token_path, "rb").read : nil
56
+ end
57
+
58
+ def authorize
59
+ return if authorized?
60
+
61
+ puts "There is no file named .github-upload-token in this folder. This file holds the OAuth token needed to communicate with GitHub."
62
+ puts "You will be asked to enter your GitHub password so a new OAuth token will be created."
63
+ print "GitHub Password: "
64
+ system "stty -echo" # disable echoing of entered chars so password is not shown on console
65
+ pw = STDIN.gets.chomp
66
+ system "stty echo" # enable echoing of entered chars
67
+ puts ""
68
+
69
+ # check if the user already granted access for "Githhub Uploader (gem)" by checking the available authorizations
70
+ response = RestClient.get "https://#{@login}:#{pw}@api.github.com/authorizations"
71
+ JSON.parse(response.to_str).each do |auth|
72
+ if auth["note"] == AUTH_NOTE
73
+ # user already granted access, so we reuse the existing token
74
+ @token = auth["token"]
75
+ end
76
+ end
77
+
78
+ ## we need to create a new token
79
+ unless @token
80
+ payload = {
81
+ :scopes => ["public_repo"],
82
+ :note => AUTH_NOTE,
83
+ :note_url => "https://github.com/#{@username}/#{@repo}"
84
+ }
85
+ response = RestClient.post "https://#{@login}:#{pw}@api.github.com/authorizations", payload.to_json, :content_type => :json
86
+ @token = JSON.parse(response.to_str)["token"]
87
+ end
88
+
89
+ # finally save the token into .github-upload-token
90
+ File.open(".github-upload-token", 'w') {|f| f.write(@token)}
91
+ end
92
+
93
+ def upload_file(filename, description, file)
94
+ return false unless authorized?
95
+
96
+ gh = Github.new :user => @username, :repo => @repo, :oauth_token => @token
97
+
98
+ # remvove previous download with the same name
99
+ gh.repos.downloads do |download|
100
+ if filename == download.name
101
+ gh.repos.delete_download @username, @repo, download.id
102
+ break
103
+ end
104
+ end
105
+
106
+ # step 1
107
+ hash = gh.repos.create_download @username, @repo,
108
+ "name" => filename,
109
+ "size" => File.size(file),
110
+ "description" => description,
111
+ "content_type" => "application/json"
112
+
113
+ # step 2
114
+ gh.repos.upload hash, file
115
+
116
+ return true
117
+ end
118
+
119
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'github_uploader'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestGithubUploader < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: github_uploader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Yehuda Katz
9
+ - Tom Dale
10
+ - Charles Jolley
11
+ - Ember.js contributors
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+ date: 2012-05-01 00:00:00.000000000Z
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: rest-client
19
+ requirement: &70184088111180 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ! '>='
23
+ - !ruby/object:Gem::Version
24
+ version: '0'
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: *70184088111180
28
+ - !ruby/object:Gem::Dependency
29
+ name: github_api
30
+ requirement: &70184088110700 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - =
34
+ - !ruby/object:Gem::Version
35
+ version: 0.4.11
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: *70184088110700
39
+ - !ruby/object:Gem::Dependency
40
+ name: shoulda
41
+ requirement: &70184088110220 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: *70184088110220
50
+ - !ruby/object:Gem::Dependency
51
+ name: rdoc
52
+ requirement: &70184088109740 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: '3.12'
58
+ type: :development
59
+ prerelease: false
60
+ version_requirements: *70184088109740
61
+ - !ruby/object:Gem::Dependency
62
+ name: bundler
63
+ requirement: &70184088126400 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.1.0
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: *70184088126400
72
+ - !ruby/object:Gem::Dependency
73
+ name: jeweler
74
+ requirement: &70184088125920 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ~>
78
+ - !ruby/object:Gem::Version
79
+ version: 1.8.3
80
+ type: :development
81
+ prerelease: false
82
+ version_requirements: *70184088125920
83
+ description: Upload files to github, useful in your Rakefile.
84
+ email: collintmiller@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.mdown
90
+ files:
91
+ - .document
92
+ - .rvmrc
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.mdown
97
+ - Rakefile
98
+ - VERSION
99
+ - github_uploader.gemspec
100
+ - lib/github_uploader.rb
101
+ - test/helper.rb
102
+ - test/test_github_uploader.rb
103
+ homepage: http://github.com/collin/github_uploader
104
+ licenses:
105
+ - MIT
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ segments:
117
+ - 0
118
+ hash: 523407705993375625
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 1.8.10
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: Upload files to github.
131
+ test_files: []