Class: Issue::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/issue/payload.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_data, event) ⇒ Payload

Initialize Issue::Payload object with:

json_data: the parsed 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 when the event is ‘issue_comment’ also:

comment_body
comment_created_at
comment_url


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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/issue/payload.rb', line 33

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_id] = json_data.dig("comment", "id")
    @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

Instance Attribute Details

#contextObject

Returns the value of attribute context.



6
7
8
# File 'lib/issue/payload.rb', line 6

def context
  @context
end

Instance Method Details

#assigned?Boolean

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

Returns:

  • (Boolean)


114
115
116
# File 'lib/issue/payload.rb', line 114

def assigned?
  action == "assigned" || action == "unassigned"
end

#closed?Boolean

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

Returns:

  • (Boolean)


84
85
86
# File 'lib/issue/payload.rb', line 84

def closed?
  action == "closed"
end

#commented?Boolean

True if the payload is coming from a new comment

Returns:

  • (Boolean)


89
90
91
# File 'lib/issue/payload.rb', line 89

def commented?
  action == "created"
end

#edited?Boolean

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

Returns:

  • (Boolean)


94
95
96
# File 'lib/issue/payload.rb', line 94

def edited?
  action == "edited"
end

#labeled?Boolean

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

Returns:

  • (Boolean)


119
120
121
# File 'lib/issue/payload.rb', line 119

def labeled?
  action == "labeled" || action == "unlabeled"
end

#locked?Boolean

True if the payload is coming from locking an issue

Returns:

  • (Boolean)


99
100
101
# File 'lib/issue/payload.rb', line 99

def locked?
  action == "locked"
end

#opened?Boolean

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

Returns:

  • (Boolean)


79
80
81
# File 'lib/issue/payload.rb', line 79

def opened?
  action == "opened" || action == "reopened"
end

#pinned?Boolean

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

Returns:

  • (Boolean)


109
110
111
# File 'lib/issue/payload.rb', line 109

def pinned?
  action == "pinned" || action == "unpinned"
end

#unlocked?Boolean

True if the payload is coming from unlocking an issue

Returns:

  • (Boolean)


104
105
106
# File 'lib/issue/payload.rb', line 104

def unlocked?
  action == "unlocked"
end