Skip to content

Commit

Permalink
Merge pull request #5458 from dodona-edu/fix/replace-http-urls
Browse files Browse the repository at this point in the history
Convert github https clone urls to ssh clone urls
  • Loading branch information
jorg-vr authored Apr 2, 2024
2 parents 1a27f58 + 50352bf commit 07d7e21
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/models/concerns/gitable.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
require 'open3'
module Gitable
extend ActiveSupport::Concern
# regex to detect github or gitlab https remotes
# group 1: protocol
# group 2: domain
# group 6: user + repo
HTTPS_GITHUB_REMOTE_REGEX = %r{^(https?://)(([^/]+\.)?(github|gitlab)([^/]*))/([^.]*)(\.git)?$}

included do
before_create :fix_remote

enum clone_status: { queued: 1, running: 2, complete: 3, failed: 4 }, _prefix: :clone
end

Expand Down Expand Up @@ -60,4 +67,10 @@ def repo_is_accessible
def github_remote?
remote =~ %r{^(git@)|(https://)(github|gitlab)}
end

def fix_remote
return unless remote =~ HTTPS_GITHUB_REMOTE_REGEX

self.remote = remote.gsub(HTTPS_GITHUB_REMOTE_REGEX, 'git@\2:\6.git')
end
end
32 changes: 32 additions & 0 deletions test/models/repository_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -460,4 +460,36 @@ def teardown

assert_not_includes Repository.featured, @repository
end

test 'https remote should be converted to ssh' do
repository = create :repository, :git_stubbed, remote: 'https://github.com/dodona-edu/dodona.git'

assert_equal 'git@github.com:dodona-edu/dodona.git', repository.remote

repository = create :repository, :git_stubbed, remote: 'https://gitlab.com/dodona-edu/dodona.git'

assert_equal 'git@gitlab.com:dodona-edu/dodona.git', repository.remote

repository = create :repository, :git_stubbed, remote: 'http://github.ugent.be/dodona-edu/dodona'

assert_equal 'git@github.ugent.be:dodona-edu/dodona.git', repository.remote

repository = create :repository, :git_stubbed, remote: 'http://foo.gitlab.bar/bazz.git'

assert_equal 'git@foo.gitlab.bar:bazz.git', repository.remote
end

test 'non github/gitlab remote should not be converted to ssh' do
repository = create :repository, :git_stubbed, remote: 'https://foo.bar/bazz'

assert_equal 'https://foo.bar/bazz', repository.remote

repository = create :repository, :git_stubbed, remote: 'git@foo.bar/bazz.git'

assert_equal 'git@foo.bar/bazz.git', repository.remote

repository = create :repository, :git_stubbed, remote: 'git@github.ugent.be:dodona-edu/dodona.git'

assert_equal 'git@github.ugent.be:dodona-edu/dodona.git', repository.remote
end
end

0 comments on commit 07d7e21

Please sign in to comment.