From 61179bfcf92dfbfca9fc36cf753712f97b7a34cd Mon Sep 17 00:00:00 2001 From: gileri Date: Sun, 17 Apr 2022 15:11:17 +0200 Subject: [PATCH 1/9] [GiteaBridge]: Rewrite to decouple from GogsBridge --- bridges/GiteaBridge.php | 211 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 206 insertions(+), 5 deletions(-) diff --git a/bridges/GiteaBridge.php b/bridges/GiteaBridge.php index 332478734e6..d8eb4ad4dac 100644 --- a/bridges/GiteaBridge.php +++ b/bridges/GiteaBridge.php @@ -1,27 +1,228 @@ array( + 'host' => array( + 'name' => 'Host', + 'exampleValue' => 'https://try.gitea.io', + 'required' => true, + 'title' => 'Host name with its protocol, without trailing slash', + ), + 'user' => array( + 'name' => 'Username', + 'exampleValue' => 'gitea', + 'required' => true, + 'title' => 'User name as it appears in the URL', + ), + 'project' => array( + 'name' => 'Project name', + 'exampleValue' => 'gitea', + 'required' => true, + 'title' => 'Project name as it appears in the URL', + ), + ), + 'Commits' => array( + 'branch' => array( + 'name' => 'Branch name', + 'defaultValue' => 'master', + 'required' => true, + 'title' => 'Branch name as it appears in the URL', + ), + ), + 'Issues' => array( + 'include_description' => array( + 'name' => 'Include issue description', + 'type' => 'checkbox', + 'title' => 'Activate to include the issue description', + ), + ), + 'Single issue' => array( + 'issue' => array( + 'name' => 'Issue number', + 'type' => 'number', + 'exampleValue' => 100, + 'required' => true, + 'title' => 'Issue number from the issues list', + ), + ), + 'Releases' => array(), + 'Tags' => array(), + ); + + private $title = ''; + + public function getIcon() { + return 'https://gitea.io/images/gitea.png'; + } + + public function getName() { + switch($this->queriedContext) { + case 'Commits': + case 'Issues': + case 'Releases': return $this->title . ' ' . $this->queriedContext; + case 'Single issue': return ' Issue ' . $this->getInput('issue') . ': ' . $this->title; + case 'Tags': return 'Tags for ' . $this->title; + default: return parent::getName(); + } + } + + public function getURI() { + switch($this->queriedContext) { + case 'Commits': { + return $this->getInput('host') + . '/' . $this->getInput('user') + . '/' . $this->getInput('project') + . '/commits/' . $this->getInput('branch'); + } break; + case 'Issues': { + return $this->getInput('host') + . '/' . $this->getInput('user') + . '/' . $this->getInput('project') + . '/issues/'; + } break; + case 'Single issue': { + return $this->getInput('host') + . '/' . $this->getInput('user') + . '/' . $this->getInput('project') + . '/issues/' . $this->getInput('issue'); + } break; + case 'Releases': { + return $this->getInput('host') + . '/' . $this->getInput('user') + . '/' . $this->getInput('project') + . '/releases/'; + } break; + case 'Tags': { + return $this->getInput('host') + . '/' . $this->getInput('user') + . '/' . $this->getInput('project') + . '/tags/'; + } break; + default: return parent::getURI(); + } + } + + public function collectData() { + $html = getSimpleHTMLDOM($this->getURI()) + or returnServerError('Could not request ' . $this->getURI()); + $html = defaultLinkTo($html, $this->getURI()); + + $this->title = $html->find('[property="og:title"]', 0)->content; + + switch($this->queriedContext) { + case 'Commits': { + $this->collectCommitsData($html); + } break; + case 'Issues': { + $this->collectIssuesData($html); + } break; + case 'Single issue': { + $this->collectSingleIssueData($html); + } break; + case 'Releases': { + $this->collectReleasesData($html); + } break; + case 'Tags': { + $this->collectTagsData($html); + } break; + } + } + protected function collectReleasesData($html) { $releases = $html->find('#release-list > li') or returnServerError('Unable to find releases'); foreach($releases as $release) { $this->items[] = array( + 'author' => $release->find('.author', 0)->plaintext, 'uri' => $release->find('a', 0)->href, - 'title' => 'Release ' . $release->find('h3', 0)->plaintext, + 'title' => 'Release ' . $release->find('h4', 0)->plaintext, + 'timestamp' => $release->find('.time-since', 0)->title, + ); + } + } + + protected function collectTagsData($html) { + $tags = $html->find('table#tags-table > tbody > tr') + or returnServerError('Unable to find tags'); + + foreach($tags as $tag) { + $this->items[] = array( + 'uri' => $tag->find('a', 0)->href, + 'title' => 'Tag ' . $tag->find('.release-tag-name > a', 0)->plaintext, + ); + } + } + + protected function collectCommitsData($html) { + $commits = $html->find('#commits-table tbody tr') + or returnServerError('Unable to find commits'); + + foreach($commits as $commit) { + $this->items[] = array( + 'uri' => $commit->find('a.sha', 0)->href, + 'title' => $commit->find('.message span', 0)->plaintext, + 'author' => $commit->find('.author', 0)->plaintext, + 'timestamp' => $commit->find('.time-since', 0)->title, + 'uid' => $commit->find('.sha', 0)->plaintext, + ); + } + } + + protected function collectIssuesData($html) { + $issues = $html->find('.issue.list li') + or returnServerError('Unable to find issues'); + + foreach($issues as $issue) { + $uri = $issue->find('a', 0)->href; + + $item = array( + 'uri' => $uri, + 'title' => trim($issue->find('a.index', 0)->plaintext) . ' | ' . $issue->find('a.title', 0)->plaintext, + 'author' => $issue->find('.desc a', 1)->plaintext, + 'timestamp' => $issue->find('.time-since', 0)->title, ); + + if($this->getInput('include_description')) { + $issue_html = getSimpleHTMLDOMCached($uri, 3600) + or returnServerError('Unable to load issue description'); + + $issue_html = defaultLinkTo($issue_html, $uri); + + $item['content'] = $issue_html->find('.comment .markup', 0); + } + + $this->items[] = $item; } } + + protected function collectSingleIssueData($html) { + $comments = $html->find('.comment') + or returnServerError('Unable to find comments'); + + foreach($comments as $comment) { + $commentLink = $comment->find('a[href*="#issue"]', 0); + $this->items[] = array( + 'uri' => $commentLink->href, + 'title' => str_replace($commentLink->plaintext, '', $comment->find('span', 0)->plaintext), + 'author' => $comment->find('.author a', 0)->plaintext, + 'timestamp' => $comment->find('.time-since', 0)->title, + 'content' => $comment->find('.render-content', 0), + ); + } + + $this->items = array_reverse($this->items); + } } From ad6aec858a76605b4df91209b8fa9a9cbc225747 Mon Sep 17 00:00:00 2001 From: gileri Date: Tue, 10 May 2022 23:46:06 +0200 Subject: [PATCH 2/9] [GiteaBridge] Add Pull requests context --- bridges/GiteaBridge.php | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/bridges/GiteaBridge.php b/bridges/GiteaBridge.php index d8eb4ad4dac..44912d82f2e 100644 --- a/bridges/GiteaBridge.php +++ b/bridges/GiteaBridge.php @@ -57,6 +57,13 @@ class GiteaBridge extends BridgeAbstract { 'title' => 'Issue number from the issues list', ), ), + 'Pull requests' => array( + 'include_description' => array( + 'name' => 'Include issue description', + 'type' => 'checkbox', + 'title' => 'Activate to include the issue description', + ), + ), 'Releases' => array(), 'Tags' => array(), ); @@ -71,6 +78,7 @@ public function getName() { switch($this->queriedContext) { case 'Commits': case 'Issues': + case 'Pull requests': case 'Releases': return $this->title . ' ' . $this->queriedContext; case 'Single issue': return ' Issue ' . $this->getInput('issue') . ': ' . $this->title; case 'Tags': return 'Tags for ' . $this->title; @@ -110,6 +118,12 @@ public function getURI() { . '/' . $this->getInput('project') . '/tags/'; } break; + case 'Pull requests': { + return $this->getInput('host') + . '/' . $this->getInput('user') + . '/' . $this->getInput('project') + . '/pulls/'; + } break; default: return parent::getURI(); } } @@ -128,6 +142,9 @@ public function collectData() { case 'Issues': { $this->collectIssuesData($html); } break; + case 'Pull requests': { + $this->collectPullRequestsData($html); + } break; case 'Single issue': { $this->collectSingleIssueData($html); } break; @@ -225,4 +242,31 @@ protected function collectSingleIssueData($html) { $this->items = array_reverse($this->items); } + + protected function collectPullRequestsData($html) { + $issues = $html->find('.issue.list li') + or returnServerError('Unable to find pull requests'); + + foreach($issues as $issue) { + $uri = $issue->find('a', 0)->href; + + $item = array( + 'uri' => $uri, + 'title' => trim($issue->find('a.index', 0)->plaintext) . ' | ' . $issue->find('a.title', 0)->plaintext, + 'author' => $issue->find('.desc a', 1)->plaintext, + 'timestamp' => $issue->find('.time-since', 0)->title, + ); + + if($this->getInput('include_description')) { + $issue_html = getSimpleHTMLDOMCached($uri, 3600) + or returnServerError('Unable to load issue description'); + + $issue_html = defaultLinkTo($issue_html, $uri); + + $item['content'] = $issue_html->find('.comment .markup', 0); + } + + $this->items[] = $item; + } + } } From b7c89525639ef17c224cd37f28245ed6de1f4665 Mon Sep 17 00:00:00 2001 From: gileri Date: Wed, 11 May 2022 00:15:28 +0200 Subject: [PATCH 3/9] [GiteaBridge] Add single pull request context --- bridges/GiteaBridge.php | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/bridges/GiteaBridge.php b/bridges/GiteaBridge.php index 44912d82f2e..896878dc339 100644 --- a/bridges/GiteaBridge.php +++ b/bridges/GiteaBridge.php @@ -57,6 +57,15 @@ class GiteaBridge extends BridgeAbstract { 'title' => 'Issue number from the issues list', ), ), + 'Single pull request' => array( + 'pull_request' => array( + 'name' => 'Pull request number', + 'type' => 'number', + 'exampleValue' => 100, + 'required' => true, + 'title' => 'Pull request number from the issues list', + ), + ), 'Pull requests' => array( 'include_description' => array( 'name' => 'Include issue description', @@ -81,6 +90,7 @@ public function getName() { case 'Pull requests': case 'Releases': return $this->title . ' ' . $this->queriedContext; case 'Single issue': return ' Issue ' . $this->getInput('issue') . ': ' . $this->title; + case 'Single pull request': return 'Pull request ' . $this->getInput('pull_request') . ': ' . $this->title; case 'Tags': return 'Tags for ' . $this->title; default: return parent::getName(); } @@ -124,6 +134,12 @@ public function getURI() { . '/' . $this->getInput('project') . '/pulls/'; } break; + case 'Single pull request': { + return $this->getInput('host') + . '/' . $this->getInput('user') + . '/' . $this->getInput('project') + . '/pulls/' . $this->getInput('pull_request'); + } break; default: return parent::getURI(); } } @@ -148,6 +164,9 @@ public function collectData() { case 'Single issue': { $this->collectSingleIssueData($html); } break; + case 'Single pull request': { + $this->collectSinglePullRequestData($html); + } break; case 'Releases': { $this->collectReleasesData($html); } break; @@ -269,4 +288,28 @@ protected function collectPullRequestsData($html) { $this->items[] = $item; } } + + protected function collectSinglePullRequestData($html) { + $comments = $html->find('.comment') + or returnServerError('Unable to find comments'); + + foreach($comments as $comment) { + $commentLink = $comment->find('a[href*="#issue"]', 0); + if (strpos($comment->getAttribute('class'), 'form') !== false + || strpos($comment->getAttribute('class'), 'merge') !== false + ) { + // Ignore comment form and merge information + continue; + } + $this->items[] = array( + 'uri' => $comment->find('a[href*="#issue"]', 0)->href, + 'title' => str_replace($commentLink->plaintext, '', $comment->find('span', 0)->plaintext), + 'author' => $comment->find('.content a', 0)->plaintext, + 'timestamp' => $comment->find('.time-since', 0)->title, + 'content' => $comment->find('.markup', 0), + ); + } + + $this->items = array_reverse($this->items); + } } From e4837a1fec0366a012e02a88c743c24b99d45d64 Mon Sep 17 00:00:00 2001 From: gileri Date: Wed, 11 May 2022 00:26:17 +0200 Subject: [PATCH 4/9] [GiteaBridge] Fix parameter name and title --- bridges/GiteaBridge.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bridges/GiteaBridge.php b/bridges/GiteaBridge.php index 896878dc339..8e65d3e6700 100644 --- a/bridges/GiteaBridge.php +++ b/bridges/GiteaBridge.php @@ -68,9 +68,9 @@ class GiteaBridge extends BridgeAbstract { ), 'Pull requests' => array( 'include_description' => array( - 'name' => 'Include issue description', + 'name' => 'Include pull request description', 'type' => 'checkbox', - 'title' => 'Activate to include the issue description', + 'title' => 'Activate to include the pull request description', ), ), 'Releases' => array(), From 51827066394223345efe997aae54a3890d4b2fd8 Mon Sep 17 00:00:00 2001 From: gileri Date: Wed, 11 May 2022 18:52:41 +0200 Subject: [PATCH 5/9] [GiteaBridge]: Suggest a "real" repository in exampleValues --- bridges/GiteaBridge.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bridges/GiteaBridge.php b/bridges/GiteaBridge.php index 8e65d3e6700..4207cf8eca4 100644 --- a/bridges/GiteaBridge.php +++ b/bridges/GiteaBridge.php @@ -16,7 +16,7 @@ class GiteaBridge extends BridgeAbstract { 'global' => array( 'host' => array( 'name' => 'Host', - 'exampleValue' => 'https://try.gitea.io', + 'exampleValue' => 'https://gitea.com', 'required' => true, 'title' => 'Host name with its protocol, without trailing slash', ), @@ -28,7 +28,7 @@ class GiteaBridge extends BridgeAbstract { ), 'project' => array( 'name' => 'Project name', - 'exampleValue' => 'gitea', + 'exampleValue' => 'helm-chart', 'required' => true, 'title' => 'Project name as it appears in the URL', ), From 2ecbfa336d5608b885f50ff685f6ee4a90e0642d Mon Sep 17 00:00:00 2001 From: gileri Date: Wed, 11 May 2022 19:03:59 +0200 Subject: [PATCH 6/9] [GiteaBridge]: Merge single PR and issue handling --- bridges/GiteaBridge.php | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/bridges/GiteaBridge.php b/bridges/GiteaBridge.php index 4207cf8eca4..a82eb4180d5 100644 --- a/bridges/GiteaBridge.php +++ b/bridges/GiteaBridge.php @@ -162,10 +162,10 @@ public function collectData() { $this->collectPullRequestsData($html); } break; case 'Single issue': { - $this->collectSingleIssueData($html); + $this->collectSingleIssueOrPrData($html); } break; case 'Single pull request': { - $this->collectSinglePullRequestData($html); + $this->collectSingleIssueOrPrData($html); } break; case 'Releases': { $this->collectReleasesData($html); @@ -244,11 +244,17 @@ protected function collectIssuesData($html) { } } - protected function collectSingleIssueData($html) { + protected function collectSingleIssueOrPrData($html) { $comments = $html->find('.comment') or returnServerError('Unable to find comments'); foreach($comments as $comment) { + if (strpos($comment->getAttribute('class'), 'form') !== false + || strpos($comment->getAttribute('class'), 'merge') !== false + ) { + // Ignore comment form and merge information + continue; + } $commentLink = $comment->find('a[href*="#issue"]', 0); $this->items[] = array( 'uri' => $commentLink->href, @@ -288,28 +294,4 @@ protected function collectPullRequestsData($html) { $this->items[] = $item; } } - - protected function collectSinglePullRequestData($html) { - $comments = $html->find('.comment') - or returnServerError('Unable to find comments'); - - foreach($comments as $comment) { - $commentLink = $comment->find('a[href*="#issue"]', 0); - if (strpos($comment->getAttribute('class'), 'form') !== false - || strpos($comment->getAttribute('class'), 'merge') !== false - ) { - // Ignore comment form and merge information - continue; - } - $this->items[] = array( - 'uri' => $comment->find('a[href*="#issue"]', 0)->href, - 'title' => str_replace($commentLink->plaintext, '', $comment->find('span', 0)->plaintext), - 'author' => $comment->find('.content a', 0)->plaintext, - 'timestamp' => $comment->find('.time-since', 0)->title, - 'content' => $comment->find('.markup', 0), - ); - } - - $this->items = array_reverse($this->items); - } } From 0c05978145d1f15b2a2e781499a1458dbbf7a880 Mon Sep 17 00:00:00 2001 From: gileri Date: Wed, 11 May 2022 19:04:30 +0200 Subject: [PATCH 7/9] [GiteaBridge]: Fix single issue/PR author handling --- bridges/GiteaBridge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/GiteaBridge.php b/bridges/GiteaBridge.php index a82eb4180d5..77a2c449c34 100644 --- a/bridges/GiteaBridge.php +++ b/bridges/GiteaBridge.php @@ -259,7 +259,7 @@ protected function collectSingleIssueOrPrData($html) { $this->items[] = array( 'uri' => $commentLink->href, 'title' => str_replace($commentLink->plaintext, '', $comment->find('span', 0)->plaintext), - 'author' => $comment->find('.author a', 0)->plaintext, + 'author' => $comment->find('a.author', 0)->plaintext, 'timestamp' => $comment->find('.time-since', 0)->title, 'content' => $comment->find('.render-content', 0), ); From bf3abdea4320795e5b57116fd5603df40e763ca0 Mon Sep 17 00:00:00 2001 From: gileri Date: Wed, 11 May 2022 23:43:32 +0200 Subject: [PATCH 8/9] [GiteaBridge] Handle review request comments --- bridges/GiteaBridge.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/bridges/GiteaBridge.php b/bridges/GiteaBridge.php index 77a2c449c34..5044afff4e1 100644 --- a/bridges/GiteaBridge.php +++ b/bridges/GiteaBridge.php @@ -256,13 +256,21 @@ protected function collectSingleIssueOrPrData($html) { continue; } $commentLink = $comment->find('a[href*="#issue"]', 0); - $this->items[] = array( - 'uri' => $commentLink->href, - 'title' => str_replace($commentLink->plaintext, '', $comment->find('span', 0)->plaintext), + $item = array( 'author' => $comment->find('a.author', 0)->plaintext, - 'timestamp' => $comment->find('.time-since', 0)->title, 'content' => $comment->find('.render-content', 0), ); + if ($commentLink !== null) { + // Regular comment + $item['uri'] = $commentLink->href; + $item['title'] = str_replace($commentLink->plaintext, '', $comment->find('span', 0)->plaintext); + $item['timestamp'] = $comment->find('.time-since', 0)->title; + } else { + // Change request comment + $item['uri'] = $this->getURI() . '#' . $comment->getAttribute('id'); + $item['title'] = $comment->find('.comment-header .text', 0)->plaintext; + } + $this->items[] = $item; } $this->items = array_reverse($this->items); From f14aecd28ada02a98c83ba788fee2f4ebeaeede1 Mon Sep 17 00:00:00 2001 From: gileri Date: Wed, 11 May 2022 23:46:22 +0200 Subject: [PATCH 9/9] [GiteaBridge]: Cleaner titles --- bridges/GiteaBridge.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bridges/GiteaBridge.php b/bridges/GiteaBridge.php index 5044afff4e1..5b393d54998 100644 --- a/bridges/GiteaBridge.php +++ b/bridges/GiteaBridge.php @@ -88,10 +88,10 @@ public function getName() { case 'Commits': case 'Issues': case 'Pull requests': - case 'Releases': return $this->title . ' ' . $this->queriedContext; - case 'Single issue': return ' Issue ' . $this->getInput('issue') . ': ' . $this->title; + case 'Releases': + case 'Tags': return $this->title . ' ' . $this->queriedContext; + case 'Single issue': return 'Issue ' . $this->getInput('issue') . ': ' . $this->title; case 'Single pull request': return 'Pull request ' . $this->getInput('pull_request') . ': ' . $this->title; - case 'Tags': return 'Tags for ' . $this->title; default: return parent::getName(); } }