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
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 :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) }
......
---
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
t.string "name", null: false
t.datetime_with_timezone "created_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", "url"], name: "index_release_links_on_release_id_and_url", unique: true
end
......
......@@ -5,5 +5,6 @@ FactoryBot.define do
release
sequence(:name) { |n| "release-18.#{n}.dmg" }
sequence(:url) { |n| "https://example.com/scrambled-url/app-#{n}.zip" }
sequence(:filepath) { |n| "/binaries/awesome-app-#{n}" }
end
end
......@@ -133,6 +133,7 @@ Releases::Link:
- id
- url
- name
- filepath
- created_at
- updated_at
ProjectMember:
......
......@@ -13,6 +13,7 @@ describe Releases::Link do
describe 'validation' do
it { is_expected.to validate_presence_of(:url) }
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
let(:link) { build(:release_link, url: 'hoge') }
......@@ -43,6 +44,16 @@ describe Releases::Link do
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
subject { described_class.sorted }
......@@ -101,4 +112,38 @@ describe Releases::Link do
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
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