summaryrefslogtreecommitdiff
path: root/lib/gitlab/checks/project_moved.rb
blob: 3a1c0a3455e475741eb5f423a8ec469324d7fdb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module Gitlab
  module Checks
    class ProjectMoved
      REDIRECT_NAMESPACE = "redirect_namespace".freeze

      def initialize(project, user, redirected_path, protocol)
        @project = project
        @user = user
        @redirected_path = redirected_path
        @protocol = protocol
      end

      def self.fetch_redirect_message(user_id, project_id)
        redirect_key = redirect_message_key(user_id, project_id)

        Gitlab::Redis::SharedState.with do |redis|
          message = redis.get(redirect_key)
          redis.del(redirect_key)
          message
        end
      end

      def add_redirect_message
        Gitlab::Redis::SharedState.with do |redis|
          key = self.class.redirect_message_key(user.id, project.id)
          redis.setex(key, 5.minutes, redirect_message)
        end
      end

      def redirect_message(rejected: false)
        <<~MESSAGE.strip_heredoc
        Project '#{redirected_path}' was moved to '#{project.full_path}'.

        Please update your Git remote:

          #{remote_url_message(rejected)}
        MESSAGE
      end

      def permanent_redirect?
        RedirectRoute.permanent.exists?(path: redirected_path)
      end

      private

      attr_reader :project, :redirected_path, :protocol, :user

      def self.redirect_message_key(user_id, project_id)
        "#{REDIRECT_NAMESPACE}:#{user_id}:#{project_id}"
      end

      def remote_url_message(rejected)
        if rejected
          "git remote set-url origin #{url} and try again."
        else
          "git remote set-url origin #{url}"
        end
      end

      def url
        protocol == 'ssh' ? project.ssh_url_to_repo : project.http_url_to_repo
      end
    end
  end
end