Commit a97fd577 authored by Stan Hu's avatar Stan Hu

Merge branch '27300-enable-a-direct-link-to-a-release-and-release-assets-2' into 'master'

Add filepath column to release_links

See merge request gitlab-org/gitlab!25512
parents 08753594 255b205c
...@@ -6,8 +6,11 @@ module Releases ...@@ -6,8 +6,11 @@ module Releases
belongs_to :release belongs_to :release
FILEPATH_REGEX = /\A\/([\-\.\w]+\/?)*[\da-zA-Z]+\z/.freeze
validates :url, presence: true, addressable_url: { schemes: %w(http https ftp) }, uniqueness: { scope: :release } validates :url, presence: true, addressable_url: { schemes: %w(http https ftp) }, uniqueness: { scope: :release }
validates :name, presence: true, uniqueness: { scope: :release } validates :name, presence: true, uniqueness: { scope: :release }
validates :filepath, uniqueness: { scope: :release }, format: { with: FILEPATH_REGEX }, allow_blank: true, length: { maximum: 128 }
scope :sorted, -> { order(created_at: :desc) } scope :sorted, -> { order(created_at: :desc) }
......
---
title: Add filepath to ReleaseLink
merge_request: 25512
author:
type: added
# frozen_string_literal: true
class AddFilepathToReleaseLinks < ActiveRecord::Migration[6.0]
DOWNTIME = false
def change
add_column :release_links, :filepath, :string, limit: 128
end
end
...@@ -3648,6 +3648,7 @@ ActiveRecord::Schema.define(version: 2020_02_21_144534) do ...@@ -3648,6 +3648,7 @@ ActiveRecord::Schema.define(version: 2020_02_21_144534) do
t.string "name", null: false t.string "name", null: false
t.datetime_with_timezone "created_at", null: false t.datetime_with_timezone "created_at", null: false
t.datetime_with_timezone "updated_at", null: false t.datetime_with_timezone "updated_at", null: false
t.string "filepath", limit: 128
t.index ["release_id", "name"], name: "index_release_links_on_release_id_and_name", unique: true t.index ["release_id", "name"], name: "index_release_links_on_release_id_and_name", unique: true
t.index ["release_id", "url"], name: "index_release_links_on_release_id_and_url", unique: true t.index ["release_id", "url"], name: "index_release_links_on_release_id_and_url", unique: true
end end
......
...@@ -5,5 +5,6 @@ FactoryBot.define do ...@@ -5,5 +5,6 @@ FactoryBot.define do
release release
sequence(:name) { |n| "release-18.#{n}.dmg" } sequence(:name) { |n| "release-18.#{n}.dmg" }
sequence(:url) { |n| "https://example.com/scrambled-url/app-#{n}.zip" } sequence(:url) { |n| "https://example.com/scrambled-url/app-#{n}.zip" }
sequence(:filepath) { |n| "/binaries/awesome-app-#{n}" }
end end
end end
...@@ -133,6 +133,7 @@ Releases::Link: ...@@ -133,6 +133,7 @@ Releases::Link:
- id - id
- url - url
- name - name
- filepath
- created_at - created_at
- updated_at - updated_at
ProjectMember: ProjectMember:
......
...@@ -13,6 +13,7 @@ describe Releases::Link do ...@@ -13,6 +13,7 @@ describe Releases::Link do
describe 'validation' do describe 'validation' do
it { is_expected.to validate_presence_of(:url) } it { is_expected.to validate_presence_of(:url) }
it { is_expected.to validate_presence_of(:name) } it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_length_of(:filepath).is_at_most(128) }
context 'when url is invalid' do context 'when url is invalid' do
let(:link) { build(:release_link, url: 'hoge') } let(:link) { build(:release_link, url: 'hoge') }
...@@ -43,6 +44,16 @@ describe Releases::Link do ...@@ -43,6 +44,16 @@ describe Releases::Link do
end end
end end
context 'when duplicate filepath is added to a release' do
let!(:link) { create(:release_link, filepath: '/binaries/gitlab-runner-linux-amd64', release: release) }
it 'raises an error' do
expect do
create(:release_link, filepath: '/binaries/gitlab-runner-linux-amd64', release: release)
end.to raise_error(ActiveRecord::RecordInvalid)
end
end
describe '.sorted' do describe '.sorted' do
subject { described_class.sorted } subject { described_class.sorted }
...@@ -101,4 +112,38 @@ describe Releases::Link do ...@@ -101,4 +112,38 @@ describe Releases::Link do
end end
end end
end end
describe 'FILEPATH_REGEX with table' do
using RSpec::Parameterized::TableSyntax
let(:link) { build(:release_link)}
where(:reason, :filepath, :result) do
'cannot contain `//`' | '/https//www.example.com' | be_invalid
'cannot start with `//`' | '//www.example.com' | be_invalid
'cannot contain a `?`' | '/example.com/?stuff=true' | be_invalid
'cannot contain a `:`' | '/example:5000' | be_invalid
'cannot end in a `-`' | '/binaries/awesome-app.dmg-' | be_invalid
'cannot end in a `.`' | '/binaries/awesome-app.dmg.' | be_invalid
'cannot end in a `_`' | '/binaries/awesome-app.dmg_' | be_invalid
'cannot start with a `.`' | '.binaries/awesome-app.dmg' | be_invalid
'cannot start with a `-`' | '-binaries/awesome-app.dmg' | be_invalid
'cannot start with a `_`' | '_binaries/awesome-app.dmg' | be_invalid
'cannot start with a number' | '3binaries/awesome-app.dmg' | be_invalid
'cannot start with a letter' | 'binaries/awesome-app.dmg' | be_invalid
'cannot contain accents' | '/binarïes/âwésome-app.dmg' | be_invalid
'can end in a character' | '/binaries/awesome-app.dmg' | be_valid
'can end in a number' | '/binaries/awesome-app-1' | be_valid
'can contain one or more dots, dashes or underscores' | '/sub_tr__ee.ex..ample-2--1/v99.com' | be_valid
'can contain multiple non-sequential slashes' | '/example.com/path/to/file.exe' | be_valid
'can be nil' | nil | be_valid
end
with_them do
specify do
link.filepath = filepath
expect(link).to result
end
end
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