Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dop
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use DrupalPatchUtils\Command\CreateIssue;
use DrupalPatchUtils\Command\Login;
use DrupalPatchUtils\Command\Logout;
use DrupalPatchUtils\Command\PostComment;
use DrupalPatchUtils\Command\PostIssueComment;
use DrupalPatchUtils\Command\SearchIssuePatch;
use DrupalPatchUtils\Command\SearchRtbcPatches;
use DrupalPatchUtils\Command\ValidatePatch;
Expand All @@ -23,6 +24,7 @@ $application->add(new SearchIssuePatch());
$application->add(new SearchRtbcPatches());
$application->add(new ValidatePatch());
$application->add(new ValidateRtbcPatches());
$application->add(new PostIssueComment());
$application->add(new Login());
$application->add(new Logout());
$application->run();
16 changes: 16 additions & 0 deletions src/DrupalPatchUtils/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace DrupalPatchUtils\Command;

use DrupalPatchUtils\Config;
use DrupalPatchUtils\DoBrowser;
use DrupalPatchUtils\Issue;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -90,4 +91,19 @@ protected function getDialog() {
protected function askConfirmation (OutputInterface $output, $question, $default = FALSE) {
return $this->getDialog()->askConfirmation($output, $question, $default);
}

/**
* Ensure that the user is logged in.
*
* @param \DrupalPatchUtils\DoBrowser $browser
* The d.o. browser
* @param \Symfony\Component\Console\Output\OutputInterface $output
* The output.
*/
protected function ensureUserIsLoggedIn(DoBrowser $browser, OutputInterface $output) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already something there for this $this->login()

if (!$browser->loggedIn()) {
$browser->login($this->getConfig()->getDrupalUser(), $this->ask($output, "Enter your Drupal.org password: ", '', TRUE));
}
}

}
23 changes: 5 additions & 18 deletions src/DrupalPatchUtils/Command/CreateIssue.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use DrupalPatchUtils\DoBrowser;
use DrupalPatchUtils\IssueSummaryTemplate;
use DrupalPatchUtils\TextEditor;
use DrupalPatchUtils\Uuid;
use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -44,9 +45,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$browser = new DoBrowser();
if (!$browser->loggedIn()) {
$browser->login($this->getConfig()->getDrupalUser(), $this->ask($output, "Enter your Drupal.org password: ", '', TRUE));
}
$this->ensureUserIsLoggedIn($browser, $output);

$project = $input->getArgument('project');
$project_form = $browser->getIssueForm($project);
Expand Down Expand Up @@ -74,21 +73,8 @@ protected function execute(InputInterface $input, OutputInterface $output)

