BigW Consortium Gitlab

workhorse.rb 2.09 KB
Newer Older
1 2 3 4 5
require 'base64'
require 'json'

module Gitlab
  class Workhorse
Jacob Vosmaer committed
6
    SEND_DATA_HEADER = 'Gitlab-Workhorse-Send-Data'
7
    VERSION_FILE = 'GITLAB_WORKHORSE_VERSION'
8

Jacob Vosmaer committed
9
    class << self
10 11
      def git_http_ok(repository, user)
        {
12
          'GL_ID' => Gitlab::GlId.gl_id(user),
13 14 15 16
          'RepoPath' => repository.path_to_repo,
        }
      end

17
      def send_git_blob(repository, blob)
18
        params = {
19 20 21 22 23
          'RepoPath' => repository.path_to_repo,
          'BlobId' => blob.id,
        }

        [
24
          SEND_DATA_HEADER,
Douwe Maan committed
25
          "git-blob:#{encode(params)}"
26 27
        ]
      end
28

29
      def send_git_archive(repository, ref:, format:)
30 31
        format ||= 'tar.gz'
        format.downcase!
32
        params = repository.archive_metadata(ref, Gitlab.config.gitlab.repository_downloads_path, format)
33 34 35 36
        raise "Repository or ref not found" if params.empty?

        [
          SEND_DATA_HEADER,
Douwe Maan committed
37
          "git-archive:#{encode(params)}"
38 39
        ]
      end
40

Douwe Maan committed
41
      def send_git_diff(repository, diff_refs)
42
        params = {
Douwe Maan committed
43
          'RepoPath'  => repository.path_to_repo,
44 45
          'ShaFrom'   => diff_refs.start_sha,
          'ShaTo'     => diff_refs.head_sha
46 47 48 49 50
        }

        [
          SEND_DATA_HEADER,
          "git-diff:#{encode(params)}"
51 52
        ]
      end
53

Douwe Maan committed
54
      def send_git_patch(repository, diff_refs)
55
        params = {
56
          'RepoPath'  => repository.path_to_repo,
Douwe Maan committed
57 58
          'ShaFrom'   => diff_refs.start_sha,
          'ShaTo'     => diff_refs.head_sha
59 60 61
        }

        [
62
          SEND_DATA_HEADER,
63 64 65 66
          "git-format-patch:#{encode(params)}"
        ]
      end

67 68 69 70 71 72 73 74 75 76 77 78
      def send_artifacts_entry(build, entry)
        params = {
          'Archive' => build.artifacts_file.path,
          'Entry' => Base64.encode64(entry.path)
        }

        [
          SEND_DATA_HEADER,
          "artifacts-entry:#{encode(params)}"
        ]
      end

79
      def version
80 81
        path = Rails.root.join(VERSION_FILE)
        path.readable? ? path.read.chomp : 'unknown'
82 83
      end

84
      protected
85

86 87 88
      def encode(hash)
        Base64.urlsafe_encode64(JSON.dump(hash))
      end
89 90
    end
  end
Jacob Vosmaer committed
91
end