Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 48fdbce

Browse files
committed
Initial commit
0 parents  commit 48fdbce

File tree

10 files changed

+332
-0
lines changed

10 files changed

+332
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
db
2+
dev
3+
.DS_Store
4+
files

Gemfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
source 'http://rubygems.org'
2+
3+
ruby '1.9.3'
4+
5+
gem 'octokit'
6+
gem 'oauth'
7+
gem 'multi_json', '1.6.1'
8+
gem 'sinatra'
9+
gem 'mongo_mapper'
10+
gem 'bson_ext'
11+
gem 'pry'
12+
gem 'rake'
13+
gem 'toml', '~> 0.0.3'
14+
gem 'sidekiq'

Gemfile.lock

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
GEM
2+
remote: http://rubygems.org/
3+
specs:
4+
activemodel (3.2.13)
5+
activesupport (= 3.2.13)
6+
builder (~> 3.0.0)
7+
activesupport (3.2.13)
8+
i18n (= 0.6.1)
9+
multi_json (~> 1.0)
10+
addressable (2.3.5)
11+
blankslate (2.1.2.4)
12+
bson (1.9.1)
13+
bson_ext (1.9.1)
14+
bson (~> 1.9.1)
15+
builder (3.0.4)
16+
celluloid (0.15.2)
17+
timers (~> 1.1.0)
18+
coderay (1.0.9)
19+
connection_pool (1.1.0)
20+
faraday (0.8.8)
21+
multipart-post (~> 1.2.0)
22+
i18n (0.6.1)
23+
json (1.8.0)
24+
method_source (0.8.2)
25+
mongo (1.9.1)
26+
bson (~> 1.9.1)
27+
mongo_mapper (0.12.0)
28+
activemodel (~> 3.0)
29+
activesupport (~> 3.0)
30+
plucky (~> 0.5.2)
31+
multi_json (1.6.1)
32+
multipart-post (1.2.0)
33+
oauth (0.4.7)
34+
octokit (2.3.1)
35+
sawyer (~> 0.5.1)
36+
parslet (1.5.0)
37+
blankslate (~> 2.0)
38+
plucky (0.5.2)
39+
mongo (~> 1.5)
40+
pry (0.9.12.2)
41+
coderay (~> 1.0.5)
42+
method_source (~> 0.8)
43+
slop (~> 3.4)
44+
rack (1.5.2)
45+
rack-protection (1.5.0)
46+
rack
47+
rake (10.0.4)
48+
redis (3.0.5)
49+
redis-namespace (1.3.1)
50+
redis (~> 3.0.0)
51+
sawyer (0.5.1)
52+
addressable (~> 2.3.5)
53+
faraday (~> 0.8, < 0.10)
54+
sidekiq (2.15.1)
55+
celluloid (>= 0.15.1)
56+
connection_pool (>= 1.0.0)
57+
json
58+
redis (>= 3.0.4)
59+
redis-namespace (>= 1.3.1)
60+
sinatra (1.4.3)
61+
rack (~> 1.4)
62+
rack-protection (~> 1.4)
63+
tilt (~> 1.3, >= 1.3.4)
64+
slop (3.4.6)
65+
tilt (1.4.1)
66+
timers (1.1.0)
67+
toml (0.0.4)
68+
parslet
69+
70+
PLATFORMS
71+
ruby
72+
73+
DEPENDENCIES
74+
bson_ext
75+
mongo_mapper
76+
multi_json (= 1.6.1)
77+
oauth
78+
octokit
79+
pry
80+
rake
81+
sidekiq
82+
sinatra
83+
toml (~> 0.0.3)

Procfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
web: bundle exec rackup config.ru -p $PORT
2+
worker: bundle exec sidekiq -r ./fidgit.rb -c 5 -v

