lib/puppetfactory/plugins/gitea.rb in puppetfactory-0.5.5 vs lib/puppetfactory/plugins/gitea.rb in puppetfactory-0.5.6

- old
+ new

@@ -6,45 +6,51 @@ def initialize(options) super(options) @suffix = options[:usersuffix] + @controlrepo = options[:controlrepo] + @reponame = File.basename(@controlrepo, '.git') @gitea_path = options[:gitea_path] || '/home/git/go/bin/gitea' @admin_username = options[:gitea_admin_username] || 'root' @admin_password = options[:gitea_admin_password] || 'puppetlabs' @gitea_port = options[:gitea_port] || '3000' - begin - `curl -su "#{@admin_username}:#{@admin_password}" --data 'clone_addr=https://github.com/puppetlabs-education/classroom-control-vf.git&uid=1&repo_name=classroom-control-vf' http://localhost:#{@gitea_port}/api/v1/repos/migrate` - FileUtils::mkdir_p '/var/cache/puppetfactory/gitea' - Dir.chdir('/var/cache/puppetfactory/gitea') do - `git clone --depth 1 http://#{@admin_username}:#{@admin_password}@localhost:#{@gitea_port}/#{@admin_username}/classroom-control-vf.git` + @cache_dir = '/var/cache/puppetfactory/gitea' + + if (!File.directory?("#{@cache_dir}/#{@reponame}")) + $logger.info "Migrating repository #{@reponame}" + begin + `curl -su "#{@admin_username}:#{@admin_password}" --data "clone_addr=https://github.com/puppetlabs-education/#{@controlrepo}&uid=1&repo_name=#{@reponame}" http://localhost:#{@gitea_port}/api/v1/repos/migrate` + FileUtils::mkdir_p @cache_dir + Dir.chdir(@cache_dir) do + `git clone --depth 1 http://#{@admin_username}:#{@admin_password}@localhost:#{@gitea_port}/#{@admin_username}/#{@controlrepo}` + end + rescue => e + $logger.error "Error migrating repository: #{e.message}" + return false end - rescue => e - $logger.error "Error migrating repository: #{e.message}" - return false end - end def add_collaborator(owner, repo, username, permission) `curl -su "#{@admin_username}:#{@admin_password}" -X PUT -H "Content-Type: application/json" -d '{"permissions":"#{permission}"}' http://localhost:#{@gitea_port}/api/v1/repos/#{owner}/#{repo}/collaborators/#{username}` end def make_branch(username) - Dir.chdir('/var/cache/puppetfactory/gitea/classroom-control-vf') do + Dir.chdir("#{@cache_dir}/#{@reponame}") do `git checkout -b #{username} && git push origin #{username}` end end def create(username, password) begin if password.length < 8 raise "Password must be at least 8 characters" end - `su git -c "cd && #{@gitea_path} admin create-user --name #{username} --password #{password} --email #{username}.#{@suffix}"` - add_collaborator(@admin_username, 'classroom-control-vf', username, 'write') + `su git -c "cd && #{@gitea_path} admin create-user --name #{username} --password #{password} --email #{username}@#{@suffix}"` + add_collaborator(@admin_username, @reponame, username, 'write') make_branch(username) $logger.info "Created Gitea user #{username}." rescue => e $logger.error "Error creating Gitea user #{username}: #{e.message}" return false @@ -54,12 +60,12 @@ end def delete(username) begin `curl -su "#{@admin_username}:#{@admin_password}" -X "DELETE" http://localhost:#{@gitea_port}/api/v1/admin/users/#{username}` - $logger.info "Removed GitLab user #{username}." + $logger.info "Removed Gitea user #{username}." rescue => e - $logger.error "Error removing GitLab user #{username}: #{e.message}" + $logger.error "Error removing Gitea user #{username}: #{e.message}" return false end true end