From b1da4c6c0e95e0ea1c2bf897c745ff31e35ab656 Mon Sep 17 00:00:00 2001 From: Samir Noir Date: Tue, 25 Aug 2020 16:06:13 +0200 Subject: [PATCH] Add two new GET parameters to request reference api by date. Instead of using version, this commit add timestamp and date parameters --- .gitlab-ci.yml | 2 +- app/controllers/resources_controller.rb | 4 +++- lib/grid5000/repository.rb | 21 +++++++++++++-------- spec/lib/grid5000/repository_spec.rb | 16 +++++++++++----- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f0e9d8a0..2ad907e0 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -72,7 +72,7 @@ deb-for-buster: &deb-for-buster - mk-build-deps -ir -t 'apt-get -y --no-install-recommends' - gem install bundler -v 2.1.4 - apt-get install -y curl pkg-config - - rm ../*deb + - rm -f ../*deb - dpkg-buildpackage - cp ../*deb . artifacts: diff --git a/app/controllers/resources_controller.rb b/app/controllers/resources_controller.rb index 0ad33af9..1accef09 100644 --- a/app/controllers/resources_controller.rb +++ b/app/controllers/resources_controller.rb @@ -96,7 +96,9 @@ class ResourcesController < ApplicationController object = repository.find( path.gsub(%r{/?platforms}, ''), branch: params[:branch], - version: params[:version] + version: params[:version], + timestamp: params[:timestamp], + date: params[:date] ) raise ServerUnavailable if object.is_a?(Exception) diff --git a/lib/grid5000/repository.rb b/lib/grid5000/repository.rb index fcb45a11..83b4b4d7 100644 --- a/lib/grid5000/repository.rb +++ b/lib/grid5000/repository.rb @@ -112,14 +112,17 @@ module Grid5000 def find_commit_for(options = {}) options[:branch] ||= 'master' - version, branch = options.values_at(:version, :branch) - if version.nil? - instance.branches[branch].target - elsif version.to_s.length == 40 # SHA + version, branch, timestamp, date = options.values_at(:version, :branch, :timestamp, :date) + if version && version.to_s.length == 40 # SHA instance.lookup(version) - else - # version should be a date, get the closest commit - date = version.to_i + elsif timestamp || date || version + if timestamp + ts = timestamp.to_i + elsif date + ts = Time.parse(date).to_i + else + ts = version.to_i + end return nil if instance.branches[branch].nil? @@ -128,12 +131,14 @@ module Grid5000 walker.push(instance.branches[branch].target.oid) commits = walker.select do |commit| - commit.epoch_time <= date + commit.epoch_time <= ts end commits.map! { |commit| commit.oid } sha = commits.first find_commit_for(options.merge(version: sha)) + else + instance.branches[branch].target end rescue Rugged::OdbError nil diff --git a/spec/lib/grid5000/repository_spec.rb b/spec/lib/grid5000/repository_spec.rb index e00eca27..9f257077 100644 --- a/spec/lib/grid5000/repository_spec.rb +++ b/spec/lib/grid5000/repository_spec.rb @@ -65,15 +65,21 @@ describe Grid5000::Repository do expect(commit.oid).to eq(@latest_commit) end - it 'should find the commit associated with the given version [version=DATE] 1/2' do + it 'should find the commit associated with the given timestamp [version=TS]' do date = Time.parse('2009-03-13 17:24:20 +0100') commit = @repository.find_commit_for(version: date.to_i) expect(commit.oid).to eq('b00bd30bf69c322ffe9aca7a9f6e3be0f29e20f4') end - it 'should find the commit associated with the given version [version=DATE] 2/2' do - date = Time.parse('2009-03-13 17:24:47 +0100') - commit = @repository.find_commit_for(version: date.to_i) + it 'should find the commit associated with the given timestamp [timestamp=TS] 1/2' do + date = Time.parse('2009-03-13 17:24:20 +0100') + commit = @repository.find_commit_for(timestamp: date.to_i) + expect(commit.oid).to eq('b00bd30bf69c322ffe9aca7a9f6e3be0f29e20f4') + end + + it 'should find the commit associated with the given date [date=DATE] 2/2' do + date = '2009-03-13 17:24:47 +0100' + commit = @repository.find_commit_for(date: date) expect(commit.oid).to eq('e07895a4b480aaa8e11c35549a97796dcc4a307d') end @@ -87,7 +93,7 @@ describe Grid5000::Repository do it 'should return nil when asking for a version from a branch that does not exist' do date = Time.parse('Fri Mar 13 17:24:47 2009 +0100') commit = @repository.find_commit_for( - version: date.to_i, + timestamp: date.to_i, branch: 'doesnotexist' ) expect(commit).to be_nil -- GitLab