Skip to content

Commit

Permalink
[GithubIssueBridge] Fix issue comments and events parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Mazière <[email protected]>
  • Loading branch information
Pierre Mazière authored and logmanoriginal committed Nov 29, 2018
1 parent de57598 commit d7cabfc
Showing 1 changed file with 49 additions and 51 deletions.
100 changes: 49 additions & 51 deletions bridges/GithubIssueBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,62 +66,50 @@ public function getURI(){
return parent::getURI();
}

protected function extractIssueComment($issueNbr, $title, $comment){
$class = $comment->getAttribute('class');
$classes = explode(' ', $class);
$event = false;
if(in_array('discussion-item', $classes)) {
$event = true;
}
protected function extractIssueEvent($issueNbr, $title, $comment){
$comment = $comment->firstChild();
$uri = static::URI . $this->getInput('u') . '/' . $this->getInput('p')
. '/issues/' . $issueNbr . '#' . $comment->getAttribute('id');

$author = $comment->find('.author', 0)->plaintext;

$author = 'unknown';
if($comment->find('.author', 0)) {
$author = $comment->find('.author', 0)->plaintext;
$title .= ' / ' . trim($comment->plaintext);

$content = $title;
if (null !== $comment->nextSibling()) {
$content = $comment->nextSibling()->innertext;
if ($comment->nextSibling()->nodeName() === 'span') {
$content = $comment->nextSibling()->nextSibling()->innertext;
}
}

$item = array();
$item['author'] = $author;
$item['uri'] = $uri;
$item['title'] = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
$item['timestamp'] = strtotime(
$comment->find('relative-time', 0)->getAttribute('datetime')
);
$item['content'] = $content;
return $item;
}

protected function extractIssueComment($issueNbr, $title, $comment){
$uri = static::URI . $this->getInput('u') . '/'
. $this->getInput('p') . '/issues/' . $issueNbr;

$comment = $comment->firstChild();
if(!$event) {
$comment = $comment->nextSibling();
$title .= ' / ' . trim($comment->firstChild()->plaintext);
$content = '<pre>';
$content .= $comment->find('.comment-body', 0)->innertext;
$content .= '</pre>';
}
$author = $comment->find('.author', 0)->plaintext;

if($event) {
$title .= ' / ';
$title .= substr(
$class,
strpos($class, 'discussion-item-') + strlen('discussion-item-')
);
if(!$comment->hasAttribute('id')) {
$items = array();
$timestamp = strtotime(
$comment->find('relative-time', 0)->getAttribute('datetime')
);
$content = $comment->innertext;
while($comment = $comment->nextSibling()) {
$item = array();
$item['author'] = $author;
$item['title'] = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
$item['timestamp'] = $timestamp;
$item['content'] = $content . '<p>'
. $comment->children(1)->innertext . '</p>';
$item['uri'] = $uri . '#'
. $comment->children(1)->getAttribute('id');
$items[] = $item;
}
return $items;
}
$content = $comment->parent()->innertext;
}
$title .= ' / ' . trim(
$comment->find('.comment .timeline-comment-header-text', 0)->plaintext
);

$content = $comment->find('.comment-body', 0)->innertext;

$item = array();
$item['author'] = $author;
$item['uri'] = $uri . '#' . $comment->getAttribute('id');
$item['uri'] = $uri
. '#' . $comment->firstChild()->nextSibling()->getAttribute('id');
$item['title'] = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
$item['timestamp'] = strtotime(
$comment->find('relative-time', 0)->getAttribute('datetime')
Expand All @@ -138,14 +126,24 @@ protected function extractIssueComments($issue){
);
$comments = $issue->find('.js-discussion', 0);
foreach($comments->children() as $comment) {
if (!$comment->hasChildNodes()) {
continue;
}
$comment = $comment->firstChild();
$classes = explode(' ', $comment->getAttribute('class'));
if(in_array('discussion-item', $classes)
|| in_array('timeline-comment-wrapper', $classes)) {
if (in_array('timeline-comment-wrapper', $classes)) {
$item = $this->extractIssueComment($issueNbr, $title, $comment);
if(array_keys($item) !== range(0, count($item) - 1)) {
$item = array($item);
$items[] = $item;
continue;
}
while (in_array('discussion-item', $classes)) {
$item = $this->extractIssueEvent($issueNbr, $title, $comment);
$items[] = $item;
$comment = $comment->nextSibling();
if (null == $comment) {
break;
}
$items = array_merge($items, $item);
$classes = explode(' ', $comment->getAttribute('class'));
}
}
return $items;
Expand Down

0 comments on commit d7cabfc

Please sign in to comment.