Class: PlanMyStuff::Label
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- PlanMyStuff::Label
- Defined in:
- lib/plan_my_stuff/label.rb
Overview
Wraps a GitHub label with a reference to its parent issue. Class methods provide the public API for add/remove operations.
Instance Attribute Summary
Attributes inherited from ApplicationRecord
Class Method Summary collapse
-
.add(issue:, labels:, user: nil) ⇒ Array<PlanMyStuff::Label>
Adds labels to a GitHub issue.
-
.ensure!(repo:, name:, color: 'fbca04', description: nil) ⇒ void
Ensures a label exists on the given repo, creating it if missing.
-
.remove(issue:, labels:, user: nil) ⇒ Array<Array<PlanMyStuff::Label>>
Removes labels from a GitHub issue.
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Hash
Serializes the label to a JSON-safe hash, excluding the back-reference to the parent issue to prevent recursive serialization cycles.
- #issue ⇒ PlanMyStuff::Issue?
- #name ⇒ String?
Methods inherited from ApplicationRecord
#destroyed?, #initialize, #new_record?, #persisted?, read_field
Constructor Details
This class inherits a constructor from PlanMyStuff::ApplicationRecord
Class Method Details
.add(issue:, labels:, user: nil) ⇒ Array<PlanMyStuff::Label>
Adds labels to a GitHub issue.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/plan_my_stuff/label.rb', line 21 def add(issue:, labels:, user: nil) label_names = Array.wrap(labels) result = PlanMyStuff.client.rest( :add_labels_to_an_issue, issue.repo, issue.number, label_names, ) PMS::Cache.delete_issue(issue.repo, issue.number) PlanMyStuff::Notifications.instrument( 'label.added', issue, user: user, labels: label_names, ) result.map { |gh_label| build(gh_label, issue: issue) } end |
.ensure!(repo:, name:, color: 'fbca04', description: nil) ⇒ void
This method returns an undefined value.
Ensures a label exists on the given repo, creating it if missing. Idempotent: a 404 from label triggers creation; a 422 from add_label (concurrent-creation race) is treated as success.
48 49 50 51 52 53 54 55 |
# File 'lib/plan_my_stuff/label.rb', line 48 def ensure!(repo:, name:, color: 'fbca04', description: nil) client = PlanMyStuff.client client.rest(:label, repo, name) rescue PlanMyStuff::APIError => e raise unless e.status == 404 create_label(client, repo, name, color, description) end |
.remove(issue:, labels:, user: nil) ⇒ Array<Array<PlanMyStuff::Label>>
Removes labels from a GitHub issue.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/plan_my_stuff/label.rb', line 65 def remove(issue:, labels:, user: nil) label_names = Array.wrap(labels) results = label_names.map do |label| result = PlanMyStuff.client.rest(:remove_label, issue.repo, issue.number, label) result.map { |gh_label| build(gh_label, issue: issue) } end PMS::Cache.delete_issue(issue.repo, issue.number) PlanMyStuff::Notifications.instrument( 'label.removed', issue, user: user, labels: label_names, ) results end |
Instance Method Details
#as_json(options = {}) ⇒ Hash
Serializes the label to a JSON-safe hash, excluding the back-reference to the parent issue to prevent recursive serialization cycles.
125 126 127 128 |
# File 'lib/plan_my_stuff/label.rb', line 125 def as_json( = {}) merged_except = Array.wrap([:except]) + ['issue'] super(.merge(except: merged_except)).merge('issue_number' => issue&.number) end |
#name ⇒ String?
8 |
# File 'lib/plan_my_stuff/label.rb', line 8 attribute :name, :string |