GitlabCommitsBridge.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * GitlabCommitsBridge
  4. *
  5. * @name GitlabCommits Bridge
  6. * @description Returns the commits of a project hosted on a gitlab instance
  7. */
  8. class GitlabCommitsBridge extends BridgeAbstract{
  9. public function loadMetadatas() {
  10. $this->maintainer = 'Pierre Mazière';
  11. $this->name = 'Gitlab Commits';
  12. $this->uri = '';
  13. $this->description = 'Returns the commits of a project hosted on a gitlab instance';
  14. $this->update = '2016-06-19';
  15. $this->parameters[] =
  16. '[
  17. {
  18. "name" : "Base URI",
  19. "identifier" : "uri"
  20. },
  21. {
  22. "name" : "User name",
  23. "identifier" : "u"
  24. },
  25. {
  26. "name" : "Project name",
  27. "identifier" : "p"
  28. },
  29. {
  30. "name" : "Project branch",
  31. "identifier" : "b"
  32. }
  33. ]';
  34. }
  35. public function collectData(array $param){
  36. $uri = $param['uri'].'/'.$param['u'].'/'.$param['p'].'/commits/';
  37. if(isset($param['b'])){
  38. $uri.=$param['b'];
  39. }else{
  40. $uri.='master';
  41. }
  42. $html = file_get_html($uri)
  43. or $this->returnError('No results for Gitlab Commits of project '.$param['uri'].'/'.$param['u'].'/'.$param['p'], 404);
  44. foreach($html->find('li.commit') as $commit){
  45. $item = new \Item();
  46. $item->uri=$param['uri'];
  47. foreach($commit->getElementsByTagName('a') as $a){
  48. $classes=explode(' ',$a->getAttribute("class"));
  49. if(in_array('commit-short-id',$classes) ||
  50. in_array('commit_short_id',$classes)){
  51. $href=$a->getAttribute('href');
  52. $item->uri.=substr($href,strpos($href,'/'.$param['u'].'/'.$param['p']));
  53. }
  54. if(in_array('commit-row-message',$classes)){
  55. $item->title=$a->plaintext;
  56. }
  57. if(in_array('commit-author-link',$classes)){
  58. $item->name=trim($a->plaintext);
  59. }
  60. }
  61. $pre=$commit->find('pre',0);
  62. if($pre){
  63. $item->content=$pre->outertext;
  64. }else{
  65. $item->content='';
  66. }
  67. $item->timestamp=strtotime($commit->find('time',0)->getAttribute('datetime'));
  68. $this->items[]=$item;
  69. }
  70. }
  71. public function getName(){
  72. return 'Gitlab Commits';
  73. }
  74. public function getURI(){
  75. return '';
  76. }
  77. public function getCacheDuration(){
  78. return 3600; // one hour
  79. }
  80. }