[GitHubPullRequestBridge] Add new bridge inheriting GithubIssueBridge (#2001)

This commit is contained in:
Yaman Qalieh 2021-03-29 13:15:56 -04:00 committed by GitHub
parent b4f809aa44
commit 6f75d07456
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 29 deletions

View File

@ -33,17 +33,23 @@ class GithubIssueBridge extends BridgeAbstract {
) )
); );
// Allows generalization with GithubPullRequestBridge
const BRIDGE_OPTIONS = array(0 => 'Project Issues', 1 => 'Issue comments');
const URL_PATH = 'issues';
const SEARCH_QUERY_PATH = 'issues';
const SEARCH_QUERY = '?q=is%3Aissue+sort%3Aupdated-desc';
public function getName(){ public function getName(){
$name = $this->getInput('u') . '/' . $this->getInput('p'); $name = $this->getInput('u') . '/' . $this->getInput('p');
switch($this->queriedContext) { switch($this->queriedContext) {
case 'Project Issues': case static::BRIDGE_OPTIONS[0]: // Project Issues
$prefix = static::NAME . 's for '; $prefix = static::NAME . 's for ';
if($this->getInput('c')) { if($this->getInput('c')) {
$prefix = static::NAME . 's comments for '; $prefix = static::NAME . 's comments for ';
} }
$name = $prefix . $name; $name = $prefix . $name;
break; break;
case 'Issue comments': case static::BRIDGE_OPTIONS[1]: // Issue comments
$name = static::NAME . ' ' . $name . ' #' . $this->getInput('i'); $name = static::NAME . ' ' . $name . ' #' . $this->getInput('i');
break; break;
default: return parent::getName(); default: return parent::getName();
@ -51,14 +57,14 @@ class GithubIssueBridge extends BridgeAbstract {
return $name; return $name;
} }
public function getURI(){ public function getURI() {
if(null !== $this->getInput('u') && null !== $this->getInput('p')) { if(null !== $this->getInput('u') && null !== $this->getInput('p')) {
$uri = static::URI . $this->getInput('u') . '/' $uri = static::URI . $this->getInput('u') . '/'
. $this->getInput('p') . '/issues'; . $this->getInput('p') . '/';
if($this->queriedContext === 'Issue comments') { if($this->queriedContext === static::BRIDGE_OPTIONS[1]) {
$uri .= '/' . $this->getInput('i'); $uri .= static::URL_PATH . '/' . $this->getInput('i');
} elseif($this->getInput('c')) { } else {
$uri .= '?q=is%3Aissue+sort%3Aupdated-desc'; $uri .= static::SEARCH_QUERY_PATH . static::SEARCH_QUERY;
} }
return $uri; return $uri;
} }
@ -72,19 +78,19 @@ class GithubIssueBridge extends BridgeAbstract {
. $this->getInput('u') . $this->getInput('u')
. '/' . '/'
. $this->getInput('p') . $this->getInput('p')
. '/issues/' . '/' . static::URL_PATH . '/'
. $issue_number . $issue_number
. '#' . '#'
. $comment_id; . $comment_id;
} }
private function extractIssueEvent($issueNbr, $title, $comment){ private function extractIssueEvent($issueNbr, $title, $comment) {
$uri = $this->buildGitHubIssueCommentUri($issueNbr, $comment->id); $uri = $this->buildGitHubIssueCommentUri($issueNbr, $comment->id);
$author = $comment->find('.author', 0); $author = $comment->find('.author, .avatar', 0);
if ($author) { if ($author) {
$author = $author->plaintext; $author = trim($author->href, '/');
} else { } else {
$author = ''; $author = '';
} }
@ -95,20 +101,26 @@ class GithubIssueBridge extends BridgeAbstract {
$comment->find('.octicon', 0)->getAttribute('class') $comment->find('.octicon', 0)->getAttribute('class')
)); ));
$time = $comment->find('relative-time', 0);
if ($time === null) {
return;
}
foreach($comment->find('.Details-content--hidden, .btn') as $el) {
$el->innertext = '';
}
$content = $comment->plaintext; $content = $comment->plaintext;
$item = array(); $item = array();
$item['author'] = $author; $item['author'] = $author;
$item['uri'] = $uri; $item['uri'] = $uri;
$item['title'] = html_entity_decode($title, ENT_QUOTES, 'UTF-8'); $item['title'] = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
$item['timestamp'] = strtotime( $item['timestamp'] = strtotime($time->getAttribute('datetime'));
$comment->find('relative-time', 0)->getAttribute('datetime')
);
$item['content'] = $content; $item['content'] = $content;
return $item; return $item;
} }
private function extractIssueComment($issueNbr, $title, $comment){ private function extractIssueComment($issueNbr, $title, $comment) {
$uri = $this->buildGitHubIssueCommentUri($issueNbr, $comment->id); $uri = $this->buildGitHubIssueCommentUri($issueNbr, $comment->id);
$author = $comment->find('.author', 0)->plaintext; $author = $comment->find('.author', 0)->plaintext;
@ -117,20 +129,23 @@ class GithubIssueBridge extends BridgeAbstract {
$comment->find('.timeline-comment-header-text', 0)->plaintext $comment->find('.timeline-comment-header-text', 0)->plaintext
); );
$time = $comment->find('relative-time', 0);
if ($time === null) {
return;
}
$content = $comment->find('.comment-body', 0)->innertext; $content = $comment->find('.comment-body', 0)->innertext;
$item = array(); $item = array();
$item['author'] = $author; $item['author'] = $author;
$item['uri'] = $uri; $item['uri'] = $uri;
$item['title'] = html_entity_decode($title, ENT_QUOTES, 'UTF-8'); $item['title'] = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
$item['timestamp'] = strtotime( $item['timestamp'] = strtotime($time->getAttribute('datetime'));
$comment->find('relative-time', 0)->getAttribute('datetime')
);
$item['content'] = $content; $item['content'] = $content;
return $item; return $item;
} }
private function extractIssueComments($issue){ private function extractIssueComments($issue) {
$items = array(); $items = array();
$title = $issue->find('.gh-header-title', 0)->plaintext; $title = $issue->find('.gh-header-title', 0)->plaintext;
$issueNbr = trim( $issueNbr = trim(
@ -145,29 +160,33 @@ class GithubIssueBridge extends BridgeAbstract {
if ($comment->hasClass('comment')) { if ($comment->hasClass('comment')) {
$comment = $comment->parent; $comment = $comment->parent;
$item = $this->extractIssueComment($issueNbr, $title, $comment); $item = $this->extractIssueComment($issueNbr, $title, $comment);
$items[] = $item; if ($item !== null) {
$items[] = $item;
}
continue; continue;
} else { } else {
$comment = $comment->parent; $comment = $comment->parent;
$item = $this->extractIssueEvent($issueNbr, $title, $comment); $item = $this->extractIssueEvent($issueNbr, $title, $comment);
$items[] = $item; if ($item !== null) {
$items[] = $item;
}
} }
} }
return $items; return $items;
} }
public function collectData(){ public function collectData() {
$html = getSimpleHTMLDOM($this->getURI()) $html = getSimpleHTMLDOM($this->getURI())
or returnServerError( or returnServerError(
'No results for Github Issue ' . $this->getURI() 'No results for ' . static::NAME . ' ' . $this->getURI()
); );
switch($this->queriedContext) { switch($this->queriedContext) {
case 'Issue comments': case static::BRIDGE_OPTIONS[1]: // Issue comments
$this->items = $this->extractIssueComments($html); $this->items = $this->extractIssueComments($html);
break; break;
case 'Project Issues': case static::BRIDGE_OPTIONS[0]: // Project Issues
foreach($html->find('.js-active-navigation-container .js-navigation-item') as $issue) { foreach($html->find('.js-active-navigation-container .js-navigation-item') as $issue) {
$info = $issue->find('.opened-by', 0); $info = $issue->find('.opened-by', 0);
@ -179,7 +198,7 @@ class GithubIssueBridge extends BridgeAbstract {
if($this->getInput('c')) { if($this->getInput('c')) {
$uri = static::URI . $this->getInput('u') $uri = static::URI . $this->getInput('u')
. '/' . $this->getInput('p') . '/issues/' . $issueNbr; . '/' . $this->getInput('p') . '/' . static::URL_PATH . '/' . $issueNbr;
$issue = getSimpleHTMLDOMCached($uri, static::CACHE_TIMEOUT); $issue = getSimpleHTMLDOMCached($uri, static::CACHE_TIMEOUT);
if($issue) { if($issue) {
$this->items = array_merge( $this->items = array_merge(
@ -208,7 +227,7 @@ class GithubIssueBridge extends BridgeAbstract {
$item['content'] .= "\n" . 'Comments: ' . $comment_count; $item['content'] .= "\n" . 'Comments: ' . $comment_count;
$item['uri'] = self::URI $item['uri'] = self::URI
. $issue->find('.js-navigation-open', 0)->getAttribute('href'); . trim($issue->find('.js-navigation-open', 0)->getAttribute('href'), '/');
$this->items[] = $item; $this->items[] = $item;
} }
break; break;
@ -246,7 +265,7 @@ class GithubIssueBridge extends BridgeAbstract {
$show_comments = 'off'; $show_comments = 'off';
} break; } break;
case 3: { // Project issues with issue comments case 3: { // Project issues with issue comments
if($path_segments[2] !== 'issues') { if($path_segments[2] !== static::URL_PATH) {
return null; return null;
} }
list($user, $project) = $path_segments; list($user, $project) = $path_segments;

View File

@ -0,0 +1,38 @@
<?php
require_once('GithubIssueBridge.php');
class GitHubPullRequestBridge extends GithubIssueBridge {
const MAINTAINER = 'Yaman Qalieh';
const NAME = 'GitHub Pull Request';
const DESCRIPTION = 'Returns the pull request or comments of a pull request of a GitHub project';
const PARAMETERS = array(
'global' => array(
'u' => array(
'name' => 'User name',
'required' => true
),
'p' => array(
'name' => 'Project name',
'required' => true
)
),
'Project Pull Requests' => array(
'c' => array(
'name' => 'Show Pull Request Comments',
'type' => 'checkbox'
)
),
'Pull Request comments' => array(
'i' => array(
'name' => 'Pull Request number',
'type' => 'number',
'required' => true
)
)
);
const BRIDGE_OPTIONS = array(0 => 'Project Pull Requests', 1 => 'Pull Request comments');
const URL_PATH = 'pull';
const SEARCH_QUERY_PATH = 'pulls';
const SEARCH_QUERY = '?q=is%3Apr+sort%3Aupdated-desc';
}