Skip to content
This repository
Browse code

Add upload_latest rake task

  • Loading branch information...
commit bceb02411a4cfdf789a5f20fbbc4e96da9327757 1 parent 7d2f845
Clemens Müller pangratz authored
2  .gitignore
@@ -7,4 +7,6 @@ dist/
7 7 .DS_Store
8 8 .project
9 9
  10 +.github-upload-token
  11 +
10 12 tests/ember-data-tests.js
2  .travis.yml
@@ -6,3 +6,5 @@ before_script:
6 6 - "sh -e /etc/init.d/xvfb start"
7 7 - "rake clean"
8 8 script: "rake test[all]"
  9 +notifications:
  10 + webhooks: http://emberjs-master-builds.herokuapp.com/upload/data
3  Gemfile
@@ -7,7 +7,8 @@ gem "uglifier", "~> 1.0.3"
7 7
8 8 group :development do
9 9 gem "rack"
10   - gem "github-upload"
  10 + gem "rest-client"
  11 + gem "github_api"
11 12 gem "ember-docs", :git => "https://github.com/emberjs/docs-generator.git"
12 13 gem "kicker"
13 14 end
52 Gemfile.lock
... ... @@ -1,6 +1,6 @@
1 1 GIT
2 2 remote: https://github.com/emberjs/docs-generator.git
3   - revision: cfbb82496c1c342ff6da49fc46559dd8fddf8b55
  3 + revision: cfbc2a3aabc08de305224b290ad8f37f3ef5f847
4 4 specs:
5 5 ember-docs (0.1)
6 6 rack
@@ -8,7 +8,7 @@ GIT
8 8
9 9 GIT
10 10 remote: https://github.com/livingsocial/rake-pipeline.git
11   - revision: b70ca6cad7655e58d13031f3e24df7dfc74f9030
  11 + revision: 543f4322fe70facee9572d29ddabf7f090dad68a
12 12 specs:
13 13 rake-pipeline (0.6.0)
14 14 rake (~> 0.9.0)
@@ -16,7 +16,7 @@ GIT
16 16
17 17 GIT
18 18 remote: https://github.com/wycats/rake-pipeline-web-filters.git
19   - revision: ba0b8a00356b4c854930a8e849b5629d51ffd70f
  19 + revision: 81a22fb0808dfdeab8ed92d5d8c898ad198b9938
20 20 specs:
21 21 rake-pipeline-web-filters (0.6.0)
22 22 rack
@@ -26,30 +26,35 @@ GEM
26 26 remote: http://rubygems.org/
27 27 specs:
28 28 colored (1.2)
29   - confparser (0.0.2.1)
30   - execjs (1.3.0)
  29 + execjs (1.4.0)
31 30 multi_json (~> 1.0)
32   - faster_xml_simple (0.5.0)
33   - libxml-ruby (>= 0.3.8.4)
34   - github-upload (0.0.2)
35   - confparser
36   - net-github-upload (>= 0.0.6)
37   - httpclient (2.2.4)
38   - json (1.6.6)
  31 + faraday (0.8.1)
  32 + multipart-post (~> 1.1)
  33 + github_api (0.5.4)
  34 + faraday (~> 0.8.0)
  35 + hashie (~> 1.2.0)
  36 + multi_json (~> 1.3)
  37 + nokogiri (~> 1.5.2)
  38 + oauth2 (~> 0.7)
  39 + hashie (1.2.0)
  40 + httpauth (0.1)
39 41 kicker (2.5.0)
40 42 rb-fsevent
41   - libxml-ruby (2.3.2)
42   - multi_json (1.2.0)
43   - net-github-upload (0.0.8)
44   - faster_xml_simple
45   - httpclient
46   - json
47   - nokogiri (>= 1.4.0)
48   - nokogiri (1.5.2)
  43 + mime-types (1.18)
  44 + multi_json (1.3.6)
  45 + multipart-post (1.1.5)
  46 + nokogiri (1.5.3)
  47 + oauth2 (0.7.1)
  48 + faraday (~> 0.8)
  49 + httpauth (~> 0.1)
  50 + multi_json (~> 1.0)
  51 + rack (~> 1.4)
49 52 rack (1.4.1)
50 53 rake (0.9.2.2)
51   - rb-fsevent (0.9.0)
52   - thor (0.14.6)
  54 + rb-fsevent (0.9.1)
  55 + rest-client (1.6.7)
  56 + mime-types (>= 1.16)
  57 + thor (0.15.2)
53 58 uglifier (1.0.4)
54 59 execjs (>= 0.3.0)
55 60 multi_json (>= 1.0.2)
@@ -60,9 +65,10 @@ PLATFORMS
60 65 DEPENDENCIES
61 66 colored
62 67 ember-docs!
63   - github-upload
  68 + github_api
64 69 kicker
65 70 rack
66 71 rake-pipeline!
67 72 rake-pipeline-web-filters!
  73 + rest-client