Rakefile

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env rake
2+
3+
require './fidgit.rb'
4+
require 'toml'
5+
6+
desc 'Bootstrap GitHub repos'
7+
task :bootstrap_repos do
8+
Repository.delete_all
9+
10+
incoming = TOML.load_file("setup.toml")
11+
incoming['repos'].each do |key, val|
12+
repository = Repository.create( :name => val["name"],
13+
:github_address => val["location"],
14+
:figshare_article_id => val["figshare_article_id"],
15+
:secret => val["secret"])
16+
17+
puts "Created #{repository.github_address} : #{repository.secret}"
18+
end
19+
20+
end
21+
22+
desc 'Setup JSON payload to post release information to Fidgit address'
23+
task :setup_payloads do
24+
unless File.exists?("setup.toml")
25+
puts "Have you set up your config yet? See setup.toml.example"
26+
exit 1
27+
end
28+
29+
incoming = TOML.load_file("setup.toml")
30+
31+
# GITHUB_TOKEN is instantiated in fidgit.rb
32+
client = Octokit::Client.new(:access_token => GITHUB_TOKEN)
33+
34+
incoming['repos'].each do |key, val|
35+
client.create_hook(
36+
"#{client.user.login}/#{val["name"]}",
37+
"web",
38+
{
39+
:url => "#{FIDGIT_LOCATION}/releases?secret=#{val["secret"]}",
40+
:content_type => "json"
41+
},
42+
{
43+
:events => ["release"],
44+
:active => true
45+
}
46+
)
47+
puts "Created hook for #{client.user.login}/#{val["name"]} posting to #{FIDGIT_LOCATION}/releases?secret=#{val["secret"]}"
48+
end
49+
end

config.ru

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$stdout.sync = true
2+
require './fidgit'
3+
run Sinatra::Application

