Skip to content

Commit

Permalink
RakeTasks: refactor latest_release_for
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalie committed Apr 6, 2023
1 parent 67f1396 commit 4d6c01b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 73 deletions.
29 changes: 4 additions & 25 deletions lib/travis/build/rake_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,11 @@ 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/, ''))
begin
octokit.latest_release(repo)['tag_name']
rescue
raise "Could not get latest release for #{repo}"
end
json_data.last['tag_name']
end

def file_update_sc_data
Expand Down
15 changes: 8 additions & 7 deletions public/version-aliases/ghc.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@
"9.0.1-alpha1": "9.0.1-alpha1",
"9.0": "9.0.2",
"9.0.1": "9.0.1",
"9": "9.6.0.20230302",
"9.x": "9.6.0.20230302",
"9.x.x": "9.6.0.20230302",
"9": "9.6.1",
"9.x": "9.6.1",
"9.x.x": "9.6.1",
"9.0.x": "9.0.2",
"9.0.2": "9.0.2",
"9.2.1-alpha1": "9.2.1-alpha1",
Expand All @@ -192,12 +192,13 @@
"9.4.3": "9.4.3",
"9.4.4": "9.4.4",
"9.6.0.20230111": "9.6.0.20230111",
"9.6.x": "9.6.0.20230302",
"9.6": "9.6.1-alpha3",
"9.6.x": "9.6.1",
"9.6": "9.6.1",
"9.6.0.20230128": "9.6.0.20230128",
"9.6.0.20230210": "9.6.0.20230210",
"9.6.0.20230302": "9.6.0.20230302",
"9.6.1-alpha1": "9.6.1-alpha1",
"9.6.1-alpha2": "9.6.1-alpha2",
"9.6.1-alpha3": "9.6.1-alpha3"
}
"9.6.1-alpha3": "9.6.1-alpha3",
"9.6.1": "9.6.1"
}
70 changes: 29 additions & 41 deletions spec/build/rake_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 4d6c01b

Please sign in to comment.