68 74 uglifier (~> 1.0.3)
43 Rakefile
@@ -10,6 +10,40 @@ def pipeline
10 10 Rake::Pipeline::Project.new("Assetfile")
11 11 end
12 12
  13 +def setup_uploader
  14 + require './lib/github_uploader'
  15 +
  16 + # get the github user name
  17 + login = `git config github.user`.chomp
  18 +
  19 + # get repo from git config's origin url
  20 + origin = `git config remote.origin.url`.chomp # url to origin
  21 + # extract USERNAME/REPO_NAME
  22 + # sample urls: https://github.com/emberjs/ember.js.git
  23 + # git://github.com/emberjs/ember.js.git
  24 + # git@github.com:emberjs/ember.js.git
  25 + # git@github.com:emberjs/ember.js
  26 +
  27 + repoUrl = origin.match(/github\.com[\/:]((.+?)\/(.+?))(\.git)?$/)
  28 + username = repoUrl[2] # username part of origin url
  29 + repo = repoUrl[3] # repository name part of origin url
  30 +
  31 + token = ENV["GH_OAUTH_TOKEN"]
  32 + uploader = GithubUploader.new(login, username, repo, token)
  33 + uploader.authorize
  34 +
  35 + uploader
  36 +end
  37 +
  38 +def upload_file(uploader, filename, description, file)
  39 + print "Uploading #{filename}..."
  40 + if uploader.upload_file(filename, description, file)
  41 + puts "Success"
  42 + else
  43 + puts "Failure"
  44 + end
  45 +end
  46 +
13 47 desc "Strip trailing whitespace for JavaScript files in packages"
14 48 task :strip_whitespace do
15 49 Dir["packages/**/*.js"].each do |name|
@@ -34,6 +68,15 @@ task :clean do
34 68 puts "Done"
35 69 end
36 70
  71 +desc "Upload latest Ember Data build to GitHub repository"
  72 +task :upload_latest => :dist do
  73 + uploader = setup_uploader
  74 +
  75 + # Upload minified first, so non-minified shows up on top
  76 + upload_file(uploader, 'ember-data-latest.min.js', "Ember Data Master (minified)", "dist/ember-data.min.js")
  77 + upload_file(uploader, 'ember-data-latest.js', "Ember Data Master", "dist/ember-data.js")
  78 +end
  79 +
37 80 desc "Run tests with phantomjs"
38 81 task :test, [:suite] => :dist do |t, args|
39 82 unless system("which phantomjs > /dev/null 2>&1")
86 lib/github_uploader.rb
... ... @@ -0,0 +1,86 @@
  1 +require "rest-client"
  2 +require "github_api"
  3 +
  4 +class GithubUploader
  5 +
  6 + def initialize(login, username, repo, token=nil, root=Dir.pwd)
  7 + @login = login
  8 + @username = username
  9 + @repo = repo
  10 + @root = root
  11 + @token = token || check_token
  12 + end
  13 +
  14 + def authorized?
  15 + !!@token
  16 + end
  17 +
  18 + def token_path
  19 + File.expand_path(".github-upload-token", @root)
  20 + end
  21 +
  22 + def check_token
  23 + File.exist?(token_path) ? File.open(token_path, "rb").read : nil
  24 + end
  25 +
  26 + def authorize
  27 + return if authorized?
  28 +
  29 + puts "There is no file named .github-upload-token in this folder. This file holds the OAuth token needed to communicate with GitHub."
  30 + puts "You will be asked to enter your GitHub password so a new OAuth token will be created."
  31 + print "GitHub Password: "
  32 + system "stty -echo" # disable echoing of entered chars so password is not shown on console
  33 + pw = STDIN.gets.chomp
  34 + system "stty echo" # enable echoing of entered chars
  35 + puts ""
  36 +
  37 + # check if the user already granted access for Ember.js Uploader by checking the available authorizations
  38 + response = RestClient.get "https://#{@login}:#{pw}@api.github.com/authorizations"
  39 + JSON.parse(response.to_str).each do |auth|
  40 + if auth["note"] == "Ember.js Uploader"
  41 + # user already granted access, so we reuse the existing token
  42 + @token = auth["token"]
  43 + end
  44 + end
  45 +
  46 + ## we need to create a new token
  47 + unless @token
  48 + payload = {
  49 + :scopes => ["public_repo"],
  50 + :note => "Ember.js Uploader",
  51 + :note_url => "https://github.com/#{@username}/#{@repo}"
  52 + }
  53 + response = RestClient.post "https://#{@login}:#{pw}@api.github.com/authorizations", payload.to_json, :content_type => :json
  54 + @token = JSON.parse(response.to_str)["token"]
  55 + end
  56 +
  57 + # finally save the token into .github-upload-token
  58 + File.open(".github-upload-token", 'w') {|f| f.write(@token)}
  59 + end
  60 +
  61 + def upload_file(filename, description, file)
  62 + return false unless authorized?
  63 +
  64 + gh = Github.new :user => @username, :repo => @repo, :oauth_token => @token
  65 +
  66 + # remvove previous download with the same name
  67 + gh.repos.downloads.list @username, @repo do |download|
  68 + if filename == download.name
  69 + gh.repos.downloads.delete @username, @repo, download.id
  70 + break
  71 + end
  72 + end
  73 +
  74 + # step 1
  75 + hash = gh.repos.downloads.create @username, @repo,
  76 + "name" => filename,
  77 + "size" => File.size(file),
  78 + "description" => description
  79 +
  80 + # step 2
  81 + gh.repos.downloads.upload hash, file
  82 +
  83 + return true
  84 + end
  85 +
  86 +end

0 comments on commit bceb024

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