fidgit.rb

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
%w{rubygems sinatra mongo_mapper digest pry multi_json octokit erb sidekiq toml oauth}.each { |dep| require dep }
2+
3+
require 'net/http/post/multipart'
4+
5+
if ENV['MONGOHQ_URL']
6+
uri = URI.parse(ENV['MONGOHQ_URL'])
7+
MongoMapper.connection = Mongo::Connection.from_uri(ENV['MONGOHQ_URL'])
8+
MongoMapper.database = uri.path.gsub(/^\//, '')
9+
else
10+
MongoMapper.setup({"development" => { "host" => "localhost", "database" => "fidgit_development", "port" => 27017}}, 'development')
11+
end
12+
13+
CONFIG = TOML.load_file("setup.toml")
14+
15+
# SETUP
16+
GITHUB_TOKEN = CONFIG['setup']['github_token']
17+
FIDGIT_LOCATION = CONFIG['setup']['fidgit_location']
18+
19+
class Repository
20+
include MongoMapper::Document
21+
22+
key :name, String
23+
key :secret, String
24+
key :github_address, String
25+
key :tag, String
26+
key :commit, String
27+
key :figshare_article_id, String
28+
key :synced, Boolean, :default => false
29+
timestamps!
30+
31+
many :releases
32+
33+
def latest_releases
34+
releases.reverse.take(10)
35+
end
36+
37+
def figshare_api_location
38+
"http://api.figshare.com/v1/my_data/articles/#{self.figshare_article_id}/files"
39+
end
40+
41+
def github_zip_location
42+
"#{github_address}/archive/#{tag}.zip"
43+
end
44+
45+
def update_releases(repository, release)
46+
client = Octokit::Client.new(:access_token => GITHUB_TOKEN)
47+
sha = client.tags(repository['full_name']).first.commit.sha
48+
# Update Repository commit
49+
self.releases << Release.new(:tag => release['tag_name'], :body => release)
50+
self.tag = release['tag_name']
51+
self.commit = sha
52+
save
53+
end
54+
end
55+
56+
class Release
57+
include MongoMapper::EmbeddedDocument
58+
59+
key :tag, String
60+
key :body, Hash
61+
timestamps!
62+
63+
def prerelease?
64+
body['prerelease']
65+
end
66+
end
67+
68+
class RepoWorker
69+
include Sidekiq::Worker
70+
71+
def perform(fidgit_repo_id, repository, release)
72+
@fidgit_repository = Repository.find(fidgit_repo_id)
73+
@fidgit_repository.update_releases(repository, release)
74+
75+
download(@fidgit_repository)
76+
upload(@fidgit_repository)
77+
78+
# Mark as synced
79+
@fidgit_repository.synced = true
80+
@fidgit_repository.save
81+
end
82+
83+
def download(fidgit_repository)
84+
`curl -o tmp/#{fidgit_repository.name}-#{fidgit_repository.tag}.zip -L #{fidgit_repository.github_zip_location}`
85+
end
86+
87+
def upload(fidgit_repository)
88+
consumer = OAuth::Consumer.new(CONFIG['setup']['figshare_consumer_key'], CONFIG['setup']['figshare_consumer_token'],{:site=>"http://api.figshare.com"})
89+
token = { :oauth_token => CONFIG['setup']['figshare_oauth_token'],
90+
:oauth_token_secret => CONFIG['setup']['figshare_oauth_secret']
91+
}
92+
93+
client = OAuth::AccessToken.from_hash(consumer, token)
94+
95+
url = URI.parse(fidgit_repository.figshare_api_location)
96+
97+
# Upload the file from tmp to Figshare.
98+
File.open("tmp/#{fidgit_repository.name}-#{fidgit_repository.tag}.zip") do |dataset|
99+
multipart = Net::HTTP::Put::Multipart.new url.path,
100+
"filedata" => UploadIO.new(dataset, "application/zip", "tmp/#{fidgit_repository.name}-#{fidgit_repository.tag}.zip")
101+
Net::HTTP.start(url.host, url.port) do |http|
102+
consumer.sign!(multipart, client)
103+
result = http.request(multipart)
104+
end
105+
end
106+
end
107+
end
108+
109+
before do
110+
@fidgit_repository = Repository.find_by_secret(params[:secret])
111+
return status 404 unless @fidgit_repository
112+
end
113+
114+
post '/releases' do
115+
@data = MultiJson.decode(request.body)
116+
@release = @data['release']
117+
@repository = @data['repository']
118+
RepoWorker.perform_async(@fidgit_repository.id, @repository, @release)
119+
end
120+
121+
get '/repository' do
122+
erb :repository
123+
end

setup.toml.example

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Config for setup
2+
[setup]
3+
github_token = "a3133YBT45aW3auFd95n"
4+
fidgit_location = "http://fidgit.arfon.org"
5+
figshare_consumer_key = "a3133YBT45aW3auFd95n"
6+
figshare_consumer_token = "a3133YBT45aW3auFd95n"
7+
figshare_oauth_token = "a3133YBT45aW3auFd95n"
8+
figshare_oauth_secret = "a3133YBT45aW3auFd95n"
9+
10+
[repos]
11+
[repos.fidgit]
12+
name = "fidgit"
13+
location = "https://github.com/arfon/fidgit"
14+
figshare_article_id = 821210
15+
secret = "a3133YBT45aW3auFd95n"

tmp/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is where tmp files are downloaded to from GitHub and then pushed up to Figshare.

views/repository.erb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<html>
2+
<head>
3+
<title>Fidgit</title>
4+
<style>
5+
h1{
6+
font-family: Georgia, serif;
7+
font-weight: lighter;
8+
font-size: 25px;
9+
}
10+
11+
h3{
12+
font-family: Georgia, serif;
13+
font-weight: lighter;
14+
font-size: 20px;
15+
}
16+
17+
ul{
18+
font-family: Sans-serif;
19+
}
20+
</style>
21+
</head>
22+
<body>
23+
<h1>GitHub: <%= @fidgit_repository.name %> &middot; <a href="<%= @fidgit_repository.github_address %>">View &rarr;</a></h1>
24+
<h1>Figshare: <%= @fidgit_repository.figshare_article_id %> &middot; <a href="http://figshare.com/preview/_preview/<%= @fidgit_repository.figshare_article_id %>">View &rarr;</a></h1>
25+
<h3>Tag: <%= @fidgit_repository.tag %> (<a href="<%= @fidgit_repository.github_address %>/commit/<%= @fidgit_repository.commit %>" target="_blank"><%= @fidgit_repository.commit %></a>)</h3>
26+
27+
<ul>
28+
<% @fidgit_repository.latest_releases.each do |release| %>
29+
<% if release.prerelease? %>
30+
<li style="color:red;"><%= release.tag %>: <%= release.created_at %> (prerelease)</li>
31+
<% else %>
32+
<li><%= release.tag %>: <%= release.created_at %></li>
33+
<% end %>
34+
<% end %>
35+
</ul>
36+
</body>
37+
</html>
38+

0 commit comments

Comments
 (0)