|
| 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 |
0 commit comments