Module: Pray::Confess

Defined in:
lib/pray/confess.rb

Class Method Summary collapse

Class Method Details

.confession_to_hash(confession) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/pray/confess.rb', line 98

def confession_to_hash(confession)
  hash = {
    "package" => confession.package,
    "version" => confession.version,
    "status" => confession.status
  }
  hash["note"] = confession.note if confession.note
  hash["lockfile"] = confession.lockfile if confession.lockfile
  hash["distribution_point"] = confession.distribution_point if confession.distribution_point
  hash["signer"] = confession.signer if confession.signer
  hash["timestamp"] = confession.timestamp if confession.timestamp
  hash["signature"] = confession.signature if confession.signature
  hash
end

.resolve_source_url(project, package_resolution, package_name, url) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pray/confess.rb', line 86

def resolve_source_url(project, package_resolution, package_name, url)
  return url if url

  source_name = package_resolution.declaration.source
  raise Error.resolution("package #{package_name} is missing a source") unless source_name

  source = project.manifest.sources.find { |entry| entry.name == source_name }
  raise Error.resolution("unknown source: #{source_name}") unless source

  source.url
end

.resolve_target(project, lockfile, package, from_lock, version) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pray/confess.rb', line 52

def resolve_target(project, lockfile, package, from_lock, version)
  if from_lock
    raise Error.resolution("confess --from-lock requires an existing lockfile") unless lockfile

    span = lockfile.managed_span.find { |record| record.id == from_lock }
    raise Error.resolution("lockfile span #{from_lock} not found") unless span

    package_resolution = project.packages.find { |entry| entry.declaration.name == span.package }
    raise Error.resolution("package #{span.package} not found") unless package_resolution

    locked = lockfile.package.find { |entry| entry.name == span.package }
    raise Error.resolution("lockfile package #{span.package} not found") unless locked

    if version && version != locked.version
      raise Error.resolution(
        "lockfile span #{from_lock} version #{locked.version} does not match requested version #{version}"
      )
    end
    [span.package, version || locked.version, package_resolution]
  else
    raise Error.unsupported("confess requires a package name") unless package

    package_resolution = project.packages.find { |entry| entry.declaration.name == package }
    raise Error.resolution("package #{package} not found") unless package_resolution

    if version && version != package_resolution.spec.version
      raise Error.resolution(
        "package #{package} version #{version} does not match resolved version #{package_resolution.spec.version}"
      )
    end
    [package, version || package_resolution.spec.version, package_resolution]
  end
end

.submit(package:, from_lock:, version:, accepted:, rejected:, note:, url:, project_root: Dir.pwd) ⇒ Object



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
# File 'lib/pray/confess.rb', line 16

def submit(package:, from_lock:, version:, accepted:, rejected:, note:, url:,
  project_root: Dir.pwd)
  raise Error.unsupported("confess requires exactly one of --accepted or --rejected") if accepted == rejected

  manifest_path = File.join(project_root, "Prayfile")
  project = Resolve.resolve_project(manifest_path)
  lockfile_path = File.join(project_root, "Prayfile.lock")
  lockfile = File.file?(lockfile_path) ? Lockfile.read_lockfile(lockfile_path) : nil

  package_name, resolved_version, package_resolution =
    resolve_target(project, lockfile, package, from_lock, version)
  source_url = resolve_source_url(project, package_resolution, package_name, url)
  if source_url.start_with?("pray+ssh://", "ssh+pray://")
    raise Error.unsupported("pray_ssh confession submit is not implemented yet in pray-cli Ruby")
  end

  confession = ConfessionSubmission.new(
    package: package_name,
    version: resolved_version,
    status: accepted ? "accepted" : "rejected",
    note: note,
    lockfile: lockfile&.file_hash,
    distribution_point: source_url,
    signer: Session.current_signer(project_root),
    timestamp: Time.now.to_i.to_s,
    signature: nil
  )
  confession.signature = Hashing.sha256_prefixed(JSON.generate(confession_to_hash(confession)))
  Registry.http_post(
    Registry.join_url(source_url, "v1/confessions"),
    "application/json",
    JSON.generate(confession_to_hash(confession))
  )
  puts "Confession submitted for #{confession.package} #{confession.version}"
end