diff --git a/lib/travis/build/rake_tasks.rb b/lib/travis/build/rake_tasks.rb index 09529b1086..ee9f9fa6eb 100644 --- a/lib/travis/build/rake_tasks.rb +++ b/lib/travis/build/rake_tasks.rb @@ -58,32 +58,9 @@ def fetch_githubusercontent_file(from, host: 'raw.githubusercontent.com', end def latest_release_for(repo) - conn = build_faraday_conn(host: 'api.github.com') - - response = conn.get do |req| - releases_url = "repos/#{repo}/releases" - logger.info "Fetching releases from #{conn.url_prefix}#{releases_url}" - req.url releases_url - oauth_token = ENV.fetch( - 'GITHUB_OAUTH_TOKEN', ENV.fetch('no_scope_token', 'notset') - ) - if oauth_token && !oauth_token.empty? && oauth_token != 'notset' - logger.info( - "Adding 'Authorization' header for api.github.com request" - ) - req.headers['Authorization'] = "token #{oauth_token}" - end - end - - raise "Could not find releases for #{repo}" unless response.success? - - json_data = JSON.parse(response.body) - raise "No releases found for #{repo}" if json_data.empty? - - json_data.sort! do |a, b| - semver_cmp(a['tag_name'].sub(/^v/, ''), b['tag_name'].sub(/^v/, '')) - end - json_data.last['tag_name'] + octokit.latest_release(repo)['tag_name'] + rescue + raise "Could not get latest release for #{repo}" end def file_update_sc_data diff --git a/spec/build/rake_tasks_spec.rb b/spec/build/rake_tasks_spec.rb index 1bbbf0bad6..db46ce749b 100644 --- a/spec/build/rake_tasks_spec.rb +++ b/spec/build/rake_tasks_spec.rb @@ -11,29 +11,8 @@ def tmp_top describe Travis::Build::RakeTasks do subject { described_class } - def releases_response(*versions) - [ - 200, - { 'Content-Type' => 'application/json' }, - JSON.dump( - versions.map { |v| { 'tag_name' => v } } - ) - ] - end - let :request_stubs do Faraday::Adapter::Test::Stubs.new do |stub| - %w[ - creationix/nvm - tmate-io/tmate - tools/godep - travis-ci/gimme - ].each do |repo_slug| - stub.get("/repos/#{repo_slug}/releases") do |*| - releases_response('v1.2.3', 'v1.2.5') - end - end - [ ['tmate-io/tmate', 'tmate-v1.2.5-static-linux-amd64.tar.xz'], ['tools/godep', 'godep_darwin_amd64'], @@ -168,27 +147,36 @@ def releases_response(*versions) expect(ghc_versions).to_not be_exist end - %w[ - public/files/casher - public/files/gimme - public/files/godep_darwin_amd64 - public/files/godep_linux_amd64 - public/files/nvm.sh - public/files/rustup-init.sh - public/files/sbt - public/files/sc-linux.tar.gz - public/files/sc-osx.zip - public/files/tmate-static-linux-amd64.tar.xz - public/version-aliases/ghc.json - ].each do |filename| - it "can fetch #{filename}" do - %w[ - tmp/ghc-versions.html - ].each { |t| Rake::Task[t].reenable } + context 'fetch files' do + let(:uri_template) { Addressable::Template.new 'https://api.github.com/repos/{owner}/{repo}/releases/latest' } + + %w[ + public/files/casher + public/files/gimme + public/files/godep_darwin_amd64 + public/files/godep_linux_amd64 + public/files/nvm.sh + public/files/rustup-init.sh + public/files/sbt + public/files/sc-linux.tar.gz + public/files/sc-osx.zip + public/files/tmate-static-linux-amd64.tar.xz + public/version-aliases/ghc.json + ].each do |filename| + before do + stub_request(:get, uri_template) + .to_return(headers: { content_type: 'application/json' }, body: '{"tag_name": "v1.2.5"}') + end - Rake::Task[filename].reenable - Rake::Task[filename].invoke - expect(tmp_top + filename).to be_exist + it "can fetch #{filename}" do + %w[ + tmp/ghc-versions.html + ].each { |t| Rake::Task[t].reenable } + + Rake::Task[filename].reenable + Rake::Task[filename].invoke + expect(tmp_top + filename).to be_exist + end end end