// Allow to input the main body either via an editor or in the shell.
if ($input->getOption('editor')) {
$temp_file = '/tmp/' . Uuid::generate() . ".txt";
$filesystem = new Filesystem();
$filesystem->touch($temp_file);
$filesystem->dumpFile($temp_file, IssueSummaryTemplate::BODY);

$process = new Process(sprintf('vi %s', $temp_file), NULL, NULL, NULL, 3600);

$process->setTty(TRUE);
$process->start();
$process->wait();

$output->writeln($process->getOutput());
$output->writeln($process->getErrorOutput());

$body_text = file_get_contents($temp_file);
$editor = new TextEditor();
$body_text = $editor->editor($output, IssueSummaryTemplate::BODY);
}
else {
$body_text = $dialog->ask($output, 'Enter body: ', 'TODO');
Expand Down Expand Up @@ -117,4 +103,5 @@ protected function execute(InputInterface $input, OutputInterface $output)
return;
}


}
4 changes: 1 addition & 3 deletions src/DrupalPatchUtils/Command/PostComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$issue = $this->getIssue($input->getArgument('url'));
if ($issue) {
$browser = new DoBrowser();
if (!$browser->loggedIn()) {
$browser->login($this->getConfig()->getDrupalUser(), $this->ask($output, "Enter your Drupal.org password: ", '', TRUE));
}
$this->ensureUserIsLoggedIn($browser, $output);

$comment_form = $browser->getCommentForm($issue->getUri());

Expand Down
93 changes: 93 additions & 0 deletions src/DrupalPatchUtils/Command/PostIssueComment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/**
* @file
* Contains DrupalPatchUtils\Command${NAME}.
*/


namespace DrupalPatchUtils\Command;


use DrupalPatchUtils\CommentEditor;
use DrupalPatchUtils\DoBrowser;
use DrupalPatchUtils\IssuePriority;
use DrupalPatchUtils\IssueStatus;
use DrupalPatchUtils\TextEditor;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class PostIssueComment extends CommandBase {

protected function configure()
{
$this
->setName('postIssueComment')
->setAliases(array('pic'))
->setDescription('Posts comment on an issue to d.o.')
->addArgument(
'url',
InputArgument::REQUIRED,
'What is the url/nid of the issue to retrieve?'
)
->addArgument(
'files',
InputArgument::IS_ARRAY,
'Files'
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
if (!$output instanceof ConsoleOutputInterface) {
throw new \Exception('Console output needed.');
}

$url = $input->getArgument('url');
$issue = $this->getIssue($url);

$browser = new DoBrowser();
$this->ensureUserIsLoggedIn($browser, $output);

$comment_form = $browser->getCommentForm($issue->getUri());


$files = $input->getArgument('files');

$comment_editor = new CommentEditor($comment_form);
$template = $comment_editor->generateContent($issue, $files);

$editor = new TextEditor();
$result = $editor->editor($output, $template);

$body = $comment_editor->getCommentText($result);
$metadata = $comment_editor->getMetadata($result);

$comment_form->uploadFiles($files);

$comment_form->setCommentText($body);

if (isset($metadata['status'])) {
$comment_form->setStatus(IssueStatus::toInteger($metadata['status']));
}

if (isset($metadata['priority'])) {
$comment_form->setPriority(IssuePriority::toInteger($metadata['priority']));
}

$crawler = $browser->submitForm($comment_form->getForm());

if ($errors = $browser->getErrors($crawler)) {
$output->getErrorOutput()->writeln($errors);
}
else {
$uri = $browser->getClient()->getHistory()->current()->getUri();
$output->writeln(sprintf('Posting the issue was successful: %s', $uri));
}

return;
}

}
91 changes: 91 additions & 0 deletions src/DrupalPatchUtils/CommentEditor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* @file
* Contains DrupalPatchUtils\CommentEditor.
*/

namespace DrupalPatchUtils;


class CommentEditor
{

/**
* @var \DrupalPatchUtils\CommentForm
*/
protected $commentForm;

public function __construct(CommentForm $comment_form)
{
$this->commentForm = $comment_form;
}

public function generateContent(Issue $issue, $new_files = [])
{
$output = [];

$output[] = "# Please enter the comment message for your changes. Lines starting";
$output[] = "# with '#' will be ignored, and an empty message aborts the comment.";
$output[] = '#';
$output[] = '# Comment on issue #' . $issue->getNid() . ': ' . $issue->getTitle();

if (!empty($new_files)) {
$output[] = '#';
$output[] = '# Attached files';
foreach ($new_files as $filename) {
$output[] = '# - ' . $filename;
}
}

$output[] = '#';
$output[] = '# Status: ' . IssueStatus::toString($this->commentForm->getStatus());
foreach (IssueStatus::getDefinition() as $definition) {
$output[] = '# - ' . $definition['label'] . ' - ' . implode(', ', $definition['aliases']);
}

$output[] = '#';
$output[] = '# Priority: ' . IssuePriority::toString($this->commentForm->getPriority());
foreach (IssuePriority::getDefinition() as $definition) {
$output[] = '# - ' . $definition['label'] . ' - ' . implode(', ', $definition['aliases']);
}

$output[] = '#';
$output[] = '# Tags: ' . implode(', ', $this->commentForm->getTags());

return implode("\n", $output);
}

public function getCommentText($lines)
{
$array = explode("\n", $lines);
$lines = array_filter($array, function ($value) {
return !(isset($value[0]) && $value[0] == '#');
});

return implode("\n", $lines);
}

public function getMetadata($lines)
{
$metadata = array();
foreach (explode(PHP_EOL, $lines) as $line) {
if (strpos($line, '# Tags:') === 0) {
$metadata['tags'] = trim(str_replace('# Tags: ', '', $line));
}
if (strpos($line, '# Status:') === 0) {
$status = trim(str_replace('# Status: ', '', $line));

$metadata['status'] = $status;
}
if (strpos($line, '# Priority:') === 0) {
$priority = trim(str_replace('# Priority: ', '', $line));

$metadata['priority'] = $priority;
}
}

return $metadata;
}

}
54 changes: 46 additions & 8 deletions src/DrupalPatchUtils/CommentForm.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Created by JetBrains PhpStorm.
* User: alex
Expand All @@ -9,16 +10,53 @@

namespace DrupalPatchUtils;

class CommentForm extends DoFormBase
{

/**
* The browser.
*
* @var \DrupalPatchUtils\DoBrowser
*/
protected $browser;

public function __construct(DoBrowser $browser)
{
$this->browser = $browser;
$this->form = $this->getCrawler()->selectButton('Save')->form();
}

public function setCommentText($text)
{
$comment = $this->form->get('nodechanges_comment_body[value]');
$comment->setValue($text);
$this->form->set($comment);
return $this;
}

public function uploadFiles(array $files = [])
{
$file_nr = 0;
foreach ($files as $key => $file) {
$this->form = $this->getCrawler()->selectButton('Upload')->form();

while (!($this->form->has("files[field_issue_files_und_$file_nr]"))) {
$file_nr++;
}
$this->form["files[field_issue_files_und_$file_nr]"]->setFilePath($file);

use Symfony\Component\DomCrawler\Form;
$this->browser->getClient()->submit($this->form);
}

class CommentForm extends DoFormBase {
$this->form = $this->getCrawler()->selectButton('Save')->form();
}

public function setCommentText($text) {
$comment = $this->form->get('nodechanges_comment_body[value]');
$comment->setValue($text);
$this->form->set($comment);
return $this;
}
/**
* @return null|\Symfony\Component\DomCrawler\Crawler
*/
protected function getCrawler()
{
return $this->browser->getClient()->getCrawler();
}

}
12 changes: 6 additions & 6 deletions src/DrupalPatchUtils/DoBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public function __construct() {
* @return bool
*/
public function loggedIn() {
$crawler = $this->client->request('GET', 'https://drupal.org/user/');
$crawler = $this->client->request('GET', 'https://www.drupal.org/user/');

$log_in_button = $crawler->selectButton('Log in');
return $log_in_button->count() == 0;
}

public function login($user, $pass) {
$crawler = $this->client->request('GET', 'https://drupal.org/user/');
$crawler = $this->client->request('GET', 'https://www.drupal.org/user/');
// Check if already logged in.
if (($select_button = $crawler->selectButton('Log in')) && $select_button->count()) {
$form = $select_button->form();
Expand All @@ -60,7 +60,7 @@ public function login($user, $pass) {

public function logout() {
if ($this->loggedIn()) {
$this->client->request('GET', 'https://drupal.org/user/logout');
$this->client->request('GET', 'https://www.drupal.org/user/logout');
}
}

Expand All @@ -69,12 +69,12 @@ public function logout() {
* @return \DrupalPatchUtils\CommentForm
*/
public function getCommentForm($issue_uri) {
$crawler = $this->client->request('GET', $issue_uri . '/edit');
return new CommentForm($crawler->selectButton('Save')->form());
$this->client->request('GET', $issue_uri . '/edit');
return new CommentForm($this);
}

public function getIssueForm($project) {
$uri = 'https://drupal.org/' . 'node/add/project-issue/' . $project;
$uri = 'https://www.drupal.org/' . 'node/add/project-issue/' . $project;
$crawler = $this->client->request('GET', $uri);
return new IssueForm($crawler->selectButton('Save')->form());
}
Expand Down
Loading