3 final class NuanceGitHubRawEvent
extends Phobject
{
8 const TYPE_ISSUE
= 'issue';
9 const TYPE_REPOSITORY
= 'repository';
11 public static function newEvent($type, array $raw) {
18 public function getRepositoryFullName() {
19 return $this->getRepositoryFullRawName();
22 public function isIssueEvent() {
23 if ($this->isPullRequestEvent()) {
27 if ($this->type
== self
::TYPE_ISSUE
) {
31 switch ($this->getIssueRawKind()) {
34 case 'IssueCommentEvent':
35 if (!$this->getRawPullRequestData()) {
44 public function isPullRequestEvent() {
45 if ($this->type
== self
::TYPE_ISSUE
) {
46 // TODO: This is wrong, some of these are pull events.
52 switch ($this->getIssueRawKind()) {
53 case 'PullRequestEvent':
55 case 'IssueCommentEvent':
56 if ($this->getRawPullRequestData()) {
65 public function getIssueNumber() {
66 if (!$this->isIssueEvent()) {
70 return $this->getRawIssueNumber();
73 public function getPullRequestNumber() {
74 if (!$this->isPullRequestEvent()) {
78 return $this->getRawIssueNumber();
82 public function getID() {
85 $id = idx($raw, 'id');
93 public function getComment() {
94 if (!$this->isIssueEvent() && !$this->isPullRequestEvent()) {
100 return idxv($raw, array('payload', 'comment', 'body'));
103 public function getURI() {
106 if ($this->isIssueEvent() ||
$this->isPullRequestEvent()) {
107 if ($this->type
== self
::TYPE_ISSUE
) {
108 $uri = idxv($raw, array('issue', 'html_url'));
109 $uri = $uri.'#event-'.$this->getID();
111 // The format of pull request events varies so we need to fish around
112 // a bit to find the correct URI.
113 $uri = idxv($raw, array('payload', 'pull_request', 'html_url'));
116 // For comments, we get a different anchor to link to the comment. In
117 // this case, the URI comes with an anchor already.
119 $uri = idxv($raw, array('payload', 'comment', 'html_url'));
120 $need_anchor = false;
124 $uri = idxv($raw, array('payload', 'issue', 'html_url'));
129 $uri = $uri.'#event-'.$this->getID();
133 switch ($this->getIssueRawKind()) {
135 $ref = idxv($raw, array('payload', 'ref'));
137 $repo = $this->getRepositoryFullRawName();
138 return "https://github.com/{$repo}/commits/{$ref}";
140 // These don't really have a URI since there may be multiple commits
141 // involved and GitHub doesn't bundle the push as an object on its
142 // own. Just try to find the URI for the log. The API also does
143 // not return any HTML URI for these events.
145 $head = idxv($raw, array('payload', 'head'));
146 if ($head === null) {
150 $repo = $this->getRepositoryFullRawName();
151 return "https://github.com/{$repo}/commits/{$head}";
153 // These have no reasonable URI.
163 private function getRepositoryFullRawName() {
166 $full = idxv($raw, array('repo', 'name'));
171 // For issue events, the repository is not identified explicitly in the
172 // response body. Parse it out of the URI.
176 '(/repos/((?:[^/]+)/(?:[^/]+))/issues/events/)',
187 private function getIssueRawKind() {
189 return idxv($raw, array('type'));
192 private function getRawIssueNumber() {
195 if ($this->type
== self
::TYPE_ISSUE
) {
196 return idxv($raw, array('issue', 'number'));
199 if ($this->type
== self
::TYPE_REPOSITORY
) {
200 $issue_number = idxv($raw, array('payload', 'issue', 'number'));
202 return $issue_number;
205 $pull_number = idxv($raw, array('payload', 'number'));
214 private function getRawPullRequestData() {
216 return idxv($raw, array('payload', 'issue', 'pull_request'));
219 public function getEventFullTitle() {
220 switch ($this->type
) {
221 case self
::TYPE_ISSUE
:
222 $title = $this->getRawIssueEventTitle();
224 case self
::TYPE_REPOSITORY
:
225 $title = $this->getRawRepositoryEventTitle();
228 $title = pht('Unknown Event Type ("%s")', $this->type
);
234 $this->getRepositoryFullRawName(),
235 $this->getTargetObjectName(),
239 public function getActorGitHubUserID() {
241 return (int)idxv($raw, array('actor', 'id'));
244 private function getTargetObjectName() {
245 if ($this->isPullRequestEvent()) {
246 $number = $this->getRawIssueNumber();
247 return pht('Pull Request #%d', $number);
248 } else if ($this->isIssueEvent()) {
249 $number = $this->getRawIssueNumber();
250 return pht('Issue #%d', $number);
251 } else if ($this->type
== self
::TYPE_REPOSITORY
) {
255 $type = idx($raw, 'type');
258 $ref = idxv($raw, array('payload', 'ref'));
259 $ref_type = idxv($raw, array('payload', 'ref_type'));
263 return pht('Branch %s', $ref);
265 return pht('Tag %s', $ref);
267 return pht('Ref %s', $ref);
271 $ref = idxv($raw, array('payload', 'ref'));
272 if (preg_match('(^refs/heads/)', $ref)) {
273 return pht('Branch %s', substr($ref, strlen('refs/heads/')));
275 return pht('Ref %s', $ref);
279 $actor = idxv($raw, array('actor', 'login'));
280 return pht('User %s', $actor);
283 return pht('Unknown Object');
285 return pht('Unknown Object');
289 private function getRawIssueEventTitle() {
292 $action = idxv($raw, array('event'));
295 $assignee = idxv($raw, array('assignee', 'login'));
296 $title = pht('Assigned: %s', $assignee);
299 $title = pht('Closed');
302 $milestone = idxv($raw, array('milestone', 'title'));
303 $title = pht('Removed Milestone: %s', $milestone);
306 $label = idxv($raw, array('label', 'name'));
307 $title = pht('Added Label: %s', $label);
310 $title = pht('Locked');
313 $milestone = idxv($raw, array('milestone', 'title'));
314 $title = pht('Added Milestone: %s', $milestone);
317 $title = pht('Renamed');
320 $title = pht('Reopened');
323 $assignee = idxv($raw, array('assignee', 'login'));
324 $title = pht('Unassigned: %s', $assignee);
327 $label = idxv($raw, array('label', 'name'));
328 $title = pht('Removed Label: %s', $label);
331 $title = pht('Unlocked');
334 $title = pht('"%s"', $action);
342 private function getRawRepositoryEventTitle() {
345 $type = idx($raw, 'type');
348 return pht('Created');
350 $head = idxv($raw, array('payload', 'head'));
351 $head = substr($head, 0, 12);
352 return pht('Pushed: %s', $head);
354 $action = idxv($raw, array('payload', 'action'));
357 return pht('Closed');
359 return pht('Created');
361 return pht('Reopened');
363 return pht('"%s"', $action);
366 case 'IssueCommentEvent':
367 $action = idxv($raw, array('payload', 'action'));
370 return pht('Comment');
372 return pht('"%s"', $action);
375 case 'PullRequestEvent':
376 $action = idxv($raw, array('payload', 'action'));
379 return pht('Created');
381 return pht('"%s"', $action);
385 return pht('Watched');
388 return pht('"%s"', $type);