Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / nuance / github / NuanceGitHubRawEvent.php
blobb28a9222dc4d7eacaf8532031fec05db8a128621
1 <?php
3 final class NuanceGitHubRawEvent extends Phobject {
5 private $raw;
6 private $type;
8 const TYPE_ISSUE = 'issue';
9 const TYPE_REPOSITORY = 'repository';
11 public static function newEvent($type, array $raw) {
12 $event = new self();
13 $event->type = $type;
14 $event->raw = $raw;
15 return $event;
18 public function getRepositoryFullName() {
19 return $this->getRepositoryFullRawName();
22 public function isIssueEvent() {
23 if ($this->isPullRequestEvent()) {
24 return false;
27 if ($this->type == self::TYPE_ISSUE) {
28 return true;
31 switch ($this->getIssueRawKind()) {
32 case 'IssuesEvent':
33 return true;
34 case 'IssueCommentEvent':
35 if (!$this->getRawPullRequestData()) {
36 return true;
38 break;
41 return false;
44 public function isPullRequestEvent() {
45 if ($this->type == self::TYPE_ISSUE) {
46 // TODO: This is wrong, some of these are pull events.
47 return false;
50 $raw = $this->raw;
52 switch ($this->getIssueRawKind()) {
53 case 'PullRequestEvent':
54 return true;
55 case 'IssueCommentEvent':
56 if ($this->getRawPullRequestData()) {
57 return true;
59 break;
62 return false;
65 public function getIssueNumber() {
66 if (!$this->isIssueEvent()) {
67 return null;
70 return $this->getRawIssueNumber();
73 public function getPullRequestNumber() {
74 if (!$this->isPullRequestEvent()) {
75 return null;
78 return $this->getRawIssueNumber();
82 public function getID() {
83 $raw = $this->raw;
85 $id = idx($raw, 'id');
86 if ($id) {
87 return (int)$id;
90 return null;
93 public function getComment() {
94 if (!$this->isIssueEvent() && !$this->isPullRequestEvent()) {
95 return null;
98 $raw = $this->raw;
100 return idxv($raw, array('payload', 'comment', 'body'));
103 public function getURI() {
104 $raw = $this->raw;
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();
110 } else {
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'));
114 $need_anchor = true;
116 // For comments, we get a different anchor to link to the comment. In
117 // this case, the URI comes with an anchor already.
118 if (!$uri) {
119 $uri = idxv($raw, array('payload', 'comment', 'html_url'));
120 $need_anchor = false;
123 if (!$uri) {
124 $uri = idxv($raw, array('payload', 'issue', 'html_url'));
125 $need_anchor = true;
128 if ($need_anchor) {
129 $uri = $uri.'#event-'.$this->getID();
132 } else {
133 switch ($this->getIssueRawKind()) {
134 case 'CreateEvent':
135 $ref = idxv($raw, array('payload', 'ref'));
137 $repo = $this->getRepositoryFullRawName();
138 return "https://github.com/{$repo}/commits/{$ref}";
139 case 'PushEvent':
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) {
147 return null;
150 $repo = $this->getRepositoryFullRawName();
151 return "https://github.com/{$repo}/commits/{$head}";
152 case 'WatchEvent':
153 // These have no reasonable URI.
154 return null;
155 default:
156 return null;
160 return $uri;
163 private function getRepositoryFullRawName() {
164 $raw = $this->raw;
166 $full = idxv($raw, array('repo', 'name'));
167 if (strlen($full)) {
168 return $full;
171 // For issue events, the repository is not identified explicitly in the
172 // response body. Parse it out of the URI.
174 $matches = null;
175 $ok = preg_match(
176 '(/repos/((?:[^/]+)/(?:[^/]+))/issues/events/)',
177 idx($raw, 'url'),
178 $matches);
180 if ($ok) {
181 return $matches[1];
184 return null;
187 private function getIssueRawKind() {
188 $raw = $this->raw;
189 return idxv($raw, array('type'));
192 private function getRawIssueNumber() {
193 $raw = $this->raw;
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'));
201 if ($issue_number) {
202 return $issue_number;
205 $pull_number = idxv($raw, array('payload', 'number'));
206 if ($pull_number) {
207 return $pull_number;
211 return null;
214 private function getRawPullRequestData() {
215 $raw = $this->raw;
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();
223 break;
224 case self::TYPE_REPOSITORY:
225 $title = $this->getRawRepositoryEventTitle();
226 break;
227 default:
228 $title = pht('Unknown Event Type ("%s")', $this->type);
229 break;
232 return pht(
233 'GitHub %s %s (%s)',
234 $this->getRepositoryFullRawName(),
235 $this->getTargetObjectName(),
236 $title);
239 public function getActorGitHubUserID() {
240 $raw = $this->raw;
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) {
252 $raw = $this->raw;
255 $type = idx($raw, 'type');
256 switch ($type) {
257 case 'CreateEvent':
258 $ref = idxv($raw, array('payload', 'ref'));
259 $ref_type = idxv($raw, array('payload', 'ref_type'));
261 switch ($ref_type) {
262 case 'branch':
263 return pht('Branch %s', $ref);
264 case 'tag':
265 return pht('Tag %s', $ref);
266 default:
267 return pht('Ref %s', $ref);
269 break;
270 case 'PushEvent':
271 $ref = idxv($raw, array('payload', 'ref'));
272 if (preg_match('(^refs/heads/)', $ref)) {
273 return pht('Branch %s', substr($ref, strlen('refs/heads/')));
274 } else {
275 return pht('Ref %s', $ref);
277 break;
278 case 'WatchEvent':
279 $actor = idxv($raw, array('actor', 'login'));
280 return pht('User %s', $actor);
283 return pht('Unknown Object');
284 } else {
285 return pht('Unknown Object');
289 private function getRawIssueEventTitle() {
290 $raw = $this->raw;
292 $action = idxv($raw, array('event'));
293 switch ($action) {
294 case 'assigned':
295 $assignee = idxv($raw, array('assignee', 'login'));
296 $title = pht('Assigned: %s', $assignee);
297 break;
298 case 'closed':
299 $title = pht('Closed');
300 break;
301 case 'demilestoned':
302 $milestone = idxv($raw, array('milestone', 'title'));
303 $title = pht('Removed Milestone: %s', $milestone);
304 break;
305 case 'labeled':
306 $label = idxv($raw, array('label', 'name'));
307 $title = pht('Added Label: %s', $label);
308 break;
309 case 'locked':
310 $title = pht('Locked');
311 break;
312 case 'milestoned':
313 $milestone = idxv($raw, array('milestone', 'title'));
314 $title = pht('Added Milestone: %s', $milestone);
315 break;
316 case 'renamed':
317 $title = pht('Renamed');
318 break;
319 case 'reopened':
320 $title = pht('Reopened');
321 break;
322 case 'unassigned':
323 $assignee = idxv($raw, array('assignee', 'login'));
324 $title = pht('Unassigned: %s', $assignee);
325 break;
326 case 'unlabeled':
327 $label = idxv($raw, array('label', 'name'));
328 $title = pht('Removed Label: %s', $label);
329 break;
330 case 'unlocked':
331 $title = pht('Unlocked');
332 break;
333 default:
334 $title = pht('"%s"', $action);
335 break;
339 return $title;
342 private function getRawRepositoryEventTitle() {
343 $raw = $this->raw;
345 $type = idx($raw, 'type');
346 switch ($type) {
347 case 'CreateEvent':
348 return pht('Created');
349 case 'PushEvent':
350 $head = idxv($raw, array('payload', 'head'));
351 $head = substr($head, 0, 12);
352 return pht('Pushed: %s', $head);
353 case 'IssuesEvent':
354 $action = idxv($raw, array('payload', 'action'));
355 switch ($action) {
356 case 'closed':
357 return pht('Closed');
358 case 'opened':
359 return pht('Created');
360 case 'reopened':
361 return pht('Reopened');
362 default:
363 return pht('"%s"', $action);
365 break;
366 case 'IssueCommentEvent':
367 $action = idxv($raw, array('payload', 'action'));
368 switch ($action) {
369 case 'created':
370 return pht('Comment');
371 default:
372 return pht('"%s"', $action);
374 break;
375 case 'PullRequestEvent':
376 $action = idxv($raw, array('payload', 'action'));
377 switch ($action) {
378 case 'opened':
379 return pht('Created');
380 default:
381 return pht('"%s"', $action);
383 break;
384 case 'WatchEvent':
385 return pht('Watched');
388 return pht('"%s"', $type);