Skip to content

Commit

Permalink
Merge pull request #5118 from dodona-edu/fix/unique_clone_url
Browse files Browse the repository at this point in the history
Add uniqueness validation to repository remote url
  • Loading branch information
jorg-vr authored Nov 8, 2023
2 parents d9cad73 + 6709a75 commit f8052d3
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/models/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Repository < ApplicationRecord
MEDIA_DIR = 'media'.freeze

validates :name, presence: true, uniqueness: { case_sensitive: false }
validates :remote, presence: true
validates :remote, presence: true, uniqueness: { case_sensitive: false }

validate :repo_is_accessible, on: :create

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20231107160330_add_unique_to_repository_remote.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUniqueToRepositoryRemote < ActiveRecord::Migration[7.1]
def change
add_index :repositories, :remote, unique: true
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2023_10_26_075353) do
ActiveRecord::Schema[7.1].define(version: 2023_11_07_160330) do
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
Expand Down Expand Up @@ -396,6 +396,7 @@
t.index ["judge_id"], name: "index_repositories_on_judge_id"
t.index ["name"], name: "index_repositories_on_name", unique: true
t.index ["path"], name: "index_repositories_on_path", unique: true
t.index ["remote"], name: "index_repositories_on_remote", unique: true
end

create_table "repository_admins", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
Expand Down
3 changes: 3 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ def fill_series_with_realistic_submissions(s)
puts activity_repo.errors.full_messages unless activity_repo.valid?
activity_repo.process_activities

# remote must be unique for the repository, and we want to clone it again
activity_repo.update!(remote: '-')

big_activity_repo = Repository.create name: 'A lot of python activities', remote: '[email protected]:dodona-edu/example-exercises.git', judge: python_judge, allowed_courses: courses
Delayed::Worker.delay_jobs = true
Dir.glob("#{big_activity_repo.full_path}/*")
Expand Down
8 changes: 5 additions & 3 deletions test/controllers/repositories_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ class RepositoryGitControllerTest < ActionDispatch::IntegrationTest
@remote.update_json('echo/config.json', 'make echo private') do |config|
config.update 'access' => 'private'
end

@second_remote = local_remote('exercises/lasagna')
end

def find_echo
Expand All @@ -222,16 +224,16 @@ def find_echo
user = users(:staff)
judge = create :judge, :git_stubbed
sign_in user
post repositories_path, params: { repository: { name: 'test', remote: @remote.path, judge_id: judge.id } }
post repositories_path, params: { repository: { name: 'test', remote: @second_remote.path, judge_id: judge.id } }
end

test 'should email during repository creation' do
user = users(:staff)
judge = create :judge, :git_stubbed
sign_in user
@remote.update_file('echo/config.json', 'break config') { '(╯°□°)╯︵ ┻━┻' }
@second_remote.update_file('exercises/extra/echo/config.json', 'break config') { '(╯°□°)╯︵ ┻━┻' }
assert_difference 'ActionMailer::Base.deliveries.size', +1 do
post repositories_path, params: { repository: { name: 'test', remote: @remote.path, judge_id: judge.id } }
post repositories_path, params: { repository: { name: 'test', remote: @second_remote.path, judge_id: judge.id } }
end
end

Expand Down

0 comments on commit f8052d3

Please sign in to comment.