summaryrefslogtreecommitdiff
path: root/lib/gitlab_update.rb
blob: 8a403a5436bbc555a8cc2119c784a3fd74552b6a (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
require_relative 'gitlab_init'
require_relative 'gitlab_net'

class GitlabUpdate
  def initialize(repo_path, key_id, refname)
    @repo_name = repo_path
    @repo_name.gsub!(GitlabConfig.new.repos_path.to_s, "")
    @repo_name.gsub!(/.git$/, "")
    @repo_name.gsub!(/^\//, "")

    @key_id = key_id
    @refname = /refs\/heads\/([\w\.-]+)/.match(refname).to_a.last
  end

  def exec
    # Skip update hook for local push when key_id is nil
    # It required for gitlab instance to make local pushes
    # without validation of access
    exit 0 if @key_id.nil?

    if api.allowed?('git-receive-pack', @repo_name, @key_id, @refname)
      exit 0
    else
      puts "GitLab: You are not allowed to access #{@refname}! "
      exit 1
    end
  end

  protected

  def api
    GitlabNet.new
  end
end