You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.1 KiB

  1. <?php
  2. namespace XRay\Formats;
  3. use DateTime, DateTimeZone;
  4. use Parse, Config;
  5. use cebe\markdown\GithubMarkdown;
  6. class GitHub {
  7. public static function parse($http, $url, $creds, $json=null) {
  8. if(!$json) {
  9. // Transform the GitHub URL to an API request
  10. if(preg_match('~https://github.com/([^/]+)/([^/]+)/pull/(\d+)$~', $url, $match)) {
  11. $type = 'pull';
  12. $org = $match[1];
  13. $repo = $match[2];
  14. $pull = $match[3];
  15. $apiurl = 'https://api.github.com/repos/'.$org.'/'.$repo.'/pulls/'.$pull;
  16. } elseif(preg_match('~https://github.com/([^/]+)/([^/]+)/issues/(\d+)$~', $url, $match)) {
  17. $type = 'issue';
  18. $org = $match[1];
  19. $repo = $match[2];
  20. $issue = $match[3];
  21. $apiurl = 'https://api.github.com/repos/'.$org.'/'.$repo.'/issues/'.$issue;
  22. } elseif(preg_match('~https://github.com/([^/]+)/([^/]+)$~', $url, $match)) {
  23. $type = 'repo';
  24. $org = $match[1];
  25. $repo = $match[2];
  26. $apiurl = 'https://api.github.com/repos/'.$org.'/'.$repo;
  27. } elseif(preg_match('~https://github.com/([^/]+)/([^/]+)/issues/(\d+)#issuecomment-(\d+)~', $url, $match)) {
  28. $type = 'comment';
  29. $org = $match[1];
  30. $repo = $match[2];
  31. $issue = $match[3];
  32. $comment = $match[4];
  33. $apiurl = 'https://api.github.com/repos/'.$org.'/'.$repo.'/issues/comments/'.$comment;
  34. } else {
  35. return [null, null, 0];
  36. }
  37. $response = $http->get($apiurl, ['User-Agent: XRay ('.Config::$base.')']);
  38. if($response['code'] != 200) {
  39. return [null, $response['body'], $response['code']];
  40. }
  41. $data = json_decode($response['body'], true);
  42. } else {
  43. $data = json_decode($json, true);
  44. }
  45. if(!$data) {
  46. return [null, null, 0];
  47. }
  48. // Start building the h-entry
  49. $entry = array(
  50. 'type' => ($type == 'repo' ? 'repo' : 'entry'),
  51. 'url' => $url,
  52. 'author' => [
  53. 'type' => 'card',
  54. 'name' => null,
  55. 'photo' => null,
  56. 'url' => null
  57. ]
  58. );
  59. if($type == 'repo')
  60. $authorkey = 'owner';
  61. else
  62. $authorkey = 'user';
  63. $entry['author']['name'] = $data[$authorkey]['login'];
  64. $entry['author']['photo'] = $data[$authorkey]['avatar_url'];
  65. $entry['author']['url'] = $data[$authorkey]['html_url'];
  66. if($type == 'pull') {
  67. $entry['name'] = '#' . $pull . ' ' . $data['title'];
  68. } elseif($type == 'issue') {
  69. $entry['name'] = '#' . $issue . ' ' . $data['title'];
  70. } elseif($type == 'repo') {
  71. $entry['name'] = $data['name'];
  72. }
  73. if($type == 'repo') {
  74. if(!empty($data['description']))
  75. $entry['summary'] = $data['description'];
  76. }
  77. if($type != 'repo' && !empty($data['body'])) {
  78. $parser = new GithubMarkdown();
  79. $entry['content'] = [
  80. 'text' => $data['body'],
  81. 'html' => $parser->parse($data['body'])
  82. ];
  83. }
  84. if(!empty($data['labels'])) {
  85. $entry['category'] = $data['labels'];
  86. }
  87. $entry['published'] = $data['created_at'];
  88. $response = [
  89. 'data' => $entry
  90. ];
  91. return [$response, $json, $response['code']];
  92. }
  93. }