Skip to content

Instantly share code, notes, and snippets.

@prethiee
Last active March 10, 2020 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prethiee/400f748d1ade59317a3cb41f2f6435fe to your computer and use it in GitHub Desktop.
Save prethiee/400f748d1ade59317a3cb41f2f6435fe to your computer and use it in GitHub Desktop.
<?php
/**
* {@inheritdoc}
*/
public function storeCommentsByDrupalOrgUser($uid, UserInterface $user) {
foreach ($this->contribRetriever->getDrupalOrgCommentsByAuthor($uid) as $comment) {
$nid = $comment->node->id;
$link = sprintf("https://www.drupal.org/node/%s", $nid);
// Logger Notice
// If we have stored this comment, we have stored everything after it.
if ($this->contribStorage->getNodeForDrupalOrgIssueComment($comment->url)) {
$this->logger->notice('Skipping @comment, and all after it.', ['@comment' => $comment->url]);
break;
}
// Retrieving Issue comment data.
// This is a new comment. Get the issue node first.
$this->logger->info('Retrieving issue @nid...', ['@nid' => $nid]);
$issueData = $this->contribRetriever->getDrupalOrgNode($nid, FALSE, REQUEST_TIME + 180);
if (isset($issueData->type) && $issueData->type == 'project_issue') {
$issueNode = $this->contribStorage->getNodeForDrupalOrgIssue($link);
if (!$issueNode) {
$issueNode = $this->contribStorage->saveIssue($issueData, $user);
}
// Retrieve patch file details associated with the comment.
// Get the files in the reverse order.
$patchFiles = $totalFiles = 0;
$matched = FALSE;
if (!empty($issueData->field_issue_files)) {
$this->logger->info('Found @files files for the issue.', [
'@files' => count($issueData->field_issue_files),
]);
foreach (array_reverse($issueData->field_issue_files) as $fileRecord) {
$fileId = $fileRecord->file->id;
$this->logger->info('Getting file @fid...', ['@fid' => $fileId]);
$fileData = $this->contribRetriever->getFile($fileId);
if ($fileData->timestamp == $comment->created) {
$totalFiles++;
if ($this->isPatchFile($fileData)) {
$patchFiles++;
}
// We have found the file.
$matched = TRUE;
}
elseif ($matched) {
// We have matched at least one file. If we don't have a match
// anymore, stop looking for more.
break;
}
}
}
$this->logger->info('Matched @total files, of which @patch are patches.', [
'@total' => $totalFiles,
'@patch' => $patchFiles,
]);
// Determine the status of the Issue.
// Since we cannot access the revisions directly, we will see if the
// issue was updated at the same time as this comment (by using the
// 'changed' field). If it was, it is a safe assumption that the issue
// status reflects the status set in the comment.
// This is not accurate, especially for historic scans, but it is fairly
// accurate for new issues and comments.
$status = ($comment->created == $issueData->changed) ?
$this->getStatusFromCode((int) $issueData->field_issue_status) :
'';
// Retrieve the project name for the issue.
$this->logger->info('Getting project @nid...', ['@nid' => $issueData->field_project->id]);
$projectData = $this->contribRetriever->getDrupalOrgNode($issueData->field_project->id, FALSE, REQUEST_TIME + (6 * 3600));
if (!empty($projectData->title)) {
$projectTerm = $this->contribStorage->getProjectTerm($projectData->title);
// Save the Issue comment.
// We have everything we need. Save the issue comment as a code
// contribution node.
$this->logger->notice('Saving issue comment @link...', ['@link' => $comment->url]);
$this->contribStorage->saveIssueComment($comment, $issueNode, $projectTerm, $user, $patchFiles, $totalFiles, $status);
// Send Slack Notification.
$this->sendSlackNotification($user, $uid, $comment, $issueNode, $projectData, $patchFiles, $totalFiles, $status);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment