Class: PlanMyStuff::AttachmentUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/plan_my_stuff/attachment_uploader.rb

Overview

Uploads attachment files to a shared attachment repo (config.attachment_repo under config.organization) using GitHub’s Git Data API, returning SHA-pinned raw.githubusercontent.com permalinks wrapped in PlanMyStuff::Attachment instances.

One commit per batch (atomic): all files in a single upload_all! call land in the same tree/commit so partial failures cannot leave config.main_branch in an inconsistent state.

The attachment repo is assumed to exist; the uploader does not create it. Per-source-repo attachments are namespaced under <repo_key>/issue-<number>/<uuid>.<ext>, where repo_key falls back to repo.name for repos resolved without a config.repos entry.

See Also:

Class Method Summary collapse

Class Method Details

.upload_all!(repo:, issue_number:, files:) ⇒ Array<PlanMyStuff::Attachment>

Uploads each entry in files to the attachment repo and returns the resulting Attachment instances. Order is not preserved: passthrough Attachment entries are returned before newly-uploaded ones. Attachment entries are passed through untouched (no upload), supporting round-trip through Comment#save! without double-uploading.

Empty / nil files short-circuits with no API calls.

Parameters:

  • repo (PlanMyStuff::Repo)

    source issues repo (used to namespace the path)

  • issue_number (Integer)
  • files (Array)

    mix of already-uploaded PlanMyStuff::Attachment instances, uploaded-file objects responding to #path and #original_filename (e.g. Rails ActionDispatch::Http::UploadedFile), and String/Pathname paths to local files

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/plan_my_stuff/attachment_uploader.rb', line 38

def upload_all!(repo:, issue_number:, files:)
  entries = Array.wrap(files)
  return [] if entries.empty?

  slots = entries.map { |entry| classify(entry) }
  attachments = slots.grep(PlanMyStuff::Attachment)
  pending = slots.grep(Hash)

  return attachments if pending.blank?

  commit_sha = create_commit!(repo: repo, issue_number: issue_number, pending: pending)

  pending.each do |slot|
    slot[:attachment] = build_attachment(commit_sha: commit_sha, slot: slot)
  end

  publish_commit!(commit_sha)

  attachments + pending.pluck(:attachment)
end