| 1 | # frozen_string_literal: true |
| 2 | |
| 3 | # Actions on mirror repositories: detach (convert to a normal writable repo and |
| 4 | # stop syncing) and a manual sync trigger. Owner-only. |
| 5 | class MirrorsController < ApplicationController |
| 6 | before_action :require_sign_in! |
| 7 | before_action :noindex! |
| 8 | before_action :load_repository |
| 9 | before_action :ensure_owner! |
| 10 | |
| 11 | # POST /:username/:repository/mirror/detach |
| 12 | # Flips the mirror into a normal writable repo and stops syncing. Idempotent. |
| 13 | def detach |
| 14 | if @repository.detach_mirror! |
| 15 | redirect_to repository_path(@owner.username, @repository.name), |
| 16 | notice: "Mirror detached. This is now a normal repository you can push to." |
| 17 | else |
| 18 | redirect_to repository_path(@owner.username, @repository.name), |
| 19 | alert: "This repository isn't a mirror." |
| 20 | end |
| 21 | end |
| 22 | |
| 23 | # POST /:username/:repository/mirror/sync |
| 24 | # Enqueue an immediate sync (in addition to the scheduled ones). |
| 25 | def sync |
| 26 | unless @repository.mirror? |
| 27 | return redirect_to repository_path(@owner.username, @repository.name), |
| 28 | alert: "This repository isn't a mirror." |
| 29 | end |
| 30 | |
| 31 | MirrorSyncJob.perform_later(@repository.id) |
| 32 | redirect_to repository_path(@owner.username, @repository.name), |
| 33 | notice: "Sync queued." |
| 34 | end |
| 35 | |
| 36 | private |
| 37 | |
| 38 | def load_repository |
| 39 | @owner = User.find_by!(username: params[:username]) |
| 40 | @repository = @owner.repositories.find_by!(name: params[:repository]) |
| 41 | rescue ActiveRecord::RecordNotFound |
| 42 | render file: Rails.public_path.join("404.html"), status: :not_found, layout: false |
| 43 | end |
| 44 | |
| 45 | def ensure_owner! |
| 46 | unless signed_in? && current_user == @owner |
| 47 | render file: Rails.public_path.join("404.html"), status: :not_found, layout: false |
| 48 | end |
| 49 | end |
| 50 | end |