class Issue::Payload

Attributes

context[RW]

Public Class Methods

new(json_data, event) click to toggle source

Initialize Issue::Payload object with: json_data: the json sent from a GitHub webhook event: the value of the HTTP_X_GITHUB_EVENT header

Initializing a new Issue::Payload instance makes all this info from the json webhook available via accessor methods:

action
event
issue_id
issue_title
issue_body
issue_author
repo
sender
event_action
raw_payload

And if the case the event is 'issue_comment' also:

comment_body
comment_created_at
comment_url
# File lib/issue/payload.rb, line 32
def initialize(json_data, event)
  action = json_data.dig("action")
  sender = json_data.dig("sender", "login")
  repo = json_data.dig("repository", "full_name")

  if event == "pull_request"
    issue_id = json_data.dig("pull_request", "number")
    issue_title = json_data.dig("pull_request", "title")
    issue_body = json_data.dig("pull_request", "body")
    issue_labels = json_data.dig("pull_request", "labels")
    issue_author = json_data.dig("pull_request", "user", "login")
  else
    issue_id = json_data.dig("issue", "number")
    issue_title = json_data.dig("issue", "title")
    issue_body = json_data.dig("issue", "body")
    issue_labels = json_data.dig("issue", "labels")
    issue_author = json_data.dig("issue", "user", "login")
  end

  @context = OpenStruct.new(
    action: action,
    event: event,
    issue_id: issue_id,
    issue_title: issue_title,
    issue_body: issue_body,
    issue_author: issue_author,
    issue_labels: issue_labels,
    repo: repo,
    sender: sender,
    event_action: "#{event}.#{action}",
    raw_payload: json_data
  )

  if event == "issue_comment"
    @context[:comment_body] = json_data.dig("comment", "body")
    @context[:comment_created_at] = json_data.dig("comment", "created_at")
    @context[:comment_url] = json_data.dig("comment", "html_url")
  end

  @context.each_pair do |method_name, value|
    define_singleton_method(method_name) {value}
  end
end

Public Instance Methods

assigned?() click to toggle source

True if the payload is coming from un/assigning an issue

# File lib/issue/payload.rb, line 112
def assigned?
  action == "assigned" || action == "unassigned"
end
closed?() click to toggle source

True if the payload is coming from an issue that has just been closed

# File lib/issue/payload.rb, line 82
def closed?
  action == "closed"
end
commented?() click to toggle source

True if the payload is coming from a new comment

# File lib/issue/payload.rb, line 87
def commented?
  action == "created"
end
edited?() click to toggle source

True if the payload is coming from an edition of a comment or issue

# File lib/issue/payload.rb, line 92
def edited?
  action == "edited"
end
labeled?() click to toggle source

True if the payload is coming from un/labeling an issue

# File lib/issue/payload.rb, line 117
def labeled?
  action == "labeled" || action == "unlabeled"
end
locked?() click to toggle source

True if the payload is coming from locking an issue

# File lib/issue/payload.rb, line 97
def locked?
  action == "locked"
end
opened?() click to toggle source

True if the payload is coming from an issue that has just been opened

# File lib/issue/payload.rb, line 77
def opened?
  action == "opened" || action == "reopened"
end
pinned?() click to toggle source

True if the payload is coming from pinning or unpinning an issue

# File lib/issue/payload.rb, line 107
def pinned?
  action == "pinned" || action == "unpinned"
end
unlocked?() click to toggle source

True if the payload is coming from unlocking an issue

# File lib/issue/payload.rb, line 102
def unlocked?
  action == "unlocked"
end