1
0

BxGitHubApi.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defdroup GitHub GitHub
  7. * @indroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. *
  13. * Creating a fine-grained personal access token
  14. * https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token
  15. *
  16. * Creating a personal access token (classic)
  17. * https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-personal-access-token-classic
  18. *
  19. * Web application flow
  20. * https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authorizing-oauth-apps#web-application-flow
  21. *
  22. * PHP GitHub API
  23. * https://github.com/KnpLabs/php-github-api/tree/master/doc
  24. *
  25. */
  26. class BxGitHubApi extends BxDol
  27. {
  28. protected $_oModule;
  29. protected $_oClient;
  30. protected $_sToken;
  31. protected $_bInit;
  32. public function __construct(&$oModule)
  33. {
  34. $this->_oModule = $oModule;
  35. $this->_oClient = new \Github\Client();
  36. $this->_sToken = '';
  37. $this->_bInit = false;
  38. }
  39. public function init($sToken)
  40. {
  41. if(!$sToken)
  42. return false;
  43. $this->_sToken = $sToken;
  44. $this->_bInit = true;
  45. return true;
  46. }
  47. public function authenticate()
  48. {
  49. if(!$this->_bInit)
  50. return false;
  51. $this->_oClient->authenticate($this->_sToken, '', Github\AuthMethod::ACCESS_TOKEN);
  52. return true;
  53. }
  54. public function getRepositories($sUsername)
  55. {
  56. $aRepositories = [];
  57. if(!$this->authenticate())
  58. return $aRepositories;
  59. try {
  60. $aRepositories = $this->_oClient->api('user')->repositories($sUsername);
  61. } catch (Exception $ex) {
  62. $this->_processException('Get Repositories:', $oException);
  63. }
  64. return $aRepositories;
  65. }
  66. public function getIssues($sUsername, $sRepository, $sState = 'open')
  67. {
  68. $aIssues = [];
  69. if(!$this->authenticate())
  70. return $aIssues;
  71. try {
  72. $aIssues = $this->_oClient->api('issue')->all($sUsername, $sRepository, ['state' => $sState]);
  73. }
  74. catch (Exception $oException) {
  75. $this->_processException('Get Issues:', $oException);
  76. }
  77. return $aIssues;
  78. }
  79. public function getIssue($sUsername, $sRepository, $iIssue)
  80. {
  81. $aIssue = [];
  82. if(!$this->authenticate() || !$iIssue)
  83. return $aIssue;
  84. try {
  85. $aIssue = $this->_oClient->api('issue')->show($sUsername, $sRepository, $iIssue);
  86. }
  87. catch (Exception $oException) {
  88. $this->_processException('Get Issue:', $oException);
  89. }
  90. return $aIssue;
  91. }
  92. public function createIssue($sUsername, $sRepository, $sTitle, $sText = '', $aParams = [])
  93. {
  94. $aIssue = [];
  95. if(!$this->authenticate())
  96. return $aIssue;
  97. try {
  98. $aIssue = $this->_oClient->api('issue')->create($sUsername, $sRepository, array_merge(['title' => $sTitle, 'body' => $sText], $aParams));
  99. }
  100. catch (Exception $oException) {
  101. $this->_processException('Create Issue:', $oException);
  102. }
  103. return $aIssue;
  104. }
  105. public function updateIssue($sUsername, $sRepository, $iIssue, $aParams)
  106. {
  107. if(!$this->authenticate() || !$iIssue || !$aParams || !is_array($aParams))
  108. return false;
  109. $aIssue = false;
  110. try {
  111. $aIssue = $this->_oClient->api('issue')->update($sUsername, $sRepository, $iIssue, $aParams);
  112. }
  113. catch (Exception $oException) {
  114. $this->_processException('Update issue:', $oException);
  115. }
  116. return $aIssue;
  117. }
  118. public function createLabel($sUsername, $sRepository, $mixedLabel)
  119. {
  120. $aResult = [];
  121. if(!$this->authenticate() || !$mixedLabel)
  122. return $aResult;
  123. return $this->_oClient->api('issue')->labels()->create($sUsername, $sRepository, is_array($mixedLabel) ? $mixedLabel : [
  124. 'name' => $mixedLabel
  125. ]);
  126. }
  127. public function addLabel($sUsername, $sRepository, $iIssue, $sLabel)
  128. {
  129. if(!$this->authenticate() || !$iIssue || !$sLabel)
  130. return false;
  131. return $this->_oClient->api('issue')->labels()->add($sUsername, $sRepository, $iIssue, $sLabel);
  132. }
  133. protected function _processException($sMessage, &$oException)
  134. {
  135. $sError = $oException->getMessage();
  136. $this->_logError($sMessage . ' ' . $sError);
  137. return false;
  138. }
  139. protected function _logError($sContents)
  140. {
  141. $this->_oModule->log($sContents, BX_LOG_ERR);
  142. }
  143. }