Commit f933ace6 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Implement upload package logic without workhorse for now

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 52ffee74
class Packages::MavenMetadatum < ActiveRecord::Base class Packages::MavenMetadatum < ActiveRecord::Base
belongs_to :package belongs_to :package
validates :package, presence: true
end end
...@@ -2,4 +2,6 @@ class Packages::Package < ActiveRecord::Base ...@@ -2,4 +2,6 @@ class Packages::Package < ActiveRecord::Base
belongs_to :project belongs_to :project
has_many :package_files has_many :package_files
has_one :maven_metadatum has_one :maven_metadatum
validates :project, presence: true
end end
class Packages::PackageFile < ActiveRecord::Base class Packages::PackageFile < ActiveRecord::Base
belongs_to :package belongs_to :package
validates :package, presence: true
mount_uploader :file, PackageFileUploader mount_uploader :file, PackageFileUploader
end end
...@@ -4,6 +4,10 @@ class Packages::PackageFileUploader < GitlabUploader ...@@ -4,6 +4,10 @@ class Packages::PackageFileUploader < GitlabUploader
storage_options Gitlab.config.packages storage_options Gitlab.config.packages
def filename
model.file_name
end
def store_dir def store_dir
dynamic_segment dynamic_segment
end end
......
...@@ -9,6 +9,7 @@ class CreatePackagesPackageFiles < ActiveRecord::Migration ...@@ -9,6 +9,7 @@ class CreatePackagesPackageFiles < ActiveRecord::Migration
create_table :packages_package_files do |t| create_table :packages_package_files do |t|
t.references :package, index: true, null: false t.references :package, index: true, null: false
t.string :file t.string :file
t.string :file_name, null: false
t.integer :file_type t.integer :file_type
t.integer :file_store t.integer :file_store
t.integer :size t.integer :size
......
...@@ -1921,6 +1921,42 @@ ActiveRecord::Schema.define(version: 20180807153545) do ...@@ -1921,6 +1921,42 @@ ActiveRecord::Schema.define(version: 20180807153545) do
t.string "nonce", null: false t.string "nonce", null: false
end end
create_table "packages_maven_metadata", force: :cascade do |t|
t.integer "package_id", null: false
t.string "app_group", null: false
t.string "app_name", null: false
t.string "app_version", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "packages_maven_metadata", ["package_id"], name: "index_packages_maven_metadata_on_package_id", using: :btree
create_table "packages_package_files", force: :cascade do |t|
t.integer "package_id", null: false
t.string "file"
t.string "file_name", null: false
t.integer "file_type"
t.integer "file_store"
t.integer "size"
t.binary "file_md5"
t.binary "file_sha1"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "packages_package_files", ["package_id"], name: "index_packages_package_files_on_package_id", using: :btree
create_table "packages_packages", force: :cascade do |t|
t.integer "project_id", null: false
t.string "name"
t.string "version"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "packages_packages", ["project_id"], name: "index_packages_packages_on_project_id", using: :btree
create_table "pages_domains", force: :cascade do |t| create_table "pages_domains", force: :cascade do |t|
t.integer "project_id" t.integer "project_id"
t.text "certificate" t.text "certificate"
......
...@@ -6,6 +6,8 @@ module API ...@@ -6,6 +6,8 @@ module API
file_name: API::NO_SLASH_URL_PART_REGEX file_name: API::NO_SLASH_URL_PART_REGEX
}.freeze }.freeze
MAVEN_METADATA_FILE = 'maven-metadata.xml'.freeze
content_type :md5, 'text/plain' content_type :md5, 'text/plain'
content_type :sha1, 'text/plain' content_type :sha1, 'text/plain'
content_type :binary, 'application/octet-stream' content_type :binary, 'application/octet-stream'
...@@ -42,8 +44,7 @@ module API ...@@ -42,8 +44,7 @@ module API
app_name: params[:app_name], app_name: params[:app_name],
app_version: params[:app_version]) app_version: params[:app_version])
package_file = metadata.package.package_files.find_by!(file_name: file_name)
package_file = metadata.package.package_files.find_by!(file: file_name)
case format case format
when 'md5' when 'md5'
...@@ -59,7 +60,55 @@ module API ...@@ -59,7 +60,55 @@ module API
detail 'This feature was introduced in GitLab 11.3' detail 'This feature was introduced in GitLab 11.3'
end end
put ':id/packages/maven/*app_group/:app_name/:app_version/:file_name', requirements: MAVEN_ENDPOINT_REQUIREMENTS do put ':id/packages/maven/*app_group/:app_name/:app_version/:file_name', requirements: MAVEN_ENDPOINT_REQUIREMENTS do
# TODO: Implement me file_name, format = extract_format(params[:file_name])
metadata = ::Packages::MavenMetadatum.find_by(app_group: params[:app_group],
app_name: params[:app_name],
app_version: params[:app_version])
if metadata
# Everything seems legit. We can proceed to file uploading
else
if file_name == MAVEN_METADATA_FILE
xml = env['api.request.input']
version = Nokogiri::XML(xml).css('metadata:root > version').text
# Skip handling top level maven-metadata.xml for now
# Also stop request if version in metadata file differs from one in URL
return unless version || version != params[:app_version]
end
package = Packages::Package.crate(project: user_project)
metadata = ::Packages::MavenMetadatum.crate_by!(
project: package.project,
app_group: params[:app_group],
app_name: params[:app_name],
app_version: params[:app_version]
)
end
# Convert string into CarrierWave compatible StringIO object
string_file = CarrierWaveStringFile.new(env['api.request.input'])
if format
package_file = metadata.package.package_files.find_by!(file_name: file_name)
case format
when 'md5'
package_file.file_md5 = string_file
when 'sha1'
package_file.file_sha1 = string_file
end
package_file.save!
else
package_file = metadata.package.package_files.new
package_file.file_name = file_name
package_file.file_type = file_name.rpartition('.').last
package_file.file = string_file
package_file.save!
end
end end
end end
end end
......
require 'rails_helper' require 'rails_helper'
RSpec.describe Packages::MavenMetadatum, type: :model do RSpec.describe Packages::MavenMetadatum, type: :model do
pending "add some examples to (or delete) #{__FILE__}" describe 'relationships' do
it { is_expected.to belong_to(:package) }
end
describe 'validations' do
it { is_expected.to validate_presence_of(:package) }
end
end end
require 'rails_helper' require 'rails_helper'
RSpec.describe Packages::PackageFile, type: :model do RSpec.describe Packages::PackageFile, type: :model do
pending "add some examples to (or delete) #{__FILE__}" describe 'relationships' do
it { is_expected.to belong_to(:package) }
end
describe 'validations' do
it { is_expected.to validate_presence_of(:package) }
end
end end
require 'rails_helper' require 'rails_helper'
RSpec.describe Packages::Package, type: :model do RSpec.describe Packages::Package, type: :model do
pending "add some examples to (or delete) #{__FILE__}" describe 'relationships' do
it { is_expected.to belong_to(:project) }
it { is_expected.to have_many(:package_files) }
end
describe 'validations' do
it { is_expected.to validate_presence_of(:project) }
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment