versions_controller.rb 5.76 KB
Newer Older
PARISOT Clement's avatar
github    
PARISOT Clement committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright (c) 2009-2011 Cyril Rohr, INRIA Rennes - Bretagne Atlantique
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
class VersionsController < ApplicationController
16
17
  include Swagger::Blocks

18
  MAX_AGE = 60.seconds
19

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  swagger_path '/versions' do
    operation :get do
      key :summary, 'List reference-repository versions'
      key :description, 'Fetch a collection of reference-repository git version. '\
        'A version is a Git commit.'
      key :tags, ['version']

      [:branch, :limit, :offset].each do |param|
        parameter do
          key :$ref, param
        end
      end

      response 200 do
        key :description, "Grid'5000's reference-repository commit collection."
35
        content api_media_type(:g5kcollectionjson)
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
      end
    end
  end

  swagger_path '/versions/{versionId}' do
    operation :get do
      key :summary, 'Get version of reference-repository'
      key :description, 'Fetch a specific version commit item of reference-repository.'
      key :tags, ['version']

      [:versionId, :branch, :limit].each do |param|
        parameter do
          key :$ref, param
        end
      end

      response 200 do
        key :description, "Reference-repository's commit item."
54
        content api_media_type(:g5kitemjson)
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
      end

      response 404 do
        key :description, "Reference-repository's commit not found."
        content :'text/plain'
      end
    end
  end

  swagger_path '/sites/{siteId}/versions' do
    operation :get do
      key :summary, "List sites's reference-repository versions"
      key :description, 'Fetch a collection of reference-repository git versions '\
        'for a specific site. A version is a Git commit.'
      key :tags, ['version']

      [:siteId, :branch, :limit].each do |param|
        parameter do
          key :$ref, param
        end
      end

      response 200 do
        key :description, "Grid'5000's reference-repository commit collection."
79
        content api_media_type(:g5kcollectionjson)
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
      end
    end
  end

  swagger_path '/sites/{siteId}/versions/{versionId}' do
    operation :get do
      key :summary, "Get version of site's reference-repository"
      key :description, 'Fetch a specific version commit item of reference-repository.'
      key :tags, ['version']

      [:siteId, :versionId, :branch, :limit].each do |param|
        parameter do
          key :$ref, param
        end
      end

      response 200 do
        key :description, "Reference-repository's commit item."
98
        content api_media_type(:g5kitemjson)
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
      end

      response 404 do
        key :description, "Reference-repository's commit not found."
        content :'text/plain'
      end
    end
  end

  swagger_component do
    parameter :versionId do
      key :name, :versionId
      key :in, :path
      key :description, 'ID of version to fetch, as a Git commit hash.'
      key :required, true
114
115
116
      schema do
        key :type, :string
      end
117
118
119
    end
  end

120
  def index
121
    vary_on :accept; allow :get
122
123
124
125

    branch = params[:branch] || 'master'
    branch = ['origin', branch].join('/') unless Rails.env == 'test'

Cyril Rohr's avatar
Cyril Rohr committed
126
    versions = repository.versions_for(
127
      resource_path,
128
      branch: branch,
Samir Noir's avatar
Samir Noir committed
129
130
      offset: params[:offset],
      limit: params[:limit]
Cyril Rohr's avatar
Cyril Rohr committed
131
    )
132

Samir Noir's avatar
Samir Noir committed
133
    raise NotFound, "#{resource_path} does not exist." if versions['total'] == 0
134

Samir Noir's avatar
Samir Noir committed
135
    versions['items'].map! do |commit|
136
      metadata_for_commit(commit, resource_path)
Samir Noir's avatar
Samir Noir committed
137
138
    end
    versions['links'] = [
139
      {
Samir Noir's avatar
Samir Noir committed
140
141
142
        'rel' => 'self',
        'href' => uri_to("#{resource_path}/versions"),
        'type' => api_media_type(:g5kcollectionjson)
143
144
      },
      {
Samir Noir's avatar
Samir Noir committed
145
146
147
        'rel' => 'parent',
        'href' => uri_to(resource_path.split('/')[0..-2].join('/').to_s),
        'type' => api_media_type(:g5kitemjson)
148
149
      }
    ]
150

151
    etag versions.hash
Samir Noir's avatar
Samir Noir committed
152
    expires_in MAX_AGE, public: true
153

154
    render_result(versions)
155
  end
156

157
  def show
158
159
    vary_on :accept; allow :get
    version = params[:id]
160

Cyril Rohr's avatar
Cyril Rohr committed
161
    versions = repository.versions_for(
162
      resource_path,
Samir Noir's avatar
Samir Noir committed
163
164
165
      branch: version,
      offset: 0,
      limit: 1
Cyril Rohr's avatar
Cyril Rohr committed
166
    )
Samir Noir's avatar
Samir Noir committed
167
    if versions['total'] == 0
168
      raise NotFound, "The resource '#{resource_path}' does not exist."
Samir Noir's avatar
Samir Noir committed
169
    end
Samir Noir's avatar
Samir Noir committed
170

171
    # etag compute_etag(commit.id, resource_uri, response['Content-Type'], options.release_hash)
172

Samir Noir's avatar
Samir Noir committed
173
    output = metadata_for_commit(versions['items'][0], resource_path)
174
175

    etag versions.hash
Samir Noir's avatar
Samir Noir committed
176
    expires_in MAX_AGE, public: true
177

178
    render_result(output)
179
  end
180

181
182
183
184
185
186
187
  def latest
    vary_on :accept; allow :get

    sha = repository.find(resource_path).oid
    render location: request.fullpath.gsub(/latest(.json)?$/, sha), status: 307, plain: ''
  end

188
  protected
Samir Noir's avatar
Samir Noir committed
189

190
  def resource_path
191
192
193
194
195
    if params[:resource]
      @resource_path ||= params[:resource].gsub(%r{/?platforms}, '')
    else
      ''
    end
196
  end
197

198
  def metadata_for_commit(commit, resource_path)
199
200
201
    {
      'uid' => commit.oid,
      'date' => commit.time.httpdate,
202
      'message' => commit.message.chomp,
203
      'author' => commit.author[:name],
204
205
206
      'type' => 'version',
      'links' => [
        {
Samir Noir's avatar
Samir Noir committed
207
208
209
          'rel' => 'self',
          'href' => uri_to("#{resource_path}/versions/#{commit.oid}"),
          'type' => api_media_type(:g5kitemjson)
210
211
        },
        {
Samir Noir's avatar
Samir Noir committed
212
213
214
          'rel' => 'parent',
          'href' => uri_to(resource_path),
          'type' => api_media_type(:g5kitemjson)
215
        }
216
      ]
217
218
219
    }
  end
end