/src/Knp/Bundle/KnpBundlesBundle/Github/Repo.php

https://github.com/knpEdgar/knpbundles · PHP · 213 lines · 146 code · 27 blank · 40 comment · 19 complexity · 1b37df422a44717ea47af521c914b4f2 MD5 · raw file

  1. <?php
  2. namespace Knp\Bundle\KnpBundlesBundle\Github;
  3. use Symfony\Component\Console\Output\OutputInterface;
  4. use Knp\Bundle\KnpBundlesBundle\Entity;
  5. use Knp\Bundle\KnpBundlesBundle\Git;
  6. use Knp\Bundle\KnpBundlesBundle\Detector;
  7. class Repo
  8. {
  9. /**
  10. * php-github-api instance used to request GitHub API
  11. *
  12. * @var \Github_Client
  13. */
  14. protected $github = null;
  15. /**
  16. * Output buffer
  17. *
  18. * @var OutputInterface
  19. */
  20. protected $output = null;
  21. public function __construct(\Github_Client $github, OutputInterface $output, Git\RepoManager $gitRepoManager)
  22. {
  23. $this->github = $github;
  24. $this->output = $output;
  25. $this->gitRepoManager = $gitRepoManager;
  26. }
  27. public function update(Entity\Repo $repo)
  28. {
  29. try {
  30. $this->gitRepoManager->getRepo($repo)->update();
  31. } catch (\GitRuntimeException $e) {
  32. return false;
  33. }
  34. if (!$this->updateInfos($repo)) {
  35. return false;
  36. }
  37. if (!$this->updateFiles($repo)) {
  38. return false;
  39. }
  40. if (!$this->updateCommits($repo)) {
  41. return false;
  42. }
  43. if (!$this->updateTags($repo)) {
  44. return false;
  45. }
  46. $repo->recalculateScore();
  47. return $repo;
  48. }
  49. /**
  50. * Return true if the Repo exists on GitHub, false otherwise
  51. *
  52. * @param Entity\Repo $repo
  53. * @param array $data
  54. * @return boolean whether the Repo exists on GitHub
  55. */
  56. public function updateInfos(Entity\Repo $repo)
  57. {
  58. $this->output->write(' infos');
  59. try {
  60. $data = $this->github->getRepoApi()->show($repo->getUsername(), $repo->getName());
  61. } catch (\Github_HttpClient_Exception $e) {
  62. if (404 == $e->getCode()) {
  63. return false;
  64. }
  65. throw $e;
  66. }
  67. if($data['fork']) {
  68. if ($data['watchers'] >= 10) {
  69. // Let's try to keep a forked repo with lots of watchers
  70. } else {
  71. return false;
  72. }
  73. }
  74. $repo->setDescription(empty($data['description']) ? null : $data['description']);
  75. $repo->setNbFollowers($data['watchers']);
  76. $repo->setNbForks($data['forks']);
  77. $repo->setCreatedAt(new \DateTime($data['created_at']));
  78. $repo->setHomepage(empty($data['homepage']) ? null : $data['homepage']);
  79. return $repo;
  80. }
  81. public function updateCommits(Entity\Repo $repo)
  82. {
  83. $this->output->write(' commits');
  84. try {
  85. $commits = $this->github->getCommitApi()->getBranchCommits($repo->getUsername(), $repo->getName(), 'HEAD');
  86. } catch (\Github_HttpClient_Exception $e) {
  87. if (404 == $e->getCode()) {
  88. return false;
  89. }
  90. throw $e;
  91. }
  92. if (empty($commits)) {
  93. return false;
  94. }
  95. $repo->setLastCommits(array_slice($commits, 0, 30));
  96. return $repo;
  97. }
  98. public function updateCommitsFromGitRepo(Entity\Repo $repo)
  99. {
  100. $this->output->write(' commits');
  101. $commits = $this->gitRepoManager->getRepo($repo)->getCommits(30);
  102. $repo->setLastCommits($commits);
  103. return $repo;
  104. }
  105. public function updateFiles(Entity\Repo $repo)
  106. {
  107. $this->output->write(' files');
  108. $gitRepo = $this->gitRepoManager->getRepo($repo);
  109. if ($repo instanceof Entity\Project) {
  110. $detector = new Detector\Project();
  111. if (!$detector->matches($gitRepo)) {
  112. return false;
  113. }
  114. }
  115. foreach(array('README.markdown', 'README.md', 'README') as $readmeFilename) {
  116. if ($gitRepo->hasFile($readmeFilename)) {
  117. $repo->setReadme($gitRepo->getFileContent($readmeFilename));
  118. }
  119. }
  120. return $repo;
  121. }
  122. public function updateTags(Entity\Repo $repo)
  123. {
  124. $this->output->write(' tags');
  125. $gitRepo = $this->gitRepoManager->getRepo($repo);
  126. $tags = $gitRepo->getGitRepo()->getTags();
  127. $repo->setTags($tags);
  128. return $repo;
  129. }
  130. public function getContributorNames(Entity\Repo $repo)
  131. {
  132. try {
  133. $contributors = $this->github->getRepoApi()->getRepoContributors($repo->getUsername(), $repo->getName());
  134. } catch (\Github_HttpClient_Exception $e) {
  135. if (404 == $e->getCode()) {
  136. return array();
  137. }
  138. throw $e;
  139. }
  140. $names = array();
  141. foreach ($contributors as $contributor) {
  142. if ($repo->getUsername() != $contributor['login']) {
  143. $names[] = $contributor['login'];
  144. }
  145. }
  146. return $names;
  147. }
  148. /**
  149. * Get output
  150. *
  151. * @return OutputInterface
  152. */
  153. public function getOutput()
  154. {
  155. return $this->output;
  156. }
  157. /**
  158. * Set output
  159. *
  160. * @param OutputInterface
  161. * @return null
  162. */
  163. public function setOutput($output)
  164. {
  165. $this->output = $output;
  166. }
  167. /**
  168. * Get github
  169. *
  170. * @return \Github_Client
  171. */
  172. public function getGithubClient()
  173. {
  174. return $this->github;
  175. }
  176. /**
  177. * Set github
  178. *
  179. * @param \Github_Client
  180. * @return null
  181. */
  182. public function setGithubClient($github)
  183. {
  184. $this->github = $github;
  185. }
  186. }