github_downloads 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/github_downloads/uploader.rb +126 -0
- data/lib/github_downloads/version.rb +3 -0
- data/lib/github_downloads.rb +1 -0
- metadata +96 -0
@@ -0,0 +1,126 @@
|
|
1
|
+
require "rest-client"
|
2
|
+
require "github_api"
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
module GithubDownloads
|
6
|
+
class Uploader
|
7
|
+
|
8
|
+
def initialize(login=nil, username=nil, repo=nil, token=nil, root=Dir.pwd)
|
9
|
+
@login = init_login(login)
|
10
|
+
@username = init_username(username)
|
11
|
+
@repo = init_repo(repo)
|
12
|
+
@root = root
|
13
|
+
@token = token || ENV['GH_OAUTH_TOKEN'] || check_token
|
14
|
+
end
|
15
|
+
|
16
|
+
def init_login(login=nil)
|
17
|
+
login || ENV['GH_LOGIN'] || `git config github.user`.chomp
|
18
|
+
end
|
19
|
+
|
20
|
+
def init_username(username=nil)
|
21
|
+
username || ENV['GH_USERNAME'] || repo_url[2]
|
22
|
+
end
|
23
|
+
|
24
|
+
def init_repo(repo=nil)
|
25
|
+
repo || ENV['GH_REPOSITORY'] || repo_url[3]
|
26
|
+
end
|
27
|
+
|
28
|
+
def repo_url
|
29
|
+
origin = `git config remote.origin.url`.chomp
|
30
|
+
origin.match(/github\.com[\/:]((.+?)\/(.+?))(\.git)?$/) || []
|
31
|
+
end
|
32
|
+
|
33
|
+
def authorized?
|
34
|
+
!!@token
|
35
|
+
end
|
36
|
+
|
37
|
+
def token_path
|
38
|
+
File.expand_path(".github-upload-token", @root)
|
39
|
+
end
|
40
|
+
|
41
|
+
def check_token
|
42
|
+
File.exist?(token_path) ? File.open(token_path, "rb").read : nil
|
43
|
+
end
|
44
|
+
|
45
|
+
def authorize
|
46
|
+
return if authorized?
|
47
|
+
|
48
|
+
require 'cgi'
|
49
|
+
|
50
|
+
puts "There is no file named .github-upload-token in this folder. This file holds the OAuth token needed to communicate with GitHub."
|
51
|
+
puts "You will be asked to enter your GitHub password so a new OAuth token will be created."
|
52
|
+
print "GitHub Password for #{@login}: "
|
53
|
+
system "stty -echo" # disable echoing of entered chars so password is not shown on console
|
54
|
+
pw = STDIN.gets.chomp
|
55
|
+
system "stty echo" # enable echoing of entered chars
|
56
|
+
puts ""
|
57
|
+
|
58
|
+
# check if the user already granted access for Ember.js Uploader by checking the available authorizations
|
59
|
+
response = RestClient.get "https://#{CGI.escape(@login)}:#{CGI.escape(pw)}@api.github.com/authorizations"
|
60
|
+
JSON.parse(response.to_str).each do |auth|
|
61
|
+
if auth["note"] == "GitHub Downloads Gem"
|
62
|
+
# user already granted access, so we reuse the existing token
|
63
|
+
@token = auth["token"]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
## we need to create a new token
|
68
|
+
unless @token
|
69
|
+
payload = {
|
70
|
+
:scopes => ["public_repo"],
|
71
|
+
:note => "GitHub Downloads Gem",
|
72
|
+
:note_url => "https://github.com/pangratz/github_downloads"
|
73
|
+
}
|
74
|
+
response = RestClient.post "https://#{@login}:#{pw}@api.github.com/authorizations", payload.to_json, :content_type => :json
|
75
|
+
@token = JSON.parse(response.to_str)["token"]
|
76
|
+
end
|
77
|
+
|
78
|
+
# finally save the token into .github-upload-token
|
79
|
+
File.open(token_path, 'w') {|f| f.write(@token)}
|
80
|
+
|
81
|
+
# add entry to .gitignore if not already exists
|
82
|
+
gitignore = File.expand_path(".gitignore", @root)
|
83
|
+
includes = File.open(gitignore).lines.any? { |line| line.chomp == '.github-upload-token' }
|
84
|
+
if !includes
|
85
|
+
File.open(gitignore, "a") do |f|
|
86
|
+
f.puts("\n# .github-upload-token stores OAuth token, used by github_downloads gem")
|
87
|
+
f.puts(".github-upload-token")
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def remove_file(filename)
|
93
|
+
return false unless authorized?
|
94
|
+
|
95
|
+
gh = Github.new :user => @username, :repo => @repo, :oauth_token => @token
|
96
|
+
|
97
|
+
# remvove previous download with the same name
|
98
|
+
gh.repos.downloads.list @username, @repo do |download|
|
99
|
+
if filename == download.name
|
100
|
+
gh.repos.downloads.delete @username, @repo, download.id
|
101
|
+
break
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def upload_file(filename, description, file)
|
107
|
+
return false unless authorized?
|
108
|
+
|
109
|
+
remove_file(filename)
|
110
|
+
|
111
|
+
gh = Github.new :user => @username, :repo => @repo, :oauth_token => @token
|
112
|
+
|
113
|
+
# step 1
|
114
|
+
hash = gh.repos.downloads.create @username, @repo,
|
115
|
+
"name" => filename,
|
116
|
+
"size" => File.size(file),
|
117
|
+
"description" => description
|
118
|
+
|
119
|
+
# step 2
|
120
|
+
gh.repos.downloads.upload hash, file
|
121
|
+
|
122
|
+
return true
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'github_downloads/uploader'
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: github_downloads
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Clemens Müller
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rest-client
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: github_api
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Library to upload files to GitHub Downloads section of a specific repository
|
63
|
+
email:
|
64
|
+
- cmueller.418@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- lib/github_downloads.rb
|
70
|
+
- lib/github_downloads/uploader.rb
|
71
|
+
- lib/github_downloads/version.rb
|
72
|
+
homepage: https://github.com/pangratz/github_downloads
|
73
|
+
licenses: []
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 1.8.24
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: Upload files to GitHub Downloads
|
96
|
+
test_files: []
|