Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor upload_latest task to use github_downloads gem

  • Loading branch information...
commit 4c6cd8f18fe77001570bacfc4c760bd600dc3619 1 parent 45603a5
@pangratz pangratz authored
Showing with 18 additions and 119 deletions.
  1. +1 −2  Gemfile
  2. +15 −9 Gemfile.lock
  3. +2 −20 Rakefile
  4. +0 −88 lib/github_uploader.rb
View
3  Gemfile
@@ -7,8 +7,7 @@ gem "uglifier", "~> 1.0.3"
group :development do
gem "rack"
- gem "rest-client"
- gem "github_api"
+ gem "github_downloads"
gem "ember-docs", :git => "https://github.com/emberjs/docs-generator.git"
gem "kicker"
end
View
24 Gemfile.lock
@@ -30,25 +30,32 @@ GEM
multi_json (~> 1.0)
faraday (0.8.1)
multipart-post (~> 1.1)
- github_api (0.5.4)
- faraday (~> 0.8.0)
+ github_api (0.6.1)
+ faraday (~> 0.8.1)
hashie (~> 1.2.0)
multi_json (~> 1.3)
nokogiri (~> 1.5.2)
- oauth2 (~> 0.7)
+ oauth2
+ github_downloads (0.1.1)
+ github_api (~> 0.6.0)
+ rest-client (~> 1.6.7)
hashie (1.2.0)
httpauth (0.1)
+ json (1.7.3)
+ jwt (0.1.4)
+ json (>= 1.2.4)
kicker (2.5.0)
rb-fsevent
- mime-types (1.18)
+ mime-types (1.19)
multi_json (1.3.6)
multipart-post (1.1.5)
- nokogiri (1.5.3)
- oauth2 (0.7.1)
+ nokogiri (1.5.5)
+ oauth2 (0.8.0)
faraday (~> 0.8)
httpauth (~> 0.1)
+ jwt (~> 0.1.4)
multi_json (~> 1.0)
- rack (~> 1.4)
+ rack (~> 1.2)
rack (1.4.1)
rake (0.9.2.2)
rb-fsevent (0.9.1)
@@ -65,10 +72,9 @@ PLATFORMS
DEPENDENCIES
colored
ember-docs!
- github_api
+ github_downloads
kicker
rack
rake-pipeline!
rake-pipeline-web-filters!
- rest-client
uglifier (~> 1.0.3)
View
22 Rakefile
@@ -10,27 +10,9 @@ def pipeline
end
def setup_uploader
- require './lib/github_uploader'
-
- # get the github user name
- login = `git config github.user`.chomp
-
- # get repo from git config's origin url
- origin = `git config remote.origin.url`.chomp # url to origin
- # extract USERNAME/REPO_NAME
- # sample urls: https://github.com/emberjs/ember.js.git
- # git://github.com/emberjs/ember.js.git
- # git@github.com:emberjs/ember.js.git
- # git@github.com:emberjs/ember.js
-
- repoUrl = origin.match(/github\.com[\/:]((.+?)\/(.+?))(\.git)?$/)
- username = repoUrl[2] # username part of origin url
- repo = repoUrl[3] # repository name part of origin url
-
- token = ENV["GH_OAUTH_TOKEN"]
- uploader = GithubUploader.new(login, username, repo, token)
+ require 'github_downloads'
+ uploader = GithubDownloads::Uploader.new
uploader.authorize
-
uploader
end
View
88 lib/github_uploader.rb
@@ -1,88 +0,0 @@
-require "rest-client"
-require "github_api"
-
-class GithubUploader
-
- def initialize(login, username, repo, token=nil, root=Dir.pwd)
- @login = login
- @username = username
- @repo = repo
- @root = root
- @token = token || check_token
- end
-
- def authorized?
- !!@token
- end
-
- def token_path
- File.expand_path(".github-upload-token", @root)
- end
-
- def check_token
- File.exist?(token_path) ? File.open(token_path, "rb").read : nil
- end
-
- def authorize
- return if authorized?
-
- require 'cgi'
-
- puts "There is no file named .github-upload-token in this folder. This file holds the OAuth token needed to communicate with GitHub."
- puts "You will be asked to enter your GitHub password so a new OAuth token will be created."
- print "GitHub Password: "
- system "stty -echo" # disable echoing of entered chars so password is not shown on console
- pw = STDIN.gets.chomp
- system "stty echo" # enable echoing of entered chars
- puts ""
-
- # check if the user already granted access for Ember.js Uploader by checking the available authorizations
- response = RestClient.get "https://#{CGI.escape(@login)}:#{CGI.escape(pw)}@api.github.com/authorizations"
- JSON.parse(response.to_str).each do |auth|
- if auth["note"] == "Ember.js Uploader"
- # user already granted access, so we reuse the existing token
- @token = auth["token"]
- end
- end
-
- ## we need to create a new token
- unless @token
- payload = {
- :scopes => ["public_repo"],
- :note => "Ember.js Uploader",
- :note_url => "https://github.com/#{@username}/#{@repo}"
- }
- response = RestClient.post "https://#{@login}:#{pw}@api.github.com/authorizations", payload.to_json, :content_type => :json
- @token = JSON.parse(response.to_str)["token"]
- end
-
- # finally save the token into .github-upload-token
- File.open(".github-upload-token", 'w') {|f| f.write(@token)}
- end
-
- def upload_file(filename, description, file)
- return false unless authorized?
-
- gh = Github.new :user => @username, :repo => @repo, :oauth_token => @token
-
- # remvove previous download with the same name
- gh.repos.downloads.list @username, @repo do |download|
- if filename == download.name
- gh.repos.downloads.delete @username, @repo, download.id
- break
- end
- end
-
- # step 1
- hash = gh.repos.downloads.create @username, @repo,
- "name" => filename,
- "size" => File.size(file),
- "description" => description
-
- # step 2
- gh.repos.downloads.upload hash, file
-
- return true
- end
-
-end
Please sign in to comment.
Something went wrong with that request. Please try again.