Class: Datadog::CI::Git::UploadPackfile

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/ci/git/upload_packfile.rb

Defined Under Namespace

Classes: ApiError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api:, head_commit_sha:, repository_url:) ⇒ UploadPackfile

Returns a new instance of UploadPackfile.



19
20
21
22
23
# File 'lib/datadog/ci/git/upload_packfile.rb', line 19

def initialize(api:, head_commit_sha:, repository_url:)
  @api = api
  @head_commit_sha = head_commit_sha
  @repository_url = repository_url
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



17
18
19
# File 'lib/datadog/ci/git/upload_packfile.rb', line 17

def api
  @api
end

#head_commit_shaObject (readonly)

Returns the value of attribute head_commit_sha.



17
18
19
# File 'lib/datadog/ci/git/upload_packfile.rb', line 17

def head_commit_sha
  @head_commit_sha
end

#repository_urlObject (readonly)

Returns the value of attribute repository_url.



17
18
19
# File 'lib/datadog/ci/git/upload_packfile.rb', line 17

def repository_url
  @repository_url
end

Instance Method Details

#call(filepath:) ⇒ Object

Raises:



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
# File 'lib/datadog/ci/git/upload_packfile.rb', line 25

def call(filepath:)
  raise ApiError, "test visibility API is not configured" if api.nil?

  payload_boundary = SecureRandom.uuid

  filename = File.basename(filepath)
  packfile_contents = read_file(filepath)

  payload = request_payload(payload_boundary, filename, packfile_contents)
  content_type = "#{Ext::Transport::CONTENT_TYPE_MULTIPART_FORM_DATA}; boundary=#{payload_boundary}"

  http_response = api.api_request(
    path: Ext::Transport::DD_API_GIT_UPLOAD_PACKFILE_PATH,
    payload: payload,
    headers: {Ext::Transport::HEADER_CONTENT_TYPE => content_type}
  )

  Transport::Telemetry.api_requests(
    Ext::Telemetry::METRIC_GIT_REQUESTS_OBJECT_PACK,
    1,
    compressed: http_response.request_compressed
  )
  Utils::Telemetry.distribution(Ext::Telemetry::METRIC_GIT_REQUESTS_OBJECT_PACK_MS, http_response.duration_ms)
  Utils::Telemetry.distribution(Ext::Telemetry::METRIC_GIT_REQUESTS_OBJECT_PACK_BYTES, http_response.request_size.to_f)

  unless http_response.ok?
    Transport::Telemetry.api_requests_errors(
      Ext::Telemetry::METRIC_GIT_REQUESTS_OBJECT_PACK_ERRORS,
      1,
      error_type: http_response.telemetry_error_type,
      status_code: http_response.code
    )
    raise ApiError, "Failed to upload packfile: #{http_response.inspect}"
  end
end