Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
api: added /version endpoint
Browse files Browse the repository at this point in the history
With this endpoint clients will be able to fetch:

- Info from the version as stated in the VERSION file.
- Info of the git repository if possible (tag, commit and branch).
- Supported API versions by the server.

Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
  • Loading branch information
mssola committed Nov 24, 2017
1 parent cfc5aa9 commit 4a79f22
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
9 changes: 7 additions & 2 deletions config/initializers/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ def self.git?
`git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3 2>/dev/null`.chomp
end

# Read the version from the file.
def self.from_file
version = Rails.root.join("VERSION")
File.read(version).chomp if File.exist?(version)
end

# Version.value returns the app version
# Priority: git tag > git branch/commit > VERSION file
def self.value
Expand All @@ -32,8 +38,7 @@ def self.value
COMMIT.to_s
end
else
version = Rails.root.join("VERSION")
File.read(version).chomp if File.exist?(version)
Version.from_file
end
end
end
2 changes: 2 additions & 0 deletions lib/api/root_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require "api/v1/tags"
require "api/v1/teams"
require "api/v1/users"
require "api/version"

module API
class RootAPI < Grape::API
Expand Down Expand Up @@ -52,6 +53,7 @@ class RootAPI < Grape::API
mount ::API::V1::Tags
mount ::API::V1::Teams
mount ::API::V1::Users
mount ::API::Version

route :any, "*path" do
error!("Not found", 404)
Expand Down
28 changes: 28 additions & 0 deletions lib/api/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module API
class Version < Grape::API
before do
authorization!(force_admin: false)
end

desc "Fetch the version of Portus",
tags: ["version"],
detail: "Returns the version of Portus and the supported API versions"

get "/version" do
version = ::Version.from_file
git = if ::Version.git?
if ::Version::TAG.present?
{ tag: ::Version::TAG }
else
{ branch: ::Version::BRANCH, commit: ::Version::COMMIT }
end
end

{
"api-versions": ["v1"],
git: git,
version: version
}
end
end
end
44 changes: 44 additions & 0 deletions spec/api/grape_api/v1/version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require "rails_helper"

describe API::Version do
let!(:admin) { create(:admin) }
let!(:token) { create(:application_token, user: admin) }

before :each do
@header = build_token_header(token)
end

context "GET /api/version" do
it "returns the proper versioning" do
expect(::Version).to receive(:git?).and_return(true)

get "/api/version", nil, @header

resp = JSON.parse(response.body)
expect(resp["api-versions"]).to eq(["v1"])
expect(resp["version"]).to eq(::Version.from_file)
expect(resp["git"]).to_not include("tag")
end

it "returns the tag when present" do
expect(::Version).to receive(:git?).and_return(true)
expect(::Version::TAG).to receive(:present?).and_return(true)

get "/api/version", nil, @header

resp = JSON.parse(response.body)
expect(resp["git"]).to include("tag")
end

it "returns a null git on stable releases" do
expect(::Version).to receive(:git?).and_return(false)

get "/api/version", nil, @header

resp = JSON.parse(response.body)
expect(resp["api-versions"]).to eq(["v1"])
expect(resp["version"]).to eq(::Version.from_file)
expect(resp["git"]).to be_nil
end
end
end

0 comments on commit 4a79f22

Please sign in to comment.